Passed
Pull Request — master (#257)
by Wilmer
28:13 queued 13:12
created

MiddlewareDispatcherTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
dl 0
loc 69
rs 10
c 5
b 0
f 0
eloc 31
wmc 5
1
<?php
2
3
namespace Yiisoft\Yii\Web\Tests;
4
5
use Nyholm\Psr7\Response;
6
use Nyholm\Psr7\ServerRequest;
7
use PHPUnit\Framework\TestCase;
8
use Psr\EventDispatcher\EventDispatcherInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\MiddlewareInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
use Yiisoft\Di\Container;
13
use Yiisoft\Yii\Web\MiddlewareDispatcher;
14
15
class MiddlewareDispatcherTest extends TestCase
16
{
17
    private MiddlewareDispatcher $middlewareDispatcher;
18
    private RequestHandlerInterface $fallbackHandlerMock;
19
20
    public function setUp(): void
21
    {
22
        parent::setUp();
23
        $container = new Container(
24
            [
25
                EventDispatcherInterface::class => $this->createMock(EventDispatcherInterface::class),
26
            ]
27
        );
28
        $this->fallbackHandlerMock = $this->createMock(RequestHandlerInterface::class);
29
        $this->middlewareDispatcher = new MiddlewareDispatcher($container, $this->fallbackHandlerMock);
30
    }
31
32
    public function testAddThrowsInvalidArgumentExceptionWhenMiddlewareIsNotOfCorrectType(): void
33
    {
34
        $this->expectException(\InvalidArgumentException::class);
35
        $incorrectInput = new \stdClass();
36
37
        $this->middlewareDispatcher->addMiddleware($incorrectInput);
38
    }
39
40
    /**
41
     * @doesNotPerformAssertions
42
     */
43
    public function testAddAddsCallableToMiddlewareArrayWithoutThrowingException(): void
44
    {
45
        $callable = static function () {
46
            echo 'example function for testing purposes';
47
        };
48
        $this->middlewareDispatcher->addMiddleware($callable);
49
    }
50
51
    /**
52
     * @doesNotPerformAssertions
53
     */
54
    public function testAddAddsMiddlewareInterfaceToMiddlewareArrayWithoutThrowingException(): void
55
    {
56
        $middleware = $this->createMock(MiddlewareInterface::class);
57
        $this->middlewareDispatcher->addMiddleware($middleware);
58
    }
59
60
    public function testDispatchExecutesMiddlewareStack(): void
61
    {
62
        $request = new ServerRequest('GET', '/');
63
        $this->fallbackHandlerMock
64
            ->expects($this->never())
65
            ->method('handle')
66
            ->with($request);
67
68
        $middleware1 = static function (ServerRequestInterface $request, RequestHandlerInterface $handler) {
69
            $request = $request->withAttribute('middleware', 'middleware1');
70
71
            return $handler->handle($request);
72
        };
73
        $middleware2 = static function (ServerRequestInterface $request) {
74
            return new Response(200, [], null, '1.1', implode($request->getAttributes()));
75
        };
76
77
        $this->middlewareDispatcher->addMiddleware($middleware2)->addMiddleware($middleware1);
78
        $response = $this->middlewareDispatcher->dispatch($request);
79
80
        $this->assertSame(200, $response->getStatusCode());
81
        $this->assertSame('middleware1', $response->getReasonPhrase());
82
        // ensure that dispatcher could be called multiple times
83
        $this->middlewareDispatcher->dispatch($request);
84
    }
85
}
86