summaryrefslogtreecommitdiff
path: root/tests/Utils/TestMiddleware.php
blob: e273d057a40deeb196e2b0a1f30fec48eaa3881c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php

declare(strict_types=1);

namespace Lightscale\Router\Test\Utils;

use Lightscale\Router\Contracts\Middleware;
use PHPUnit\Framework\Assert;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class TestMiddleware implements Middleware
{
    private static int $staticCalls = 0;

    private int $call;
    private int $calls = 0;

    public static function resetCalls(): void
    {
        self::$staticCalls = 0;
    }

    public function handle(ServerRequestInterface $request, callable $next): ResponseInterface
    {
        ++$this->calls;
        $this->call = ++self::$staticCalls;

        return $next($request);
    }

    public function assertCalled(): void
    {
        Assert::assertGreaterThan(0, $this->calls);
    }

    public function assertCalledTimes(int $val): void
    {
        Assert::assertSame($val, $this->calls);
    }

    public function assertCalledOnce(): void
    {
        $this->assertCalledTimes(1);
    }

    public function assertNotCalled(): void
    {
        $this->assertCalledTimes(0);
    }

    public function assertCallNumber(int $num): void
    {
        Assert::assertSame($num, $this->call);
    }
}