|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpSchool\PhpWorkshop\ExerciseRunner\Context; |
| 4 | + |
| 5 | +use PhpSchool\PhpWorkshop\Exception\RuntimeException; |
| 6 | +use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface; |
| 7 | +use PhpSchool\PhpWorkshop\Exercise\MockExercise; |
| 8 | +use PhpSchool\PhpWorkshop\Input\Input; |
| 9 | +use PhpSchool\PhpWorkshop\Solution\SolutionInterface; |
| 10 | +use PhpSchool\PhpWorkshop\Utils\System; |
| 11 | +use Symfony\Component\Filesystem\Filesystem; |
| 12 | +use PhpSchool\PhpWorkshop\Utils\Path; |
| 13 | + |
| 14 | +class TestContext extends ExecutionContext |
| 15 | +{ |
| 16 | + public Filesystem $filesystem; |
| 17 | + public ExerciseInterface $exercise; |
| 18 | + |
| 19 | + private function __construct( |
| 20 | + ExerciseInterface $exercise = null, |
| 21 | + Input $input = null |
| 22 | + ) { |
| 23 | + $this->exercise = $exercise ?? new MockExercise(); |
| 24 | + |
| 25 | + $this->filesystem = new Filesystem(); |
| 26 | + |
| 27 | + parent::__construct( |
| 28 | + System::randomTempDir(), |
| 29 | + System::randomTempDir(), |
| 30 | + $this->exercise, |
| 31 | + $input ? $input : new Input('test', ['program' => 'solution.php']), |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + public function importStudentFileFromString(string $content, string $filename = 'solution.php'): void |
| 36 | + { |
| 37 | + if (!$this->filesystem->exists($this->getStudentExecutionDirectory())) { |
| 38 | + throw new RuntimeException( |
| 39 | + sprintf('Execution directories not created. Use %s::withDirectories() method instead.', self::class) |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + file_put_contents(Path::join($this->getStudentExecutionDirectory(), $filename), $content); |
| 44 | + } |
| 45 | + |
| 46 | + public function importStudentSolution(string $file): void |
| 47 | + { |
| 48 | + if (!$this->filesystem->exists($this->getStudentExecutionDirectory())) { |
| 49 | + throw new RuntimeException( |
| 50 | + sprintf('Execution directories not created. Use %s::withDirectories() method instead.', self::class) |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + copy($file, Path::join($this->getStudentExecutionDirectory(), 'solution.php')); |
| 55 | + } |
| 56 | + |
| 57 | + public function importStudentSolutionFolder(string $folder): void |
| 58 | + { |
| 59 | + if (!$this->filesystem->exists($this->getStudentExecutionDirectory())) { |
| 60 | + throw new RuntimeException( |
| 61 | + sprintf('Execution directories not created. Use %s::withDirectories() method instead.', self::class) |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + $this->filesystem->mirror($folder, $this->getStudentExecutionDirectory()); |
| 66 | + } |
| 67 | + |
| 68 | + public function importReferenceFileFromString(string $content, string $filename = 'solution.php'): void |
| 69 | + { |
| 70 | + if (!$this->filesystem->exists($this->getReferenceExecutionDirectory())) { |
| 71 | + throw new RuntimeException( |
| 72 | + sprintf('Execution directories not created. Use %s::withDirectories() method instead.', self::class) |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + file_put_contents(Path::join($this->getReferenceExecutionDirectory(), $filename), $content); |
| 77 | + } |
| 78 | + |
| 79 | + public function importReferenceSolution(SolutionInterface $solution): void |
| 80 | + { |
| 81 | + if (!$this->filesystem->exists($this->getReferenceExecutionDirectory())) { |
| 82 | + throw new RuntimeException( |
| 83 | + sprintf('Execution directories not created. Use %s::withDirectories() method instead.', self::class) |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + foreach ($solution->getFiles() as $file) { |
| 88 | + $this->filesystem->copy( |
| 89 | + $file->getAbsolutePath(), |
| 90 | + Path::join($this->getReferenceExecutionDirectory(), $file->getRelativePath()) |
| 91 | + ); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public static function withDirectories(Input $input = null, ExerciseInterface $exercise = null): self |
| 96 | + { |
| 97 | + $self = new self($exercise, $input); |
| 98 | + |
| 99 | + $self->filesystem->mkdir($self->getStudentExecutionDirectory()); |
| 100 | + $self->filesystem->mkdir($self->getReferenceExecutionDirectory()); |
| 101 | + |
| 102 | + return $self; |
| 103 | + } |
| 104 | + |
| 105 | + public static function withoutDirectories(Input $input = null, ExerciseInterface $exercise = null): self |
| 106 | + { |
| 107 | + return new self($exercise, $input); |
| 108 | + } |
| 109 | + |
| 110 | + public function __destruct() |
| 111 | + { |
| 112 | + $this->filesystem->remove($this->getStudentExecutionDirectory()); |
| 113 | + $this->filesystem->remove($this->getReferenceExecutionDirectory()); |
| 114 | + } |
| 115 | +} |
0 commit comments