1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
<?php
namespace Engelsystem\Test\Unit\Middleware;
use Engelsystem\Http\Request;
use Engelsystem\Middleware\RouteDispatcher;
use FastRoute\Dispatcher as FastRouteDispatcher;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
class RouteDispatcherTest extends TestCase
{
/**
* @covers \Engelsystem\Middleware\RouteDispatcher::process
* @covers \Engelsystem\Middleware\RouteDispatcher::__construct
*/
public function testProcess()
{
/** @var FastRouteDispatcher|MockObject $dispatcher */
/** @var ResponseInterface|MockObject $response */
/** @var ServerRequestInterface|MockObject $request */
/** @var RequestHandlerInterface|MockObject $handler */
list($dispatcher, $response, $request, $handler) = $this->getMocks();
$dispatcher->expects($this->once())
->method('dispatch')
->with('HEAD', '/foo!bar')
->willReturn([FastRouteDispatcher::FOUND, $handler, ['foo' => 'bar', 'lorem' => 'ipsum']]);
$request->expects($this->exactly(4))
->method('withAttribute')
->withConsecutive(
['route-request-handler', $handler],
['route-request-path', '/foo!bar'],
['foo', 'bar'],
['lorem', 'ipsum']
)
->willReturn($request);
$handler->expects($this->once())
->method('handle')
->with($request)
->willReturn($response);
$middleware = new RouteDispatcher($dispatcher, $response);
$return = $middleware->process($request, $handler);
$this->assertEquals($response, $return);
}
/**
* @covers \Engelsystem\Middleware\RouteDispatcher::process
*/
public function testProcessNotFound()
{
/** @var FastRouteDispatcher|MockObject $dispatcher */
/** @var ResponseInterface|MockObject $response */
/** @var ServerRequestInterface|MockObject $request */
/** @var RequestHandlerInterface|MockObject $handler */
list($dispatcher, $response, $request, $handler) = $this->getMocks();
/** @var MiddlewareInterface|MockObject $notFound */
$notFound = $this->createMock(MiddlewareInterface::class);
$dispatcher->expects($this->exactly(2))
->method('dispatch')
->with('HEAD', '/foo!bar')
->willReturn([FastRouteDispatcher::NOT_FOUND]);
$response->expects($this->once())
->method('withStatus')
->with(404)
->willReturn($response);
$notFound->expects($this->once())
->method('process')
->with($request, $handler)
->willReturn($response);
$middleware = new RouteDispatcher($dispatcher, $response, $notFound);
$return = $middleware->process($request, $handler);
$this->assertEquals($response, $return);
$middleware = new RouteDispatcher($dispatcher, $response);
$return = $middleware->process($request, $handler);
$this->assertEquals($response, $return);
}
/**
* @covers \Engelsystem\Middleware\RouteDispatcher::process
*/
public function testProcessNotAllowed()
{
/** @var FastRouteDispatcher|MockObject $dispatcher */
/** @var ResponseInterface|MockObject $response */
/** @var ServerRequestInterface|MockObject $request */
/** @var RequestHandlerInterface|MockObject $handler */
list($dispatcher, $response, $request, $handler) = $this->getMocks();
$dispatcher->expects($this->once())
->method('dispatch')
->with('HEAD', '/foo!bar')
->willReturn([FastRouteDispatcher::METHOD_NOT_ALLOWED, ['POST', 'TEST']]);
$response->expects($this->once())
->method('withStatus')
->with(405)
->willReturn($response);
$response->expects($this->once())
->method('withHeader')
->with('Allow', 'POST, TEST')
->willReturn($response);
$middleware = new RouteDispatcher($dispatcher, $response);
$return = $middleware->process($request, $handler);
$this->assertEquals($response, $return);
}
/**
* @return array
*/
protected function getMocks(): array
{
/** @var FastRouteDispatcher|MockObject $dispatcher */
$dispatcher = $this->getMockForAbstractClass(FastRouteDispatcher::class);
/** @var ResponseInterface|MockObject $response */
$response = $this->getMockForAbstractClass(ResponseInterface::class);
/** @var ServerRequestInterface|MockObject $request */
$request = $this->createMock(Request::class);
/** @var RequestHandlerInterface|MockObject $handler */
$handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
/** @var UriInterface|MockObject $uriInterface */
$uriInterface = $this->getMockForAbstractClass(UriInterface::class);
$request->expects($this->atLeastOnce())
->method('getMethod')
->willReturn('HEAD');
$request->expects($this->atLeastOnce())
->method('getUri')
->willReturn($uriInterface);
$request->expects($this->atLeastOnce())
->method('getPathInfo')
->willReturn('/foo%21bar');
$uriInterface->expects($this->atLeastOnce())
->method('getPath')
->willReturn('/lorem/foo%21bar');
return [$dispatcher, $response, $request, $handler];
}
}
|