|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpSchool\PhpWorkshopTest\ExerciseRunner; |
| 4 | + |
| 5 | +use PhpSchool\PhpWorkshop\Event\EventDispatcher; |
| 6 | +use PhpSchool\PhpWorkshop\Event\ExerciseRunnerEvent; |
| 7 | +use PhpSchool\PhpWorkshop\Exercise\MockExercise; |
| 8 | +use PhpSchool\PhpWorkshop\Exercise\Scenario\CliScenario; |
| 9 | +use PhpSchool\PhpWorkshop\ExerciseRunner\Context\TestContext; |
| 10 | +use PhpSchool\PhpWorkshop\ExerciseRunner\EnvironmentManager; |
| 11 | +use PhpSchool\PhpWorkshop\Input\Input; |
| 12 | +use PhpSchool\PhpWorkshop\ResultAggregator; |
| 13 | +use PhpSchool\PhpWorkshop\Solution\DirectorySolution; |
| 14 | +use PhpSchool\PhpWorkshop\Solution\SingleFileSolution; |
| 15 | +use PhpSchool\PhpWorkshopTest\Asset\CliExerciseImpl; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Symfony\Component\Filesystem\Filesystem; |
| 18 | + |
| 19 | +class EnvironmentManagerTest extends TestCase |
| 20 | +{ |
| 21 | + public function testPrepareStudentCopiesAllScenarioFilesToExecutionDirectory(): void |
| 22 | + { |
| 23 | + $context = new TestContext(); |
| 24 | + $context->createStudentSolutionDirectory(); |
| 25 | + |
| 26 | + $scenario = (new CliScenario()) |
| 27 | + ->withFile('file.txt', 'content') |
| 28 | + ->withFile('file2.txt', 'content2'); |
| 29 | + |
| 30 | + $manager = new EnvironmentManager(new Filesystem(), new EventDispatcher(new ResultAggregator())); |
| 31 | + $manager->prepareStudent($context, $scenario); |
| 32 | + |
| 33 | + static::assertStringEqualsFile($context->getStudentExecutionDirectory() . '/file.txt', 'content'); |
| 34 | + static::assertStringEqualsFile($context->getStudentExecutionDirectory() . '/file2.txt', 'content2'); |
| 35 | + } |
| 36 | + |
| 37 | + public function testPrepareReferenceCopiesAllScenarioFilesAndSolutionFilesToExecutionDirectory(): void |
| 38 | + { |
| 39 | + $exercise = new CliExerciseImpl(); |
| 40 | + $solution = SingleFileSolution::fromFile(realpath(__DIR__ . '/../res/cli/solution.php')); |
| 41 | + $exercise->setSolution($solution); |
| 42 | + |
| 43 | + $context = new TestContext($exercise); |
| 44 | + |
| 45 | + $scenario = (new CliScenario()) |
| 46 | + ->withFile('file.txt', 'content') |
| 47 | + ->withFile('file2.txt', 'content2'); |
| 48 | + |
| 49 | + $manager = new EnvironmentManager(new Filesystem(), new EventDispatcher(new ResultAggregator())); |
| 50 | + $manager->prepareReference($context, $scenario); |
| 51 | + |
| 52 | + static::assertFileEquals($context->getReferenceExecutionDirectory() . '/solution.php', __DIR__ . '/../res/cli/solution.php'); |
| 53 | + static::assertStringEqualsFile($context->getReferenceExecutionDirectory() . '/file.txt', 'content'); |
| 54 | + static::assertStringEqualsFile($context->getReferenceExecutionDirectory() . '/file2.txt', 'content2'); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @dataProvider finishEvents |
| 59 | + */ |
| 60 | + public function testFileAreCleanedUpOnlyWhenFinishEventIsDispatched(string $eventName): void |
| 61 | + { |
| 62 | + $exercise = new CliExerciseImpl(); |
| 63 | + $solution = SingleFileSolution::fromFile(realpath(__DIR__ . '/../res/cli/solution.php')); |
| 64 | + $exercise->setSolution($solution); |
| 65 | + |
| 66 | + $context = new TestContext($exercise); |
| 67 | + $context->createStudentSolutionDirectory(); |
| 68 | + |
| 69 | + $scenario = (new CliScenario()) |
| 70 | + ->withFile('file.txt', 'content') |
| 71 | + ->withFile('file2.txt', 'content2'); |
| 72 | + |
| 73 | + $eventDispatcher = new EventDispatcher(new ResultAggregator()); |
| 74 | + $manager = new EnvironmentManager(new Filesystem(), $eventDispatcher); |
| 75 | + $manager->prepareStudent($context, $scenario); |
| 76 | + $manager->prepareReference($context, $scenario); |
| 77 | + |
| 78 | + static::assertFileExists($context->getStudentExecutionDirectory()); |
| 79 | + static::assertFileExists($context->getReferenceExecutionDirectory()); |
| 80 | + |
| 81 | + $eventDispatcher->dispatch(new ExerciseRunnerEvent($eventName, $exercise, new Input('app', ['program' => '']))); |
| 82 | + |
| 83 | + static::assertFileExists($context->getStudentExecutionDirectory()); |
| 84 | + static::assertFileNotExists($context->getReferenceExecutionDirectory() . '/file.txt'); |
| 85 | + static::assertFileNotExists($context->getReferenceExecutionDirectory() . '/file2.txt'); |
| 86 | + static::assertFileNotExists($context->getReferenceExecutionDirectory()); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @return array<array{0: string}> |
| 91 | + */ |
| 92 | + public function finishEvents(): array |
| 93 | + { |
| 94 | + return [ |
| 95 | + ['run.finish'], |
| 96 | + ['verify.finish'], |
| 97 | + ]; |
| 98 | + } |
| 99 | +} |
0 commit comments