1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace DelOlmo\Middleware; |
6
|
|
|
|
7
|
|
|
use DelOlmo\Middleware\SymfonyRouterMiddleware; |
8
|
|
|
use Middlewares\Utils\Dispatcher; |
9
|
|
|
use Middlewares\Utils\Factory; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
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\UrlMatcher; |
15
|
|
|
use Symfony\Component\Routing\Matcher\RequestMatcherInterface; |
16
|
|
|
use Symfony\Component\Routing\RequestContext; |
17
|
|
|
use Symfony\Component\Routing\RouteCollection; |
18
|
|
|
use Symfony\Component\Routing\Route; |
19
|
|
|
use Symfony\Component\Routing\Router; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Antonio del Olmo García <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class SymfonyRouterMiddlewareTest extends TestCase |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* |
29
|
|
|
* @var \Symfony\Component\Routing\RouteCollection |
30
|
|
|
*/ |
31
|
|
|
private $routes = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* |
35
|
|
|
* @var \Symfony\Component\Routing\Router |
36
|
|
|
*/ |
37
|
|
|
private $router = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* |
41
|
|
|
* @var \Symfony\Component\Config\Loader\LoaderInterface |
42
|
|
|
*/ |
43
|
|
|
private $loader = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
protected function setUp() |
49
|
|
|
{ |
50
|
|
|
$this->loader = $this->getMockBuilder(LoaderInterface::class)->getMock(); |
|
|
|
|
51
|
|
|
$this->router = new Router($this->loader, 'routing.yml'); |
52
|
|
|
|
53
|
|
|
$this->routes = new RouteCollection(); |
54
|
|
|
$this->routes->add('test', new Route('/users', [], [], [], '', [], ['GET'])); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* |
59
|
|
|
*/ |
60
|
|
|
public function testResourceNotFoundException() |
61
|
|
|
{ |
62
|
|
|
$request = Factory::createServerRequest('GET', '/posts'); |
63
|
|
|
|
64
|
|
|
$factory = new HttpFoundationFactory(); |
65
|
|
|
|
66
|
|
|
$symfonyRequest = $factory->createRequest($request); |
67
|
|
|
|
68
|
|
|
$context = (new RequestContext()) |
69
|
|
|
->fromRequest($symfonyRequest); |
70
|
|
|
|
71
|
|
|
$matcher = new UrlMatcher($this->routes, $context); |
72
|
|
|
|
73
|
|
|
$p = new \ReflectionProperty($this->router, 'matcher'); |
74
|
|
|
|
75
|
|
|
$p->setAccessible(true); |
76
|
|
|
|
77
|
|
|
$p->setValue($this->router, $matcher); |
78
|
|
|
|
79
|
|
|
$response = Dispatcher::run([ |
80
|
|
|
new SymfonyRouterMiddleware($this->router) |
81
|
|
|
], $request); |
82
|
|
|
|
83
|
|
|
$this->assertEquals(404, $response->getStatusCode()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* |
88
|
|
|
*/ |
89
|
|
|
public function testMethodNotAllowedException() |
90
|
|
|
{ |
91
|
|
|
$request = Factory::createServerRequest('POST', '/users'); |
92
|
|
|
|
93
|
|
|
$factory = new HttpFoundationFactory(); |
94
|
|
|
|
95
|
|
|
$symfonyRequest = $factory->createRequest($request); |
96
|
|
|
|
97
|
|
|
$context = (new RequestContext()) |
98
|
|
|
->fromRequest($symfonyRequest); |
99
|
|
|
|
100
|
|
|
$matcher = new UrlMatcher($this->routes, $context); |
101
|
|
|
|
102
|
|
|
$p = new \ReflectionProperty($this->router, 'matcher'); |
103
|
|
|
|
104
|
|
|
$p->setAccessible(true); |
105
|
|
|
|
106
|
|
|
$p->setValue($this->router, $matcher); |
107
|
|
|
|
108
|
|
|
$response = Dispatcher::run([ |
109
|
|
|
new SymfonyRouterMiddleware($this->router) |
110
|
|
|
], $request); |
111
|
|
|
|
112
|
|
|
$this->assertEquals(405, $response->getStatusCode()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* |
117
|
|
|
*/ |
118
|
|
|
public function testNoConfigurationException() |
119
|
|
|
{ |
120
|
|
|
$request = Factory::createServerRequest('POST', '/users'); |
121
|
|
|
|
122
|
|
|
$matcher = $this->createMock(RequestMatcherInterface::class); |
123
|
|
|
|
124
|
|
|
$matcher->method("matchRequest") |
|
|
|
|
125
|
|
|
->will($this->throwException(new NoConfigurationException())); |
126
|
|
|
|
127
|
|
|
$p = new \ReflectionProperty($this->router, "matcher"); |
128
|
|
|
|
129
|
|
|
$p->setAccessible(true); |
130
|
|
|
|
131
|
|
|
$p->setValue($this->router, $matcher); |
132
|
|
|
|
133
|
|
|
$response = Dispatcher::run([ |
134
|
|
|
new SymfonyRouterMiddleware($this->router) |
135
|
|
|
], $request); |
136
|
|
|
|
137
|
|
|
$this->assertEquals(500, $response->getStatusCode()); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* |
142
|
|
|
*/ |
143
|
|
|
public function testRouteMatched() |
144
|
|
|
{ |
145
|
|
|
$request = Factory::createServerRequest('GET', '/users'); |
146
|
|
|
|
147
|
|
|
$factory = new HttpFoundationFactory(); |
148
|
|
|
$symfonyRequest = $factory->createRequest($request); |
149
|
|
|
$context = (new RequestContext())->fromRequest($symfonyRequest); |
150
|
|
|
$matcher = new UrlMatcher($this->routes, $context); |
151
|
|
|
$p = new \ReflectionProperty($this->router, 'matcher'); |
152
|
|
|
$p->setAccessible(true); |
153
|
|
|
$p->setValue($this->router, $matcher); |
154
|
|
|
|
155
|
|
|
$response = Dispatcher::run([ |
156
|
|
|
new SymfonyRouterMiddleware($this->router), |
157
|
|
|
function ($request) { |
158
|
|
|
echo $request->getAttribute('_route'); |
159
|
|
|
} |
160
|
|
|
], $request); |
161
|
|
|
|
162
|
|
|
$this->assertEquals('test', (string) $response->getBody()); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..