1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace DelOlmo\Middleware; |
6
|
|
|
|
7
|
|
|
use Middlewares\Utils\Dispatcher; |
8
|
|
|
use Middlewares\Utils\Factory; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use ReflectionProperty; |
11
|
|
|
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; |
12
|
|
|
use Symfony\Component\Config\Loader\LoaderInterface; |
13
|
|
|
use Symfony\Component\Routing\Exception\NoConfigurationException; |
14
|
|
|
use Symfony\Component\Routing\Matcher\RequestMatcherInterface; |
15
|
|
|
use Symfony\Component\Routing\Matcher\UrlMatcher; |
16
|
|
|
use Symfony\Component\Routing\RequestContext; |
17
|
|
|
use Symfony\Component\Routing\Route; |
18
|
|
|
use Symfony\Component\Routing\RouteCollection; |
19
|
|
|
use Symfony\Component\Routing\Router; |
20
|
|
|
|
21
|
|
|
class SymfonyRouterMiddlewareTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
private RouteCollection $routes; |
24
|
|
|
|
25
|
|
|
private Router $router; |
26
|
|
|
|
27
|
|
|
private LoaderInterface $loader; |
28
|
|
|
|
29
|
|
|
protected function setUp() : void |
30
|
|
|
{ |
31
|
|
|
$this->loader = $this->getMockBuilder(LoaderInterface::class)->getMock(); |
32
|
|
|
$this->router = new Router($this->loader, 'routing.yml'); |
33
|
|
|
|
34
|
|
|
$this->routes = new RouteCollection(); |
35
|
|
|
$this->routes->add('test', new Route('/users', [], [], [], '', [], ['GET'])); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testRequestContextBeingUpdated() : void |
39
|
|
|
{ |
40
|
|
|
$context = $this->createMock(RequestContext::class); |
41
|
|
|
|
42
|
|
|
$router = $this->createMock(Router::class); |
43
|
|
|
|
44
|
|
|
$router |
45
|
|
|
->expects(self::once()) |
46
|
|
|
->method('getContext') |
47
|
|
|
->willReturn($context); |
48
|
|
|
|
49
|
|
|
$request = Factory::createServerRequest('GET', '/posts'); |
50
|
|
|
|
51
|
|
|
$symfonyRequest = (new HttpFoundationFactory()) |
52
|
|
|
->createRequest($request); |
53
|
|
|
|
54
|
|
|
$context |
55
|
|
|
->expects(self::once()) |
56
|
|
|
->method('fromRequest') |
57
|
|
|
->with($symfonyRequest); |
58
|
|
|
|
59
|
|
|
$router |
60
|
|
|
->expects(self::once()) |
61
|
|
|
->method('matchRequest') |
62
|
|
|
->with($symfonyRequest) |
63
|
|
|
->willReturn([]); |
64
|
|
|
|
65
|
|
|
$factory = Factory::getResponseFactory(); |
66
|
|
|
|
67
|
|
|
$middleware = new SymfonyRouterMiddleware($router, $factory); |
68
|
|
|
|
69
|
|
|
Dispatcher::run([$middleware], $request); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testResourceNotFoundException() : void |
73
|
|
|
{ |
74
|
|
|
$request = Factory::createServerRequest('GET', '/posts'); |
75
|
|
|
|
76
|
|
|
$factory = new HttpFoundationFactory(); |
77
|
|
|
|
78
|
|
|
$symfonyRequest = $factory->createRequest($request); |
79
|
|
|
|
80
|
|
|
$context = (new RequestContext()) |
81
|
|
|
->fromRequest($symfonyRequest); |
82
|
|
|
|
83
|
|
|
$matcher = new UrlMatcher($this->routes, $context); |
84
|
|
|
|
85
|
|
|
$p = new ReflectionProperty($this->router, 'matcher'); |
86
|
|
|
|
87
|
|
|
$p->setAccessible(true); |
88
|
|
|
|
89
|
|
|
$p->setValue($this->router, $matcher); |
90
|
|
|
|
91
|
|
|
$factory = Factory::getResponseFactory(); |
92
|
|
|
|
93
|
|
|
$middleware = new SymfonyRouterMiddleware($this->router, $factory); |
94
|
|
|
|
95
|
|
|
$response = Dispatcher::run([$middleware], $request); |
96
|
|
|
|
97
|
|
|
self::assertSame(404, $response->getStatusCode()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testMethodNotAllowedException() : void |
101
|
|
|
{ |
102
|
|
|
$request = Factory::createServerRequest('POST', '/users'); |
103
|
|
|
|
104
|
|
|
$factory = new HttpFoundationFactory(); |
105
|
|
|
|
106
|
|
|
$symfonyRequest = $factory->createRequest($request); |
107
|
|
|
|
108
|
|
|
$context = (new RequestContext()) |
109
|
|
|
->fromRequest($symfonyRequest); |
110
|
|
|
|
111
|
|
|
$matcher = new UrlMatcher($this->routes, $context); |
112
|
|
|
|
113
|
|
|
$p = new ReflectionProperty($this->router, 'matcher'); |
114
|
|
|
|
115
|
|
|
$p->setAccessible(true); |
116
|
|
|
|
117
|
|
|
$p->setValue($this->router, $matcher); |
118
|
|
|
|
119
|
|
|
$factory = Factory::getResponseFactory(); |
120
|
|
|
|
121
|
|
|
$middleware = new SymfonyRouterMiddleware($this->router, $factory); |
122
|
|
|
|
123
|
|
|
$response = Dispatcher::run([$middleware], $request); |
124
|
|
|
|
125
|
|
|
self::assertSame(405, $response->getStatusCode()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testNoConfigurationException() : void |
129
|
|
|
{ |
130
|
|
|
$request = Factory::createServerRequest('POST', '/users'); |
131
|
|
|
|
132
|
|
|
$matcher = $this->createMock(RequestMatcherInterface::class); |
133
|
|
|
|
134
|
|
|
$matcher->method('matchRequest') |
135
|
|
|
->will(self::throwException(new NoConfigurationException())); |
136
|
|
|
|
137
|
|
|
$p = new ReflectionProperty($this->router, 'matcher'); |
138
|
|
|
|
139
|
|
|
$p->setAccessible(true); |
140
|
|
|
|
141
|
|
|
$p->setValue($this->router, $matcher); |
142
|
|
|
|
143
|
|
|
$factory = Factory::getResponseFactory(); |
144
|
|
|
|
145
|
|
|
$middleware = new SymfonyRouterMiddleware($this->router, $factory); |
146
|
|
|
|
147
|
|
|
$response = Dispatcher::run([$middleware], $request); |
148
|
|
|
|
149
|
|
|
self::assertSame(500, $response->getStatusCode()); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function testRouteMatched() : void |
153
|
|
|
{ |
154
|
|
|
$request = Factory::createServerRequest('GET', '/users'); |
155
|
|
|
|
156
|
|
|
$factory = new HttpFoundationFactory(); |
157
|
|
|
|
158
|
|
|
$symfonyRequest = $factory->createRequest($request); |
159
|
|
|
|
160
|
|
|
$context = (new RequestContext()) |
161
|
|
|
->fromRequest($symfonyRequest); |
162
|
|
|
|
163
|
|
|
$matcher = new UrlMatcher($this->routes, $context); |
164
|
|
|
|
165
|
|
|
$p = new ReflectionProperty($this->router, 'matcher'); |
166
|
|
|
|
167
|
|
|
$p->setAccessible(true); |
168
|
|
|
|
169
|
|
|
$p->setValue($this->router, $matcher); |
170
|
|
|
|
171
|
|
|
$factory = Factory::getResponseFactory(); |
172
|
|
|
|
173
|
|
|
$middleware = new SymfonyRouterMiddleware($this->router, $factory); |
174
|
|
|
|
175
|
|
|
$dummyFn = static function ($request) : void { |
176
|
|
|
echo $request->getAttribute('_route'); |
177
|
|
|
}; |
178
|
|
|
|
179
|
|
|
$response = Dispatcher::run([$middleware, $dummyFn], $request); |
180
|
|
|
|
181
|
|
|
self::assertSame('test', (string) $response->getBody()); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|