|
4 | 4 |
|
5 | 5 | use InvalidArgumentException; |
6 | 6 | use PhpSchool\PhpWorkshop\Solution\DirectorySolution; |
| 7 | +use PhpSchool\PhpWorkshop\Utils\Path; |
7 | 8 | use PHPUnit\Framework\TestCase; |
| 9 | +use Symfony\Component\Filesystem\Filesystem; |
8 | 10 |
|
9 | 11 | class DirectorySolutionTest extends TestCase |
10 | 12 | { |
| 13 | + /** |
| 14 | + * @var string |
| 15 | + */ |
| 16 | + private $tempPath; |
| 17 | + |
| 18 | + public function setUp(): void |
| 19 | + { |
| 20 | + $this->tempPath = sprintf('%s/%s', realpath(sys_get_temp_dir()), $this->getName()); |
| 21 | + @mkdir($this->tempPath); |
| 22 | + } |
| 23 | + |
| 24 | + public function tearDown(): void |
| 25 | + { |
| 26 | + $fileSystem = new Filesystem(); |
| 27 | + $fileSystem->remove(Path::join(realpath(sys_get_temp_dir()), 'php-school')); |
| 28 | + $fileSystem->remove($this->tempPath); |
| 29 | + } |
| 30 | + |
11 | 31 | public function testExceptionIsThrownIfEntryPointDoesNotExist(): void |
12 | 32 | { |
13 | | - $tempPath = sprintf('%s/%s', realpath(sys_get_temp_dir()), $this->getName()); |
14 | | - @mkdir($tempPath, 0775, true); |
15 | | - touch(sprintf('%s/some-class.php', $tempPath)); |
| 33 | + touch(sprintf('%s/some-class.php', $this->tempPath)); |
16 | 34 |
|
17 | 35 | $this->expectException(InvalidArgumentException::class); |
18 | | - $this->expectExceptionMessage(sprintf('Entry point: "solution.php" does not exist in: "%s"', $tempPath)); |
| 36 | + $this->expectExceptionMessageMatches('/Entry point: "solution.php" does not exist in: ".*"/'); |
19 | 37 |
|
20 | | - DirectorySolution::fromDirectory($tempPath); |
21 | | - |
22 | | - unlink(sprintf('%s/some-class.php', $tempPath)); |
23 | | - rmdir($tempPath); |
| 38 | + DirectorySolution::fromDirectory($this->tempPath); |
24 | 39 | } |
25 | 40 |
|
26 | 41 | public function testWithDefaultEntryPoint(): void |
27 | 42 | { |
28 | | - $tempPath = sprintf('%s/%s', realpath(sys_get_temp_dir()), $this->getName()); |
29 | | - @mkdir($tempPath, 0775, true); |
30 | | - touch(sprintf('%s/solution.php', $tempPath)); |
31 | | - touch(sprintf('%s/some-class.php', $tempPath)); |
| 43 | + file_put_contents(sprintf('%s/solution.php', $this->tempPath), 'ENTRYPOINT'); |
| 44 | + file_put_contents(sprintf('%s/some-class.php', $this->tempPath), 'SOME CLASS'); |
32 | 45 |
|
33 | | - $solution = DirectorySolution::fromDirectory($tempPath); |
| 46 | + $solution = DirectorySolution::fromDirectory($this->tempPath); |
34 | 47 |
|
35 | | - $this->assertSame($tempPath, $solution->getBaseDirectory()); |
36 | | - $this->assertFalse($solution->hasComposerFile()); |
37 | | - $this->assertSame(sprintf('%s/solution.php', $tempPath), $solution->getEntryPoint()); |
| 48 | + self::assertFalse($solution->hasComposerFile()); |
| 49 | + self::assertSame('ENTRYPOINT', file_get_contents($solution->getEntryPoint())); |
38 | 50 | $files = $solution->getFiles(); |
39 | | - $this->assertCount(2, $files); |
40 | | - |
41 | | - $this->assertSame(sprintf('%s/solution.php', $tempPath), $files[0]->__toString()); |
42 | | - $this->assertSame(sprintf('%s/some-class.php', $tempPath), $files[1]->__toString()); |
| 51 | + self::assertCount(2, $files); |
43 | 52 |
|
44 | | - unlink(sprintf('%s/solution.php', $tempPath)); |
45 | | - unlink(sprintf('%s/some-class.php', $tempPath)); |
46 | | - rmdir($tempPath); |
| 53 | + self::assertSame('ENTRYPOINT', file_get_contents($files[0]->__toString())); |
| 54 | + self::assertSame('SOME CLASS', file_get_contents($files[1]->__toString())); |
47 | 55 | } |
48 | 56 |
|
49 | 57 | public function testWithManualEntryPoint(): void |
50 | 58 | { |
51 | | - $tempPath = sprintf('%s/%s', realpath(sys_get_temp_dir()), $this->getName()); |
52 | | - @mkdir($tempPath, 0775, true); |
53 | | - touch(sprintf('%s/index.php', $tempPath)); |
54 | | - touch(sprintf('%s/some-class.php', $tempPath)); |
| 59 | + file_put_contents(sprintf('%s/index.php', $this->tempPath), 'ENTRYPOINT'); |
| 60 | + file_put_contents(sprintf('%s/some-class.php', $this->tempPath), 'SOME CLASS'); |
55 | 61 |
|
56 | | - $solution = DirectorySolution::fromDirectory($tempPath, [], 'index.php'); |
| 62 | + $solution = DirectorySolution::fromDirectory($this->tempPath, [], 'index.php'); |
57 | 63 |
|
58 | | - $this->assertSame($tempPath, $solution->getBaseDirectory()); |
59 | | - $this->assertFalse($solution->hasComposerFile()); |
60 | | - $this->assertSame(sprintf('%s/index.php', $tempPath), $solution->getEntryPoint()); |
| 64 | + self::assertFalse($solution->hasComposerFile()); |
| 65 | + self::assertSame('ENTRYPOINT', file_get_contents($solution->getEntryPoint())); |
61 | 66 | $files = $solution->getFiles(); |
62 | | - $this->assertCount(2, $files); |
| 67 | + self::assertCount(2, $files); |
63 | 68 |
|
64 | | - $this->assertSame(sprintf('%s/index.php', $tempPath), $files[0]->__toString()); |
65 | | - $this->assertSame(sprintf('%s/some-class.php', $tempPath), $files[1]->__toString()); |
66 | | - |
67 | | - unlink(sprintf('%s/index.php', $tempPath)); |
68 | | - unlink(sprintf('%s/some-class.php', $tempPath)); |
69 | | - rmdir($tempPath); |
| 69 | + self::assertSame('ENTRYPOINT', file_get_contents($files[0]->__toString())); |
| 70 | + self::assertSame('SOME CLASS', file_get_contents($files[1]->__toString())); |
70 | 71 | } |
71 | 72 |
|
72 | 73 | public function testHasComposerFileReturnsTrueIfPresent(): void |
73 | 74 | { |
74 | | - $tempPath = sprintf('%s/%s', realpath(sys_get_temp_dir()), $this->getName()); |
75 | | - @mkdir($tempPath, 0775, true); |
76 | | - touch(sprintf('%s/solution.php', $tempPath)); |
77 | | - touch(sprintf('%s/some-class.php', $tempPath)); |
78 | | - touch(sprintf('%s/composer.lock', $tempPath)); |
| 75 | + file_put_contents(sprintf('%s/solution.php', $this->tempPath), 'ENTRYPOINT'); |
| 76 | + file_put_contents(sprintf('%s/some-class.php', $this->tempPath), 'SOME CLASS'); |
| 77 | + touch(sprintf('%s/composer.lock', $this->tempPath)); |
79 | 78 |
|
80 | | - $solution = DirectorySolution::fromDirectory($tempPath); |
| 79 | + $solution = DirectorySolution::fromDirectory($this->tempPath); |
81 | 80 |
|
82 | | - $this->assertSame($tempPath, $solution->getBaseDirectory()); |
83 | | - $this->assertTrue($solution->hasComposerFile()); |
84 | | - $this->assertSame(sprintf('%s/solution.php', $tempPath), $solution->getEntryPoint()); |
| 81 | + self::assertTrue($solution->hasComposerFile()); |
| 82 | + self::assertSame('ENTRYPOINT', file_get_contents($solution->getEntryPoint())); |
85 | 83 | $files = $solution->getFiles(); |
86 | | - $this->assertCount(2, $files); |
87 | | - |
88 | | - $this->assertSame(sprintf('%s/solution.php', $tempPath), $files[0]->__toString()); |
89 | | - $this->assertSame(sprintf('%s/some-class.php', $tempPath), $files[1]->__toString()); |
| 84 | + self::assertCount(2, $files); |
90 | 85 |
|
91 | | - unlink(sprintf('%s/composer.lock', $tempPath)); |
92 | | - unlink(sprintf('%s/solution.php', $tempPath)); |
93 | | - unlink(sprintf('%s/some-class.php', $tempPath)); |
| 86 | + self::assertSame('ENTRYPOINT', file_get_contents($files[0]->__toString())); |
| 87 | + self::assertSame('SOME CLASS', file_get_contents($files[1]->__toString())); |
94 | 88 | } |
95 | 89 |
|
96 | 90 | public function testWithExceptions(): void |
97 | 91 | { |
98 | | - $tempPath = sprintf('%s/%s', realpath(sys_get_temp_dir()), $this->getName()); |
99 | | - @mkdir($tempPath, 0775, true); |
100 | | - touch(sprintf('%s/solution.php', $tempPath)); |
101 | | - touch(sprintf('%s/some-class.php', $tempPath)); |
102 | | - touch(sprintf('%s/exclude.txt', $tempPath)); |
| 92 | + file_put_contents(sprintf('%s/solution.php', $this->tempPath), 'ENTRYPOINT'); |
| 93 | + file_put_contents(sprintf('%s/some-class.php', $this->tempPath), 'SOME CLASS'); |
| 94 | + touch(sprintf('%s/exclude.txt', $this->tempPath)); |
103 | 95 |
|
104 | 96 | $exclusions = ['exclude.txt']; |
105 | 97 |
|
106 | | - $solution = DirectorySolution::fromDirectory($tempPath, $exclusions); |
| 98 | + $solution = DirectorySolution::fromDirectory($this->tempPath, $exclusions); |
107 | 99 |
|
108 | | - $this->assertSame(sprintf('%s/solution.php', $tempPath), $solution->getEntryPoint()); |
| 100 | + self::assertSame('ENTRYPOINT', file_get_contents($solution->getEntryPoint())); |
109 | 101 | $files = $solution->getFiles(); |
110 | | - $this->assertCount(2, $files); |
| 102 | + self::assertCount(2, $files); |
111 | 103 |
|
112 | | - $this->assertSame(sprintf('%s/solution.php', $tempPath), $files[0]->__toString()); |
113 | | - $this->assertSame(sprintf('%s/some-class.php', $tempPath), $files[1]->__toString()); |
114 | | - |
115 | | - unlink(sprintf('%s/solution.php', $tempPath)); |
116 | | - unlink(sprintf('%s/some-class.php', $tempPath)); |
117 | | - unlink(sprintf('%s/exclude.txt', $tempPath)); |
118 | | - rmdir($tempPath); |
| 104 | + self::assertSame('ENTRYPOINT', file_get_contents($files[0]->__toString())); |
| 105 | + self::assertSame('SOME CLASS', file_get_contents($files[1]->__toString())); |
119 | 106 | } |
120 | 107 |
|
121 | 108 | public function testWithNestedDirectories(): void |
122 | 109 | { |
123 | | - $tempPath = sprintf('%s/%s', realpath(sys_get_temp_dir()), $this->getName()); |
124 | | - @mkdir($tempPath, 0775, true); |
125 | | - |
126 | | - @mkdir(sprintf('%s/nested', $tempPath), 0775, true); |
127 | | - @mkdir(sprintf('%s/nested/deep', $tempPath), 0775, true); |
| 110 | + @mkdir(sprintf('%s/nested', $this->tempPath), 0775, true); |
| 111 | + @mkdir(sprintf('%s/nested/deep', $this->tempPath), 0775, true); |
128 | 112 |
|
129 | | - touch(sprintf('%s/solution.php', $tempPath)); |
130 | | - touch(sprintf('%s/some-class.php', $tempPath)); |
131 | | - touch(sprintf('%s/composer.json', $tempPath)); |
132 | | - touch(sprintf('%s/nested/another-class.php', $tempPath)); |
133 | | - touch(sprintf('%s/nested/deep/even-more.php', $tempPath)); |
| 113 | + file_put_contents(sprintf('%s/solution.php', $this->tempPath), 'ENTRYPOINT'); |
| 114 | + file_put_contents(sprintf('%s/some-class.php', $this->tempPath), 'SOME CLASS'); |
| 115 | + file_put_contents(sprintf('%s/composer.json', $this->tempPath), 'COMPOSER DATA'); |
| 116 | + file_put_contents(sprintf('%s/nested/another-class.php', $this->tempPath), 'ANOTHER CLASS'); |
| 117 | + file_put_contents(sprintf('%s/nested/deep/even-more.php', $this->tempPath), 'EVEN MOAR'); |
134 | 118 |
|
135 | | - $solution = DirectorySolution::fromDirectory($tempPath); |
| 119 | + $solution = DirectorySolution::fromDirectory($this->tempPath); |
136 | 120 |
|
137 | | - $this->assertSame(sprintf('%s/solution.php', $tempPath), $solution->getEntryPoint()); |
| 121 | + self::assertSame('ENTRYPOINT', file_get_contents($solution->getEntryPoint())); |
138 | 122 | $files = $solution->getFiles(); |
139 | | - $this->assertCount(5, $files); |
140 | | - |
141 | | - $this->assertSame(sprintf('%s/composer.json', $tempPath), $files[0]->__toString()); |
142 | | - $this->assertSame(sprintf('%s/nested/another-class.php', $tempPath), $files[1]->__toString()); |
143 | | - $this->assertSame(sprintf('%s/nested/deep/even-more.php', $tempPath), $files[2]->__toString()); |
144 | | - $this->assertSame(sprintf('%s/solution.php', $tempPath), $files[3]->__toString()); |
145 | | - $this->assertSame(sprintf('%s/some-class.php', $tempPath), $files[4]->__toString()); |
146 | | - |
147 | | - unlink(sprintf('%s/solution.php', $tempPath)); |
148 | | - unlink(sprintf('%s/some-class.php', $tempPath)); |
149 | | - unlink(sprintf('%s/composer.json', $tempPath)); |
150 | | - unlink(sprintf('%s/nested/another-class.php', $tempPath)); |
151 | | - unlink(sprintf('%s/nested/deep/even-more.php', $tempPath)); |
152 | | - rmdir(sprintf('%s/nested/deep', $tempPath)); |
153 | | - rmdir(sprintf('%s/nested', $tempPath)); |
154 | | - rmdir($tempPath); |
| 123 | + self::assertCount(5, $files); |
| 124 | + |
| 125 | + self::assertSame('COMPOSER DATA', file_get_contents($files[0]->__toString())); |
| 126 | + self::assertSame('ANOTHER CLASS', file_get_contents($files[1]->__toString())); |
| 127 | + self::assertSame('EVEN MOAR', file_get_contents($files[2]->__toString())); |
| 128 | + self::assertSame('ENTRYPOINT', file_get_contents($files[3]->__toString())); |
| 129 | + self::assertSame('SOME CLASS', file_get_contents($files[4]->__toString())); |
155 | 130 | } |
156 | 131 |
|
157 | 132 | public function testExceptionsWithNestedDirectories(): void |
158 | 133 | { |
159 | | - $tempPath = sprintf('%s/%s', realpath(sys_get_temp_dir()), $this->getName()); |
160 | | - @mkdir($tempPath, 0775, true); |
161 | | - |
162 | | - @mkdir(sprintf('%s/nested', $tempPath), 0775, true); |
163 | | - @mkdir(sprintf('%s/nested/deep', $tempPath), 0775, true); |
164 | | - @mkdir(sprintf('%s/vendor', $tempPath), 0775, true); |
165 | | - @mkdir(sprintf('%s/vendor/somelib', $tempPath), 0775, true); |
166 | | - |
167 | | - touch(sprintf('%s/solution.php', $tempPath)); |
168 | | - touch(sprintf('%s/some-class.php', $tempPath)); |
169 | | - touch(sprintf('%s/exclude.txt', $tempPath)); |
170 | | - touch(sprintf('%s/nested/exclude.txt', $tempPath)); |
171 | | - touch(sprintf('%s/nested/deep/exclude.txt', $tempPath)); |
172 | | - touch(sprintf('%s/vendor/somelib/app.php', $tempPath)); |
| 134 | + @mkdir(sprintf('%s/nested', $this->tempPath), 0775, true); |
| 135 | + @mkdir(sprintf('%s/nested/deep', $this->tempPath), 0775, true); |
| 136 | + @mkdir(sprintf('%s/vendor', $this->tempPath), 0775, true); |
| 137 | + @mkdir(sprintf('%s/vendor/somelib', $this->tempPath), 0775, true); |
| 138 | + |
| 139 | + file_put_contents(sprintf('%s/solution.php', $this->tempPath), 'ENTRYPOINT'); |
| 140 | + file_put_contents(sprintf('%s/some-class.php', $this->tempPath), 'SOME CLASS'); |
| 141 | + touch(sprintf('%s/exclude.txt', $this->tempPath)); |
| 142 | + touch(sprintf('%s/nested/exclude.txt', $this->tempPath)); |
| 143 | + touch(sprintf('%s/nested/deep/exclude.txt', $this->tempPath)); |
| 144 | + touch(sprintf('%s/vendor/somelib/app.php', $this->tempPath)); |
173 | 145 |
|
174 | 146 | $exclusions = ['exclude.txt', 'vendor']; |
175 | 147 |
|
176 | | - $solution = DirectorySolution::fromDirectory($tempPath, $exclusions); |
| 148 | + $solution = DirectorySolution::fromDirectory($this->tempPath, $exclusions); |
177 | 149 |
|
178 | | - $this->assertSame(sprintf('%s/solution.php', $tempPath), $solution->getEntryPoint()); |
| 150 | + self::assertSame('ENTRYPOINT', file_get_contents($solution->getEntryPoint())); |
179 | 151 | $files = $solution->getFiles(); |
180 | | - $this->assertCount(2, $files); |
181 | | - |
182 | | - $this->assertSame(sprintf('%s/solution.php', $tempPath), $files[0]->__toString()); |
183 | | - $this->assertSame(sprintf('%s/some-class.php', $tempPath), $files[1]->__toString()); |
184 | | - |
185 | | - unlink(sprintf('%s/solution.php', $tempPath)); |
186 | | - unlink(sprintf('%s/some-class.php', $tempPath)); |
187 | | - unlink(sprintf('%s/exclude.txt', $tempPath)); |
188 | | - unlink(sprintf('%s/nested/exclude.txt', $tempPath)); |
189 | | - unlink(sprintf('%s/nested/deep/exclude.txt', $tempPath)); |
190 | | - unlink(sprintf('%s/vendor/somelib/app.php', $tempPath)); |
191 | | - rmdir(sprintf('%s/nested/deep', $tempPath)); |
192 | | - rmdir(sprintf('%s/nested', $tempPath)); |
193 | | - rmdir(sprintf('%s/vendor/somelib', $tempPath)); |
194 | | - rmdir(sprintf('%s/vendor', $tempPath)); |
195 | | - rmdir($tempPath); |
| 152 | + self::assertCount(2, $files); |
| 153 | + |
| 154 | + self::assertSame('ENTRYPOINT', file_get_contents($files[0]->__toString())); |
| 155 | + self::assertSame('SOME CLASS', file_get_contents($files[1]->__toString())); |
196 | 156 | } |
197 | 157 | } |
0 commit comments