4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Filesystem\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Filesystem\Filesystem;
17 class FilesystemTestCase extends TestCase
21 protected $longPathNamesWindows = array();
24 * @var \Symfony\Component\Filesystem\Filesystem
26 protected $filesystem = null;
31 protected $workspace = null;
33 private static $symlinkOnWindows = null;
35 public static function setUpBeforeClass()
37 if ('\\' === DIRECTORY_SEPARATOR && null === self::$symlinkOnWindows) {
38 $target = tempnam(sys_get_temp_dir(), 'sl');
39 $link = sys_get_temp_dir().'/sl'.microtime(true).mt_rand();
40 self::$symlinkOnWindows = @symlink($target, $link) && is_link($link);
46 protected function setUp()
48 $this->umask = umask(0);
49 $this->filesystem = new Filesystem();
50 $this->workspace = sys_get_temp_dir().'/'.microtime(true).'.'.mt_rand();
51 mkdir($this->workspace, 0777, true);
52 $this->workspace = realpath($this->workspace);
55 protected function tearDown()
57 if (!empty($this->longPathNamesWindows)) {
58 foreach ($this->longPathNamesWindows as $path) {
61 $this->longPathNamesWindows = array();
64 $this->filesystem->remove($this->workspace);
69 * @param int $expectedFilePerms expected file permissions as three digits (i.e. 755)
70 * @param string $filePath
72 protected function assertFilePermissions($expectedFilePerms, $filePath)
74 $actualFilePerms = (int) substr(sprintf('%o', fileperms($filePath)), -3);
78 sprintf('File permissions for %s must be %s. Actual %s', $filePath, $expectedFilePerms, $actualFilePerms)
82 protected function getFileOwner($filepath)
84 $this->markAsSkippedIfPosixIsMissing();
86 $infos = stat($filepath);
87 if ($datas = posix_getpwuid($infos['uid'])) {
88 return $datas['name'];
92 protected function getFileGroup($filepath)
94 $this->markAsSkippedIfPosixIsMissing();
96 $infos = stat($filepath);
97 if ($datas = posix_getgrgid($infos['gid'])) {
98 return $datas['name'];
101 $this->markTestSkipped('Unable to retrieve file group name');
104 protected function markAsSkippedIfSymlinkIsMissing($relative = false)
106 if ('\\' === DIRECTORY_SEPARATOR && false === self::$symlinkOnWindows) {
107 $this->markTestSkipped('symlink requires "Create symbolic links" privilege on Windows');
110 // https://bugs.php.net/bug.php?id=69473
111 if ($relative && '\\' === DIRECTORY_SEPARATOR && 1 === PHP_ZTS) {
112 $this->markTestSkipped('symlink does not support relative paths on thread safe Windows PHP versions');
116 protected function markAsSkippedIfChmodIsMissing()
118 if ('\\' === DIRECTORY_SEPARATOR) {
119 $this->markTestSkipped('chmod is not supported on Windows');
123 protected function markAsSkippedIfPosixIsMissing()
125 if (!function_exists('posix_isatty')) {
126 $this->markTestSkipped('Function posix_isatty is required.');