Passed
Pull Request — master (#219)
by Dmitriy
02:01
created

testAddAddsMiddlewareInterfaceToMiddlewareArrayWithoutThrowingException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
eloc 2
nc 1
nop 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\Di\Container;
13
use Yiisoft\Yii\Web\Emitter\SapiEmitter;
14
use Yiisoft\Yii\Web\MiddlewareDispatcher;
15
16
class MiddlewareDispatcherTest extends TestCase
17
{
18
    /**
19
     * @var MiddlewareDispatcher
20
     */
21
    private $middlewareDispatcher;
22
23
    /**
24
     * @var Container
25
     */
26
    private $containerMock;
27
28
    /**
29
     * @var RequestHandlerInterface
30
     */
31
    private $fallbackHandlerMock;
32
33
    /**
34
     * @var MiddlewareInterface[]
35
     */
36
    private $middlewareMocks;
37
38
    public function setUp(): void
39
    {
40
        parent::setUp();
41
        $this->containerMock = $this->createMock(ContainerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Psr\Co...tainerInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Yiisoft\Di\Container of property $containerMock.

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...
42
        $this->fallbackHandlerMock = $this->createMock(RequestHandlerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Psr\Ht...andlerInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Psr\Http\Server\RequestHandlerInterface of property $fallbackHandlerMock.

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...
43
        $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...
44
            $this->createMock(MiddlewareInterface::class),
45
            $this->createMock(MiddlewareInterface::class)
46
        ];
47
        $this->middlewareDispatcher = new MiddlewareDispatcher($this->middlewareMocks, $this->containerMock, $this->fallbackHandlerMock);
48
    }
49
50
    public function testConstructThrowsExceptionWhenMiddlewaresAreNotDefined(): void
51
    {
52
        $this->expectException(\InvalidArgumentException::class);
53
        new MiddlewareDispatcher(
54
            [],
55
            $this->containerMock,
56
            $this->fallbackHandlerMock
57
        );
58
    }
59
60
    public function testAddThrowsInvalidArgumentExceptionWhenMiddlewareIsNotOfCorrectType(): void
61
    {
62
        $this->expectException(\InvalidArgumentException::class);
63
        $exampleInput = new SapiEmitter();
64
65
        $this->middlewareDispatcher->addMiddleware($exampleInput);
66
    }
67
68
    /**
69
     * @doesNotPerformAssertions
70
     */
71
    public function testAddAddsCallableToMiddlewareArrayWithoutThrowingException(): void
72
    {
73
        $callable = static function () {
74
            echo 'example function for testing purposes';
75
        };
76
        $this->middlewareDispatcher->addMiddleware($callable);
77
    }
78
79
    /**
80
     * @doesNotPerformAssertions
81
     */
82
    public function testAddAddsMiddlewareInterfaceToMiddlewareArrayWithoutThrowingException(): void
83
    {
84
        $middleware = $this->createMock(MiddlewareInterface::class);
85
        $this->middlewareDispatcher->addMiddleware($middleware);
86
    }
87
88
    public function testDispatchCallsMiddlewareFullStack(): void
89
    {
90
        $request = new ServerRequest('GET', '/');
91
        $this->fallbackHandlerMock
92
            ->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

92
            ->/** @scrutinizer ignore-call */ 
93
              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...
93
            ->method('handle')
94
            ->with($request);
95
96
        $middleware1 = function (ServerRequestInterface $request, RequestHandlerInterface $handler) {
97
            $request = $request->withAttribute('middleware', 'middleware1');
98
            return $handler->handle($request);
99
        };
100
        $middleware2 = function (ServerRequestInterface $request) {
101
            return new Response(200, [], null, '1.1', implode($request->getAttributes()));
102
        };
103
        $middlewareDispatcher = new MiddlewareDispatcher([$middleware1, $middleware2], $this->containerMock, $this->fallbackHandlerMock);
104
        $response = $middlewareDispatcher->dispatch($request);
105
106
        $this->assertSame(200, $response->getStatusCode());
107
        $this->assertSame('middleware1', $response->getReasonPhrase());
108
        // ensure that dispatcher could be called multiple times
109
        $this->middlewareDispatcher->dispatch($request);
110
    }
111
}
112