Skip to content

Commit a8e570e

Browse files
author
NanoSector
committed
doc: Document PHPUnit extension usage
1 parent f65d617 commit a8e570e

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

docs/mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ nav:
100100
- Performance & caching: other/performance-and-caching.md
101101
- App & framework integration: other/app-and-framework-integration.md
102102
- Static analysis — PHPStan/Psalm: other/static-analysis.md
103+
- PHPUnit extension: other/phpunit-extension.md
103104
- Project:
104105
- Upgrading: project/upgrading.md
105106
- Alternatives: project/alternatives.md
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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

Comments
 (0)