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;
34 * @var null|bool Flag for hard links on Windows
36 private static $linkOnWindows = null;
39 * @var null|bool Flag for symbolic links on Windows
41 private static $symlinkOnWindows = null;
43 public static function setUpBeforeClass()
45 if ('\\' === DIRECTORY_SEPARATOR) {
46 self::$linkOnWindows = true;
47 $originFile = tempnam(sys_get_temp_dir(), 'li');
48 $targetFile = tempnam(sys_get_temp_dir(), 'li');
49 if (true !== @link($originFile, $targetFile)) {
50 $report = error_get_last();
51 if (is_array($report) && false !== strpos($report['message'], 'error code(1314)')) {
52 self::$linkOnWindows = false;
58 self::$symlinkOnWindows = true;
59 $originDir = tempnam(sys_get_temp_dir(), 'sl');
60 $targetDir = tempnam(sys_get_temp_dir(), 'sl');
61 if (true !== @symlink($originDir, $targetDir)) {
62 $report = error_get_last();
63 if (is_array($report) && false !== strpos($report['message'], 'error code(1314)')) {
64 self::$symlinkOnWindows = false;
72 protected function setUp()
74 $this->umask = umask(0);
75 $this->filesystem = new Filesystem();
76 $this->workspace = sys_get_temp_dir().'/'.microtime(true).'.'.mt_rand();
77 mkdir($this->workspace, 0777, true);
78 $this->workspace = realpath($this->workspace);
81 protected function tearDown()
83 if (!empty($this->longPathNamesWindows)) {
84 foreach ($this->longPathNamesWindows as $path) {
87 $this->longPathNamesWindows = array();
90 $this->filesystem->remove($this->workspace);
95 * @param int $expectedFilePerms Expected file permissions as three digits (i.e. 755)
96 * @param string $filePath
98 protected function assertFilePermissions($expectedFilePerms, $filePath)
100 $actualFilePerms = (int) substr(sprintf('%o', fileperms($filePath)), -3);
104 sprintf('File permissions for %s must be %s. Actual %s', $filePath, $expectedFilePerms, $actualFilePerms)
108 protected function getFileOwner($filepath)
110 $this->markAsSkippedIfPosixIsMissing();
112 $infos = stat($filepath);
113 if ($datas = posix_getpwuid($infos['uid'])) {
114 return $datas['name'];
118 protected function getFileGroup($filepath)
120 $this->markAsSkippedIfPosixIsMissing();
122 $infos = stat($filepath);
123 if ($datas = posix_getgrgid($infos['gid'])) {
124 return $datas['name'];
127 $this->markTestSkipped('Unable to retrieve file group name');
130 protected function markAsSkippedIfLinkIsMissing()
132 if (!function_exists('link')) {
133 $this->markTestSkipped('link is not supported');
136 if ('\\' === DIRECTORY_SEPARATOR && false === self::$linkOnWindows) {
137 $this->markTestSkipped('link requires "Create hard links" privilege on windows');
141 protected function markAsSkippedIfSymlinkIsMissing($relative = false)
143 if ('\\' === DIRECTORY_SEPARATOR && false === self::$symlinkOnWindows) {
144 $this->markTestSkipped('symlink requires "Create symbolic links" privilege on Windows');
147 // https://bugs.php.net/bug.php?id=69473
148 if ($relative && '\\' === DIRECTORY_SEPARATOR && 1 === PHP_ZTS) {
149 $this->markTestSkipped('symlink does not support relative paths on thread safe Windows PHP versions');
153 protected function markAsSkippedIfChmodIsMissing()
155 if ('\\' === DIRECTORY_SEPARATOR) {
156 $this->markTestSkipped('chmod is not supported on Windows');
160 protected function markAsSkippedIfPosixIsMissing()
162 if (!function_exists('posix_isatty')) {
163 $this->markTestSkipped('Function posix_isatty is required.');