RouteTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. <?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
  2. /**
  3. * Description of RouteTest
  4. *
  5. * @group kohana
  6. * @group kohana.core
  7. * @group kohana.core.route
  8. *
  9. * @package Kohana
  10. * @category Tests
  11. * @author Kohana Team
  12. * @author BRMatt <[email protected]>
  13. * @copyright (c) 2008-2012 Kohana Team
  14. * @license http://kohanaframework.org/license
  15. */
  16. include Kohana::find_file('tests', 'test_data/callback_routes');
  17. class Kohana_RouteTest extends Unittest_TestCase
  18. {
  19. /**
  20. * Remove all caches
  21. */
  22. // @codingStandardsIgnoreStart
  23. public function setUp()
  24. // @codingStandardsIgnoreEnd
  25. {
  26. parent::setUp();
  27. $this->cleanCacheDir();
  28. }
  29. /**
  30. * Removes cache files created during tests
  31. */
  32. // @codingStandardsIgnoreStart
  33. public function tearDown()
  34. // @codingStandardsIgnoreEnd
  35. {
  36. parent::tearDown();
  37. $this->cleanCacheDir();
  38. }
  39. /**
  40. * If Route::get() is asked for a route that does not exist then
  41. * it should throw a Kohana_Exception
  42. *
  43. * Note use of @expectedException
  44. *
  45. * @test
  46. * @covers Route::get
  47. * @expectedException Kohana_Exception
  48. */
  49. public function test_get_throws_exception_if_route_dnx()
  50. {
  51. Route::get('HAHAHAHAHAHAHAHAHA');
  52. }
  53. /**
  54. * Route::all() should return all routes defined via Route::set()
  55. * and not through new Route()
  56. *
  57. * @test
  58. * @covers Route::all
  59. */
  60. public function test_all_returns_all_defined_routes()
  61. {
  62. $defined_routes = self::readAttribute('Route', '_routes');
  63. $this->assertSame($defined_routes, Route::all());
  64. }
  65. /**
  66. * Route::name() should fetch the name of a passed route
  67. * If route is not found then it should return FALSE
  68. *
  69. * @TODO: This test needs to segregate the Route::$_routes singleton
  70. * @test
  71. * @covers Route::name
  72. */
  73. public function test_name_returns_routes_name_or_false_if_dnx()
  74. {
  75. $route = Route::set('flamingo_people', 'flamingo/dance');
  76. $this->assertSame('flamingo_people', Route::name($route));
  77. $route = new Route('dance/dance');
  78. $this->assertFalse(Route::name($route));
  79. }
  80. /**
  81. * If Route::cache() was able to restore routes from the cache then
  82. * it should return TRUE and load the cached routes
  83. *
  84. * @test
  85. * @covers Route::cache
  86. */
  87. public function test_cache_stores_route_objects()
  88. {
  89. $routes = Route::all();
  90. // First we create the cache
  91. Route::cache(TRUE);
  92. // Now lets modify the "current" routes
  93. Route::set('nonsensical_route', 'flabbadaga/ding_dong');
  94. // Then try and load said cache
  95. $this->assertTrue(Route::cache());
  96. // Check the route cache flag
  97. $this->assertTrue(Route::$cache);
  98. // And if all went ok the nonsensical route should be gone...
  99. $this->assertEquals($routes, Route::all());
  100. }
  101. /**
  102. * Check appending cached routes. See http://dev.kohanaframework.org/issues/4347
  103. *
  104. * @test
  105. * @covers Route::cache
  106. */
  107. public function test_cache_append_routes()
  108. {
  109. $cached = Route::all();
  110. // First we create the cache
  111. Route::cache(TRUE);
  112. // Now lets modify the "current" routes
  113. Route::set('nonsensical_route', 'flabbadaga/ding_dong');
  114. $modified = Route::all();
  115. // Then try and load said cache
  116. $this->assertTrue(Route::cache(NULL, TRUE));
  117. // Check the route cache flag
  118. $this->assertTrue(Route::$cache);
  119. // And if all went ok the nonsensical route should exist with the other routes...
  120. $this->assertEquals(Route::all(), $cached + $modified);
  121. }
  122. /**
  123. * Route::cache() should return FALSE if cached routes could not be found
  124. *
  125. * The cache is cleared before and after each test in setUp tearDown
  126. * by cleanCacheDir()
  127. *
  128. * @test
  129. * @covers Route::cache
  130. */
  131. public function test_cache_returns_false_if_cache_dnx()
  132. {
  133. $this->assertSame(FALSE, Route::cache(), 'Route cache was not empty');
  134. // Check the route cache flag
  135. $this->assertFalse(Route::$cache);
  136. }
  137. /**
  138. * If the constructor is passed a NULL uri then it should assume it's
  139. * being loaded from the cache & therefore shouldn't override the cached attributes
  140. *
  141. * @test
  142. * @covers Route::__construct
  143. */
  144. public function test_constructor_returns_if_uri_is_null()
  145. {
  146. // We use a mock object to make sure that the route wasn't recompiled
  147. $route = $this->getMock('Route', array('_compile'), array(), '', FALSE);
  148. $route
  149. ->expects($this->never())
  150. ->method('_compile');
  151. $route->__construct(NULL,NULL);
  152. $this->assertAttributeSame('', '_uri', $route);
  153. $this->assertAttributeSame(array(), '_regex', $route);
  154. $this->assertAttributeSame(array('action' => 'index', 'host' => FALSE), '_defaults', $route);
  155. $this->assertAttributeSame(NULL, '_route_regex', $route);
  156. }
  157. /**
  158. * Provider for test_constructor_only_changes_custom_regex_if_passed
  159. *
  160. * @return array
  161. */
  162. public function provider_constructor_only_changes_custom_regex_if_passed()
  163. {
  164. return array(
  165. array('<controller>/<action>', '<controller>/<action>'),
  166. );
  167. }
  168. /**
  169. * The constructor should only use custom regex if passed a non-empty array
  170. *
  171. * Technically we can't "test" this as the default regex is an empty array, this
  172. * is purely for improving test coverage
  173. *
  174. * @dataProvider provider_constructor_only_changes_custom_regex_if_passed
  175. *
  176. * @test
  177. * @covers Route::__construct
  178. */
  179. public function test_constructor_only_changes_custom_regex_if_passed($uri, $uri2)
  180. {
  181. $route = new Route($uri, array());
  182. $this->assertAttributeSame(array(), '_regex', $route);
  183. $route = new Route($uri2, NULL);
  184. $this->assertAttributeSame(array(), '_regex', $route);
  185. }
  186. /**
  187. * When we pass custom regex to the route's constructor it should it
  188. * in leu of the default. This does not apply to callback/lambda routes
  189. *
  190. * @test
  191. * @covers Route::__construct
  192. * @covers Route::compile
  193. */
  194. public function test_route_uses_custom_regex_passed_to_constructor()
  195. {
  196. $regex = array('id' => '[0-9]{1,2}');
  197. $route = new Route('<controller>(/<action>(/<id>))', $regex);
  198. $this->assertAttributeSame($regex, '_regex', $route);
  199. $this->assertAttributeContains(
  200. $regex['id'],
  201. '_route_regex',
  202. $route
  203. );
  204. }
  205. /**
  206. * Provider for test_matches_returns_false_on_failure
  207. *
  208. * @return array
  209. */
  210. public function provider_matches_returns_false_on_failure()
  211. {
  212. return array(
  213. array('projects/(<project_id>/(<controller>(/<action>(/<id>))))', 'apple/pie'),
  214. );
  215. }
  216. /**
  217. * Route::matches() should return false if the route doesn't match against a uri
  218. *
  219. * @dataProvider provider_matches_returns_false_on_failure
  220. *
  221. * @test
  222. * @covers Route::matches
  223. */
  224. public function test_matches_returns_false_on_failure($uri, $match)
  225. {
  226. $route = new Route($uri);
  227. // Mock a request class with the $match uri
  228. $stub = $this->getMock('Request', array('uri'), array($match));
  229. $stub->expects($this->any())
  230. ->method('uri')
  231. // Request::uri() called by Route::matches() will return $match
  232. ->will($this->returnValue($match));
  233. $this->assertSame(FALSE, $route->matches($stub));
  234. }
  235. /**
  236. * Provider for test_matches_returns_array_of_parameters_on_successful_match
  237. *
  238. * @return array
  239. */
  240. public function provider_matches_returns_array_of_parameters_on_successful_match()
  241. {
  242. return array(
  243. array(
  244. '(<controller>(/<action>(/<id>)))',
  245. 'welcome/index',
  246. 'Welcome',
  247. 'index',
  248. ),
  249. );
  250. }
  251. /**
  252. * Route::matches() should return an array of parameters when a match is made
  253. * An parameters that are not matched should not be present in the array of matches
  254. *
  255. * @dataProvider provider_matches_returns_array_of_parameters_on_successful_match
  256. *
  257. * @test
  258. * @covers Route::matches
  259. */
  260. public function test_matches_returns_array_of_parameters_on_successful_match($uri, $m, $c, $a)
  261. {
  262. $route = new Route($uri);
  263. // Mock a request class with the $m uri
  264. $request = $this->getMock('Request', array('uri'), array($m));
  265. $request->expects($this->any())
  266. ->method('uri')
  267. // Request::uri() called by Route::matches() will return $m
  268. ->will($this->returnValue($m));
  269. $matches = $route->matches($request);
  270. $this->assertInternalType('array', $matches);
  271. $this->assertArrayHasKey('controller', $matches);
  272. $this->assertArrayHasKey('action', $matches);
  273. $this->assertArrayNotHasKey('id', $matches);
  274. // $this->assertSame(5, count($matches));
  275. $this->assertSame($c, $matches['controller']);
  276. $this->assertSame($a, $matches['action']);
  277. }
  278. /**
  279. * Provider for test_matches_returns_array_of_parameters_on_successful_match
  280. *
  281. * @return array
  282. */
  283. public function provider_defaults_are_used_if_params_arent_specified()
  284. {
  285. return array(
  286. array(
  287. '<controller>(/<action>(/<id>))',
  288. NULL,
  289. array('controller' => 'Welcome', 'action' => 'index'),
  290. 'Welcome',
  291. 'index',
  292. 'unit/test/1',
  293. array(
  294. 'controller' => 'unit',
  295. 'action' => 'test',
  296. 'id' => '1'
  297. ),
  298. 'Welcome',
  299. ),
  300. array(
  301. '(<controller>(/<action>(/<id>)))',
  302. NULL,
  303. array('controller' => 'welcome', 'action' => 'index'),
  304. 'Welcome',
  305. 'index',
  306. 'unit/test/1',
  307. array(
  308. 'controller' => 'unit',
  309. 'action' => 'test',
  310. 'id' => '1'
  311. ),
  312. '',
  313. ),
  314. /**
  315. * Specifying this should cause controller and action to show up
  316. * refs #4113
  317. */
  318. array(
  319. '(<controller>(/<action>(/<id>)))',
  320. NULL,
  321. array('controller' => 'welcome', 'action' => 'index'),
  322. 'Welcome',
  323. 'index',
  324. 'welcome/index/1',
  325. array(
  326. 'id' => '1'
  327. ),
  328. '',
  329. ),
  330. array(
  331. '<controller>(/<action>(/<id>))',
  332. NULL,
  333. array('controller' => 'welcome', 'action' => 'index'),
  334. 'Welcome',
  335. 'index',
  336. 'welcome/foo',
  337. array(
  338. 'action' => 'foo',
  339. ),
  340. 'welcome',
  341. ),
  342. );
  343. }
  344. /**
  345. * Defaults specified with defaults() should be used if their values aren't
  346. * present in the uri
  347. *
  348. * @dataProvider provider_defaults_are_used_if_params_arent_specified
  349. *
  350. * @test
  351. * @covers Route::matches
  352. */
  353. public function test_defaults_are_used_if_params_arent_specified($uri, $regex, $defaults, $c, $a, $test_uri, $test_uri_array, $default_uri)
  354. {
  355. $route = new Route($uri, $regex);
  356. $route->defaults($defaults);
  357. $this->assertSame($defaults, $route->defaults());
  358. // Mock a request class
  359. $request = $this->getMock('Request', array('uri'), array($default_uri));
  360. $request->expects($this->any())
  361. ->method('uri')
  362. ->will($this->returnValue($default_uri));
  363. $matches = $route->matches($request);
  364. $this->assertInternalType('array', $matches);
  365. $this->assertArrayHasKey('controller', $matches);
  366. $this->assertArrayHasKey('action', $matches);
  367. $this->assertArrayNotHasKey('id', $matches);
  368. // $this->assertSame(4, count($matches));
  369. $this->assertSame($c, $matches['controller']);
  370. $this->assertSame($a, $matches['action']);
  371. $this->assertSame($test_uri, $route->uri($test_uri_array));
  372. $this->assertSame($default_uri, $route->uri());
  373. }
  374. /**
  375. * Optional params should not be used if what is passed in is identical
  376. * to the default.
  377. *
  378. * refs #4116
  379. *
  380. * @test
  381. * @covers Route::uri
  382. */
  383. public function test_defaults_are_not_used_if_param_is_identical()
  384. {
  385. $route = new Route('(<controller>(/<action>(/<id>)))');
  386. $route->defaults(array(
  387. 'controller' => 'welcome',
  388. 'action' => 'index'
  389. ));
  390. $this->assertSame('', $route->uri(array('controller' => 'welcome')));
  391. $this->assertSame('welcome2', $route->uri(array('controller' => 'welcome2')));
  392. }
  393. /**
  394. * Provider for test_required_parameters_are_needed
  395. *
  396. * @return array
  397. */
  398. public function provider_required_parameters_are_needed()
  399. {
  400. return array(
  401. array(
  402. 'admin(/<controller>(/<action>(/<id>)))',
  403. 'admin',
  404. 'admin/users/add',
  405. ),
  406. );
  407. }
  408. /**
  409. * This tests that routes with required parameters will not match uris without them present
  410. *
  411. * @dataProvider provider_required_parameters_are_needed
  412. *
  413. * @test
  414. * @covers Route::matches
  415. */
  416. public function test_required_parameters_are_needed($uri, $matches_route1, $matches_route2)
  417. {
  418. $route = new Route($uri);
  419. // Mock a request class that will return empty uri
  420. $request = $this->getMock('Request', array('uri'), array(''));
  421. $request->expects($this->any())
  422. ->method('uri')
  423. ->will($this->returnValue(''));
  424. $this->assertFalse($route->matches($request));
  425. // Mock a request class that will return route1
  426. $request = $this->getMock('Request', array('uri'), array($matches_route1));
  427. $request->expects($this->any())
  428. ->method('uri')
  429. ->will($this->returnValue($matches_route1));
  430. $matches = $route->matches($request);
  431. $this->assertInternalType('array', $matches);
  432. // Mock a request class that will return route2 uri
  433. $request = $this->getMock('Request', array('uri'), array($matches_route2));
  434. $request->expects($this->any())
  435. ->method('uri')
  436. ->will($this->returnValue($matches_route2));
  437. $matches = $route->matches($request);
  438. $this->assertInternalType('array', $matches);
  439. // $this->assertSame(5, count($matches));
  440. $this->assertArrayHasKey('controller', $matches);
  441. $this->assertArrayHasKey('action', $matches);
  442. }
  443. /**
  444. * Provider for test_required_parameters_are_needed
  445. *
  446. * @return array
  447. */
  448. public function provider_reverse_routing_returns_routes_uri_if_route_is_static()
  449. {
  450. return array(
  451. array(
  452. 'info/about_us',
  453. NULL,
  454. 'info/about_us',
  455. array('some' => 'random', 'params' => 'to confuse'),
  456. ),
  457. );
  458. }
  459. /**
  460. * This tests the reverse routing returns the uri specified in the route
  461. * if it's a static route
  462. *
  463. * A static route is a route without any parameters
  464. *
  465. * @dataProvider provider_reverse_routing_returns_routes_uri_if_route_is_static
  466. *
  467. * @test
  468. * @covers Route::uri
  469. */
  470. public function test_reverse_routing_returns_routes_uri_if_route_is_static($uri, $regex, $target_uri, $uri_params)
  471. {
  472. $route = new Route($uri, $regex);
  473. $this->assertSame($target_uri, $route->uri($uri_params));
  474. }
  475. /**
  476. * Provider for test_uri_throws_exception_if_required_params_are_missing
  477. *
  478. * @return array
  479. */
  480. public function provider_uri_throws_exception_if_required_params_are_missing()
  481. {
  482. return array(
  483. array(
  484. '<controller>(/<action)',
  485. NULL,
  486. array('action' => 'awesome-action'),
  487. ),
  488. );
  489. }
  490. /**
  491. * When Route::uri is working on a uri that requires certain parameters to be present
  492. * (i.e. <controller> in '<controller(/<action)') then it should throw an exception
  493. * if the param was not provided
  494. *
  495. * @dataProvider provider_uri_throws_exception_if_required_params_are_missing
  496. *
  497. * @test
  498. * @covers Route::uri
  499. */
  500. public function test_uri_throws_exception_if_required_params_are_missing($uri, $regex, $uri_array)
  501. {
  502. $route = new Route($uri, $regex);
  503. try
  504. {
  505. $route->uri($uri_array);
  506. $this->fail('Route::uri should throw exception if required param is not provided');
  507. }
  508. catch(Exception $e)
  509. {
  510. $this->assertInstanceOf('Kohana_Exception', $e);
  511. // Check that the error in question is about the controller param
  512. $this->assertContains('controller', $e->getMessage());
  513. }
  514. }
  515. /**
  516. * Provider for test_uri_fills_required_uri_segments_from_params
  517. *
  518. * @return array
  519. */
  520. public function provider_uri_fills_required_uri_segments_from_params()
  521. {
  522. return array(
  523. array(
  524. '<controller>/<action>(/<id>)',
  525. NULL,
  526. 'users/edit',
  527. array(
  528. 'controller' => 'users',
  529. 'action' => 'edit',
  530. ),
  531. 'users/edit/god',
  532. array(
  533. 'controller' => 'users',
  534. 'action' => 'edit',
  535. 'id' => 'god',
  536. ),
  537. ),
  538. );
  539. }
  540. /**
  541. * The logic for replacing required segments is separate (but similar) to that for
  542. * replacing optional segments.
  543. *
  544. * This test asserts that Route::uri will replace required segments with provided
  545. * params
  546. *
  547. * @dataProvider provider_uri_fills_required_uri_segments_from_params
  548. *
  549. * @test
  550. * @covers Route::uri
  551. */
  552. public function test_uri_fills_required_uri_segments_from_params($uri, $regex, $uri_string1, $uri_array1, $uri_string2, $uri_array2)
  553. {
  554. $route = new Route($uri, $regex);
  555. $this->assertSame(
  556. $uri_string1,
  557. $route->uri($uri_array1)
  558. );
  559. $this->assertSame(
  560. $uri_string2,
  561. $route->uri($uri_array2)
  562. );
  563. }
  564. /**
  565. * Provides test data for test_composing_url_from_route()
  566. * @return array
  567. */
  568. public function provider_composing_url_from_route()
  569. {
  570. return array(
  571. array('/'),
  572. array('/news/view/42', array('controller' => 'news', 'action' => 'view', 'id' => 42)),
  573. array('http://kohanaframework.org/news', array('controller' => 'news'), 'http')
  574. );
  575. }
  576. /**
  577. * Tests Route::url()
  578. *
  579. * Checks the url composing from specific route via Route::url() shortcut
  580. *
  581. * @test
  582. * @dataProvider provider_composing_url_from_route
  583. * @param string $expected
  584. * @param array $params
  585. * @param boolean $protocol
  586. */
  587. public function test_composing_url_from_route($expected, $params = NULL, $protocol = NULL)
  588. {
  589. Route::set('foobar', '(<controller>(/<action>(/<id>)))')
  590. ->defaults(array(
  591. 'controller' => 'welcome',
  592. )
  593. );
  594. $this->setEnvironment(array(
  595. '_SERVER' => array('HTTP_HOST' => 'kohanaframework.org'),
  596. 'Kohana::$base_url' => '/',
  597. 'Kohana::$index_file' => '',
  598. ));
  599. $this->assertSame($expected, Route::url('foobar', $params, $protocol));
  600. }
  601. /**
  602. * Tests Route::compile()
  603. *
  604. * Makes sure that compile will use custom regex if specified
  605. *
  606. * @test
  607. * @covers Route::compile
  608. */
  609. public function test_compile_uses_custom_regex_if_specificed()
  610. {
  611. $compiled = Route::compile(
  612. '<controller>(/<action>(/<id>))',
  613. array(
  614. 'controller' => '[a-z]+',
  615. 'id' => '\d+',
  616. )
  617. );
  618. $this->assertSame('#^(?P<controller>[a-z]+)(?:/(?P<action>[^/.,;?\n]++)(?:/(?P<id>\d+))?)?$#uD', $compiled);
  619. }
  620. /**
  621. * Tests Route::is_external(), ensuring the host can return
  622. * whether internal or external host
  623. */
  624. public function test_is_external_route_from_host()
  625. {
  626. // Setup local route
  627. Route::set('internal', 'local/test/route')
  628. ->defaults(array(
  629. 'controller' => 'foo',
  630. 'action' => 'bar'
  631. )
  632. );
  633. // Setup external route
  634. Route::set('external', 'local/test/route')
  635. ->defaults(array(
  636. 'controller' => 'foo',
  637. 'action' => 'bar',
  638. 'host' => 'http://kohanaframework.org'
  639. )
  640. );
  641. // Test internal route
  642. $this->assertFalse(Route::get('internal')->is_external());
  643. // Test external route
  644. $this->assertTrue(Route::get('external')->is_external());
  645. }
  646. /**
  647. * Provider for test_external_route_includes_params_in_uri
  648. *
  649. * @return array
  650. */
  651. public function provider_external_route_includes_params_in_uri()
  652. {
  653. return array(
  654. array(
  655. '<controller>/<action>',
  656. array(
  657. 'controller' => 'foo',
  658. 'action' => 'bar',
  659. 'host' => 'kohanaframework.org'
  660. ),
  661. 'http://kohanaframework.org/foo/bar'
  662. ),
  663. array(
  664. '<controller>/<action>',
  665. array(
  666. 'controller' => 'foo',
  667. 'action' => 'bar',
  668. 'host' => 'http://kohanaframework.org'
  669. ),
  670. 'http://kohanaframework.org/foo/bar'
  671. ),
  672. array(
  673. 'foo/bar',
  674. array(
  675. 'controller' => 'foo',
  676. 'host' => 'http://kohanaframework.org'
  677. ),
  678. 'http://kohanaframework.org/foo/bar'
  679. ),
  680. );
  681. }
  682. /**
  683. * Tests the external route include route parameters
  684. *
  685. * @dataProvider provider_external_route_includes_params_in_uri
  686. */
  687. public function test_external_route_includes_params_in_uri($route, $defaults, $expected_uri)
  688. {
  689. Route::set('test', $route)
  690. ->defaults($defaults);
  691. $this->assertSame($expected_uri, Route::get('test')->uri());
  692. }
  693. /**
  694. * Provider for test_route_filter_modify_params
  695. *
  696. * @return array
  697. */
  698. public function provider_route_filter_modify_params()
  699. {
  700. return array(
  701. array(
  702. '<controller>/<action>',
  703. array(
  704. 'controller' => 'Test',
  705. 'action' => 'same',
  706. ),
  707. array('Route_Holder', 'route_filter_modify_params_array'),
  708. 'test/different',
  709. array(
  710. 'controller' => 'Test',
  711. 'action' => 'modified',
  712. ),
  713. ),
  714. array(
  715. '<controller>/<action>',
  716. array(
  717. 'controller' => 'test',
  718. 'action' => 'same',
  719. ),
  720. array('Route_Holder', 'route_filter_modify_params_false'),
  721. 'test/fail',
  722. FALSE,
  723. ),
  724. );
  725. }
  726. /**
  727. * Tests that route filters can modify parameters
  728. *
  729. * @covers Route::filter
  730. * @dataProvider provider_route_filter_modify_params
  731. */
  732. public function test_route_filter_modify_params($route, $defaults, $filter, $uri, $expected_params)
  733. {
  734. $route = new Route($route);
  735. // Mock a request class
  736. $request = $this->getMock('Request', array('uri'), array($uri));
  737. $request->expects($this->any())
  738. ->method('uri')
  739. ->will($this->returnValue($uri));
  740. $params = $route->defaults($defaults)->filter($filter)->matches($request);
  741. $this->assertSame($expected_params, $params);
  742. }
  743. }