Skip to content

Commit 8ea224d

Browse files
committed
Move the automatic expectation verification from trait to test case
1 parent 6edd12c commit 8ea224d

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

README.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,25 @@ final class FileCreatedExpectation implements Expectation
483483
}
484484
```
485485

486-
Now, to be able to use this method, include the trait `PHPUnitExtras\Expectation\Expectations` in your test case class,
487-
or inherit it from `PHPUnitExtras\TestCase`, where this trait is already included.
486+
Now, to be able to use this expectation, inherit your test case class from `PHPUnitExtras\TestCase`
487+
(recommended) or include the `PHPUnitExtras\Expectation\Expectations` trait:
488+
489+
```php
490+
use PHPUnit\Framework\TestCase;
491+
use PHPUnitExtras\Expectation\Expectations;
492+
493+
final class MyTest extends TestCase
494+
{
495+
use Expectations;
496+
497+
protected function tearDown() : void
498+
{
499+
$this->verifyExpectations();
500+
}
501+
502+
// ...
503+
}
504+
```
488505
After that, call your expectation as shown below:
489506

490507
```php
@@ -497,7 +514,7 @@ public function testDumpPdfToFile() : void
497514
}
498515
```
499516

500-
For convenience, you can put this statement in a separate method or even group your expectations into a trait:
517+
For convenience, you can put this statement in a separate method and group your expectations into a trait:
501518

502519
```php
503520
namespace App\Tests\PhpUnit;
@@ -511,6 +528,8 @@ trait FileExpectations
511528
$this->expect(new FileCreatedExpectation($filename));
512529
}
513530

531+
// ...
532+
514533
abstract protected function expect(Expectation $expectation) : void;
515534
}
516535
```

src/Expectation/Expectations.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ trait Expectations
1818
/** @var ChainExpectation|null */
1919
private $expectations;
2020

21-
/**
22-
* @after
23-
*/
2421
final protected function verifyExpectations() : void
2522
{
2623
$this->getExpectations()->verify();

src/TestCase.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,12 @@ final protected function resolvePlaceholders(string $value) : string
4141

4242
return $resolver->resolve($value, Target::fromTestCase($this));
4343
}
44+
45+
/**
46+
* @after
47+
*/
48+
final protected function verifyTestCaseExpectations() : void
49+
{
50+
$this->verifyExpectations();
51+
}
4452
}

0 commit comments

Comments
 (0)