Permalink
Browse files

[Filesystem] create FilesystemTestCase from FilesystemTest

  • Loading branch information...
1 parent 7975f9d commit 18460cf04fc28664e37c37a72d4635273fd97b8f @zakharovvi zakharovvi committed Jun 21, 2013
Showing with 137 additions and 115 deletions.
  1. +12 −115 Tests/FilesystemTest.php
  2. +125 −0 Tests/FilesystemTestCase.php
View
@@ -16,63 +16,17 @@
/**
* Test class for Filesystem.
*/
-class FilesystemTest extends \PHPUnit_Framework_TestCase
+class FilesystemTest extends FilesystemTestCase
{
- /**
- * @var string $workspace
- */
- private $workspace = null;
-
/**
* @var \Symfony\Component\Filesystem\Filesystem $filesystem
*/
private $filesystem = null;
- private static $symlinkOnWindows = null;
-
- public static function setUpBeforeClass()
- {
- if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
- self::$symlinkOnWindows = true;
- $originDir = tempnam(sys_get_temp_dir(), 'sl');
- $targetDir = tempnam(sys_get_temp_dir(), 'sl');
- if (true !== @symlink($originDir, $targetDir)) {
- $report = error_get_last();
- if (is_array($report) && false !== strpos($report['message'], 'error code(1314)')) {
- self::$symlinkOnWindows = false;
- }
- }
- }
- }
-
public function setUp()
{
+ parent::setUp();
$this->filesystem = new Filesystem();
- $this->workspace = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.time().rand(0, 1000);
- mkdir($this->workspace, 0777, true);
- $this->workspace = realpath($this->workspace);
- }
-
- public function tearDown()
- {
- $this->clean($this->workspace);
- }
-
- /**
- * @param string $file
- */
- private function clean($file)
- {
- if (is_dir($file) && !is_link($file)) {
- $dir = new \FilesystemIterator($file);
- foreach ($dir as $childFile) {
- $this->clean($childFile);
- }
-
- rmdir($file);
- } else {
- unlink($file);
- }
}
public function testCopyCreatesNewFile()
@@ -406,8 +360,8 @@ public function testChmodChangesFileMode()
$this->filesystem->chmod($file, 0400);
$this->filesystem->chmod($dir, 0753);
- $this->assertEquals(753, $this->getFilePermissions($dir));
- $this->assertEquals(400, $this->getFilePermissions($file));
+ $this->assertFilePermissions(753, $dir);
+ $this->assertFilePermissions(400, $file);
}
public function testChmodWrongMod()
@@ -432,8 +386,8 @@ public function testChmodRecursive()
$this->filesystem->chmod($file, 0400, 0000, true);
$this->filesystem->chmod($dir, 0753, 0000, true);
- $this->assertEquals(753, $this->getFilePermissions($dir));
- $this->assertEquals(753, $this->getFilePermissions($file));
+ $this->assertFilePermissions(753, $dir);
+ $this->assertFilePermissions(753, $file);
}
public function testChmodAppliesUmask()
@@ -444,7 +398,7 @@ public function testChmodAppliesUmask()
touch($file);
$this->filesystem->chmod($file, 0770, 0022);
- $this->assertEquals(750, $this->getFilePermissions($file));
+ $this->assertFilePermissions(750, $file);
}
public function testChmodChangesModeOfArrayOfFiles()
@@ -460,8 +414,8 @@ public function testChmodChangesModeOfArrayOfFiles()
$this->filesystem->chmod($files, 0753);
- $this->assertEquals(753, $this->getFilePermissions($file));
- $this->assertEquals(753, $this->getFilePermissions($directory));
+ $this->assertFilePermissions(753, $file);
+ $this->assertFilePermissions(753, $directory);
}
public function testChmodChangesModeOfTraversableFileObject()
@@ -477,8 +431,8 @@ public function testChmodChangesModeOfTraversableFileObject()
$this->filesystem->chmod($files, 0753);
- $this->assertEquals(753, $this->getFilePermissions($file));
- $this->assertEquals(753, $this->getFilePermissions($directory));
+ $this->assertFilePermissions(753, $file);
+ $this->assertFilePermissions(753, $directory);
}
public function testChown()
@@ -908,7 +862,7 @@ public function testDumpFile()
// skip mode check on windows
if (!defined('PHP_WINDOWS_VERSION_MAJOR')) {
- $this->assertEquals(753, $this->getFilePermissions($filename));
+ $this->assertFilePermissions(753, $filename);
}
}
@@ -922,61 +876,4 @@ public function testDumpFileOverwritesAnExistingFile()
$this->assertFileExists($filename);
$this->assertSame('bar', file_get_contents($filename));
}
-
- /**
- * Returns file permissions as three digits (i.e. 755)
- *
- * @param string $filePath
- *
- * @return integer
- */
- private function getFilePermissions($filePath)
- {
- return (int) substr(sprintf('%o', fileperms($filePath)), -3);
- }
-
- private function getFileOwner($filepath)
- {
- $this->markAsSkippedIfPosixIsMissing();
-
- $infos = stat($filepath);
- if ($datas = posix_getpwuid($infos['uid'])) {
- return $datas['name'];
- }
- }
-
- private function getFileGroup($filepath)
- {
- $this->markAsSkippedIfPosixIsMissing();
-
- $infos = stat($filepath);
- if ($datas = posix_getgrgid($infos['gid'])) {
- return $datas['name'];
- }
- }
-
- private function markAsSkippedIfSymlinkIsMissing()
- {
- if (!function_exists('symlink')) {
- $this->markTestSkipped('symlink is not supported');
- }
-
- if (defined('PHP_WINDOWS_VERSION_MAJOR') && false === self::$symlinkOnWindows) {
- $this->markTestSkipped('symlink requires "Create symbolic links" privilege on windows');
- }
- }
-
- private function markAsSkippedIfChmodIsMissing()
- {
- if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
- $this->markTestSkipped('chmod is not supported on windows');
- }
- }
-
- private function markAsSkippedIfPosixIsMissing()
- {
- if (defined('PHP_WINDOWS_VERSION_MAJOR') || !function_exists('posix_isatty')) {
- $this->markTestSkipped('Posix is not supported');
- }
- }
}
@@ -0,0 +1,125 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Filesystem\Tests;
+
+class FilesystemTestCase extends \PHPUnit_Framework_TestCase
+{
+ /**
+ * @var string $workspace
+ */
+ protected $workspace = null;
+
+ protected static $symlinkOnWindows = null;
+
+ public static function setUpBeforeClass()
+ {
+ if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
+ static::$symlinkOnWindows = true;
+ $originDir = tempnam(sys_get_temp_dir(), 'sl');
+ $targetDir = tempnam(sys_get_temp_dir(), 'sl');
+ if (true !== @symlink($originDir, $targetDir)) {
+ $report = error_get_last();
+ if (is_array($report) && false !== strpos($report['message'], 'error code(1314)')) {
+ static::$symlinkOnWindows = false;
+ }
+ }
+ }
+ }
+
+ public function setUp()
+ {
+ $this->workspace = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.time().rand(0, 1000);
+ mkdir($this->workspace, 0777, true);
+ $this->workspace = realpath($this->workspace);
+ }
+
+ public function tearDown()
+ {
+ $this->clean($this->workspace);
+ }
+
+ /**
+ * @param string $file
+ */
+ protected function clean($file)
+ {
+ if (is_dir($file) && !is_link($file)) {
+ $dir = new \FilesystemIterator($file);
+ foreach ($dir as $childFile) {
+ $this->clean($childFile);
+ }
+
+ rmdir($file);
+ } else {
+ unlink($file);
+ }
+ }
+
+ /**
+ * @param int $expectedFilePerms expected file permissions as three digits (i.e. 755)
+ * @param string $filePath
+ */
+ protected function assertFilePermissions($expectedFilePerms, $filePath)
+ {
+ $actualFilePerms = (int) substr(sprintf('%o', fileperms($filePath)), -3);
+ $this->assertEquals(
+ $expectedFilePerms,
+ $actualFilePerms,
+ sprintf('File permissions for %s must be %s. Actual %s', $filePath, $expectedFilePerms, $actualFilePerms)
+ );
+ }
+
+ protected function getFileOwner($filepath)
+ {
+ $this->markAsSkippedIfPosixIsMissing();
+
+ $infos = stat($filepath);
+ if ($datas = posix_getpwuid($infos['uid'])) {
+ return $datas['name'];
+ }
+ }
+
+ protected function getFileGroup($filepath)
+ {
+ $this->markAsSkippedIfPosixIsMissing();
+
+ $infos = stat($filepath);
+ if ($datas = posix_getgrgid($infos['gid'])) {
+ return $datas['name'];
+ }
+ }
+
+ protected function markAsSkippedIfSymlinkIsMissing()
+ {
+ if (!function_exists('symlink')) {
+ $this->markTestSkipped('symlink is not supported');
+ }
+
+ if (defined('PHP_WINDOWS_VERSION_MAJOR') && false === static::$symlinkOnWindows) {
+ $this->markTestSkipped('symlink requires "Create symbolic links" privilege on windows');
+ }
+ }
+
+ protected function markAsSkippedIfChmodIsMissing()
+ {
+ if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
+ $this->markTestSkipped('chmod is not supported on windows');
+ }
+ }
+
+ protected function markAsSkippedIfPosixIsMissing()
+ {
+ if (defined('PHP_WINDOWS_VERSION_MAJOR') || !function_exists('posix_isatty')) {
+ $this->markTestSkipped('Posix is not supported');
+ }
+ }
+}

0 comments on commit 18460cf

Please sign in to comment.