FilesystemTestCase.php (3409B)
1 <?php 2 3 /* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 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 use Symfony\Component\Filesystem\Filesystem; 15 16 class FilesystemTestCase extends \PHPUnit_Framework_TestCase 17 { 18 private $umask; 19 20 /** 21 * @var \Symfony\Component\Filesystem\Filesystem 22 */ 23 protected $filesystem = null; 24 25 /** 26 * @var string 27 */ 28 protected $workspace = null; 29 30 private static $symlinkOnWindows = null; 31 32 public static function setUpBeforeClass() 33 { 34 if ('\\' === DIRECTORY_SEPARATOR && null === self::$symlinkOnWindows) { 35 $target = tempnam(sys_get_temp_dir(), 'sl'); 36 $link = sys_get_temp_dir().'/sl'.microtime(true).mt_rand(); 37 if (self::$symlinkOnWindows = @symlink($target, $link)) { 38 unlink($link); 39 } 40 unlink($target); 41 } 42 } 43 44 protected function setUp() 45 { 46 $this->umask = umask(0); 47 $this->workspace = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.time().mt_rand(0, 1000); 48 mkdir($this->workspace, 0777, true); 49 $this->workspace = realpath($this->workspace); 50 $this->filesystem = new Filesystem(); 51 } 52 53 protected function tearDown() 54 { 55 $this->filesystem->remove($this->workspace); 56 umask($this->umask); 57 } 58 59 /** 60 * @param int $expectedFilePerms expected file permissions as three digits (i.e. 755) 61 * @param string $filePath 62 */ 63 protected function assertFilePermissions($expectedFilePerms, $filePath) 64 { 65 $actualFilePerms = (int) substr(sprintf('%o', fileperms($filePath)), -3); 66 $this->assertEquals( 67 $expectedFilePerms, 68 $actualFilePerms, 69 sprintf('File permissions for %s must be %s. Actual %s', $filePath, $expectedFilePerms, $actualFilePerms) 70 ); 71 } 72 73 protected function getFileOwner($filepath) 74 { 75 $this->markAsSkippedIfPosixIsMissing(); 76 77 $infos = stat($filepath); 78 if ($datas = posix_getpwuid($infos['uid'])) { 79 return $datas['name']; 80 } 81 } 82 83 protected function getFileGroup($filepath) 84 { 85 $this->markAsSkippedIfPosixIsMissing(); 86 87 $infos = stat($filepath); 88 if ($datas = posix_getgrgid($infos['gid'])) { 89 return $datas['name']; 90 } 91 92 $this->markTestSkipped('Unable to retrieve file group name'); 93 } 94 95 protected function markAsSkippedIfSymlinkIsMissing() 96 { 97 if (!function_exists('symlink')) { 98 $this->markTestSkipped('symlink is not supported'); 99 } 100 101 if ('\\' === DIRECTORY_SEPARATOR && false === self::$symlinkOnWindows) { 102 $this->markTestSkipped('symlink requires "Create symbolic links" privilege on windows'); 103 } 104 } 105 106 protected function markAsSkippedIfChmodIsMissing() 107 { 108 if ('\\' === DIRECTORY_SEPARATOR) { 109 $this->markTestSkipped('chmod is not supported on windows'); 110 } 111 } 112 113 protected function markAsSkippedIfPosixIsMissing() 114 { 115 if ('\\' === DIRECTORY_SEPARATOR || !function_exists('posix_isatty')) { 116 $this->markTestSkipped('Posix is not supported'); 117 } 118 } 119 }