DashedRouteTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Routing\Route;
  16. use Cake\Core\App;
  17. use Cake\Routing\Router;
  18. use Cake\Routing\Route\DashedRoute;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * Test case for DashedRoute
  22. */
  23. class DashedRouteTest extends TestCase {
  24. /**
  25. * test that routes match their pattern.
  26. *
  27. * @return void
  28. */
  29. public function testMatchBasic() {
  30. $route = new DashedRoute('/:controller/:action/:id', ['plugin' => null]);
  31. $result = $route->match(['controller' => 'Posts', 'action' => 'myView', 'plugin' => null]);
  32. $this->assertFalse($result);
  33. $result = $route->match([
  34. 'plugin' => null,
  35. 'controller' => 'Posts',
  36. 'action' => 'myView',
  37. 0
  38. ]);
  39. $this->assertFalse($result);
  40. $result = $route->match([
  41. 'plugin' => null,
  42. 'controller' => 'MyPosts',
  43. 'action' => 'myView',
  44. 'id' => 1
  45. ]);
  46. $this->assertEquals('/my-posts/my-view/1', $result);
  47. $route = new DashedRoute('/', ['controller' => 'Pages', 'action' => 'myDisplay', 'home']);
  48. $result = $route->match(['controller' => 'Pages', 'action' => 'myDisplay', 'home']);
  49. $this->assertEquals('/', $result);
  50. $result = $route->match(['controller' => 'Pages', 'action' => 'display', 'about']);
  51. $this->assertFalse($result);
  52. $route = new DashedRoute('/blog/:action', ['controller' => 'Posts']);
  53. $result = $route->match(['controller' => 'Posts', 'action' => 'myView']);
  54. $this->assertEquals('/blog/my-view', $result);
  55. $result = $route->match(['controller' => 'Posts', 'action' => 'myView', 'id' => 2]);
  56. $this->assertEquals('/blog/my-view?id=2', $result);
  57. $result = $route->match(['controller' => 'Posts', 'action' => 'myView', 1]);
  58. $this->assertFalse($result);
  59. $route = new DashedRoute('/foo/:controller/:action', ['action' => 'index']);
  60. $result = $route->match(['controller' => 'Posts', 'action' => 'myView']);
  61. $this->assertEquals('/foo/posts/my-view', $result);
  62. $route = new DashedRoute('/:plugin/:id/*', ['controller' => 'Posts', 'action' => 'myView']);
  63. $result = $route->match([
  64. 'plugin' => 'TestPlugin',
  65. 'controller' => 'Posts',
  66. 'action' => 'myView',
  67. 'id' => '1'
  68. ]);
  69. $this->assertEquals('/test-plugin/1/', $result);
  70. $result = $route->match([
  71. 'plugin' => 'TestPlugin',
  72. 'controller' => 'Posts',
  73. 'action' => 'myView',
  74. 'id' => '1',
  75. '0'
  76. ]);
  77. $this->assertEquals('/test-plugin/1/0', $result);
  78. $result = $route->match([
  79. 'plugin' => 'TestPlugin',
  80. 'controller' => 'Nodes',
  81. 'action' => 'myView',
  82. 'id' => 1
  83. ]);
  84. $this->assertFalse($result);
  85. $result = $route->match([
  86. 'plugin' => 'TestPlugin',
  87. 'controller' => 'Posts',
  88. 'action' => 'edit',
  89. 'id' => 1
  90. ]);
  91. $this->assertFalse($result);
  92. $route = new DashedRoute('/admin/subscriptions/:action/*', [
  93. 'controller' => 'Subscribe', 'prefix' => 'admin'
  94. ]);
  95. $result = $route->match([
  96. 'controller' => 'Subscribe',
  97. 'prefix' => 'admin',
  98. 'action' => 'editAdminE',
  99. 1
  100. ]);
  101. $expected = '/admin/subscriptions/edit-admin-e/1';
  102. $this->assertEquals($expected, $result);
  103. }
  104. /**
  105. * test the parse method of DashedRoute.
  106. *
  107. * @return void
  108. */
  109. public function testParse() {
  110. $route = new DashedRoute(
  111. '/:controller/:action/:id',
  112. ['controller' => 'Testing4', 'id' => null],
  113. ['id' => Router::ID]
  114. );
  115. $route->compile();
  116. $result = $route->parse('/my-posts/my-view/1');
  117. $this->assertEquals('MyPosts', $result['controller']);
  118. $this->assertEquals('myView', $result['action']);
  119. $this->assertEquals('1', $result['id']);
  120. $route = new DashedRoute(
  121. '/admin/:controller',
  122. ['prefix' => 'admin', 'admin' => 1, 'action' => 'index']
  123. );
  124. $route->compile();
  125. $result = $route->parse('/admin/');
  126. $this->assertFalse($result);
  127. $result = $route->parse('/admin/my-posts');
  128. $this->assertEquals('MyPosts', $result['controller']);
  129. $this->assertEquals('index', $result['action']);
  130. $route = new DashedRoute(
  131. '/media/search/*',
  132. ['controller' => 'Media', 'action' => 'searchIt']
  133. );
  134. $result = $route->parse('/media/search');
  135. $this->assertEquals('Media', $result['controller']);
  136. $this->assertEquals('searchIt', $result['action']);
  137. $this->assertEquals([], $result['pass']);
  138. $result = $route->parse('/media/search/tv_shows');
  139. $this->assertEquals('Media', $result['controller']);
  140. $this->assertEquals('searchIt', $result['action']);
  141. $this->assertEquals(['tv_shows'], $result['pass']);
  142. }
  143. }