RouteCollectionTest.php 25 KB

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