RouteCollectionTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Routing;
  17. use Cake\Http\ServerRequest;
  18. use Cake\Routing\Exception\DuplicateNamedRouteException;
  19. use Cake\Routing\Exception\MissingRouteException;
  20. use Cake\Routing\Route\Route;
  21. use Cake\Routing\RouteBuilder;
  22. use Cake\Routing\RouteCollection;
  23. use Cake\TestSuite\TestCase;
  24. use RuntimeException;
  25. class RouteCollectionTest extends TestCase
  26. {
  27. /**
  28. * @var \Cake\Routing\RouteCollection
  29. */
  30. protected $collection;
  31. /**
  32. * Setup method
  33. */
  34. public function setUp(): void
  35. {
  36. parent::setUp();
  37. $this->collection = new RouteCollection();
  38. }
  39. /**
  40. * Test parse() throws an error on unknown routes.
  41. */
  42. public function testParseMissingRoute(): void
  43. {
  44. $this->expectException(MissingRouteException::class);
  45. $this->expectExceptionMessage('A route matching "/" could not be found');
  46. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  47. $routes->connect('/', ['controller' => 'Articles']);
  48. $routes->connect('/{id}', ['controller' => 'Articles', 'action' => 'view']);
  49. $result = $this->collection->parse('/');
  50. $this->assertEquals([], $result, 'Should not match, missing /b');
  51. }
  52. /**
  53. * Test parse() throws an error on known routes called with unknown methods.
  54. */
  55. public function testParseMissingRouteMethod(): void
  56. {
  57. $this->expectException(MissingRouteException::class);
  58. $this->expectExceptionMessage('A "POST" route matching "/b" could not be found');
  59. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  60. $routes->connect('/', ['controller' => 'Articles', '_method' => ['GET']]);
  61. $result = $this->collection->parse('/b', 'GET');
  62. $this->assertNotEmpty($result, 'Route should be found');
  63. $result = $this->collection->parse('/b', 'POST');
  64. $this->assertEquals([], $result, 'Should not match with missing method');
  65. }
  66. /**
  67. * Test parsing routes.
  68. */
  69. public function testParse(): void
  70. {
  71. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  72. $routes->connect('/', ['controller' => 'Articles']);
  73. $routes->connect('/{id}', ['controller' => 'Articles', 'action' => 'view']);
  74. $routes->connect('/media/search/*', ['controller' => 'Media', 'action' => 'search']);
  75. $result = $this->collection->parse('/b/');
  76. $expected = [
  77. 'controller' => 'Articles',
  78. 'action' => 'index',
  79. 'pass' => [],
  80. 'plugin' => null,
  81. 'key' => 'value',
  82. '_matchedRoute' => '/b',
  83. ];
  84. $this->assertEquals($expected, $result);
  85. $result = $this->collection->parse('/b/the-thing?one=two');
  86. $expected = [
  87. 'controller' => 'Articles',
  88. 'action' => 'view',
  89. 'id' => 'the-thing',
  90. 'pass' => [],
  91. 'plugin' => null,
  92. 'key' => 'value',
  93. '?' => ['one' => 'two'],
  94. '_matchedRoute' => '/b/{id}',
  95. ];
  96. $this->assertEquals($expected, $result);
  97. $result = $this->collection->parse('/b/media/search');
  98. $expected = [
  99. 'key' => 'value',
  100. 'pass' => [],
  101. 'plugin' => null,
  102. 'controller' => 'Media',
  103. 'action' => 'search',
  104. '_matchedRoute' => '/b/media/search/*',
  105. ];
  106. $this->assertEquals($expected, $result);
  107. $result = $this->collection->parse('/b/media/search/thing');
  108. $expected = [
  109. 'key' => 'value',
  110. 'pass' => ['thing'],
  111. 'plugin' => null,
  112. 'controller' => 'Media',
  113. 'action' => 'search',
  114. '_matchedRoute' => '/b/media/search/*',
  115. ];
  116. $this->assertEquals($expected, $result);
  117. }
  118. /**
  119. * Test parsing routes with and without _name.
  120. */
  121. public function testParseWithNameParameter(): void
  122. {
  123. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  124. $routes->connect('/', ['controller' => 'Articles']);
  125. $routes->connect('/{id}', ['controller' => 'Articles', 'action' => 'view']);
  126. $routes->connect('/media/search/*', ['controller' => 'Media', 'action' => 'search'], ['_name' => 'media_search']);
  127. $result = $this->collection->parse('/b/');
  128. $expected = [
  129. 'controller' => 'Articles',
  130. 'action' => 'index',
  131. 'pass' => [],
  132. 'plugin' => null,
  133. 'key' => 'value',
  134. '_matchedRoute' => '/b',
  135. ];
  136. $this->assertEquals($expected, $result);
  137. $result = $this->collection->parse('/b/the-thing?one=two');
  138. $expected = [
  139. 'controller' => 'Articles',
  140. 'action' => 'view',
  141. 'id' => 'the-thing',
  142. 'pass' => [],
  143. 'plugin' => null,
  144. 'key' => 'value',
  145. '?' => ['one' => 'two'],
  146. '_matchedRoute' => '/b/{id}',
  147. ];
  148. $this->assertEquals($expected, $result);
  149. $result = $this->collection->parse('/b/media/search');
  150. $expected = [
  151. 'key' => 'value',
  152. 'pass' => [],
  153. 'plugin' => null,
  154. 'controller' => 'Media',
  155. 'action' => 'search',
  156. '_matchedRoute' => '/b/media/search/*',
  157. '_name' => 'media_search',
  158. ];
  159. $this->assertEquals($expected, $result);
  160. $result = $this->collection->parse('/b/media/search/thing');
  161. $expected = [
  162. 'key' => 'value',
  163. 'pass' => ['thing'],
  164. 'plugin' => null,
  165. 'controller' => 'Media',
  166. 'action' => 'search',
  167. '_matchedRoute' => '/b/media/search/*',
  168. '_name' => 'media_search',
  169. ];
  170. $this->assertEquals($expected, $result);
  171. }
  172. /**
  173. * Test that parse decodes URL data before matching.
  174. * This is important for multibyte URLs that pass through URL rewriting.
  175. */
  176. public function testParseEncodedBytesInFixedSegment(): void
  177. {
  178. $routes = new RouteBuilder($this->collection, '/');
  179. $routes->connect('/ден/{day}-{month}', ['controller' => 'Events', 'action' => 'index']);
  180. $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';
  181. $result = $this->collection->parse($url);
  182. $expected = [
  183. 'pass' => [],
  184. 'plugin' => null,
  185. 'controller' => 'Events',
  186. 'action' => 'index',
  187. 'day' => '15',
  188. 'month' => 'октомври',
  189. '?' => ['test' => 'foo'],
  190. '_matchedRoute' => '/ден/{day}-{month}',
  191. ];
  192. $this->assertEquals($expected, $result);
  193. $request = new ServerRequest(['url' => $url]);
  194. $result = $this->collection->parseRequest($request);
  195. $this->assertEquals($expected, $result);
  196. }
  197. /**
  198. * Test that parsing checks all the related path scopes.
  199. */
  200. public function testParseFallback(): void
  201. {
  202. $routes = new RouteBuilder($this->collection, '/', []);
  203. $routes->resources('Articles');
  204. $routes->connect('/{controller}', ['action' => 'index'], ['routeClass' => 'InflectedRoute']);
  205. $routes->connect('/{controller}/{action}', [], ['routeClass' => 'InflectedRoute']);
  206. $result = $this->collection->parse('/articles/add');
  207. $expected = [
  208. 'controller' => 'Articles',
  209. 'action' => 'add',
  210. 'plugin' => null,
  211. 'pass' => [],
  212. '_matchedRoute' => '/{controller}/{action}',
  213. ];
  214. $this->assertEquals($expected, $result);
  215. }
  216. /**
  217. * Test parseRequest() throws an error on unknown routes.
  218. */
  219. public function testParseRequestMissingRoute(): void
  220. {
  221. $this->expectException(MissingRouteException::class);
  222. $this->expectExceptionMessage('A route matching "/" could not be found');
  223. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  224. $routes->connect('/', ['controller' => 'Articles']);
  225. $routes->connect('/{id}', ['controller' => 'Articles', 'action' => 'view']);
  226. $request = new ServerRequest(['url' => '/']);
  227. $result = $this->collection->parseRequest($request);
  228. $this->assertEquals([], $result, 'Should not match, missing /b');
  229. }
  230. /**
  231. * Test parseRequest() checks host conditions
  232. */
  233. public function testParseRequestCheckHostCondition(): void
  234. {
  235. $routes = new RouteBuilder($this->collection, '/');
  236. $routes->connect(
  237. '/fallback',
  238. ['controller' => 'Articles', 'action' => 'index'],
  239. ['_host' => '*.example.com']
  240. );
  241. $request = new ServerRequest([
  242. 'environment' => [
  243. 'HTTP_HOST' => 'a.example.com',
  244. 'PATH_INFO' => '/fallback',
  245. ],
  246. ]);
  247. $result = $this->collection->parseRequest($request);
  248. $expected = [
  249. 'controller' => 'Articles',
  250. 'action' => 'index',
  251. 'pass' => [],
  252. 'plugin' => null,
  253. '_matchedRoute' => '/fallback',
  254. ];
  255. $this->assertEquals($expected, $result, 'Should match, domain is correct');
  256. $request = new ServerRequest([
  257. 'environment' => [
  258. 'HTTP_HOST' => 'foo.bar.example.com',
  259. 'PATH_INFO' => '/fallback',
  260. ],
  261. ]);
  262. $result = $this->collection->parseRequest($request);
  263. $this->assertEquals($expected, $result, 'Should match, domain is a matching subdomain');
  264. $request = new ServerRequest([
  265. 'environment' => [
  266. 'HTTP_HOST' => 'example.test.com',
  267. 'PATH_INFO' => '/fallback',
  268. ],
  269. ]);
  270. try {
  271. $this->collection->parseRequest($request);
  272. $this->fail('No exception raised');
  273. } catch (MissingRouteException $e) {
  274. $this->assertStringContainsString('/fallback', $e->getMessage());
  275. }
  276. }
  277. /**
  278. * Get a list of hostnames
  279. *
  280. * @return array
  281. */
  282. public static function hostProvider(): array
  283. {
  284. return [
  285. ['wrong.example'],
  286. ['example.com'],
  287. ['aexample.com'],
  288. ];
  289. }
  290. /**
  291. * Test parseRequest() checks host conditions
  292. *
  293. * @dataProvider hostProvider
  294. */
  295. public function testParseRequestCheckHostConditionFail(string $host): void
  296. {
  297. $this->expectException(MissingRouteException::class);
  298. $this->expectExceptionMessage('A route matching "/fallback" could not be found');
  299. $routes = new RouteBuilder($this->collection, '/');
  300. $routes->connect(
  301. '/fallback',
  302. ['controller' => 'Articles', 'action' => 'index'],
  303. ['_host' => '*.example.com']
  304. );
  305. $request = new ServerRequest([
  306. 'environment' => [
  307. 'HTTP_HOST' => $host,
  308. 'PATH_INFO' => '/fallback',
  309. ],
  310. ]);
  311. $this->collection->parseRequest($request);
  312. }
  313. /**
  314. * Test parsing routes.
  315. */
  316. public function testParseRequest(): void
  317. {
  318. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  319. $routes->connect('/', ['controller' => 'Articles']);
  320. $routes->connect('/{id}', ['controller' => 'Articles', 'action' => 'view']);
  321. $routes->connect('/media/search/*', ['controller' => 'Media', 'action' => 'search']);
  322. $request = new ServerRequest(['url' => '/b/']);
  323. $result = $this->collection->parseRequest($request);
  324. $expected = [
  325. 'controller' => 'Articles',
  326. 'action' => 'index',
  327. 'pass' => [],
  328. 'plugin' => null,
  329. 'key' => 'value',
  330. '_matchedRoute' => '/b',
  331. ];
  332. $this->assertEquals($expected, $result);
  333. $request = new ServerRequest(['url' => '/b/media/search']);
  334. $result = $this->collection->parseRequest($request);
  335. $expected = [
  336. 'key' => 'value',
  337. 'pass' => [],
  338. 'plugin' => null,
  339. 'controller' => 'Media',
  340. 'action' => 'search',
  341. '_matchedRoute' => '/b/media/search/*',
  342. ];
  343. $this->assertEquals($expected, $result);
  344. $request = new ServerRequest(['url' => '/b/media/search/thing']);
  345. $result = $this->collection->parseRequest($request);
  346. $expected = [
  347. 'key' => 'value',
  348. 'pass' => ['thing'],
  349. 'plugin' => null,
  350. 'controller' => 'Media',
  351. 'action' => 'search',
  352. '_matchedRoute' => '/b/media/search/*',
  353. ];
  354. $this->assertEquals($expected, $result);
  355. $request = new ServerRequest(['url' => '/b/the-thing?one=two']);
  356. $result = $this->collection->parseRequest($request);
  357. $expected = [
  358. 'controller' => 'Articles',
  359. 'action' => 'view',
  360. 'id' => 'the-thing',
  361. 'pass' => [],
  362. 'plugin' => null,
  363. 'key' => 'value',
  364. '?' => ['one' => 'two'],
  365. '_matchedRoute' => '/b/{id}',
  366. ];
  367. $this->assertEquals($expected, $result);
  368. }
  369. /**
  370. * Test parsing routes that match non-ascii urls
  371. */
  372. public function testParseRequestUnicode(): void
  373. {
  374. $routes = new RouteBuilder($this->collection, '/b', []);
  375. $routes->connect('/alta/confirmación', ['controller' => 'Media', 'action' => 'confirm']);
  376. $request = new ServerRequest(['url' => '/b/alta/confirmaci%C3%B3n']);
  377. $result = $this->collection->parseRequest($request);
  378. $expected = [
  379. 'controller' => 'Media',
  380. 'action' => 'confirm',
  381. 'pass' => [],
  382. 'plugin' => null,
  383. '_matchedRoute' => '/b/alta/confirmación',
  384. ];
  385. $this->assertEquals($expected, $result);
  386. $request = new ServerRequest(['url' => '/b/alta/confirmación']);
  387. $result = $this->collection->parseRequest($request);
  388. $this->assertEquals($expected, $result);
  389. }
  390. /**
  391. * Test match() throws an error on unknown routes.
  392. */
  393. public function testMatchError(): void
  394. {
  395. $this->expectException(MissingRouteException::class);
  396. $this->expectExceptionMessage('A route matching "array (');
  397. $context = [
  398. '_base' => '/',
  399. '_scheme' => 'http',
  400. '_host' => 'example.org',
  401. ];
  402. $routes = new RouteBuilder($this->collection, '/b');
  403. $routes->connect('/', ['controller' => 'Articles']);
  404. $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'add'], $context);
  405. }
  406. /**
  407. * Test matching routes.
  408. */
  409. public function testMatch(): void
  410. {
  411. $context = [
  412. '_base' => '/',
  413. '_scheme' => 'http',
  414. '_host' => 'example.org',
  415. ];
  416. $routes = new RouteBuilder($this->collection, '/b');
  417. $routes->connect('/', ['controller' => 'Articles']);
  418. $routes->connect('/{id}', ['controller' => 'Articles', 'action' => 'view']);
  419. $result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'index'], $context);
  420. $this->assertSame('b', $result);
  421. $result = $this->collection->match(
  422. ['id' => 'thing', 'plugin' => null, 'controller' => 'Articles', 'action' => 'view'],
  423. $context
  424. );
  425. $this->assertSame('b/thing', $result);
  426. }
  427. /**
  428. * Test matching routes with names
  429. */
  430. public function testMatchNamed(): void
  431. {
  432. $context = [
  433. '_base' => '/',
  434. '_scheme' => 'http',
  435. '_host' => 'example.org',
  436. ];
  437. $routes = new RouteBuilder($this->collection, '/b');
  438. $routes->connect('/', ['controller' => 'Articles']);
  439. $routes->connect('/{id}', ['controller' => 'Articles', 'action' => 'view'], ['_name' => 'article:view']);
  440. $result = $this->collection->match(['_name' => 'article:view', 'id' => '2'], $context);
  441. $this->assertSame('/b/2', $result);
  442. $result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'view', 'id' => '2'], $context);
  443. $this->assertSame('b/2', $result);
  444. }
  445. /**
  446. * Test match() throws an error on named routes that fail to match
  447. */
  448. public function testMatchNamedError(): void
  449. {
  450. $this->expectException(MissingRouteException::class);
  451. $this->expectExceptionMessage('A named route was found for `fail`, but matching failed');
  452. $context = [
  453. '_base' => '/',
  454. '_scheme' => 'http',
  455. '_host' => 'example.org',
  456. ];
  457. $routes = new RouteBuilder($this->collection, '/b');
  458. $routes->connect('/{lang}/articles', ['controller' => 'Articles'], ['_name' => 'fail']);
  459. $this->collection->match(['_name' => 'fail'], $context);
  460. }
  461. /**
  462. * Test matching routes with names and failing
  463. */
  464. public function testMatchNamedMissingError(): void
  465. {
  466. $this->expectException(MissingRouteException::class);
  467. $context = [
  468. '_base' => '/',
  469. '_scheme' => 'http',
  470. '_host' => 'example.org',
  471. ];
  472. $routes = new RouteBuilder($this->collection, '/b');
  473. $routes->connect('/{id}', ['controller' => 'Articles', 'action' => 'view'], ['_name' => 'article:view']);
  474. $this->collection->match(['_name' => 'derp'], $context);
  475. }
  476. /**
  477. * Test matching plugin routes.
  478. */
  479. public function testMatchPlugin(): void
  480. {
  481. $context = [
  482. '_base' => '/',
  483. '_scheme' => 'http',
  484. '_host' => 'example.org',
  485. ];
  486. $routes = new RouteBuilder($this->collection, '/contacts', ['plugin' => 'Contacts']);
  487. $routes->connect('/', ['controller' => 'Contacts']);
  488. $result = $this->collection->match(['plugin' => 'Contacts', 'controller' => 'Contacts', 'action' => 'index'], $context);
  489. $this->assertSame('contacts', $result);
  490. }
  491. /**
  492. * Test that prefixes increase the specificity of a route match.
  493. *
  494. * Connect the admin route after the non prefixed version, this means
  495. * the non-prefix route would have a more specific name (users:index)
  496. */
  497. public function testMatchPrefixSpecificity(): void
  498. {
  499. $context = [
  500. '_base' => '/',
  501. '_scheme' => 'http',
  502. '_host' => 'example.org',
  503. ];
  504. $routes = new RouteBuilder($this->collection, '/');
  505. $routes->connect('/{action}/*', ['controller' => 'Users']);
  506. $routes->connect('/admin/{controller}', ['prefix' => 'Admin', 'action' => 'index']);
  507. $url = [
  508. 'plugin' => null,
  509. 'prefix' => 'Admin',
  510. 'controller' => 'Users',
  511. 'action' => 'index',
  512. ];
  513. $result = $this->collection->match($url, $context);
  514. $this->assertSame('admin/Users', $result);
  515. $url = [
  516. 'plugin' => null,
  517. 'controller' => 'Users',
  518. 'action' => 'index',
  519. ];
  520. $result = $this->collection->match($url, $context);
  521. $this->assertSame('index', $result);
  522. }
  523. /**
  524. * Test getting named routes.
  525. */
  526. public function testNamed(): void
  527. {
  528. $routes = new RouteBuilder($this->collection, '/l');
  529. $routes->connect('/{controller}', ['action' => 'index'], ['_name' => 'cntrl']);
  530. $routes->connect('/{controller}/{action}/*');
  531. $all = $this->collection->named();
  532. $this->assertCount(1, $all);
  533. $this->assertInstanceOf('Cake\Routing\Route\Route', $all['cntrl']);
  534. $this->assertSame('/l/{controller}', $all['cntrl']->template);
  535. }
  536. /**
  537. * Test the add() and routes() method.
  538. */
  539. public function testAddingRoutes(): void
  540. {
  541. $one = new Route('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
  542. $two = new Route('/', ['controller' => 'Dashboards', 'action' => 'display']);
  543. $this->collection->add($one);
  544. $this->collection->add($two);
  545. $routes = $this->collection->routes();
  546. $this->assertCount(2, $routes);
  547. $this->assertSame($one, $routes[0]);
  548. $this->assertSame($two, $routes[1]);
  549. }
  550. /**
  551. * Test the add() with some _name.
  552. */
  553. public function testAddingDuplicateNamedRoutes(): void
  554. {
  555. $this->expectException(DuplicateNamedRouteException::class);
  556. $one = new Route('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
  557. $two = new Route('/', ['controller' => 'Dashboards', 'action' => 'display']);
  558. $this->collection->add($one, ['_name' => 'test']);
  559. $this->collection->add($two, ['_name' => 'test']);
  560. }
  561. /**
  562. * Test basic setExtension and its getter.
  563. */
  564. public function testSetExtensions(): void
  565. {
  566. $this->assertEquals([], $this->collection->getExtensions());
  567. $this->collection->setExtensions(['json']);
  568. $this->assertEquals(['json'], $this->collection->getExtensions());
  569. $this->collection->setExtensions(['rss', 'xml']);
  570. $this->assertEquals(['json', 'rss', 'xml'], $this->collection->getExtensions());
  571. $this->collection->setExtensions(['csv'], false);
  572. $this->assertEquals(['csv'], $this->collection->getExtensions());
  573. }
  574. /**
  575. * Test adding middleware to the collection.
  576. */
  577. public function testRegisterMiddleware(): void
  578. {
  579. $result = $this->collection->registerMiddleware('closure', function (): void {
  580. });
  581. $this->assertSame($result, $this->collection);
  582. $mock = $this->getMockBuilder('\stdClass')
  583. ->addMethods(['__invoke'])
  584. ->getMock();
  585. $result = $this->collection->registerMiddleware('callable', $mock);
  586. $this->assertSame($result, $this->collection);
  587. $this->assertTrue($this->collection->hasMiddleware('closure'));
  588. $this->assertTrue($this->collection->hasMiddleware('callable'));
  589. $this->collection->registerMiddleware('class', 'Dumb');
  590. }
  591. /**
  592. * Test adding a middleware group to the collection.
  593. */
  594. public function testMiddlewareGroup(): void
  595. {
  596. $this->collection->registerMiddleware('closure', function (): void {
  597. });
  598. $mock = $this->getMockBuilder('\stdClass')
  599. ->addMethods(['__invoke'])
  600. ->getMock();
  601. $result = $this->collection->registerMiddleware('callable', $mock);
  602. $this->collection->registerMiddleware('callable', $mock);
  603. $this->collection->middlewareGroup('group', ['closure', 'callable']);
  604. $this->assertTrue($this->collection->hasMiddlewareGroup('group'));
  605. }
  606. /**
  607. * Test adding a middleware group with the same name overwrites the original list
  608. */
  609. public function testMiddlewareGroupOverwrite(): void
  610. {
  611. $stub = function (): void {
  612. };
  613. $this->collection->registerMiddleware('closure', $stub);
  614. $result = $this->collection->registerMiddleware('callable', $stub);
  615. $this->collection->registerMiddleware('callable', $stub);
  616. $this->collection->middlewareGroup('group', ['callable']);
  617. $this->collection->middlewareGroup('group', ['closure', 'callable']);
  618. $this->assertSame([$stub, $stub], $this->collection->getMiddleware(['group']));
  619. }
  620. /**
  621. * Test adding ab unregistered middleware to a middleware group fails.
  622. */
  623. public function testMiddlewareGroupUnregisteredMiddleware(): void
  624. {
  625. $this->expectException(RuntimeException::class);
  626. $this->expectExceptionMessage('Cannot add \'bad\' middleware to group \'group\'. It has not been registered.');
  627. $this->collection->middlewareGroup('group', ['bad']);
  628. }
  629. }