Skip to content

Commit f7215a4

Browse files
authored
Merge pull request #223 from php-school/more-test-utils
More test utils
2 parents 2677e76 + 9b3988b commit f7215a4

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/TestUtils/WorkshopExerciseTest.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
use PhpSchool\PhpWorkshop\Result\ResultGroupInterface;
2222
use PhpSchool\PhpWorkshop\Result\ResultInterface;
2323
use PhpSchool\PhpWorkshop\ResultAggregator;
24+
use PhpSchool\PhpWorkshop\Utils\ArrayObject;
2425
use PhpSchool\PhpWorkshop\Utils\Collection;
2526
use PHPUnit\Framework\ExpectationFailedException;
2627
use PHPUnit\Framework\TestCase;
2728
use Psr\Container\ContainerInterface;
2829
use PhpSchool\PhpWorkshop\Input\Input;
30+
use Symfony\Component\Filesystem\Filesystem;
2931
use Symfony\Component\Process\Process;
3032

3133
abstract class WorkshopExerciseTest extends TestCase
@@ -58,7 +60,7 @@ abstract public function getExerciseClass(): string;
5860

5961
abstract public function getApplication(): Application;
6062

61-
private function getExercise(): ExerciseInterface
63+
protected function getExercise(): ExerciseInterface
6264
{
6365
return $this->container->get(ExerciseRepository::class)
6466
->findByClassName($this->getExerciseClass());
@@ -147,7 +149,23 @@ public function assertResultsHasFailure(string $resultClass, string $reason): vo
147149
return $failure->getReason() === $reason;
148150
});
149151

150-
$this->assertCount(1, $failures, "No failure with reason: '$reason'");
152+
$allFailures = (new Collection($this->results->getIterator()->getArrayCopy()))
153+
->filter(function (ResultInterface $result) {
154+
return $result instanceof FailureInterface;
155+
})
156+
->map(function (FailureInterface $failure) {
157+
return sprintf(
158+
' * %s%s',
159+
get_class($failure),
160+
$failure instanceof Failure ? ": {$failure->getReason()}" : ''
161+
);
162+
});
163+
164+
$help = $allFailures->isEmpty()
165+
? ""
166+
: sprintf("\n\nAll Failures:\n\n\n", $allFailures->implode("\n"));
167+
168+
$this->assertCount(1, $failures, "No failure with reason: '$reason' . $help");
151169
}
152170

153171
public function assertOutputWasIncorrect(): void
@@ -203,4 +221,16 @@ public function assertResultsHasFailureAndMatches(string $resultClass, callable
203221
$this->assertTrue($matcher($failure));
204222
});
205223
}
224+
225+
public function removeSolutionAsset(string $file): void
226+
{
227+
$path = sprintf(
228+
'%s/test/solutions/%s/%s',
229+
rtrim($this->container->get('basePath'), '/'),
230+
AbstractExercise::normaliseName($this->getExercise()->getName()),
231+
$file
232+
);
233+
234+
(new Filesystem())->remove($path);
235+
}
206236
}

0 commit comments

Comments
 (0)