Skip to content

Commit

Permalink
Move the automatic expectation verification from trait to test case
Browse files Browse the repository at this point in the history
  • Loading branch information
rybakit committed May 9, 2020
1 parent 6edd12c commit 8ea224d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,25 @@ final class FileCreatedExpectation implements Expectation
}
```

Now, to be able to use this method, include the trait `PHPUnitExtras\Expectation\Expectations` in your test case class,
or inherit it from `PHPUnitExtras\TestCase`, where this trait is already included.
Now, to be able to use this expectation, inherit your test case class from `PHPUnitExtras\TestCase`
(recommended) or include the `PHPUnitExtras\Expectation\Expectations` trait:

```php
use PHPUnit\Framework\TestCase;
use PHPUnitExtras\Expectation\Expectations;

final class MyTest extends TestCase
{
use Expectations;

protected function tearDown() : void
{
$this->verifyExpectations();
}

// ...
}
```
After that, call your expectation as shown below:

```php
Expand All @@ -497,7 +514,7 @@ public function testDumpPdfToFile() : void
}
```

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

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

// ...

abstract protected function expect(Expectation $expectation) : void;
}
```
Expand Down
3 changes: 0 additions & 3 deletions src/Expectation/Expectations.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ trait Expectations
/** @var ChainExpectation|null */
private $expectations;

/**
* @after
*/
final protected function verifyExpectations() : void
{
$this->getExpectations()->verify();
Expand Down
8 changes: 8 additions & 0 deletions src/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,12 @@ final protected function resolvePlaceholders(string $value) : string

return $resolver->resolve($value, Target::fromTestCase($this));
}

/**
* @after
*/
final protected function verifyTestCaseExpectations() : void
{
$this->verifyExpectations();
}
}

0 comments on commit 8ea224d

Please sign in to comment.