Skip to content

Commit afd79ea

Browse files
committed
merged branch zakharovvi/filesystem_test_case (PR symfony#8333)
This PR was merged into the master branch. Discussion ---------- [Filesystem] create FilesystemTestCase from FilesystemTest | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - FilesystemTest methods are useful for testing any class are working with file system Commits ------- 266df16 [Filesystem] create FilesystemTestCase from FilesystemTest
2 parents 7c326bd + 266df16 commit afd79ea

File tree

2 files changed

+137
-115
lines changed

2 files changed

+137
-115
lines changed

src/Symfony/Component/Filesystem/Tests/FilesystemTest.php

Lines changed: 12 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -16,63 +16,17 @@
1616
/**
1717
* Test class for Filesystem.
1818
*/
19-
class FilesystemTest extends \PHPUnit_Framework_TestCase
19+
class FilesystemTest extends FilesystemTestCase
2020
{
21-
/**
22-
* @var string $workspace
23-
*/
24-
private $workspace = null;
25-
2621
/**
2722
* @var \Symfony\Component\Filesystem\Filesystem $filesystem
2823
*/
2924
private $filesystem = null;
3025

31-
private static $symlinkOnWindows = null;
32-
33-
public static function setUpBeforeClass()
34-
{
35-
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
36-
self::$symlinkOnWindows = true;
37-
$originDir = tempnam(sys_get_temp_dir(), 'sl');
38-
$targetDir = tempnam(sys_get_temp_dir(), 'sl');
39-
if (true !== @symlink($originDir, $targetDir)) {
40-
$report = error_get_last();
41-
if (is_array($report) && false !== strpos($report['message'], 'error code(1314)')) {
42-
self::$symlinkOnWindows = false;
43-
}
44-
}
45-
}
46-
}
47-
4826
public function setUp()
4927
{
28+
parent::setUp();
5029
$this->filesystem = new Filesystem();
51-
$this->workspace = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.time().rand(0, 1000);
52-
mkdir($this->workspace, 0777, true);
53-
$this->workspace = realpath($this->workspace);
54-
}
55-
56-
public function tearDown()
57-
{
58-
$this->clean($this->workspace);
59-
}
60-
61-
/**
62-
* @param string $file
63-
*/
64-
private function clean($file)
65-
{
66-
if (is_dir($file) && !is_link($file)) {
67-
$dir = new \FilesystemIterator($file);
68-
foreach ($dir as $childFile) {
69-
$this->clean($childFile);
70-
}
71-
72-
rmdir($file);
73-
} else {
74-
unlink($file);
75-
}
7630
}
7731

7832
public function testCopyCreatesNewFile()
@@ -406,8 +360,8 @@ public function testChmodChangesFileMode()
406360
$this->filesystem->chmod($file, 0400);
407361
$this->filesystem->chmod($dir, 0753);
408362

409-
$this->assertEquals(753, $this->getFilePermissions($dir));
410-
$this->assertEquals(400, $this->getFilePermissions($file));
363+
$this->assertFilePermissions(753, $dir);
364+
$this->assertFilePermissions(400, $file);
411365
}
412366

413367
public function testChmodWrongMod()
@@ -432,8 +386,8 @@ public function testChmodRecursive()
432386
$this->filesystem->chmod($file, 0400, 0000, true);
433387
$this->filesystem->chmod($dir, 0753, 0000, true);
434388

435-
$this->assertEquals(753, $this->getFilePermissions($dir));
436-
$this->assertEquals(753, $this->getFilePermissions($file));
389+
$this->assertFilePermissions(753, $dir);
390+
$this->assertFilePermissions(753, $file);
437391
}
438392

439393
public function testChmodAppliesUmask()
@@ -444,7 +398,7 @@ public function testChmodAppliesUmask()
444398
touch($file);
445399

446400
$this->filesystem->chmod($file, 0770, 0022);
447-
$this->assertEquals(750, $this->getFilePermissions($file));
401+
$this->assertFilePermissions(750, $file);
448402
}
449403

450404
public function testChmodChangesModeOfArrayOfFiles()
@@ -460,8 +414,8 @@ public function testChmodChangesModeOfArrayOfFiles()
460414

461415
$this->filesystem->chmod($files, 0753);
462416

463-
$this->assertEquals(753, $this->getFilePermissions($file));
464-
$this->assertEquals(753, $this->getFilePermissions($directory));
417+
$this->assertFilePermissions(753, $file);
418+
$this->assertFilePermissions(753, $directory);
465419
}
466420

467421
public function testChmodChangesModeOfTraversableFileObject()
@@ -477,8 +431,8 @@ public function testChmodChangesModeOfTraversableFileObject()
477431

478432
$this->filesystem->chmod($files, 0753);
479433

480-
$this->assertEquals(753, $this->getFilePermissions($file));
481-
$this->assertEquals(753, $this->getFilePermissions($directory));
434+
$this->assertFilePermissions(753, $file);
435+
$this->assertFilePermissions(753, $directory);
482436
}
483437

484438
public function testChown()
@@ -908,7 +862,7 @@ public function testDumpFile()
908862

909863
// skip mode check on windows
910864
if (!defined('PHP_WINDOWS_VERSION_MAJOR')) {
911-
$this->assertEquals(753, $this->getFilePermissions($filename));
865+
$this->assertFilePermissions(753, $filename);
912866
}
913867
}
914868

@@ -922,61 +876,4 @@ public function testDumpFileOverwritesAnExistingFile()
922876
$this->assertFileExists($filename);
923877
$this->assertSame('bar', file_get_contents($filename));
924878
}
925-
926-
/**
927-
* Returns file permissions as three digits (i.e. 755)
928-
*
929-
* @param string $filePath
930-
*
931-
* @return integer
932-
*/
933-
private function getFilePermissions($filePath)
934-
{
935-
return (int) substr(sprintf('%o', fileperms($filePath)), -3);
936-
}
937-
938-
private function getFileOwner($filepath)
939-
{
940-
$this->markAsSkippedIfPosixIsMissing();
941-
942-
$infos = stat($filepath);
943-
if ($datas = posix_getpwuid($infos['uid'])) {
944-
return $datas['name'];
945-
}
946-
}
947-
948-
private function getFileGroup($filepath)
949-
{
950-
$this->markAsSkippedIfPosixIsMissing();
951-
952-
$infos = stat($filepath);
953-
if ($datas = posix_getgrgid($infos['gid'])) {
954-
return $datas['name'];
955-
}
956-
}
957-
958-
private function markAsSkippedIfSymlinkIsMissing()
959-
{
960-
if (!function_exists('symlink')) {
961-
$this->markTestSkipped('symlink is not supported');
962-
}
963-
964-
if (defined('PHP_WINDOWS_VERSION_MAJOR') && false === self::$symlinkOnWindows) {
965-
$this->markTestSkipped('symlink requires "Create symbolic links" privilege on windows');
966-
}
967-
}
968-
969-
private function markAsSkippedIfChmodIsMissing()
970-
{
971-
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
972-
$this->markTestSkipped('chmod is not supported on windows');
973-
}
974-
}
975-
976-
private function markAsSkippedIfPosixIsMissing()
977-
{
978-
if (defined('PHP_WINDOWS_VERSION_MAJOR') || !function_exists('posix_isatty')) {
979-
$this->markTestSkipped('Posix is not supported');
980-
}
981-
}
982879
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Filesystem\Tests;
13+
14+
class FilesystemTestCase extends \PHPUnit_Framework_TestCase
15+
{
16+
/**
17+
* @var string $workspace
18+
*/
19+
protected $workspace = null;
20+
21+
protected static $symlinkOnWindows = null;
22+
23+
public static function setUpBeforeClass()
24+
{
25+
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
26+
static::$symlinkOnWindows = true;
27+
$originDir = tempnam(sys_get_temp_dir(), 'sl');
28+
$targetDir = tempnam(sys_get_temp_dir(), 'sl');
29+
if (true !== @symlink($originDir, $targetDir)) {
30+
$report = error_get_last();
31+
if (is_array($report) && false !== strpos($report['message'], 'error code(1314)')) {
32+
static::$symlinkOnWindows = false;
33+
}
34+
}
35+
}
36+
}
37+
38+
public function setUp()
39+
{
40+
$this->workspace = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.time().rand(0, 1000);
41+
mkdir($this->workspace, 0777, true);
42+
$this->workspace = realpath($this->workspace);
43+
}
44+
45+
public function tearDown()
46+
{
47+
$this->clean($this->workspace);
48+
}
49+
50+
/**
51+
* @param string $file
52+
*/
53+
protected function clean($file)
54+
{
55+
if (is_dir($file) && !is_link($file)) {
56+
$dir = new \FilesystemIterator($file);
57+
foreach ($dir as $childFile) {
58+
$this->clean($childFile);
59+
}
60+
61+
rmdir($file);
62+
} else {
63+
unlink($file);
64+
}
65+
}
66+
67+
/**
68+
* @param int $expectedFilePerms expected file permissions as three digits (i.e. 755)
69+
* @param string $filePath
70+
*/
71+
protected function assertFilePermissions($expectedFilePerms, $filePath)
72+
{
73+
$actualFilePerms = (int) substr(sprintf('%o', fileperms($filePath)), -3);
74+
$this->assertEquals(
75+
$expectedFilePerms,
76+
$actualFilePerms,
77+
sprintf('File permissions for %s must be %s. Actual %s', $filePath, $expectedFilePerms, $actualFilePerms)
78+
);
79+
}
80+
81+
protected function getFileOwner($filepath)
82+
{
83+
$this->markAsSkippedIfPosixIsMissing();
84+
85+
$infos = stat($filepath);
86+
if ($datas = posix_getpwuid($infos['uid'])) {
87+
return $datas['name'];
88+
}
89+
}
90+
91+
protected function getFileGroup($filepath)
92+
{
93+
$this->markAsSkippedIfPosixIsMissing();
94+
95+
$infos = stat($filepath);
96+
if ($datas = posix_getgrgid($infos['gid'])) {
97+
return $datas['name'];
98+
}
99+
}
100+
101+
protected function markAsSkippedIfSymlinkIsMissing()
102+
{
103+
if (!function_exists('symlink')) {
104+
$this->markTestSkipped('symlink is not supported');
105+
}
106+
107+
if (defined('PHP_WINDOWS_VERSION_MAJOR') && false === static::$symlinkOnWindows) {
108+
$this->markTestSkipped('symlink requires "Create symbolic links" privilege on windows');
109+
}
110+
}
111+
112+
protected function markAsSkippedIfChmodIsMissing()
113+
{
114+
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
115+
$this->markTestSkipped('chmod is not supported on windows');
116+
}
117+
}
118+
119+
protected function markAsSkippedIfPosixIsMissing()
120+
{
121+
if (defined('PHP_WINDOWS_VERSION_MAJOR') || !function_exists('posix_isatty')) {
122+
$this->markTestSkipped('Posix is not supported');
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)