pitchart /
phlunit
| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | |||
| 4 | namespace Pitchart\Phlunit\Checks; |
||
| 5 | |||
| 6 | use PHPUnit\Framework\Assert; |
||
| 7 | use PHPUnit\Framework\Constraint\Exception as ExceptionConstraint; |
||
| 8 | use PHPUnit\Framework\Constraint\ExceptionCode; |
||
| 9 | use PHPUnit\Framework\Constraint\ExceptionMessage; |
||
| 10 | use Pitchart\Phlunit\Checks\Mixin\ConstraintCheck; |
||
| 11 | use Pitchart\Phlunit\Checks\Mixin\FluentChecks; |
||
| 12 | use Pitchart\Phlunit\Checks\Mixin\TypeCheck; |
||
|
0 ignored issues
–
show
|
|||
| 13 | use Pitchart\Phlunit\Checks\Mixin\WithMessage; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Class ExceptionCheck |
||
| 17 | * |
||
| 18 | * @package Checks |
||
| 19 | * |
||
| 20 | * @author Julien VITTE <[email protected]> |
||
| 21 | * |
||
| 22 | * |
||
| 23 | * @implements FluentCheck<\Throwable> |
||
| 24 | */ |
||
| 25 | class ExceptionCheck implements FluentCheck |
||
| 26 | { |
||
| 27 | 1 | use TypeCheck, FluentChecks, WithMessage, ConstraintCheck; |
|
| 28 | |||
| 29 | /** |
||
| 30 | * @var \Throwable |
||
| 31 | */ |
||
| 32 | private $value; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * ExceptionCheck constructor. |
||
| 36 | * |
||
| 37 | * @param \Throwable $value |
||
| 38 | */ |
||
| 39 | 18 | public function __construct(\Throwable $value) |
|
| 40 | { |
||
| 41 | 18 | $this->value = $value; |
|
| 42 | 18 | } |
|
| 43 | |||
| 44 | /** |
||
| 45 | * @param string $className |
||
| 46 | * |
||
| 47 | * @return static |
||
| 48 | */ |
||
| 49 | 12 | public function isAnInstanceOf(string $className): self |
|
| 50 | { |
||
| 51 | 12 | Assert::assertThat($this->value, new ExceptionConstraint($className), $this->message); |
|
| 52 | 12 | $this->resetMessage(); |
|
| 53 | 12 | return $this; |
|
| 54 | } |
||
| 55 | |||
| 56 | 6 | public function isDescribedBy(string $message): self |
|
| 57 | { |
||
| 58 | 6 | Assert::assertThat($this->value, new ExceptionMessage($message), $this->message); |
|
| 59 | 6 | $this->resetMessage(); |
|
| 60 | 6 | return $this; |
|
| 61 | } |
||
| 62 | |||
| 63 | 1 | public function hasCode(int $code): self |
|
| 64 | { |
||
| 65 | 1 | Assert::assertThat($this->value, new ExceptionCode($code), $this->message); |
|
| 66 | 1 | $this->resetMessage(); |
|
| 67 | 1 | return $this; |
|
| 68 | } |
||
| 69 | } |
||
| 70 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: