1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiGen\Utils\Tests\FileSystem; |
4
|
|
|
|
5
|
|
|
use ApiGen\Utils\FileSystem; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
|
8
|
|
|
class FileSystemTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var FileSystem |
12
|
|
|
*/ |
13
|
|
|
private $fileSystem; |
14
|
|
|
|
15
|
|
|
protected function setUp(): void |
16
|
|
|
{ |
17
|
|
|
$this->fileSystem = new FileSystem; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function testNormalizePath(): void |
21
|
|
|
{ |
22
|
|
|
$backslashPath = 'C:' . DIRECTORY_SEPARATOR . 'Program Files' . DIRECTORY_SEPARATOR . 'ApiGen'; |
23
|
|
|
$this->assertSame($backslashPath, $this->fileSystem->normalizePath($backslashPath)); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
View Code Duplication |
public function testForceDir(): void |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$filePath = TEMP_DIR . '/some/dir/file.txt'; |
29
|
|
|
$dirPath = dirname($filePath); |
30
|
|
|
$this->assertFalse(file_exists($dirPath)); |
31
|
|
|
|
32
|
|
|
$this->fileSystem->forceDir($filePath); |
33
|
|
|
$this->assertTrue(file_exists($dirPath)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
View Code Duplication |
public function testDeleteDir(): void |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
$dir = TEMP_DIR . '/new-dir'; |
39
|
|
|
mkdir($dir); |
40
|
|
|
$this->assertTrue(file_exists($dir)); |
41
|
|
|
|
42
|
|
|
$this->fileSystem->deleteDir($dir); |
43
|
|
|
$this->assertFalse(file_exists($dir)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testPurgeDir(): void |
47
|
|
|
{ |
48
|
|
|
$dir = TEMP_DIR . '/dir-with-content'; |
49
|
|
|
mkdir($dir); |
50
|
|
|
mkdir($dir . '/dir-inside'); |
51
|
|
|
file_put_contents($dir . '/file.txt', '...'); |
52
|
|
|
|
53
|
|
|
@rmdir($dir); |
|
|
|
|
54
|
|
|
$this->assertTrue(file_exists($dir)); |
55
|
|
|
|
56
|
|
|
$this->fileSystem->purgeDir($dir); |
57
|
|
|
$this->assertTrue(file_exists($dir)); |
58
|
|
|
|
59
|
|
|
rmdir($dir); |
60
|
|
|
$this->assertFalse(file_exists($dir)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
public function testPurgeDirOnNonExistingDir(): void |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$dir = TEMP_DIR . '/not-created-dir'; |
66
|
|
|
$this->assertFalse(file_exists($dir)); |
67
|
|
|
|
68
|
|
|
$this->fileSystem->purgeDir($dir); |
69
|
|
|
$this->assertTrue(file_exists($dir)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testGetAbsolutePath(): void |
73
|
|
|
{ |
74
|
|
|
$absoluteDir = $this->fileSystem->normalizePath(TEMP_DIR . '/relative-dir'); |
75
|
|
|
mkdir($absoluteDir); |
76
|
|
|
$this->assertTrue(file_exists($absoluteDir)); |
77
|
|
|
|
78
|
|
|
$absoluteFile = $absoluteDir . DIRECTORY_SEPARATOR . 'file.txt'; |
79
|
|
|
file_put_contents($absoluteFile, '...'); |
80
|
|
|
$this->assertTrue(file_exists($absoluteFile)); |
81
|
|
|
|
82
|
|
|
$this->assertSame($absoluteDir, $this->fileSystem->getAbsolutePath($absoluteDir)); |
83
|
|
|
$this->assertSame($absoluteDir . DIRECTORY_SEPARATOR . 'file.txt', $this->fileSystem->getAbsolutePath('file.txt', [$absoluteDir])); |
84
|
|
|
|
85
|
|
|
$this->assertSame( |
86
|
|
|
'someFile.txt', |
87
|
|
|
$this->fileSystem->getAbsolutePath('someFile.txt') |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
$testFile = DIRECTORY_SEPARATOR . 'someDir' . DIRECTORY_SEPARATOR . 'someDeeperFile.txt'; |
91
|
|
|
$this->assertSame( |
92
|
|
|
$testFile, |
93
|
|
|
$this->fileSystem->getAbsolutePath($testFile) |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testIsDirEmpty(): void |
98
|
|
|
{ |
99
|
|
|
$this->assertTrue($this->fileSystem->isDirEmpty(__DIR__ . '/FileSystemSource/EmptyDir')); |
100
|
|
|
$this->assertFalse($this->fileSystem->isDirEmpty(__DIR__ . '/FileSystemSource/NonEmptyDir')); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.