1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Yiisoft\Yii\Web\Tests; |
5
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
9
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
10
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
11
|
|
|
use Yiisoft\Di\Container; |
12
|
|
|
use PHPUnit_Framework_MockObject_MockObject; |
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|PHPUnit_Framework_MockObject_MockObject |
25
|
|
|
*/ |
26
|
|
|
private $containerMock; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var RequestHandlerInterface|PHPUnit_Framework_MockObject_MockObject |
30
|
|
|
*/ |
31
|
|
|
private $fallbackHandlerMock; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var MiddlewareInterface[]|PHPUnit_Framework_MockObject_MockObject[] |
35
|
|
|
*/ |
36
|
|
|
private $middlewareMocks; |
37
|
|
|
|
38
|
|
|
public function setUp() |
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
|
|
|
/** |
51
|
|
|
* @test |
52
|
|
|
*/ |
53
|
|
|
public function constructThrowsExceptionWhenMiddlewaresAreNotDefined() |
54
|
|
|
{ |
55
|
|
|
$this->expectException(\InvalidArgumentException::class); |
56
|
|
|
new MiddlewareDispatcher( |
57
|
|
|
[], |
58
|
|
|
$this->containerMock, |
59
|
|
|
$this->fallbackHandlerMock |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @test |
65
|
|
|
*/ |
66
|
|
|
public function addThrowsInvalidArgumentExceptionWhenMiddlewareIsNotOfCorrectType() |
67
|
|
|
{ |
68
|
|
|
$this->expectException(\InvalidArgumentException::class); |
69
|
|
|
$exampleInput = new SapiEmitter(); |
70
|
|
|
|
71
|
|
|
$this->middlewareDispatcher->add($exampleInput); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @test |
76
|
|
|
* @doesNotPerformAssertions |
77
|
|
|
*/ |
78
|
|
|
public function addAddsCallableToMiddlewareArrayWithoutThrowingException() |
79
|
|
|
{ |
80
|
|
|
$callable = function () { |
81
|
|
|
echo 'example function for testing purposes'; |
82
|
|
|
}; |
83
|
|
|
$this->middlewareDispatcher->add($callable); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @test |
88
|
|
|
* @doesNotPerformAssertions |
89
|
|
|
*/ |
90
|
|
|
public function addAddsMiddlewareInterfaceToMiddlewareArrayWithoutThrowingException() |
91
|
|
|
{ |
92
|
|
|
$middleware = $this->createMock(MiddlewareInterface::class); |
93
|
|
|
$this->middlewareDispatcher->add($middleware); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @test |
98
|
|
|
*/ |
99
|
|
|
public function handleCallsMiddlewareFromQueueToProcessRequest() |
100
|
|
|
{ |
101
|
|
|
$request = $this->createMock(ServerRequestInterface::class); |
102
|
|
|
$this->fallbackHandlerMock |
103
|
|
|
->expects($this->never()) |
104
|
|
|
->method('handle') |
105
|
|
|
->with($request); |
106
|
|
|
|
107
|
|
|
$this->middlewareMocks[0] |
108
|
|
|
->expects($this->once()) |
109
|
|
|
->method('process') |
110
|
|
|
->with($request, $this->middlewareDispatcher); |
111
|
|
|
|
112
|
|
|
$this->middlewareDispatcher->handle($request); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|