|
| 1 | +# PHPUnit extension |
| 2 | + |
| 3 | +To help debug mapping errors thrown during your test suite, an extension for |
| 4 | +[PHPUnit] is provided. It will collect any uncaught instances of `MappingError` |
| 5 | +and display them per test in a formatted table at the end of the test run. |
| 6 | + |
| 7 | +If installed correctly and a mapping error is thrown, PHPUnit will show a table |
| 8 | +after execution for each test case with errors: |
| 9 | + |
| 10 | +``` |
| 11 | +The following Valinor mapping errors were thrown: |
| 12 | +
|
| 13 | ++---------- CuyZ\Valinor\QA\StaticAnalysis\phpunit\MappingTest -----------+ |
| 14 | +| Method | Path | Error | |
| 15 | ++-----------------------------+------+------------------------------------+ |
| 16 | +| testMappingWhichThrowsError | foo | Value null is not a valid string. | |
| 17 | +| testMappingWhichThrowsError | bar | Value null is not a valid boolean. | |
| 18 | ++-----------------------------+------+------------------------------------+ |
| 19 | +``` |
| 20 | + |
| 21 | +**Activating** |
| 22 | + |
| 23 | +To activate this feature, the extension must be registered with PHPUnit: |
| 24 | + |
| 25 | +```xml title="phpunit.dist.xml" |
| 26 | +<extensions> |
| 27 | + <bootstrap class="CuyZ\Valinor\QA\PHPUnit\PrettyPrintMappingErrorsExtension"/> |
| 28 | +</extensions> |
| 29 | +``` |
| 30 | + |
| 31 | +Additionally, every test case where `MappingError` may be thrown also needs to |
| 32 | +have a trait added to it: |
| 33 | + |
| 34 | +```php title="MyTestCase.php" |
| 35 | +<?php |
| 36 | + |
| 37 | +use CuyZ\Valinor\QA\PHPUnit\CollectValinorMappingErrors; |
| 38 | +use PHPUnit\Framework\TestCase; |
| 39 | + |
| 40 | +final class MappingTest extends TestCase |
| 41 | +{ |
| 42 | + use CollectValinorMappingErrors; |
| 43 | + |
| 44 | + // ... |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +This trait overrides the `TestCase::transformException` method, so if you have |
| 49 | +overridden this method already you will have to call the trait method yourself. |
| 50 | + |
| 51 | +```php title="MyTestCase.php" |
| 52 | +<?php |
| 53 | + |
| 54 | +use CuyZ\Valinor\QA\PHPUnit\CollectValinorMappingErrors; |
| 55 | +use PHPUnit\Framework\TestCase; |
| 56 | +use Throwable; |
| 57 | + |
| 58 | +final class MappingTest extends TestCase |
| 59 | +{ |
| 60 | + use CollectValinorMappingErrors { transformException as protected collectValinorMappingErrors; } |
| 61 | + |
| 62 | + protected function transformException(Throwable $t): Throwable |
| 63 | + { |
| 64 | + // ... |
| 65 | + |
| 66 | + return $this->collectValinorMappingErrors($t); |
| 67 | + } |
| 68 | +``` |
0 commit comments