Skip to content

Commit 0e0c44a

Browse files
committed
Fix tests
1 parent e04dd7f commit 0e0c44a

File tree

7 files changed

+109
-39
lines changed

7 files changed

+109
-39
lines changed

src/Listener/CodePatchListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function patch(ExerciseRunnerEvent $event): void
6262
*/
6363
public function revert(\PhpSchool\PhpWorkshop\Event\EventInterface $event): void
6464
{
65-
if (null === $this->originalCode) {
65+
if (null === $this->originalCode || empty($this->originalCode)) {
6666
throw new RuntimeException('Can only revert previously patched code');
6767
}
6868

src/Solution/InMemorySolution.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,15 @@ private function __construct(SolutionInterface $solution)
2828

2929
$intersection = array_intersect($currentPath, $solutionPath);
3030

31+
if (count($intersection) <= 1) {
32+
$intersection = explode('/', realpath(sys_get_temp_dir()));
33+
}
34+
3135
$basename = implode('/', array_diff($solutionPath, $intersection));
3236
$entrypoint = implode('/', array_diff($entryPointPath, $intersection));
3337

34-
$this->baseDirectory = sprintf('%s/php-school/%s', sys_get_temp_dir(), $basename);
35-
$this->entryPoint = sprintf('%s/php-school/%s', sys_get_temp_dir(), $entrypoint);
38+
$this->baseDirectory = sprintf('%s/php-school/%s', realpath(sys_get_temp_dir()), $basename);
39+
$this->entryPoint = sprintf('%s/php-school/%s', realpath(sys_get_temp_dir()), $entrypoint);
3640

3741
if ($fileSystem->exists($this->baseDirectory)) {
3842
$fileSystem->remove($this->baseDirectory);
@@ -56,7 +60,7 @@ private function __construct(SolutionInterface $solution)
5660
$this->files = array_map(function (SolutionFile $solutionFile) use ($intersection) {
5761
$filePath = explode('/', realpath($solutionFile->__toString()));
5862
$file = implode('/', array_diff($filePath, $intersection));
59-
return SolutionFile::fromFile(sprintf('%s/php-school/%s', sys_get_temp_dir(), $file));
63+
return SolutionFile::fromFile(sprintf('%s/php-school/%s', realpath(sys_get_temp_dir()), $file));
6064
}, $solution->getFiles());
6165
}
6266

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpSchool\PhpWorkshop\TestUtils;
6+
7+
class SolutionPathTransformer
8+
{
9+
public static function tempPathToSolutionTempPath(string $tmpFilePath): string
10+
{
11+
$tmpDir = realpath(sys_get_temp_dir());
12+
$file = str_replace($tmpDir, '', $tmpFilePath);
13+
14+
return sprintf('%s/php-school/%s', $tmpDir, ltrim($file, '/'));
15+
}
16+
}

test/Exercise/AbstractExerciseTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ public function testGetSolution(string $name): void
2424
{
2525
$exercise = new AbstractExerciseImpl($name);
2626
$path = __DIR__ . '/../../exercises/array-we-go/solution/solution.php';
27-
mkdir(dirname($path), 0777, true);
28-
touch($path);
27+
@mkdir(dirname($path), 0777, true);
28+
file_put_contents($path, 'FILE CONTENTS');
2929
$solution = $exercise->getSolution();
3030
$this->assertInstanceOf(SolutionInterface::class, $solution);
3131
$files = $solution->getFiles();
3232
$this->assertCount(1, $files);
3333
$this->assertInstanceOf(SolutionFile::class, $files[0]);
34-
$this->assertSame(realpath($path), $files[0]->__toString());
34+
$this->assertSame('FILE CONTENTS', file_get_contents($files[0]->__toString()));
3535
unlink($path);
3636
rmdir(__DIR__ . '/../../exercises/array-we-go/solution');
3737
rmdir(__DIR__ . '/../../exercises/array-we-go');

test/ResultRenderer/ResultsRendererTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ public function testRenderSuccessWithSolution(): void
119119
$exerciseRepo->method('count')->willReturn(2);
120120

121121
$tmpFile = sprintf('%s/%s/some-file', sys_get_temp_dir(), $this->getName());
122-
mkdir(dirname($tmpFile));
122+
@mkdir(dirname($tmpFile));
123123
file_put_contents($tmpFile, 'FILE CONTENTS');
124124

125125
$exercise = new CliExerciseImpl();
126-
$exercise->setSolution(new SingleFileSolution($tmpFile));
126+
$exercise->setSolution(SingleFileSolution::fromFile($tmpFile));
127127

128128
$renderer = new ResultsRenderer(
129129
'app',
@@ -165,11 +165,11 @@ public function testRenderSuccessWithPhpSolutionFileIsSyntaxHighlighted(): void
165165
$exerciseRepo->method('count')->willReturn(2);
166166

167167
$tmpFile = sprintf('%s/%s/some-file.php', sys_get_temp_dir(), $this->getName());
168-
mkdir(dirname($tmpFile));
168+
@mkdir(dirname($tmpFile));
169169
file_put_contents($tmpFile, 'FILE CONTENTS');
170170

171171
$exercise = new CliExerciseImpl();
172-
$exercise->setSolution(new SingleFileSolution($tmpFile));
172+
$exercise->setSolution(SingleFileSolution::fromFile($tmpFile));
173173

174174
$syntaxHighlighter = $this->createMock(KeyLighter::class);
175175
$php = new Php();

test/Solution/DirectorySolutionTest.php

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use InvalidArgumentException;
66
use PhpSchool\PhpWorkshop\Solution\DirectorySolution;
7+
use PhpSchool\PhpWorkshop\TestUtils\SolutionPathTransformer;
78
use PHPUnit\Framework\TestCase;
89

910
class DirectorySolutionTest extends TestCase
@@ -32,18 +33,23 @@ public function testWithDefaultEntryPoint(): void
3233

3334
$solution = DirectorySolution::fromDirectory($tempPath);
3435

35-
$this->assertSame($tempPath, $solution->getBaseDirectory());
36+
$expectedBaseDir = SolutionPathTransformer::tempPathToSolutionTempPath($tempPath);
37+
38+
$this->assertSame($expectedBaseDir, $solution->getBaseDirectory());
3639
$this->assertFalse($solution->hasComposerFile());
37-
$this->assertSame(sprintf('%s/solution.php', $tempPath), $solution->getEntryPoint());
40+
$this->assertSame(sprintf('%s/solution.php', $expectedBaseDir), $solution->getEntryPoint());
3841
$files = $solution->getFiles();
3942
$this->assertCount(2, $files);
4043

41-
$this->assertSame(sprintf('%s/solution.php', $tempPath), $files[0]->__toString());
42-
$this->assertSame(sprintf('%s/some-class.php', $tempPath), $files[1]->__toString());
44+
$this->assertSame(sprintf('%s/solution.php', $expectedBaseDir), $files[0]->__toString());
45+
$this->assertSame(sprintf('%s/some-class.php', $expectedBaseDir), $files[1]->__toString());
4346

47+
unlink(sprintf('%s/solution.php', $expectedBaseDir));
48+
unlink(sprintf('%s/some-class.php', $expectedBaseDir));
4449
unlink(sprintf('%s/solution.php', $tempPath));
4550
unlink(sprintf('%s/some-class.php', $tempPath));
4651
rmdir($tempPath);
52+
rmdir($expectedBaseDir);
4753
}
4854

4955
public function testWithManualEntryPoint(): void
@@ -55,18 +61,23 @@ public function testWithManualEntryPoint(): void
5561

5662
$solution = DirectorySolution::fromDirectory($tempPath, [], 'index.php');
5763

58-
$this->assertSame($tempPath, $solution->getBaseDirectory());
64+
$expectedBaseDir = SolutionPathTransformer::tempPathToSolutionTempPath($tempPath);
65+
66+
$this->assertSame($expectedBaseDir, $solution->getBaseDirectory());
5967
$this->assertFalse($solution->hasComposerFile());
60-
$this->assertSame(sprintf('%s/index.php', $tempPath), $solution->getEntryPoint());
68+
$this->assertSame(sprintf('%s/index.php', $expectedBaseDir), $solution->getEntryPoint());
6169
$files = $solution->getFiles();
6270
$this->assertCount(2, $files);
6371

64-
$this->assertSame(sprintf('%s/index.php', $tempPath), $files[0]->__toString());
65-
$this->assertSame(sprintf('%s/some-class.php', $tempPath), $files[1]->__toString());
72+
$this->assertSame(sprintf('%s/index.php', $expectedBaseDir), $files[0]->__toString());
73+
$this->assertSame(sprintf('%s/some-class.php', $expectedBaseDir), $files[1]->__toString());
6674

75+
unlink(sprintf('%s/index.php', $expectedBaseDir));
76+
unlink(sprintf('%s/some-class.php', $expectedBaseDir));
6777
unlink(sprintf('%s/index.php', $tempPath));
6878
unlink(sprintf('%s/some-class.php', $tempPath));
6979
rmdir($tempPath);
80+
rmdir($expectedBaseDir);
7081
}
7182

7283
public function testHasComposerFileReturnsTrueIfPresent(): void
@@ -79,15 +90,20 @@ public function testHasComposerFileReturnsTrueIfPresent(): void
7990

8091
$solution = DirectorySolution::fromDirectory($tempPath);
8192

82-
$this->assertSame($tempPath, $solution->getBaseDirectory());
93+
$expectedBaseDir = SolutionPathTransformer::tempPathToSolutionTempPath($tempPath);
94+
95+
$this->assertSame($expectedBaseDir, $solution->getBaseDirectory());
8396
$this->assertTrue($solution->hasComposerFile());
84-
$this->assertSame(sprintf('%s/solution.php', $tempPath), $solution->getEntryPoint());
97+
$this->assertSame(sprintf('%s/solution.php', $expectedBaseDir), $solution->getEntryPoint());
8598
$files = $solution->getFiles();
8699
$this->assertCount(2, $files);
87100

88-
$this->assertSame(sprintf('%s/solution.php', $tempPath), $files[0]->__toString());
89-
$this->assertSame(sprintf('%s/some-class.php', $tempPath), $files[1]->__toString());
101+
$this->assertSame(sprintf('%s/solution.php', $expectedBaseDir), $files[0]->__toString());
102+
$this->assertSame(sprintf('%s/some-class.php', $expectedBaseDir), $files[1]->__toString());
90103

104+
unlink(sprintf('%s/composer.lock', $expectedBaseDir));
105+
unlink(sprintf('%s/solution.php', $expectedBaseDir));
106+
unlink(sprintf('%s/some-class.php', $expectedBaseDir));
91107
unlink(sprintf('%s/composer.lock', $tempPath));
92108
unlink(sprintf('%s/solution.php', $tempPath));
93109
unlink(sprintf('%s/some-class.php', $tempPath));
@@ -105,17 +121,23 @@ public function testWithExceptions(): void
105121

106122
$solution = DirectorySolution::fromDirectory($tempPath, $exclusions);
107123

108-
$this->assertSame(sprintf('%s/solution.php', $tempPath), $solution->getEntryPoint());
124+
$expectedBaseDir = SolutionPathTransformer::tempPathToSolutionTempPath($tempPath);
125+
126+
$this->assertSame(sprintf('%s/solution.php', $expectedBaseDir), $solution->getEntryPoint());
109127
$files = $solution->getFiles();
110128
$this->assertCount(2, $files);
111129

112-
$this->assertSame(sprintf('%s/solution.php', $tempPath), $files[0]->__toString());
113-
$this->assertSame(sprintf('%s/some-class.php', $tempPath), $files[1]->__toString());
130+
$this->assertSame(sprintf('%s/solution.php', $expectedBaseDir), $files[0]->__toString());
131+
$this->assertSame(sprintf('%s/some-class.php', $expectedBaseDir), $files[1]->__toString());
114132

133+
unlink(sprintf('%s/solution.php', $expectedBaseDir));
134+
unlink(sprintf('%s/some-class.php', $expectedBaseDir));
135+
unlink(sprintf('%s/exclude.txt', $expectedBaseDir));
115136
unlink(sprintf('%s/solution.php', $tempPath));
116137
unlink(sprintf('%s/some-class.php', $tempPath));
117138
unlink(sprintf('%s/exclude.txt', $tempPath));
118139
rmdir($tempPath);
140+
rmdir($expectedBaseDir);
119141
}
120142

121143
public function testWithNestedDirectories(): void
@@ -134,15 +156,17 @@ public function testWithNestedDirectories(): void
134156

135157
$solution = DirectorySolution::fromDirectory($tempPath);
136158

137-
$this->assertSame(sprintf('%s/solution.php', $tempPath), $solution->getEntryPoint());
159+
$expectedBaseDir = SolutionPathTransformer::tempPathToSolutionTempPath($tempPath);
160+
161+
$this->assertSame(sprintf('%s/solution.php', $expectedBaseDir), $solution->getEntryPoint());
138162
$files = $solution->getFiles();
139163
$this->assertCount(5, $files);
140164

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());
165+
$this->assertSame(sprintf('%s/composer.json', $expectedBaseDir), $files[0]->__toString());
166+
$this->assertSame(sprintf('%s/nested/another-class.php', $expectedBaseDir), $files[1]->__toString());
167+
$this->assertSame(sprintf('%s/nested/deep/even-more.php', $expectedBaseDir), $files[2]->__toString());
168+
$this->assertSame(sprintf('%s/solution.php', $expectedBaseDir), $files[3]->__toString());
169+
$this->assertSame(sprintf('%s/some-class.php', $expectedBaseDir), $files[4]->__toString());
146170

147171
unlink(sprintf('%s/solution.php', $tempPath));
148172
unlink(sprintf('%s/some-class.php', $tempPath));
@@ -152,6 +176,14 @@ public function testWithNestedDirectories(): void
152176
rmdir(sprintf('%s/nested/deep', $tempPath));
153177
rmdir(sprintf('%s/nested', $tempPath));
154178
rmdir($tempPath);
179+
unlink(sprintf('%s/solution.php', $expectedBaseDir));
180+
unlink(sprintf('%s/some-class.php', $expectedBaseDir));
181+
unlink(sprintf('%s/composer.json', $expectedBaseDir));
182+
unlink(sprintf('%s/nested/another-class.php', $expectedBaseDir));
183+
unlink(sprintf('%s/nested/deep/even-more.php', $expectedBaseDir));
184+
rmdir(sprintf('%s/nested/deep', $expectedBaseDir));
185+
rmdir(sprintf('%s/nested', $expectedBaseDir));
186+
rmdir($expectedBaseDir);
155187
}
156188

157189
public function testExceptionsWithNestedDirectories(): void
@@ -175,12 +207,14 @@ public function testExceptionsWithNestedDirectories(): void
175207

176208
$solution = DirectorySolution::fromDirectory($tempPath, $exclusions);
177209

178-
$this->assertSame(sprintf('%s/solution.php', $tempPath), $solution->getEntryPoint());
210+
$expectedBaseDir = SolutionPathTransformer::tempPathToSolutionTempPath($tempPath);
211+
212+
$this->assertSame(sprintf('%s/solution.php', $expectedBaseDir), $solution->getEntryPoint());
179213
$files = $solution->getFiles();
180214
$this->assertCount(2, $files);
181215

182-
$this->assertSame(sprintf('%s/solution.php', $tempPath), $files[0]->__toString());
183-
$this->assertSame(sprintf('%s/some-class.php', $tempPath), $files[1]->__toString());
216+
$this->assertSame(sprintf('%s/solution.php', $expectedBaseDir), $files[0]->__toString());
217+
$this->assertSame(sprintf('%s/some-class.php', $expectedBaseDir), $files[1]->__toString());
184218

185219
unlink(sprintf('%s/solution.php', $tempPath));
186220
unlink(sprintf('%s/some-class.php', $tempPath));
@@ -193,5 +227,16 @@ public function testExceptionsWithNestedDirectories(): void
193227
rmdir(sprintf('%s/vendor/somelib', $tempPath));
194228
rmdir(sprintf('%s/vendor', $tempPath));
195229
rmdir($tempPath);
230+
unlink(sprintf('%s/solution.php', $expectedBaseDir));
231+
unlink(sprintf('%s/some-class.php', $expectedBaseDir));
232+
unlink(sprintf('%s/exclude.txt', $expectedBaseDir));
233+
unlink(sprintf('%s/nested/exclude.txt', $expectedBaseDir));
234+
unlink(sprintf('%s/nested/deep/exclude.txt', $expectedBaseDir));
235+
unlink(sprintf('%s/vendor/somelib/app.php', $expectedBaseDir));
236+
rmdir(sprintf('%s/nested/deep', $expectedBaseDir));
237+
rmdir(sprintf('%s/nested', $expectedBaseDir));
238+
rmdir(sprintf('%s/vendor/somelib', $expectedBaseDir));
239+
rmdir(sprintf('%s/vendor', $expectedBaseDir));
240+
rmdir($expectedBaseDir);
196241
}
197242
}

test/Solution/SingleFileSolutionTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,30 @@
33
namespace PhpSchool\PhpWorkshopTest\Solution;
44

55
use PhpSchool\PhpWorkshop\Solution\SingleFileSolution;
6+
use PhpSchool\PhpWorkshop\TestUtils\SolutionPathTransformer;
67
use PHPUnit\Framework\TestCase;
78

89
class SingleFileSolutionTest extends TestCase
910
{
1011
public function testGetters(): void
1112
{
12-
$tempPath = sprintf('%s/%s', realpath(sys_get_temp_dir()), $this->getName());
13+
$tmpDir = realpath(sys_get_temp_dir());
14+
$tempPath = sprintf('%s/%s', $tmpDir, $this->getName());
1315
$filePath = sprintf('%s/test.file', $tempPath);
1416

1517
@mkdir($tempPath, 0775, true);
1618
touch($filePath);
1719

1820
$solution = SingleFileSolution::fromFile($filePath);
1921

20-
$this->assertSame($filePath, $solution->getEntryPoint());
21-
$this->assertSame($tempPath, $solution->getBaseDirectory());
22+
$expectedBaseDir = SolutionPathTransformer::tempPathToSolutionTempPath($tempPath);
23+
$expectedFilePath = SolutionPathTransformer::tempPathToSolutionTempPath($filePath);
24+
25+
$this->assertSame($expectedFilePath, $solution->getEntryPoint());
26+
$this->assertSame($expectedBaseDir, $solution->getBaseDirectory());
2227
$this->assertFalse($solution->hasComposerFile());
2328
$this->assertCount(1, $solution->getFiles());
24-
$this->assertSame($filePath, $solution->getFiles()[0]->__toString());
29+
$this->assertSame($expectedFilePath, $solution->getFiles()[0]->__toString());
2530
unlink($filePath);
2631
rmdir($tempPath);
2732
}

0 commit comments

Comments
 (0)