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->middlewareDispatcher = new MiddlewareDispatcher($this->containerMock, $this->fallbackHandlerMock); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testAddThrowsInvalidArgumentExceptionWhenMiddlewareIsNotOfCorrectType(): void |
35
|
|
|
{ |
36
|
|
|
$this->expectException(\InvalidArgumentException::class); |
37
|
|
|
$exampleInput = new SapiEmitter(); |
38
|
|
|
|
39
|
|
|
$this->middlewareDispatcher->addMiddleware($exampleInput); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @doesNotPerformAssertions |
44
|
|
|
*/ |
45
|
|
|
public function testAddAddsCallableToMiddlewareArrayWithoutThrowingException(): void |
46
|
|
|
{ |
47
|
|
|
$callable = static function () { |
48
|
|
|
echo 'example function for testing purposes'; |
49
|
|
|
}; |
50
|
|
|
$this->middlewareDispatcher->addMiddleware($callable); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @doesNotPerformAssertions |
55
|
|
|
*/ |
56
|
|
|
public function testAddAddsMiddlewareInterfaceToMiddlewareArrayWithoutThrowingException(): void |
57
|
|
|
{ |
58
|
|
|
$middleware = $this->createMock(MiddlewareInterface::class); |
59
|
|
|
$this->middlewareDispatcher->addMiddleware($middleware); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testDispatchCallsMiddlewareFullStack(): void |
63
|
|
|
{ |
64
|
|
|
$request = new ServerRequest('GET', '/'); |
65
|
|
|
$this->fallbackHandlerMock |
66
|
|
|
->expects($this->never()) |
|
|
|
|
67
|
|
|
->method('handle') |
68
|
|
|
->with($request); |
69
|
|
|
|
70
|
|
|
$middleware1 = static function (ServerRequestInterface $request, RequestHandlerInterface $handler) { |
71
|
|
|
$request = $request->withAttribute('middleware', 'middleware1'); |
72
|
|
|
return $handler->handle($request); |
73
|
|
|
}; |
74
|
|
|
$middleware2 = static function (ServerRequestInterface $request) { |
75
|
|
|
return new Response(200, [], null, '1.1', implode($request->getAttributes())); |
76
|
|
|
}; |
77
|
|
|
|
78
|
|
|
$this->middlewareDispatcher->addMiddleware($middleware2)->addMiddleware($middleware1); |
79
|
|
|
$response = $this->middlewareDispatcher->dispatch($request); |
80
|
|
|
|
81
|
|
|
$this->assertSame(200, $response->getStatusCode()); |
82
|
|
|
$this->assertSame('middleware1', $response->getReasonPhrase()); |
83
|
|
|
// ensure that dispatcher could be called multiple times |
84
|
|
|
$this->middlewareDispatcher->dispatch($request); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|