|  | 
|  | 1 | +<?php | 
|  | 2 | + | 
|  | 3 | +namespace PhpSchool\PhpWorkshop\ExerciseRunner\Context; | 
|  | 4 | + | 
|  | 5 | +use PhpSchool\PhpWorkshop\Exception\RuntimeException; | 
|  | 6 | +use PhpSchool\PhpWorkshop\Exercise\CgiExercise; | 
|  | 7 | +use PhpSchool\PhpWorkshop\Exercise\CliExercise; | 
|  | 8 | +use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface; | 
|  | 9 | +use PhpSchool\PhpWorkshop\Exercise\MockExercise; | 
|  | 10 | +use PhpSchool\PhpWorkshop\Input\Input; | 
|  | 11 | +use PhpSchool\PhpWorkshop\Solution\SolutionInterface; | 
|  | 12 | +use PhpSchool\PhpWorkshop\Utils\System; | 
|  | 13 | +use Symfony\Component\Filesystem\Filesystem; | 
|  | 14 | +use PhpSchool\PhpWorkshop\Utils\Path; | 
|  | 15 | + | 
|  | 16 | +class TestContext extends ExecutionContext | 
|  | 17 | +{ | 
|  | 18 | + private Filesystem $filesystem; | 
|  | 19 | + private ExerciseInterface $exercise; | 
|  | 20 | + private bool $studentSolutionDirWasCreated = false; | 
|  | 21 | + private bool $referenceSolutionDirWasCreated = false; | 
|  | 22 | + | 
|  | 23 | + public function __construct( | 
|  | 24 | + ExerciseInterface $exercise = null, | 
|  | 25 | + Input $input = null, | 
|  | 26 | + string $studentDirectory = null, | 
|  | 27 | + ) { | 
|  | 28 | + $this->exercise = $exercise ?? new MockExercise(); | 
|  | 29 | + | 
|  | 30 | + $this->filesystem = new Filesystem(); | 
|  | 31 | + | 
|  | 32 | + if ($studentDirectory === null) { | 
|  | 33 | + $studentDirectory = System::randomTempDir(); | 
|  | 34 | + } | 
|  | 35 | + | 
|  | 36 | + parent::__construct( | 
|  | 37 | + $studentDirectory, | 
|  | 38 | + System::randomTempDir(), | 
|  | 39 | + $this->exercise, | 
|  | 40 | + $input ? $input : new Input('test', ['program' => 'solution.php']), | 
|  | 41 | + ); | 
|  | 42 | + } | 
|  | 43 | + | 
|  | 44 | + public function createStudentSolutionDirectory(): void | 
|  | 45 | + { | 
|  | 46 | + $this->filesystem->mkdir($this->getStudentExecutionDirectory()); | 
|  | 47 | + $this->studentSolutionDirWasCreated = true; | 
|  | 48 | + } | 
|  | 49 | + | 
|  | 50 | + public function createReferenceSolutionDirectory(): void | 
|  | 51 | + { | 
|  | 52 | + $this->filesystem->mkdir($this->getReferenceExecutionDirectory()); | 
|  | 53 | + $this->referenceSolutionDirWasCreated = true; | 
|  | 54 | + } | 
|  | 55 | + | 
|  | 56 | + public function importStudentFileFromString(string $content, string $filename = 'solution.php'): void | 
|  | 57 | + { | 
|  | 58 | + if (!$this->studentSolutionDirWasCreated) { | 
|  | 59 | + throw new RuntimeException( | 
|  | 60 | + sprintf('Student execution directory not created. Call %s::createStudentSolutionDirectory() first.', self::class) | 
|  | 61 | + ); | 
|  | 62 | + } | 
|  | 63 | + | 
|  | 64 | + file_put_contents(Path::join($this->getStudentExecutionDirectory(), $filename), $content); | 
|  | 65 | + } | 
|  | 66 | + | 
|  | 67 | + public function importReferenceFileFromString(string $content, string $filename = 'solution.php'): void | 
|  | 68 | + { | 
|  | 69 | + if (!$this->referenceSolutionDirWasCreated) { | 
|  | 70 | + throw new RuntimeException( | 
|  | 71 | + sprintf('Reference execution directory not created. Call %s::createReferenceSolutionDirectory() first.', self::class) | 
|  | 72 | + ); | 
|  | 73 | + } | 
|  | 74 | + | 
|  | 75 | + file_put_contents(Path::join($this->getReferenceExecutionDirectory(), $filename), $content); | 
|  | 76 | + } | 
|  | 77 | + | 
|  | 78 | + public static function fromExerciseAndStudentSolution(ExerciseInterface $exercise, string $file): self | 
|  | 79 | + { | 
|  | 80 | + if (file_exists($file)) { | 
|  | 81 | + $file = (string) realpath($file); | 
|  | 82 | + } | 
|  | 83 | + | 
|  | 84 | + $input = new Input('test', ['program' => $file]); | 
|  | 85 | + return new self( | 
|  | 86 | + exercise: $exercise, | 
|  | 87 | + input: $input, | 
|  | 88 | + studentDirectory: dirname($file) | 
|  | 89 | + ); | 
|  | 90 | + } | 
|  | 91 | + | 
|  | 92 | + public function __destruct() | 
|  | 93 | + { | 
|  | 94 | + if ($this->studentSolutionDirWasCreated) { | 
|  | 95 | + $this->filesystem->remove($this->getStudentExecutionDirectory()); | 
|  | 96 | + } | 
|  | 97 | + | 
|  | 98 | + if ($this->referenceSolutionDirWasCreated) { | 
|  | 99 | + $this->filesystem->remove($this->getReferenceExecutionDirectory()); | 
|  | 100 | + } | 
|  | 101 | + } | 
|  | 102 | +} | 
0 commit comments