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'], ['_name' => 'media_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. '_name' => 'media_search'
  353. ];
  354. $this->assertEquals($expected, $result);
  355. $request = new ServerRequest(['url' => '/b/media/search/thing']);
  356. $result = $this->collection->parseRequest($request);
  357. $expected = [
  358. 'key' => 'value',
  359. 'pass' => ['thing'],
  360. 'plugin' => null,
  361. 'controller' => 'Media',
  362. 'action' => 'search',
  363. '_matchedRoute' => '/b/media/search/*',
  364. '_name' => 'media_search'
  365. ];
  366. $this->assertEquals($expected, $result);
  367. $request = new ServerRequest(['url' => '/b/the-thing?one=two']);
  368. $result = $this->collection->parseRequest($request);
  369. $expected = [
  370. 'controller' => 'Articles',
  371. 'action' => 'view',
  372. 'id' => 'the-thing',
  373. 'pass' => [],
  374. 'plugin' => null,
  375. 'key' => 'value',
  376. '?' => ['one' => 'two'],
  377. '_matchedRoute' => '/b/:id',
  378. ];
  379. $this->assertEquals($expected, $result);
  380. }
  381. /**
  382. * Test parsing routes that match non-ascii urls
  383. *
  384. * @return void
  385. */
  386. public function testParseRequestUnicode()
  387. {
  388. $routes = new RouteBuilder($this->collection, '/b', []);
  389. $routes->connect('/alta/confirmación', ['controller' => 'Media', 'action' => 'confirm']);
  390. $request = new ServerRequest(['url' => '/b/alta/confirmaci%C3%B3n']);
  391. $result = $this->collection->parseRequest($request);
  392. $expected = [
  393. 'controller' => 'Media',
  394. 'action' => 'confirm',
  395. 'pass' => [],
  396. 'plugin' => null,
  397. '_matchedRoute' => '/b/alta/confirmación',
  398. ];
  399. $this->assertEquals($expected, $result);
  400. $request = new ServerRequest(['url' => '/b/alta/confirmación']);
  401. $result = $this->collection->parseRequest($request);
  402. $this->assertEquals($expected, $result);
  403. }
  404. /**
  405. * Test match() throws an error on unknown routes.
  406. *
  407. */
  408. public function testMatchError()
  409. {
  410. $this->expectException(\Cake\Routing\Exception\MissingRouteException::class);
  411. $this->expectExceptionMessage('A route matching "array (');
  412. $context = [
  413. '_base' => '/',
  414. '_scheme' => 'http',
  415. '_host' => 'example.org',
  416. ];
  417. $routes = new RouteBuilder($this->collection, '/b');
  418. $routes->connect('/', ['controller' => 'Articles']);
  419. $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'add'], $context);
  420. }
  421. /**
  422. * Test matching routes.
  423. *
  424. * @return void
  425. */
  426. public function testMatch()
  427. {
  428. $context = [
  429. '_base' => '/',
  430. '_scheme' => 'http',
  431. '_host' => 'example.org',
  432. ];
  433. $routes = new RouteBuilder($this->collection, '/b');
  434. $routes->connect('/', ['controller' => 'Articles']);
  435. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
  436. $result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'index'], $context);
  437. $this->assertEquals('b', $result);
  438. $result = $this->collection->match(
  439. ['id' => 'thing', 'plugin' => null, 'controller' => 'Articles', 'action' => 'view'],
  440. $context
  441. );
  442. $this->assertEquals('b/thing', $result);
  443. }
  444. /**
  445. * Test matching routes with names
  446. *
  447. * @return void
  448. */
  449. public function testMatchNamed()
  450. {
  451. $context = [
  452. '_base' => '/',
  453. '_scheme' => 'http',
  454. '_host' => 'example.org',
  455. ];
  456. $routes = new RouteBuilder($this->collection, '/b');
  457. $routes->connect('/', ['controller' => 'Articles']);
  458. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view'], ['_name' => 'article:view']);
  459. $result = $this->collection->match(['_name' => 'article:view', 'id' => '2'], $context);
  460. $this->assertEquals('/b/2', $result);
  461. $result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'view', 'id' => '2'], $context);
  462. $this->assertEquals('b/2', $result);
  463. }
  464. /**
  465. * Test match() throws an error on named routes that fail to match
  466. *
  467. */
  468. public function testMatchNamedError()
  469. {
  470. $this->expectException(\Cake\Routing\Exception\MissingRouteException::class);
  471. $this->expectExceptionMessage('A named route was found for "fail", but matching failed');
  472. $context = [
  473. '_base' => '/',
  474. '_scheme' => 'http',
  475. '_host' => 'example.org',
  476. ];
  477. $routes = new RouteBuilder($this->collection, '/b');
  478. $routes->connect('/:lang/articles', ['controller' => 'Articles'], ['_name' => 'fail']);
  479. $this->collection->match(['_name' => 'fail'], $context);
  480. }
  481. /**
  482. * Test matching routes with names and failing
  483. *
  484. * @return void
  485. */
  486. public function testMatchNamedMissingError()
  487. {
  488. $this->expectException(\Cake\Routing\Exception\MissingRouteException::class);
  489. $context = [
  490. '_base' => '/',
  491. '_scheme' => 'http',
  492. '_host' => 'example.org',
  493. ];
  494. $routes = new RouteBuilder($this->collection, '/b');
  495. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view'], ['_name' => 'article:view']);
  496. $this->collection->match(['_name' => 'derp'], $context);
  497. }
  498. /**
  499. * Test matching plugin routes.
  500. *
  501. * @return void
  502. */
  503. public function testMatchPlugin()
  504. {
  505. $context = [
  506. '_base' => '/',
  507. '_scheme' => 'http',
  508. '_host' => 'example.org',
  509. ];
  510. $routes = new RouteBuilder($this->collection, '/contacts', ['plugin' => 'Contacts']);
  511. $routes->connect('/', ['controller' => 'Contacts']);
  512. $result = $this->collection->match(['plugin' => 'Contacts', 'controller' => 'Contacts', 'action' => 'index'], $context);
  513. $this->assertEquals('contacts', $result);
  514. }
  515. /**
  516. * Test that prefixes increase the specificity of a route match.
  517. *
  518. * Connect the admin route after the non prefixed version, this means
  519. * the non-prefix route would have a more specific name (users:index)
  520. *
  521. * @return void
  522. */
  523. public function testMatchPrefixSpecificity()
  524. {
  525. $context = [
  526. '_base' => '/',
  527. '_scheme' => 'http',
  528. '_host' => 'example.org',
  529. ];
  530. $routes = new RouteBuilder($this->collection, '/');
  531. $routes->connect('/:action/*', ['controller' => 'Users']);
  532. $routes->connect('/admin/:controller', ['prefix' => 'admin', 'action' => 'index']);
  533. $url = [
  534. 'plugin' => null,
  535. 'prefix' => 'admin',
  536. 'controller' => 'Users',
  537. 'action' => 'index'
  538. ];
  539. $result = $this->collection->match($url, $context);
  540. $this->assertEquals('admin/Users', $result);
  541. $url = [
  542. 'plugin' => null,
  543. 'controller' => 'Users',
  544. 'action' => 'index'
  545. ];
  546. $result = $this->collection->match($url, $context);
  547. $this->assertEquals('index', $result);
  548. }
  549. /**
  550. * Test getting named routes.
  551. *
  552. * @return void
  553. */
  554. public function testNamed()
  555. {
  556. $routes = new RouteBuilder($this->collection, '/l');
  557. $routes->connect('/:controller', ['action' => 'index'], ['_name' => 'cntrl']);
  558. $routes->connect('/:controller/:action/*');
  559. $all = $this->collection->named();
  560. $this->assertCount(1, $all);
  561. $this->assertInstanceOf('Cake\Routing\Route\Route', $all['cntrl']);
  562. $this->assertEquals('/l/:controller', $all['cntrl']->template);
  563. }
  564. /**
  565. * Test the add() and routes() method.
  566. *
  567. * @return void
  568. */
  569. public function testAddingRoutes()
  570. {
  571. $one = new Route('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
  572. $two = new Route('/', ['controller' => 'Dashboards', 'action' => 'display']);
  573. $this->collection->add($one);
  574. $this->collection->add($two);
  575. $routes = $this->collection->routes();
  576. $this->assertCount(2, $routes);
  577. $this->assertSame($one, $routes[0]);
  578. $this->assertSame($two, $routes[1]);
  579. }
  580. /**
  581. * Test the add() with some _name.
  582. *
  583. *
  584. * @return void
  585. */
  586. public function testAddingDuplicateNamedRoutes()
  587. {
  588. $this->expectException(\Cake\Routing\Exception\DuplicateNamedRouteException::class);
  589. $one = new Route('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
  590. $two = new Route('/', ['controller' => 'Dashboards', 'action' => 'display']);
  591. $this->collection->add($one, ['_name' => 'test']);
  592. $this->collection->add($two, ['_name' => 'test']);
  593. }
  594. /**
  595. * Test combined get/set method.
  596. *
  597. * @return void
  598. * @deprecated 3.5.0
  599. */
  600. public function testExtensions()
  601. {
  602. $this->assertEquals([], $this->collection->extensions());
  603. $this->collection->extensions('json');
  604. $this->assertEquals(['json'], $this->collection->extensions());
  605. $this->collection->extensions(['rss', 'xml']);
  606. $this->assertEquals(['json', 'rss', 'xml'], $this->collection->extensions());
  607. $this->collection->extensions(['csv'], false);
  608. $this->assertEquals(['csv'], $this->collection->extensions());
  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. }