diff options
Diffstat (limited to 'tests/Utils/TestCallable.php')
| -rw-r--r-- | tests/Utils/TestCallable.php | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/tests/Utils/TestCallable.php b/tests/Utils/TestCallable.php new file mode 100644 index 0000000..42a3bb5 --- /dev/null +++ b/tests/Utils/TestCallable.php @@ -0,0 +1,69 @@ +<?php + +declare(strict_types=1); + +namespace Lightscale\Router\Test\Utils; + +use Closure; +use PHPUnit\Framework\Assert; + +class TestCallable +{ + private Closure $cb; + + /** @var TestCall[] */ + private array $calls; + + final public function __construct(callable $cb) + { + $this->cb = Closure::fromCallable($cb); + } + + public static function make(callable $cb): static + { + return new static($cb); + } + + public function __invoke(mixed ...$args): mixed + { + $call = new TestCall( + args: $args, + return: ($this->cb)(...$args), + ); + $this->calls[] = $call; + + return $call->return; + } + + public function getCallCount(): int + { + return count($this->calls); + } + + /** @return TestCall[] */ + public function getCalls(): array + { + return $this->calls; + } + + public function getLastCall(): ?TestCall + { + return $this->calls[$this->getCallCount() - 1] ?? null; + } + + public function assertIsCalled(): void + { + Assert::assertGreaterThan(0, $this->getCallCount(), 'Not been called'); + } + + public function assertNotCalled(): void + { + Assert::assertSame(0, $this->getCallCount()); + } + + public function assertCalledTimes(int $amount): void + { + Assert::assertSame($amount, $this->getCallCount()); + } + +} |
