Skip to content

Commit 0790024

Browse files
committed
Copy test solutions to temporary directory
1 parent 86979be commit 0790024

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

src/TestUtils/WorkshopExerciseTest.php

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use PhpSchool\PhpWorkshop\Result\ResultInterface;
2222
use PhpSchool\PhpWorkshop\ResultAggregator;
2323
use PhpSchool\PhpWorkshop\Utils\Collection;
24+
use PhpSchool\PhpWorkshop\Utils\Path;
2425
use PhpSchool\PhpWorkshop\Utils\System;
2526
use PHPUnit\Framework\ExpectationFailedException;
2627
use PHPUnit\Framework\TestCase;
@@ -34,30 +35,27 @@
3435

3536
abstract class WorkshopExerciseTest extends TestCase
3637
{
37-
/**
38-
* @var Application
39-
*/
40-
protected $app;
38+
private Application $app;
39+
private ContainerInterface $container;
40+
private ResultAggregator $results;
4141

42-
/**
43-
* @var ContainerInterface
44-
*/
45-
protected $container;
42+
private Filesystem $filesystem;
43+
private string $executionDirectory;
4644

47-
/**
48-
* @var ResultAggregator
49-
*/
50-
protected $results;
45+
public const SINGLE_FILE_SOLUTION = 'single-file-solution';
46+
public const DIRECTORY_SOLUTION = 'directory-solution';
5147

5248
public function setUp(): void
5349
{
5450
$this->app = $this->getApplication();
5551
$this->container = $this->app->configure();
52+
$this->filesystem = new Filesystem();
53+
$this->executionDirectory = System::randomTempDir();
5654
}
5755

5856
public function tearDown(): void
5957
{
60-
// (new Filesystem())->remove(System::tempDir());
58+
$this->filesystem->remove($this->executionDirectory);
6159
}
6260

6361
/**
@@ -73,8 +71,12 @@ protected function getExercise(): ExerciseInterface
7371
->findByClassName($this->getExerciseClass());
7472
}
7573

76-
public function runExercise(string $submissionFile): void
74+
public function runExercise(string $submissionFile, string $type = self::SINGLE_FILE_SOLUTION): void
7775
{
76+
//we copy the test solution to a random directory
77+
//so we can properly cleanup. It also saves us from patch crashes.
78+
$this->filesystem->mkdir($this->executionDirectory);
79+
7880
$exercise = $this->getExercise();
7981

8082
$submissionFileAbsolute = sprintf(
@@ -94,6 +96,14 @@ public function runExercise(string $submissionFile): void
9496
);
9597
}
9698

99+
if ($type === self::SINGLE_FILE_SOLUTION) {
100+
$this->filesystem->copy($submissionFileAbsolute, Path::join($this->executionDirectory, $submissionFile));
101+
} else {
102+
$this->filesystem->mirror(dirname($submissionFileAbsolute), $this->executionDirectory);
103+
}
104+
105+
$submissionFileAbsolute = Path::join($this->executionDirectory, basename($submissionFile));
106+
97107
if ($exercise instanceof ComposerExerciseCheck) {
98108
$this->installDeps($exercise, dirname($submissionFileAbsolute));
99109
}
@@ -102,7 +112,7 @@ public function runExercise(string $submissionFile): void
102112
'program' => $submissionFileAbsolute,
103113
]);
104114

105-
$this->results = $this->container->get(ExerciseDispatcher::class)
115+
$this->results = $this->container->make(ExerciseDispatcher::class)
106116
->verify($exercise, $input);
107117
}
108118

@@ -237,16 +247,4 @@ public function assertResultsHasFailureAndMatches(string $resultClass, callable
237247
$this->assertTrue($matcher($failure));
238248
});
239249
}
240-
241-
public function removeSolutionAsset(string $file): void
242-
{
243-
$path = sprintf(
244-
'%s/test/solutions/%s/%s',
245-
rtrim($this->container->get('basePath'), '/'),
246-
AbstractExercise::normaliseName($this->getExercise()->getName()),
247-
$file,
248-
);
249-
250-
(new Filesystem())->remove($path);
251-
}
252250
}

0 commit comments

Comments
 (0)