Passed
Pull Request — master (#219)
by Alexander
02:07
created

testDispatchCallsMiddlewareFullStack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 22
rs 9.7666
c 2
b 0
f 0
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\Container\ContainerInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\MiddlewareInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
use Yiisoft\Yii\Web\Emitter\SapiEmitter;
13
use Yiisoft\Yii\Web\MiddlewareDispatcher;
14
15
class MiddlewareDispatcherTest extends TestCase
16
{
17
    private MiddlewareDispatcher $middlewareDispatcher;
18
    private ContainerInterface $containerMock;
19
    private RequestHandlerInterface $fallbackHandlerMock;
20
21
    /**
22
     * @var MiddlewareInterface[]
23
     */
24
    private array $middlewareMocks;
25
26
    public function setUp(): void
27
    {
28
        parent::setUp();
29
        $this->containerMock = $this->createMock(ContainerInterface::class);
30
        $this->fallbackHandlerMock = $this->createMock(RequestHandlerInterface::class);
31
        $this->middlewareMocks = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array($this->createMock(...ewareInterface::class)) of type array<integer,PHPUnit\Fr...\MockObject\MockObject> is incompatible with the declared type Psr\Http\Server\MiddlewareInterface[] of property $middlewareMocks.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
32
            $this->createMock(MiddlewareInterface::class),
33
            $this->createMock(MiddlewareInterface::class)
34
        ];
35
        $this->middlewareDispatcher = new MiddlewareDispatcher($this->middlewareMocks, $this->containerMock, $this->fallbackHandlerMock);
36
    }
37
38
    public function testConstructThrowsExceptionWhenMiddlewaresAreNotDefined(): void
39
    {
40
        $this->expectException(\InvalidArgumentException::class);
41
        new MiddlewareDispatcher(
42
            [],
43
            $this->containerMock,
44
            $this->fallbackHandlerMock
45
        );
46
    }
47
48
    public function testAddThrowsInvalidArgumentExceptionWhenMiddlewareIsNotOfCorrectType(): void
49
    {
50
        $this->expectException(\InvalidArgumentException::class);
51
        $exampleInput = new SapiEmitter();
52
53
        $this->middlewareDispatcher->addMiddleware($exampleInput);
0 ignored issues
show
Bug introduced by
$exampleInput of type Yiisoft\Yii\Web\Emitter\SapiEmitter is incompatible with the type Psr\Http\Server\MiddlewareInterface|callable expected by parameter $middleware of Yiisoft\Yii\Web\Middlewa...atcher::addMiddleware(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        $this->middlewareDispatcher->addMiddleware(/** @scrutinizer ignore-type */ $exampleInput);
Loading history...
54
    }
55
56
    /**
57
     * @doesNotPerformAssertions
58
     */
59
    public function testAddAddsCallableToMiddlewareArrayWithoutThrowingException(): void
60
    {
61
        $callable = static function () {
62
            echo 'example function for testing purposes';
63
        };
64
        $this->middlewareDispatcher->addMiddleware($callable);
65
    }
66
67
    /**
68
     * @doesNotPerformAssertions
69
     */
70
    public function testAddAddsMiddlewareInterfaceToMiddlewareArrayWithoutThrowingException(): void
71
    {
72
        $middleware = $this->createMock(MiddlewareInterface::class);
73
        $this->middlewareDispatcher->addMiddleware($middleware);
74
    }
75
76
    public function testDispatchCallsMiddlewareFullStack(): void
77
    {
78
        $request = new ServerRequest('GET', '/');
79
        $this->fallbackHandlerMock
80
            ->expects($this->never())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Psr\Http\Server\RequestHandlerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
            ->/** @scrutinizer ignore-call */ 
81
              expects($this->never())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
            ->method('handle')
82
            ->with($request);
83
84
        $middleware1 = static function (ServerRequestInterface $request, RequestHandlerInterface $handler) {
85
            $request = $request->withAttribute('middleware', 'middleware1');
86
            return $handler->handle($request);
87
        };
88
        $middleware2 = static function (ServerRequestInterface $request) {
89
            return new Response(200, [], null, '1.1', implode($request->getAttributes()));
90
        };
91
        $middlewareDispatcher = new MiddlewareDispatcher([$middleware1, $middleware2], $this->containerMock, $this->fallbackHandlerMock);
92
        $response = $middlewareDispatcher->dispatch($request);
93
94
        $this->assertSame(200, $response->getStatusCode());
95
        $this->assertSame('middleware1', $response->getReasonPhrase());
96
        // ensure that dispatcher could be called multiple times
97
        $this->middlewareDispatcher->dispatch($request);
98
    }
99
}
100