pitchart /
phlunit
| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | |||
| 4 | namespace Pitchart\Phlunit\Checks; |
||
| 5 | |||
| 6 | use PHPUnit\Framework\Assert; |
||
| 7 | use Pitchart\Phlunit\Check; |
||
| 8 | use Pitchart\Phlunit\Checks\Mixin\ConstraintCheck; |
||
| 9 | use Pitchart\Phlunit\Checks\Mixin\FluentChecks; |
||
| 10 | use Pitchart\Phlunit\Checks\Mixin\TypeCheck; |
||
|
0 ignored issues
–
show
|
|||
| 11 | use Pitchart\Phlunit\Checks\Mixin\WithMessage; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Class CallableCheck |
||
| 15 | * |
||
| 16 | * @package Pitchart\Phlunit\Checks |
||
| 17 | * |
||
| 18 | * @author Julien VITTE <[email protected]> |
||
| 19 | * |
||
| 20 | * @implements FluentCheck<callable> |
||
| 21 | */ |
||
| 22 | class CallableCheck implements FluentCheck |
||
| 23 | { |
||
| 24 | 1 | use TypeCheck, FluentChecks, ConstraintCheck, WithMessage; |
|
| 25 | |||
| 26 | /** |
||
| 27 | * @var callable |
||
| 28 | */ |
||
| 29 | private $value; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | private $arguments = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var mixed |
||
| 38 | */ |
||
| 39 | private $result; |
||
| 40 | |||
| 41 | 43 | public function __construct(callable $value) |
|
| 42 | { |
||
| 43 | 43 | $this->value = $value; |
|
| 44 | 43 | } |
|
| 45 | |||
| 46 | /** |
||
| 47 | * @param array<mixed> ...$args |
||
| 48 | * |
||
| 49 | * @return CallableCheck |
||
| 50 | */ |
||
| 51 | 21 | public function with(...$args): self |
|
| 52 | { |
||
| 53 | 21 | $this->arguments = $args; |
|
| 54 | 21 | return $this; |
|
| 55 | } |
||
| 56 | |||
| 57 | 11 | public function throws(string $className): ExceptionCheck |
|
| 58 | { |
||
| 59 | 11 | $this->execute(); |
|
| 60 | 11 | Assert::assertInstanceOf(\Throwable::class, $this->result, $this->message); |
|
| 61 | 11 | $this->resetMessage(); |
|
| 62 | 11 | return (new ExceptionCheck($this->result))->isAnInstanceOf($className); |
|
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @return ArrayCheck|BooleanCheck|CallableCheck|CollectionCheck|DateTimeCheck|ExceptionCheck|FluentCheck|GenericCheck|ResponseCheck|StringCheck |
||
| 67 | */ |
||
| 68 | 30 | public function hasAResult() |
|
| 69 | { |
||
| 70 | 30 | $this->execute(); |
|
| 71 | 30 | Assert::assertNotInstanceOf(\Throwable::class, $this->result, $this->message); |
|
| 72 | 30 | $this->resetMessage(); |
|
| 73 | 30 | return Check::that($this->result); |
|
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @return \Exception|mixed |
||
| 78 | */ |
||
| 79 | 41 | private function execute() |
|
| 80 | { |
||
| 81 | try { |
||
| 82 | 41 | $this->result = \call_user_func($this->value, ...$this->arguments); |
|
| 83 | 11 | } catch (\Exception $exception) { |
|
| 84 | 11 | $this->result = $exception; |
|
| 85 | } |
||
| 86 | 41 | return $this->result; |
|
| 87 | } |
||
| 88 | } |
||
| 89 |
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: