|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Medium\CodeGeneration\Filesystem; |
|
4
|
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Directory; |
|
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
|
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\AbstractTest; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @covers \EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Directory |
|
12
|
|
|
* @covers \EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\AbstractFilesystemItem |
|
13
|
|
|
*/ |
|
14
|
|
|
class DirectoryTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
private const WORK_DIR = AbstractTest::VAR_PATH . '/DirectoryTest'; |
|
17
|
|
|
|
|
18
|
|
|
public static function setUpBeforeClass() |
|
19
|
|
|
{ |
|
20
|
|
|
mkdir(self::WORK_DIR); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @test |
|
25
|
|
|
* @medium |
|
26
|
|
|
*/ |
|
27
|
|
|
public function itCanCreateANewDirectory(): Directory |
|
28
|
|
|
{ |
|
29
|
|
|
$path = self::WORK_DIR . '/directoryToCreate'; |
|
30
|
|
|
$object = new Directory($path); |
|
31
|
|
|
$object->create(); |
|
32
|
|
|
self::assertDirectoryExists(self::WORK_DIR . '/directoryToCreate'); |
|
33
|
|
|
self::assertSame($path, $object->getPath()); |
|
34
|
|
|
self::assertSame(realpath($path), $object->getSplFileInfo()->getRealPath()); |
|
35
|
|
|
|
|
36
|
|
|
return $object; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @test |
|
41
|
|
|
* @medium |
|
42
|
|
|
* @depends itCanCreateANewDirectory |
|
43
|
|
|
* |
|
44
|
|
|
* @param Directory $object |
|
45
|
|
|
* |
|
46
|
|
|
* @throws DoctrineStaticMetaException |
|
47
|
|
|
*/ |
|
48
|
|
|
public function itCachesDirectoryObjectInstances(Directory $object): void |
|
49
|
|
|
{ |
|
50
|
|
|
self::assertSame($object->getSplFileInfo(), $object->getSplFileInfo()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @test |
|
55
|
|
|
* @medium |
|
56
|
|
|
*/ |
|
57
|
|
|
public function itCanLoadAnExistingDirectory(): void |
|
58
|
|
|
{ |
|
59
|
|
|
$path = self::WORK_DIR . '/alreadyExists'; |
|
60
|
|
|
mkdir($path); |
|
61
|
|
|
$object = new Directory($path); |
|
62
|
|
|
self::assertSame($path, $object->getPath()); |
|
63
|
|
|
self::assertTrue($object->exists()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @test |
|
68
|
|
|
* @medium |
|
69
|
|
|
*/ |
|
70
|
|
|
public function itCannotCreateADirectoryThatAlreadyExists(): void |
|
71
|
|
|
{ |
|
72
|
|
|
$path = self::WORK_DIR . '/alreadyExists2'; |
|
73
|
|
|
mkdir($path); |
|
74
|
|
|
$object = new Directory($path); |
|
75
|
|
|
$this->expectException(DoctrineStaticMetaException::class); |
|
76
|
|
|
$this->expectExceptionMessage('directory already exists at path '); |
|
77
|
|
|
$object->create(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @test |
|
82
|
|
|
* @medium |
|
83
|
|
|
*/ |
|
84
|
|
|
public function itCanSetDirectoryPermissionsToUseWhenCreating() |
|
85
|
|
|
{ |
|
86
|
|
|
$path = self::WORK_DIR . '/hasCustomPermissions'; |
|
87
|
|
|
$permissions = 0666; |
|
88
|
|
|
$object = new Directory($path); |
|
89
|
|
|
$object->setCreateMode($permissions); |
|
90
|
|
|
$object->create(); |
|
91
|
|
|
self::assertSame(decoct($permissions), decoct(fileperms($path) & 0777)); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|