Yaffs site version 1.1
[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     private static $symlinkOnWindows = null;
34
35     public static function setUpBeforeClass()
36     {
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);
41             @unlink($link);
42             unlink($target);
43         }
44     }
45
46     protected function setUp()
47     {
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);
53     }
54
55     protected function tearDown()
56     {
57         if (!empty($this->longPathNamesWindows)) {
58             foreach ($this->longPathNamesWindows as $path) {
59                 exec('DEL '.$path);
60             }
61             $this->longPathNamesWindows = array();
62         }
63
64         $this->filesystem->remove($this->workspace);
65         umask($this->umask);
66     }
67
68     /**
69      * @param int    $expectedFilePerms expected file permissions as three digits (i.e. 755)
70      * @param string $filePath
71      */
72     protected function assertFilePermissions($expectedFilePerms, $filePath)
73     {
74         $actualFilePerms = (int) substr(sprintf('%o', fileperms($filePath)), -3);
75         $this->assertEquals(
76             $expectedFilePerms,
77             $actualFilePerms,
78             sprintf('File permissions for %s must be %s. Actual %s', $filePath, $expectedFilePerms, $actualFilePerms)
79         );
80     }
81
82     protected function getFileOwner($filepath)
83     {
84         $this->markAsSkippedIfPosixIsMissing();
85
86         $infos = stat($filepath);
87         if ($datas = posix_getpwuid($infos['uid'])) {
88             return $datas['name'];
89         }
90     }
91
92     protected function getFileGroup($filepath)
93     {
94         $this->markAsSkippedIfPosixIsMissing();
95
96         $infos = stat($filepath);
97         if ($datas = posix_getgrgid($infos['gid'])) {
98             return $datas['name'];
99         }
100
101         $this->markTestSkipped('Unable to retrieve file group name');
102     }
103
104     protected function markAsSkippedIfSymlinkIsMissing($relative = false)
105     {
106         if ('\\' === DIRECTORY_SEPARATOR && false === self::$symlinkOnWindows) {
107             $this->markTestSkipped('symlink requires "Create symbolic links" privilege on Windows');
108         }
109
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');
113         }
114     }
115
116     protected function markAsSkippedIfChmodIsMissing()
117     {
118         if ('\\' === DIRECTORY_SEPARATOR) {
119             $this->markTestSkipped('chmod is not supported on Windows');
120         }
121     }
122
123     protected function markAsSkippedIfPosixIsMissing()
124     {
125         if (!function_exists('posix_isatty')) {
126             $this->markTestSkipped('Function posix_isatty is required.');
127         }
128     }
129 }