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); |
|
|
|
|
42
|
|
|
$this->fallbackHandlerMock = $this->createMock(RequestHandlerInterface::class); |
|
|
|
|
43
|
|
|
$this->middlewareMocks = [ |
|
|
|
|
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()) |
|
|
|
|
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
|
|
|
|
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..