Completed
Push — master ( 021686...0b264b )
by
unknown
02:51 queued 14s
created

FilesystemTestCase::markAsSkippedIfLinkIsMissing()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 4
nc 4
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sonata\MediaBundle\Tests\Fixtures;
6
7
use PHPUnit\Framework\TestCase;
8
use Symfony\Component\Filesystem\Filesystem;
9
10
class FilesystemTestCase extends TestCase
11
{
12
    private $umask;
13
14
    protected $longPathNamesWindows = [];
15
16
    /**
17
     * @var Filesystem
18
     */
19
    protected $filesystem = null;
20
21
    /**
22
     * @var string
23
     */
24
    protected $workspace = null;
25
26
    /**
27
     * @var bool|null Flag for hard links on Windows
28
     */
29
    private static $linkOnWindows = null;
30
31
    /**
32
     * @var bool|null Flag for symbolic links on Windows
33
     */
34
    private static $symlinkOnWindows = null;
35
36
    public static function setUpBeforeClass(): void
37
    {
38
        if ('\\' === \DIRECTORY_SEPARATOR) {
39
            self::$linkOnWindows = true;
40
            $originFile = tempnam(sys_get_temp_dir(), 'li');
41
            $targetFile = tempnam(sys_get_temp_dir(), 'li');
42
            if (true !== @link($originFile, $targetFile)) {
43
                $report = error_get_last();
44
                if (\is_array($report) && false !== strpos($report['message'], 'error code(1314)')) {
45
                    self::$linkOnWindows = false;
46
                }
47
            } else {
48
                @unlink($targetFile);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
49
            }
50
51
            self::$symlinkOnWindows = true;
52
            $originDir = tempnam(sys_get_temp_dir(), 'sl');
53
            $targetDir = tempnam(sys_get_temp_dir(), 'sl');
54
            if (true !== @symlink($originDir, $targetDir)) {
55
                $report = error_get_last();
56
                if (\is_array($report) && false !== strpos($report['message'], 'error code(1314)')) {
57
                    self::$symlinkOnWindows = false;
58
                }
59
            } else {
60
                @unlink($targetDir);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
61
            }
62
        }
63
    }
64
65
    protected function setUp(): void
66
    {
67
        $this->umask = umask(0);
68
        $this->filesystem = new Filesystem();
69
        $this->workspace = sys_get_temp_dir().'/'.microtime(true).'.'.mt_rand();
70
        mkdir($this->workspace, 0777, true);
71
        $this->workspace = realpath($this->workspace);
72
    }
73
74
    protected function tearDown(): void
75
    {
76
        if (!empty($this->longPathNamesWindows)) {
77
            foreach ($this->longPathNamesWindows as $path) {
78
                exec('DEL '.$path);
79
            }
80
            $this->longPathNamesWindows = [];
81
        }
82
83
        $this->filesystem->remove($this->workspace);
84
        umask($this->umask);
85
    }
86
87
    /**
88
     * @param int    $expectedFilePerms Expected file permissions as three digits (i.e. 755)
89
     * @param string $filePath
90
     */
91
    protected function assertFilePermissions($expectedFilePerms, $filePath)
92
    {
93
        $actualFilePerms = (int) substr(sprintf('%o', fileperms($filePath)), -3);
94
        $this->assertEquals(
95
            $expectedFilePerms,
96
            $actualFilePerms,
97
            sprintf('File permissions for %s must be %s. Actual %s', $filePath, $expectedFilePerms, $actualFilePerms)
98
        );
99
    }
100
101
    protected function getFileOwner($filepath)
102
    {
103
        $this->markAsSkippedIfPosixIsMissing();
104
105
        $infos = stat($filepath);
106
107
        return ($datas = posix_getpwuid($infos['uid'])) ? $datas['name'] : null;
108
    }
109
110
    protected function getFileGroup($filepath)
111
    {
112
        $this->markAsSkippedIfPosixIsMissing();
113
114
        $infos = stat($filepath);
115
        if ($datas = posix_getgrgid($infos['gid'])) {
116
            return $datas['name'];
117
        }
118
119
        $this->markTestSkipped('Unable to retrieve file group name');
120
    }
121
122
    protected function markAsSkippedIfLinkIsMissing()
123
    {
124
        if (!\function_exists('link')) {
125
            $this->markTestSkipped('link is not supported');
126
        }
127
128
        if ('\\' === \DIRECTORY_SEPARATOR && false === self::$linkOnWindows) {
129
            $this->markTestSkipped('link requires "Create hard links" privilege on windows');
130
        }
131
    }
132
133
    protected function markAsSkippedIfSymlinkIsMissing($relative = false)
134
    {
135
        if ('\\' === \DIRECTORY_SEPARATOR && false === self::$symlinkOnWindows) {
136
            $this->markTestSkipped('symlink requires "Create symbolic links" privilege on Windows');
137
        }
138
139
        // https://bugs.php.net/69473
140
        if ($relative && '\\' === \DIRECTORY_SEPARATOR && 1 === PHP_ZTS) {
141
            $this->markTestSkipped('symlink does not support relative paths on thread safe Windows PHP versions');
142
        }
143
    }
144
145
    protected function markAsSkippedIfChmodIsMissing()
146
    {
147
        if ('\\' === \DIRECTORY_SEPARATOR) {
148
            $this->markTestSkipped('chmod is not supported on Windows');
149
        }
150
    }
151
152
    protected function markAsSkippedIfPosixIsMissing()
153
    {
154
        if (!\function_exists('posix_isatty')) {
155
            $this->markTestSkipped('Function posix_isatty is required.');
156
        }
157
    }
158
}
159