Test Failed
Pull Request — master (#32)
by Anatoly
04:53
created

RouteTest   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 269
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 126
dl 0
loc 269
rs 10
c 0
b 0
f 0
wmc 22
1
<?php declare(strict_types=1);
2
3
namespace Sunrise\Http\Router\Tests;
4
5
/**
6
 * Import classes
7
 */
8
use PHPUnit\Framework\TestCase;
9
use Psr\Http\Server\RequestHandlerInterface;
10
use Sunrise\Http\Router\Route;
11
use Sunrise\Http\Router\RouteInterface;
12
use Sunrise\Http\ServerRequest\ServerRequestFactory;
13
14
/**
15
 * Import functions
16
 */
17
use function array_merge;
18
19
/**
20
 * RouteTest
21
 */
22
class RouteTest extends TestCase
23
{
24
25
    /**
26
     * @return void
27
     */
28
    public function testConstructor() : void
29
    {
30
        $routeName = Fixture\TestRoute::getTestRouteName();
31
        $routePath = Fixture\TestRoute::getTestRoutePath();
32
        $routeMethods = Fixture\TestRoute::getTestRouteMethods();
33
        $routeRequestHandler = Fixture\TestRoute::getTestRouteRequestHandler();
34
35
        $route = new Route(
36
            $routeName,
37
            $routePath,
38
            $routeMethods,
39
            $routeRequestHandler
40
        );
41
42
        $this->assertInstanceOf(RouteInterface::class, $route);
43
        $this->assertInstanceOf(RequestHandlerInterface::class, $route);
44
45
        $this->assertSame($routeName, $route->getName());
46
        $this->assertSame($routePath, $route->getPath());
47
        $this->assertSame($routeMethods, $route->getMethods());
48
        $this->assertSame($routeRequestHandler, $route->getRequestHandler());
49
50
        // default property values...
51
        $this->assertSame([], $route->getMiddlewares());
52
        $this->assertSame([], $route->getAttributes());
53
    }
54
55
    /**
56
     * @return void
57
     */
58
    public function testConstructorWithOptionalParams() : void
59
    {
60
        $routeName = Fixture\TestRoute::getTestRouteName();
61
        $routePath = Fixture\TestRoute::getTestRoutePath();
62
        $routeMethods = Fixture\TestRoute::getTestRouteMethods();
63
        $routeRequestHandler = Fixture\TestRoute::getTestRouteRequestHandler();
64
        $routeMiddlewares = Fixture\TestRoute::getTestRouteMiddlewares();
65
        $routeAttributes = Fixture\TestRoute::getTestRouteAttributes();
66
67
        $route = new Route(
68
            $routeName,
69
            $routePath,
70
            $routeMethods,
71
            $routeRequestHandler,
72
            $routeMiddlewares,
73
            $routeAttributes
74
        );
75
76
        $this->assertSame($routeName, $route->getName());
77
        $this->assertSame($routePath, $route->getPath());
78
        $this->assertSame($routeMethods, $route->getMethods());
79
        $this->assertSame($routeRequestHandler, $route->getRequestHandler());
80
        $this->assertSame($routeMiddlewares, $route->getMiddlewares());
81
        $this->assertSame($routeAttributes, $route->getAttributes());
82
    }
83
84
    /**
85
     * @return void
86
     */
87
    public function testSetName() : void
88
    {
89
        $route = new Fixture\TestRoute();
90
        $newRouteName = Fixture\TestRoute::getTestRouteName();
91
92
        $this->assertNotSame($route->getName(), $newRouteName);
93
        $this->assertSame($route, $route->setName($newRouteName));
94
        $this->assertSame($newRouteName, $route->getName());
95
    }
96
97
    /**
98
     * @return void
99
     */
100
    public function testSetPath() : void
101
    {
102
        $route = new Fixture\TestRoute();
103
        $newRoutePath = Fixture\TestRoute::getTestRoutePath();
104
105
        $this->assertNotSame($route->getPath(), $newRoutePath);
106
        $this->assertSame($route, $route->setPath($newRoutePath));
107
        $this->assertSame($newRoutePath, $route->getPath());
108
    }
109
110
    /**
111
     * @return void
112
     */
113
    public function testSetMethods() : void
114
    {
115
        $route = new Fixture\TestRoute();
116
        $newRouteMethods = Fixture\TestRoute::getTestRouteMethods();
117
118
        $this->assertNotSame($route->getMethods(), $newRouteMethods);
119
        $this->assertSame($route, $route->setMethods(...$newRouteMethods));
120
        $this->assertSame($newRouteMethods, $route->getMethods());
121
    }
122
123
    /**
124
     * @return void
125
     */
126
    public function testSetRequestHandler() : void
127
    {
128
        $route = new Fixture\TestRoute();
129
        $newRouteRequestHandler = Fixture\TestRoute::getTestRouteRequestHandler();
130
131
        $this->assertNotSame($route->getRequestHandler(), $newRouteRequestHandler);
132
        $this->assertSame($route, $route->setRequestHandler($newRouteRequestHandler));
133
        $this->assertSame($newRouteRequestHandler, $route->getRequestHandler());
134
    }
135
136
    /**
137
     * @return void
138
     */
139
    public function testSetMiddlewares() : void
140
    {
141
        $route = new Fixture\TestRoute();
142
        $newRouteMiddlewares = Fixture\TestRoute::getTestRouteMiddlewares();
143
144
        $this->assertNotSame($route->getMiddlewares(), $newRouteMiddlewares);
145
        $this->assertSame($route, $route->setMiddlewares(...$newRouteMiddlewares));
146
        $this->assertSame($newRouteMiddlewares, $route->getMiddlewares());
147
    }
148
149
    /**
150
     * @return void
151
     */
152
    public function testSetAttributes() : void
153
    {
154
        $route = new Fixture\TestRoute();
155
        $newRouteAttributes = Fixture\TestRoute::getTestRouteAttributes();
156
157
        $this->assertNotSame($route->getAttributes(), $newRouteAttributes);
158
        $this->assertSame($route, $route->setAttributes($newRouteAttributes));
159
        $this->assertSame($newRouteAttributes, $route->getAttributes());
160
    }
161
162
    /**
163
     * @return void
164
     */
165
    public function testAddPrefix() : void
166
    {
167
        $route = new Fixture\TestRoute();
168
        $pathPrefix = '/foo';
169
        $expectedPath = $pathPrefix . $route->getPath();
170
171
        $this->assertSame($route, $route->addPrefix($pathPrefix));
172
        $this->assertSame($expectedPath, $route->getPath());
173
    }
174
175
    /**
176
     * @return void
177
     */
178
    public function testAddSuffix() : void
179
    {
180
        $route = new Fixture\TestRoute();
181
        $pathSuffix = '.foo';
182
        $expectedPath = $route->getPath() . $pathSuffix;
183
184
        $this->assertSame($route, $route->addSuffix($pathSuffix));
185
        $this->assertSame($expectedPath, $route->getPath());
186
    }
187
188
    /**
189
     * @return void
190
     */
191
    public function testAddMethod() : void
192
    {
193
        $route = new Fixture\TestRoute();
194
        $extraMethods = Fixture\TestRoute::getTestRouteMethods();
195
        $expectedMethods = array_merge($route->getMethods(), $extraMethods);
196
197
        $this->assertSame($route, $route->addMethod(...$extraMethods));
198
        $this->assertSame($expectedMethods, $route->getMethods());
199
    }
200
201
    /**
202
     * @return void
203
     */
204
    public function testAddMiddleware() : void
205
    {
206
        $route = new Fixture\TestRoute();
207
        $extraMiddlewares = Fixture\TestRoute::getTestRouteMiddlewares();
208
        $expectedMiddlewares = array_merge($route->getMiddlewares(), $extraMiddlewares);
209
210
        $this->assertSame($route, $route->addMiddleware(...$extraMiddlewares));
211
        $this->assertSame($expectedMiddlewares, $route->getMiddlewares());
212
    }
213
214
    /**
215
     * @return void
216
     */
217
    public function testWithAddedAttributes() : void
218
    {
219
        $route = new Fixture\TestRoute();
220
        $extraAttributes = Fixture\TestRoute::getTestRouteAttributes();
221
        $expectedAttributes = $route->getAttributes() + $extraAttributes;
222
        $routeClone = $route->withAddedAttributes($extraAttributes);
223
224
        $this->assertInstanceOf(RouteInterface::class, $routeClone);
225
        $this->assertNotSame($route, $routeClone);
226
        $this->assertSame($expectedAttributes, $routeClone->getAttributes());
227
    }
228
229
    /**
230
     * @return void
231
     */
232
    public function testSetLowercasedMethods() : void
233
    {
234
        $route = new Fixture\TestRoute();
235
        $expectedMethods = ['FOO', 'BAR'];
236
237
        $route->setMethods('foo', 'bar');
238
        $this->assertSame($expectedMethods, $route->getMethods());
239
    }
240
241
    /**
242
     * @return void
243
     */
244
    public function testAddSlashEndingPrefix() : void
245
    {
246
        $route = new Fixture\TestRoute();
247
        $expectedPath = '/foo' . $route->getPath();
248
249
        $route->addPrefix('/foo/');
250
        $this->assertSame($expectedPath, $route->getPath());
251
    }
252
253
    /**
254
     * @return void
255
     */
256
    public function testAddLowercasedMethod() : void
257
    {
258
        $route = new Fixture\TestRoute();
259
        $expectedMethods = $route->getMethods();
260
        $expectedMethods[] = 'GET';
261
        $expectedMethods[] = 'POST';
262
263
        $route->addMethod('get', 'post');
264
        $this->assertSame($expectedMethods, $route->getMethods());
265
    }
266
267
    /**
268
     * @return void
269
     */
270
    public function testHandle() : void
271
    {
272
        $route = new Fixture\TestRoute();
273
        $route->handle((new ServerRequestFactory)->createServerRequest('GET', '/'));
274
275
        $this->assertTrue($route->getMiddlewares()[0]->isRunned());
276
        $this->assertTrue($route->getMiddlewares()[1]->isRunned());
277
        $this->assertTrue($route->getMiddlewares()[2]->isRunned());
278
        $this->assertTrue($route->getRequestHandler()->isRunned());
279
280
        $expectedAttributes = [Route::ATTR_NAME_FOR_ROUTE_NAME => $route->getName()];
281
        $expectedAttributes += $route->getAttributes();
282
283
        $this->assertSame($expectedAttributes, $route->getRequestHandler()->getAttributes());
284
    }
285
286
    /**
287
     * @return void
288
     */
289
    public function testHandleWithBrokenMiddleware() : void
290
    {
291
        $route = new Fixture\TestRoute(Fixture\TestRoute::WITH_BROKEN_MIDDLEWARE);
292
        $route->handle((new ServerRequestFactory)->createServerRequest('GET', '/'));
293
294
        $this->assertTrue($route->getMiddlewares()[0]->isRunned());
295
        $this->assertTrue($route->getMiddlewares()[1]->isRunned());
296
        $this->assertFalse($route->getMiddlewares()[2]->isRunned());
297
        $this->assertFalse($route->getRequestHandler()->isRunned());
298
        $this->assertSame([], $route->getRequestHandler()->getAttributes());
299
    }
300
}
301