|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpSchool\PhpWorkshop\TestUtils; |
| 6 | + |
| 7 | +use PhpSchool\PhpWorkshop\Application; |
| 8 | +use PhpSchool\PhpWorkshop\Exception\InvalidArgumentException; |
| 9 | +use PhpSchool\PhpWorkshop\Exercise\AbstractExercise; |
| 10 | +use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface; |
| 11 | +use PhpSchool\PhpWorkshop\Exercise\ExerciseType; |
| 12 | +use PhpSchool\PhpWorkshop\ExerciseDispatcher; |
| 13 | +use PhpSchool\PhpWorkshop\ExerciseRepository; |
| 14 | +use PhpSchool\PhpWorkshop\Result\Cgi\CgiResult; |
| 15 | +use PhpSchool\PhpWorkshop\Result\Cli\CliResult; |
| 16 | +use PhpSchool\PhpWorkshop\Result\Failure; |
| 17 | +use PhpSchool\PhpWorkshop\Result\FailureInterface; |
| 18 | +use PhpSchool\PhpWorkshop\Result\ResultInterface; |
| 19 | +use PhpSchool\PhpWorkshop\ResultAggregator; |
| 20 | +use PhpSchool\PhpWorkshop\Utils\Collection; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | +use Psr\Container\ContainerInterface; |
| 23 | +use PhpSchool\PhpWorkshop\Input\Input; |
| 24 | + |
| 25 | +abstract class WorkshopExerciseTest extends TestCase |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var Application |
| 29 | + */ |
| 30 | + protected $app; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var ContainerInterface |
| 34 | + */ |
| 35 | + protected $container; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var ResultAggregator |
| 39 | + */ |
| 40 | + protected $results; |
| 41 | + |
| 42 | + public function setUp(): void |
| 43 | + { |
| 44 | + $this->app = $this->getApplication(); |
| 45 | + $this->container = $this->app->configure(); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @return class-string |
| 50 | + */ |
| 51 | + abstract public function getExerciseClass(): string; |
| 52 | + |
| 53 | + abstract public function getApplication(): Application; |
| 54 | + |
| 55 | + private function getExercise(): ExerciseInterface |
| 56 | + { |
| 57 | + return $this->container->get(ExerciseRepository::class) |
| 58 | + ->findByClassName($this->getExerciseClass()); |
| 59 | + } |
| 60 | + |
| 61 | + public function runExercise(string $submissionFile) |
| 62 | + { |
| 63 | + $exercise = $this->getExercise(); |
| 64 | + |
| 65 | + $submissionFileAbsolute = sprintf( |
| 66 | + '%s/test/solutions/%s/%s', |
| 67 | + rtrim($this->container->get('basePath'), '/'), |
| 68 | + AbstractExercise::normaliseName($exercise->getName()), |
| 69 | + $submissionFile |
| 70 | + ); |
| 71 | + |
| 72 | + if (!file_exists($submissionFileAbsolute)) { |
| 73 | + throw new InvalidArgumentException( |
| 74 | + sprintf( |
| 75 | + 'Submission file "%s" does not exist in "%s"', |
| 76 | + $submissionFile, |
| 77 | + dirname($submissionFileAbsolute) |
| 78 | + ) |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + $input = new Input($this->container->get('appName'), [ |
| 83 | + 'program' => $submissionFileAbsolute |
| 84 | + ]); |
| 85 | + |
| 86 | + $this->results = $this->container->get(ExerciseDispatcher::class) |
| 87 | + ->verify($exercise, $input); |
| 88 | + } |
| 89 | + |
| 90 | + public function assertVerifyWasSuccessful(): void |
| 91 | + { |
| 92 | + $failures = (new Collection($this->results->getIterator()->getArrayCopy())) |
| 93 | + ->filter(function (ResultInterface $result) { |
| 94 | + return $result instanceof FailureInterface; |
| 95 | + }) |
| 96 | + ->map(function (Failure $failure) { |
| 97 | + return $failure->getReason(); |
| 98 | + }) |
| 99 | + ->implode(', '); |
| 100 | + |
| 101 | + |
| 102 | + $this->assertTrue($this->results->isSuccessful(), $failures); |
| 103 | + } |
| 104 | + |
| 105 | + public function assertVerifyWasNotSuccessful(): void |
| 106 | + { |
| 107 | + $this->assertFalse($this->results->isSuccessful()); |
| 108 | + } |
| 109 | + |
| 110 | + public function assertResultCount(int $count): void |
| 111 | + { |
| 112 | + $this->assertCount($count, $this->results); |
| 113 | + } |
| 114 | + |
| 115 | + public function assertResultsHasFailure(string $resultClass, string $reason): void |
| 116 | + { |
| 117 | + $failures = (new Collection($this->results->getIterator()->getArrayCopy())) |
| 118 | + ->filter(function (ResultInterface $result) { |
| 119 | + return $result instanceof Failure; |
| 120 | + }) |
| 121 | + ->filter(function (Failure $failure) use ($reason) { |
| 122 | + return $failure->getReason() === $reason; |
| 123 | + }); |
| 124 | + |
| 125 | + $this->assertCount(1, $failures, "No failure with reason: '$reason'"); |
| 126 | + } |
| 127 | + |
| 128 | + public function assertOutputWasIncorrect(): void |
| 129 | + { |
| 130 | + $exerciseType = $this->getExercise()->getType(); |
| 131 | + |
| 132 | + if ($exerciseType->equals(ExerciseType::CLI())) { |
| 133 | + $results = (new Collection($this->results->getIterator()->getArrayCopy())) |
| 134 | + ->filter(function (ResultInterface $result) { |
| 135 | + return $result instanceof CliResult; |
| 136 | + }); |
| 137 | + |
| 138 | + $this->assertCount(1, $results); |
| 139 | + } |
| 140 | + |
| 141 | + if ($exerciseType->equals(ExerciseType::CGI())) { |
| 142 | + $results = (new Collection($this->results->getIterator()->getArrayCopy())) |
| 143 | + ->filter(function (ResultInterface $result) { |
| 144 | + return $result instanceof CgiResult; |
| 145 | + }); |
| 146 | + |
| 147 | + $this->assertCount(1, $results); |
| 148 | + } |
| 149 | + |
| 150 | + $outputResults = $results->values()->get(0); |
| 151 | + |
| 152 | + $this->assertFalse($outputResults->isSuccessful()); |
| 153 | + } |
| 154 | +} |
0 commit comments