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 = [ |
|
|
|
|
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); |
|
|
|
|
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()) |
|
|
|
|
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
|
|
|
|
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..