642925ddee578d5f1c743861c167268b151b5eed
[yaffs-website] / vendor / symfony / filesystem / Tests / FilesystemTestCase.php
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 PHPUnit\Framework\TestCase;
15 use Symfony\Component\Filesystem\Filesystem;
16
17 class FilesystemTestCase extends TestCase
18 {
19     private $umask;
20
21     protected $longPathNamesWindows = array();
22
23     /**
24      * @var \Symfony\Component\Filesystem\Filesystem
25      */
26     protected $filesystem = null;
27
28     /**
29      * @var string
30      */
31     protected $workspace = null;
32
33     /**
34      * @var bool|null Flag for hard links on Windows
35      */
36     private static $linkOnWindows = null;
37
38     /**
39      * @var bool|null Flag for symbolic links on Windows
40      */
41     private static $symlinkOnWindows = null;
42
43     public static function setUpBeforeClass()
44     {
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;
53                 }
54             } else {
55                 @unlink($targetFile);
56             }
57
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;
65                 }
66             } else {
67                 @unlink($targetDir);
68             }
69         }
70     }
71
72     protected function setUp()
73     {
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);
79     }
80
81     protected function tearDown()
82     {
83         if (!empty($this->longPathNamesWindows)) {
84             foreach ($this->longPathNamesWindows as $path) {
85                 exec('DEL '.$path);
86             }
87             $this->longPathNamesWindows = array();
88         }
89
90         $this->filesystem->remove($this->workspace);
91         umask($this->umask);
92     }
93
94     /**
95      * @param int    $expectedFilePerms Expected file permissions as three digits (i.e. 755)
96      * @param string $filePath
97      */
98     protected function assertFilePermissions($expectedFilePerms, $filePath)
99     {
100         $actualFilePerms = (int) substr(sprintf('%o', fileperms($filePath)), -3);
101         $this->assertEquals(
102             $expectedFilePerms,
103             $actualFilePerms,
104             sprintf('File permissions for %s must be %s. Actual %s', $filePath, $expectedFilePerms, $actualFilePerms)
105         );
106     }
107
108     protected function getFileOwner($filepath)
109     {
110         $this->markAsSkippedIfPosixIsMissing();
111
112         $infos = stat($filepath);
113         if ($datas = posix_getpwuid($infos['uid'])) {
114             return $datas['name'];
115         }
116     }
117
118     protected function getFileGroup($filepath)
119     {
120         $this->markAsSkippedIfPosixIsMissing();
121
122         $infos = stat($filepath);
123         if ($datas = posix_getgrgid($infos['gid'])) {
124             return $datas['name'];
125         }
126
127         $this->markTestSkipped('Unable to retrieve file group name');
128     }
129
130     protected function markAsSkippedIfLinkIsMissing()
131     {
132         if (!\function_exists('link')) {
133             $this->markTestSkipped('link is not supported');
134         }
135
136         if ('\\' === \DIRECTORY_SEPARATOR && false === self::$linkOnWindows) {
137             $this->markTestSkipped('link requires "Create hard links" privilege on windows');
138         }
139     }
140
141     protected function markAsSkippedIfSymlinkIsMissing($relative = false)
142     {
143         if ('\\' === \DIRECTORY_SEPARATOR && false === self::$symlinkOnWindows) {
144             $this->markTestSkipped('symlink requires "Create symbolic links" privilege on Windows');
145         }
146
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');
150         }
151     }
152
153     protected function markAsSkippedIfChmodIsMissing()
154     {
155         if ('\\' === \DIRECTORY_SEPARATOR) {
156             $this->markTestSkipped('chmod is not supported on Windows');
157         }
158     }
159
160     protected function markAsSkippedIfPosixIsMissing()
161     {
162         if (!\function_exists('posix_isatty')) {
163             $this->markTestSkipped('Function posix_isatty is required.');
164         }
165     }
166 }