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); |
|
|
|
|
40
|
|
|
$this->fallbackHandlerMock = $this->createMock(RequestHandlerInterface::class); |
|
|
|
|
41
|
|
|
$this->middlewareMocks = [ |
|
|
|
|
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()) |
|
|
|
|
91
|
|
|
->method('handle') |
92
|
|
|
->with($request); |
93
|
|
|
|
94
|
|
|
$this->middlewareMocks[0] |
95
|
|
|
->expects($this->exactly(2)) |
|
|
|
|
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
|
|
|
|
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..