/
RouteCollectionTest.php
780 lines (696 loc) · 25.8 KB
/
RouteCollectionTest.php
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Routing;
use Cake\Http\ServerRequest;
use Cake\Routing\Exception\MissingRouteException;
use Cake\Routing\RouteBuilder;
use Cake\Routing\RouteCollection;
use Cake\Routing\Route\Route;
use Cake\TestSuite\TestCase;
use \stdClass;
class RouteCollectionTest extends TestCase
{
/**
* Setup method
*
* @return void
*/
public function setUp()
{
parent::setUp();
$this->collection = new RouteCollection();
}
/**
* Test parse() throws an error on unknown routes.
*
* @expectedException \Cake\Routing\Exception\MissingRouteException
* @expectedExceptionMessage A route matching "/" could not be found
*/
public function testParseMissingRoute()
{
$routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
$routes->connect('/', ['controller' => 'Articles']);
$routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
$result = $this->collection->parse('/');
$this->assertEquals([], $result, 'Should not match, missing /b');
}
/**
* Test parse() throws an error on known routes called with unknown methods.
*
* @expectedException \Cake\Routing\Exception\MissingRouteException
* @expectedExceptionMessage A "POST" route matching "/b" could not be found
*/
public function testParseMissingRouteMethod()
{
$routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
$routes->connect('/', ['controller' => 'Articles', '_method' => ['GET']]);
$result = $this->collection->parse('/b', 'GET');
$this->assertNotEmpty($result, 'Route should be found');
$result = $this->collection->parse('/b', 'POST');
$this->assertEquals([], $result, 'Should not match with missing method');
}
/**
* Test parsing routes.
*
* @return void
*/
public function testParse()
{
$routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
$routes->connect('/', ['controller' => 'Articles']);
$routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
$routes->connect('/media/search/*', ['controller' => 'Media', 'action' => 'search']);
$result = $this->collection->parse('/b/');
$expected = [
'controller' => 'Articles',
'action' => 'index',
'pass' => [],
'plugin' => null,
'key' => 'value',
'_matchedRoute' => '/b',
];
$this->assertEquals($expected, $result);
$result = $this->collection->parse('/b/the-thing?one=two');
$expected = [
'controller' => 'Articles',
'action' => 'view',
'id' => 'the-thing',
'pass' => [],
'plugin' => null,
'key' => 'value',
'?' => ['one' => 'two'],
'_matchedRoute' => '/b/:id',
];
$this->assertEquals($expected, $result);
$result = $this->collection->parse('/b/media/search');
$expected = [
'key' => 'value',
'pass' => [],
'plugin' => null,
'controller' => 'Media',
'action' => 'search',
'_matchedRoute' => '/b/media/search/*',
];
$this->assertEquals($expected, $result);
$result = $this->collection->parse('/b/media/search/thing');
$expected = [
'key' => 'value',
'pass' => ['thing'],
'plugin' => null,
'controller' => 'Media',
'action' => 'search',
'_matchedRoute' => '/b/media/search/*',
];
$this->assertEquals($expected, $result);
}
/**
* Test that parse decodes URL data before matching.
* This is important for multibyte URLs that pass through URL rewriting.
*
* @return void
*/
public function testParseEncodedBytesInFixedSegment()
{
$routes = new RouteBuilder($this->collection, '/');
$routes->connect('/ден/:day-:month', ['controller' => 'Events', 'action' => 'index']);
$url = '/%D0%B4%D0%B5%D0%BD/15-%D0%BE%D0%BA%D1%82%D0%BE%D0%BC%D0%B2%D1%80%D0%B8?test=foo';
$result = $this->collection->parse($url);
$expected = [
'pass' => [],
'plugin' => null,
'controller' => 'Events',
'action' => 'index',
'day' => '15',
'month' => 'октомври',
'?' => ['test' => 'foo'],
'_matchedRoute' => '/ден/:day-:month',
];
$this->assertEquals($expected, $result);
$request = new ServerRequest(['url' => $url]);
$result = $this->collection->parseRequest($request);
$this->assertEquals($expected, $result);
}
/**
* Test that parsing checks all the related path scopes.
*
* @return void
*/
public function testParseFallback()
{
$routes = new RouteBuilder($this->collection, '/', []);
$routes->resources('Articles');
$routes->connect('/:controller', ['action' => 'index'], ['routeClass' => 'InflectedRoute']);
$routes->connect('/:controller/:action', [], ['routeClass' => 'InflectedRoute']);
$result = $this->collection->parse('/articles/add');
$expected = [
'controller' => 'Articles',
'action' => 'add',
'plugin' => null,
'pass' => [],
'_matchedRoute' => '/:controller/:action',
];
$this->assertEquals($expected, $result);
}
/**
* Test parseRequest() throws an error on unknown routes.
*
* @expectedException \Cake\Routing\Exception\MissingRouteException
* @expectedExceptionMessage A route matching "/" could not be found
*/
public function testParseRequestMissingRoute()
{
$routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
$routes->connect('/', ['controller' => 'Articles']);
$routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
$request = new ServerRequest(['url' => '/']);
$result = $this->collection->parseRequest($request);
$this->assertEquals([], $result, 'Should not match, missing /b');
}
/**
* Test parseRequest() checks host conditions
*
* @return void
*/
public function testParseRequestCheckHostCondition()
{
$routes = new RouteBuilder($this->collection, '/');
$routes->connect(
'/fallback',
['controller' => 'Articles', 'action' => 'index'],
['_host' => '*.example.com']
);
$request = new ServerRequest([
'environment' => [
'HTTP_HOST' => 'a.example.com',
'PATH_INFO' => '/fallback'
]
]);
$result = $this->collection->parseRequest($request);
$expected = [
'controller' => 'Articles',
'action' => 'index',
'pass' => [],
'plugin' => null,
'_matchedRoute' => '/fallback'
];
$this->assertEquals($expected, $result, 'Should match, domain is correct');
$request = new ServerRequest([
'environment' => [
'HTTP_HOST' => 'foo.bar.example.com',
'PATH_INFO' => '/fallback'
]
]);
$result = $this->collection->parseRequest($request);
$this->assertEquals($expected, $result, 'Should match, domain is a matching subdomain');
$request = new ServerRequest([
'environment' => [
'HTTP_HOST' => 'example.test.com',
'PATH_INFO' => '/fallback'
]
]);
try {
$this->collection->parseRequest($request);
$this->fail('No exception raised');
} catch (MissingRouteException $e) {
$this->assertContains('/fallback', $e->getMessage());
}
}
/**
* Get a list of hostnames
*
* @return array
*/
public static function hostProvider()
{
return [
['wrong.example'],
['example.com'],
['aexample.com'],
];
}
/**
* Test parseRequest() checks host conditions
*
* @dataProvider hostProvider
* @expectedException \Cake\Routing\Exception\MissingRouteException
* @expectedExceptionMessage A route matching "/fallback" could not be found
*/
public function testParseRequestCheckHostConditionFail($host)
{
$routes = new RouteBuilder($this->collection, '/');
$routes->connect(
'/fallback',
['controller' => 'Articles', 'action' => 'index'],
['_host' => '*.example.com']
);
$request = new ServerRequest([
'environment' => [
'HTTP_HOST' => $host,
'PATH_INFO' => '/fallback'
]
]);
$this->collection->parseRequest($request);
}
/**
* Test parsing routes.
*
* @return void
*/
public function testParseRequest()
{
$routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
$routes->connect('/', ['controller' => 'Articles']);
$routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
$routes->connect('/media/search/*', ['controller' => 'Media', 'action' => 'search']);
$request = new ServerRequest(['url' => '/b/']);
$result = $this->collection->parseRequest($request);
$expected = [
'controller' => 'Articles',
'action' => 'index',
'pass' => [],
'plugin' => null,
'key' => 'value',
'_matchedRoute' => '/b',
];
$this->assertEquals($expected, $result);
$request = new ServerRequest(['url' => '/b/media/search']);
$result = $this->collection->parseRequest($request);
$expected = [
'key' => 'value',
'pass' => [],
'plugin' => null,
'controller' => 'Media',
'action' => 'search',
'_matchedRoute' => '/b/media/search/*',
];
$this->assertEquals($expected, $result);
$request = new ServerRequest(['url' => '/b/media/search/thing']);
$result = $this->collection->parseRequest($request);
$expected = [
'key' => 'value',
'pass' => ['thing'],
'plugin' => null,
'controller' => 'Media',
'action' => 'search',
'_matchedRoute' => '/b/media/search/*',
];
$this->assertEquals($expected, $result);
$request = new ServerRequest(['url' => '/b/the-thing?one=two']);
$result = $this->collection->parseRequest($request);
$expected = [
'controller' => 'Articles',
'action' => 'view',
'id' => 'the-thing',
'pass' => [],
'plugin' => null,
'key' => 'value',
'?' => ['one' => 'two'],
'_matchedRoute' => '/b/:id',
];
$this->assertEquals($expected, $result);
}
/**
* Test parsing routes that match non-ascii urls
*
* @return void
*/
public function testParseRequestUnicode()
{
$routes = new RouteBuilder($this->collection, '/b', []);
$routes->connect('/alta/confirmación', ['controller' => 'Media', 'action' => 'confirm']);
$request = new ServerRequest(['url' => '/b/alta/confirmaci%C3%B3n']);
$result = $this->collection->parseRequest($request);
$expected = [
'controller' => 'Media',
'action' => 'confirm',
'pass' => [],
'plugin' => null,
'_matchedRoute' => '/b/alta/confirmación',
];
$this->assertEquals($expected, $result);
$request = new ServerRequest(['url' => '/b/alta/confirmación']);
$result = $this->collection->parseRequest($request);
$this->assertEquals($expected, $result);
}
/**
* Test match() throws an error on unknown routes.
*
* @expectedException \Cake\Routing\Exception\MissingRouteException
* @expectedExceptionMessage A route matching "array (
*/
public function testMatchError()
{
$context = [
'_base' => '/',
'_scheme' => 'http',
'_host' => 'example.org',
];
$routes = new RouteBuilder($this->collection, '/b');
$routes->connect('/', ['controller' => 'Articles']);
$this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'add'], $context);
}
/**
* Test matching routes.
*
* @return void
*/
public function testMatch()
{
$context = [
'_base' => '/',
'_scheme' => 'http',
'_host' => 'example.org',
];
$routes = new RouteBuilder($this->collection, '/b');
$routes->connect('/', ['controller' => 'Articles']);
$routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
$result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'index'], $context);
$this->assertEquals('b', $result);
$result = $this->collection->match(
['id' => 'thing', 'plugin' => null, 'controller' => 'Articles', 'action' => 'view'],
$context
);
$this->assertEquals('b/thing', $result);
}
/**
* Test matching routes with names
*
* @return void
*/
public function testMatchNamed()
{
$context = [
'_base' => '/',
'_scheme' => 'http',
'_host' => 'example.org',
];
$routes = new RouteBuilder($this->collection, '/b');
$routes->connect('/', ['controller' => 'Articles']);
$routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view'], ['_name' => 'article:view']);
$result = $this->collection->match(['_name' => 'article:view', 'id' => '2'], $context);
$this->assertEquals('/b/2', $result);
$result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'view', 'id' => '2'], $context);
$this->assertEquals('b/2', $result);
}
/**
* Test match() throws an error on named routes that fail to match
*
* @expectedException \Cake\Routing\Exception\MissingRouteException
* @expectedExceptionMessage A named route was found for "fail", but matching failed
*/
public function testMatchNamedError()
{
$context = [
'_base' => '/',
'_scheme' => 'http',
'_host' => 'example.org',
];
$routes = new RouteBuilder($this->collection, '/b');
$routes->connect('/:lang/articles', ['controller' => 'Articles'], ['_name' => 'fail']);
$this->collection->match(['_name' => 'fail'], $context);
}
/**
* Test matching routes with names and failing
*
* @expectedException \Cake\Routing\Exception\MissingRouteException
* @return void
*/
public function testMatchNamedMissingError()
{
$context = [
'_base' => '/',
'_scheme' => 'http',
'_host' => 'example.org',
];
$routes = new RouteBuilder($this->collection, '/b');
$routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view'], ['_name' => 'article:view']);
$this->collection->match(['_name' => 'derp'], $context);
}
/**
* Test matching plugin routes.
*
* @return void
*/
public function testMatchPlugin()
{
$context = [
'_base' => '/',
'_scheme' => 'http',
'_host' => 'example.org',
];
$routes = new RouteBuilder($this->collection, '/contacts', ['plugin' => 'Contacts']);
$routes->connect('/', ['controller' => 'Contacts']);
$result = $this->collection->match(['plugin' => 'Contacts', 'controller' => 'Contacts', 'action' => 'index'], $context);
$this->assertEquals('contacts', $result);
}
/**
* Test that prefixes increase the specificity of a route match.
*
* Connect the admin route after the non prefixed version, this means
* the non-prefix route would have a more specific name (users:index)
*
* @return void
*/
public function testMatchPrefixSpecificity()
{
$context = [
'_base' => '/',
'_scheme' => 'http',
'_host' => 'example.org',
];
$routes = new RouteBuilder($this->collection, '/');
$routes->connect('/:action/*', ['controller' => 'Users']);
$routes->connect('/admin/:controller', ['prefix' => 'admin', 'action' => 'index']);
$url = [
'plugin' => null,
'prefix' => 'admin',
'controller' => 'Users',
'action' => 'index'
];
$result = $this->collection->match($url, $context);
$this->assertEquals('admin/Users', $result);
$url = [
'plugin' => null,
'controller' => 'Users',
'action' => 'index'
];
$result = $this->collection->match($url, $context);
$this->assertEquals('index', $result);
}
/**
* Test getting named routes.
*
* @return void
*/
public function testNamed()
{
$routes = new RouteBuilder($this->collection, '/l');
$routes->connect('/:controller', ['action' => 'index'], ['_name' => 'cntrl']);
$routes->connect('/:controller/:action/*');
$all = $this->collection->named();
$this->assertCount(1, $all);
$this->assertInstanceOf('Cake\Routing\Route\Route', $all['cntrl']);
$this->assertEquals('/l/:controller', $all['cntrl']->template);
}
/**
* Test the add() and routes() method.
*
* @return void
*/
public function testAddingRoutes()
{
$one = new Route('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
$two = new Route('/', ['controller' => 'Dashboards', 'action' => 'display']);
$this->collection->add($one);
$this->collection->add($two);
$routes = $this->collection->routes();
$this->assertCount(2, $routes);
$this->assertSame($one, $routes[0]);
$this->assertSame($two, $routes[1]);
}
/**
* Test the add() with some _name.
*
* @expectedException \Cake\Routing\Exception\DuplicateNamedRouteException
*
* @return void
*/
public function testAddingDuplicateNamedRoutes()
{
$one = new Route('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
$two = new Route('/', ['controller' => 'Dashboards', 'action' => 'display']);
$this->collection->add($one, ['_name' => 'test']);
$this->collection->add($two, ['_name' => 'test']);
}
/**
* Test basic get/set of extensions.
*
* @return void
*/
public function testExtensions()
{
$this->assertEquals([], $this->collection->extensions());
$this->collection->extensions('json');
$this->assertEquals(['json'], $this->collection->extensions());
$this->collection->extensions(['rss', 'xml']);
$this->assertEquals(['json', 'rss', 'xml'], $this->collection->extensions());
$this->collection->extensions(['csv'], false);
$this->assertEquals(['csv'], $this->collection->extensions());
}
/**
* String methods are not acceptable.
*
* @expectedException \RuntimeException
* @expectedExceptionMessage The 'bad' middleware is not a callable object.
* @return void
*/
public function testRegisterMiddlewareNoCallableString()
{
$this->collection->registerMiddleware('bad', 'strlen');
}
/**
* Test adding middleware to the collection.
*
* @return void
*/
public function testRegisterMiddleware()
{
$result = $this->collection->registerMiddleware('closure', function () {
});
$this->assertSame($result, $this->collection);
$mock = $this->getMockBuilder('\stdClass')
->setMethods(['__invoke'])
->getMock();
$result = $this->collection->registerMiddleware('callable', $mock);
$this->assertSame($result, $this->collection);
$this->assertTrue($this->collection->hasMiddleware('closure'));
$this->assertTrue($this->collection->hasMiddleware('callable'));
}
/**
* Test adding middleware with a placeholder in the path.
*
* @return void
*/
public function testApplyMiddlewareBasic()
{
$mock = $this->getMockBuilder('\stdClass')
->setMethods(['__invoke'])
->getMock();
$this->collection->registerMiddleware('callable', $mock);
$this->collection->registerMiddleware('callback_two', $mock);
$result = $this->collection->applyMiddleware('/api', ['callable', 'callback_two']);
$this->assertSame($result, $this->collection);
}
/**
* Test adding middleware with a placeholder in the path.
*
* @return void
*/
public function testGetMatchingMiddlewareBasic()
{
$mock = $this->getMockBuilder('\stdClass')
->setMethods(['__invoke'])
->getMock();
$this->collection->registerMiddleware('callable', $mock);
$this->collection->registerMiddleware('callback_two', $mock);
$result = $this->collection->applyMiddleware('/api', ['callable']);
$middleware = $this->collection->getMatchingMiddleware('/api/v1/articles');
$this->assertCount(1, $middleware);
$this->assertSame($middleware[0], $mock);
}
/**
* Test enabling and matching
*
* @return void
*/
public function testGetMatchingMiddlewareMultiplePaths()
{
$mock = $this->getMockBuilder('\stdClass')
->setMethods(['__invoke'])
->getMock();
$mockTwo = $this->getMockBuilder('\stdClass')
->setMethods(['__invoke'])
->getMock();
$this->collection->registerMiddleware('callable', $mock);
$this->collection->registerMiddleware('callback_two', $mockTwo);
$this->collection->applyMiddleware('/api', ['callable']);
$this->collection->applyMiddleware('/api/v1/articles', ['callback_two']);
$middleware = $this->collection->getMatchingMiddleware('/articles');
$this->assertCount(0, $middleware);
$middleware = $this->collection->getMatchingMiddleware('/api/v1/articles/1');
$this->assertCount(2, $middleware);
$this->assertEquals([$mock, $mockTwo], $middleware, 'Both middleware match');
$middleware = $this->collection->getMatchingMiddleware('/api/v1/comments');
$this->assertCount(1, $middleware);
$this->assertEquals([$mock], $middleware, 'Should not match /articles middleware');
}
/**
* Test enabling and matching
*
* @return void
*/
public function testGetMatchingMiddlewareDeduplicate()
{
$mock = $this->getMockBuilder('\stdClass')
->setMethods(['__invoke'])
->getMock();
$mockTwo = $this->getMockBuilder('\stdClass')
->setMethods(['__invoke'])
->getMock();
$this->collection->registerMiddleware('callable', $mock);
$this->collection->registerMiddleware('callback_two', $mockTwo);
$this->collection->applyMiddleware('/api', ['callable']);
$this->collection->applyMiddleware('/api/v1/articles', ['callback_two', 'callable']);
$middleware = $this->collection->getMatchingMiddleware('/api/v1/articles/1');
$this->assertCount(2, $middleware);
$this->assertEquals([$mock, $mockTwo], $middleware, 'Both middleware match');
}
/**
* Test adding middleware with a placeholder in the path.
*
* @return void
*/
public function testApplyMiddlewareWithPlaceholder()
{
$mock = $this->getMockBuilder('\stdClass')
->setMethods(['__invoke'])
->getMock();
$this->collection->registerMiddleware('callable', $mock);
$this->collection->applyMiddleware('/api-:version/articles/:article_id/comments', ['callable']);
$middleware = $this->collection->getMatchingMiddleware('/api-1/articles/comments');
$this->assertEmpty($middleware);
$middleware = $this->collection->getMatchingMiddleware('/api-1/articles/yay/comments');
$this->assertEquals([$mock], $middleware);
$middleware = $this->collection->getMatchingMiddleware('/api-1/articles/123/comments');
$this->assertEquals([$mock], $middleware);
$middleware = $this->collection->getMatchingMiddleware('/api-abc123/articles/abc-123/comments/99');
$this->assertEquals([$mock], $middleware);
}
/**
* Test applying middleware to a scope when it doesn't exist
*
* @expectedException \RuntimeException
* @expectedExceptionMessage Cannot apply 'bad' middleware to path '/api'. It has not been registered.
* @return void
*/
public function testApplyMiddlewareUnregistered()
{
$mock = $this->getMockBuilder('\stdClass')
->setMethods(['__invoke'])
->getMock();
$this->collection->registerMiddleware('callable', $mock);
$this->collection->applyMiddleware('/api', ['callable', 'bad']);
}
}