RouteCollectionTest.php 25 KB

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