Passed
Push — master ( dda347...d192b8 )
by Alexander
04:54
created

testDispatchCallsMiddlewareFromQueueToProcessRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 19
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace Yiisoft\Yii\Web\Tests;
4
5
use PHPUnit\Framework\TestCase;
6
use Psr\Container\ContainerInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Server\MiddlewareInterface;
9
use Psr\Http\Server\RequestHandlerInterface;
10
use Yiisoft\Di\Container;
11
use Yiisoft\Yii\Web\Emitter\SapiEmitter;
12
use Yiisoft\Yii\Web\MiddlewareDispatcher;
13
14
class MiddlewareDispatcherTest extends TestCase
15
{
16
    /**
17
     * @var MiddlewareDispatcher
18
     */
19
    private $middlewareDispatcher;
20
21
    /**
22
     * @var Container
23
     */
24
    private $containerMock;
25
26
    /**
27
     * @var RequestHandlerInterface
28
     */
29
    private $fallbackHandlerMock;
30
31
    /**
32
     * @var MiddlewareInterface[]
33
     */
34
    private $middlewareMocks;
35
36
    public function setUp(): void
37
    {
38
        parent::setUp();
39
        $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...
40
        $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...
41
        $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...
42
            $this->createMock(MiddlewareInterface::class),
43
            $this->createMock(MiddlewareInterface::class)
44
        ];
45
        $this->middlewareDispatcher = new MiddlewareDispatcher($this->middlewareMocks, $this->containerMock, $this->fallbackHandlerMock);
46
    }
47
48
    public function testConstructThrowsExceptionWhenMiddlewaresAreNotDefined(): void
49
    {
50
        $this->expectException(\InvalidArgumentException::class);
51
        new MiddlewareDispatcher(
52
            [],
53
            $this->containerMock,
54
            $this->fallbackHandlerMock
55
        );
56
    }
57
58
    public function testAddThrowsInvalidArgumentExceptionWhenMiddlewareIsNotOfCorrectType(): void
59
    {
60
        $this->expectException(\InvalidArgumentException::class);
61
        $exampleInput = new SapiEmitter();
62
63
        $this->middlewareDispatcher->add($exampleInput);
64
    }
65
66
    /**
67
     * @doesNotPerformAssertions
68
     */
69
    public function testAddAddsCallableToMiddlewareArrayWithoutThrowingException(): void
70
    {
71
        $callable = static function () {
72
            echo 'example function for testing purposes';
73
        };
74
        $this->middlewareDispatcher->add($callable);
75
    }
76
77
    /**
78
     * @doesNotPerformAssertions
79
     */
80
    public function testAddAddsMiddlewareInterfaceToMiddlewareArrayWithoutThrowingException(): void
81
    {
82
        $middleware = $this->createMock(MiddlewareInterface::class);
83
        $this->middlewareDispatcher->add($middleware);
84
    }
85
86
    public function testDispatchCallsMiddlewareFromQueueToProcessRequest(): void
87
    {
88
        $request = $this->createMock(ServerRequestInterface::class);
89
        $this->fallbackHandlerMock
90
            ->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

90
            ->/** @scrutinizer ignore-call */ 
91
              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...
91
            ->method('handle')
92
            ->with($request);
93
94
        $this->middlewareMocks[0]
95
            ->expects($this->exactly(2))
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Psr\Http\Server\MiddlewareInterface. ( Ignorable by Annotation )

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

95
            ->/** @scrutinizer ignore-call */ 
96
              expects($this->exactly(2))

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...
96
            ->method('process')
97
            ->with($request, $this->middlewareDispatcher);
98
99
        // TODO: test that second middleware is called as well
100
101
        $this->middlewareDispatcher->dispatch($request);
102
103
        // ensure that dispatcher could be called multiple times
104
        $this->middlewareDispatcher->dispatch($request);
105
    }
106
}
107