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