RoutesShellTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * CakePHP : 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 Project
  12. * @since 3.1.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Shell;
  16. use Cake\Console\Shell;
  17. use Cake\Routing\Router;
  18. use Cake\TestSuite\ConsoleIntegrationTestCase;
  19. /**
  20. * RoutesShellTest
  21. */
  22. class RoutesShellTest extends ConsoleIntegrationTestCase
  23. {
  24. /**
  25. * setUp method
  26. *
  27. * @return void
  28. */
  29. public function setUp()
  30. {
  31. parent::setUp();
  32. Router::connect('/articles/:action/*', ['controller' => 'Articles']);
  33. Router::connect('/bake/:controller/:action', ['plugin' => 'Bake']);
  34. Router::connect('/tests/:action/*', ['controller' => 'Tests'], ['_name' => 'testName']);
  35. Router::scope('/', function ($routes) {
  36. $routes->redirect('/redirect', 'http://example.com/test.html');
  37. });
  38. }
  39. /**
  40. * tearDown
  41. *
  42. * @return void
  43. */
  44. public function tearDown()
  45. {
  46. parent::tearDown();
  47. Router::reload();
  48. }
  49. /**
  50. * Test checking an non-existing route.
  51. *
  52. * @return void
  53. */
  54. public function testMain()
  55. {
  56. $this->exec('routes');
  57. $this->assertExitCode(Shell::CODE_SUCCESS);
  58. $this->assertOutputContainsRow([
  59. '<info>Route name</info>',
  60. '<info>URI template</info>',
  61. '<info>Defaults</info>',
  62. ]);
  63. $this->assertOutputContainsRow([
  64. 'articles:_action',
  65. '/articles/:action/*',
  66. '{"action":"index","controller":"Articles","plugin":null}',
  67. ]);
  68. $this->assertOutputContainsRow([
  69. 'bake._controller:_action',
  70. '/bake/:controller/:action',
  71. '{"action":"index","plugin":"Bake"}',
  72. ]);
  73. $this->assertOutputContainsRow([
  74. 'testName',
  75. '/tests/:action/*',
  76. '{"action":"index","controller":"Tests","plugin":null}',
  77. ]);
  78. }
  79. /**
  80. * Test checking an existing route.
  81. *
  82. * @return void
  83. */
  84. public function testCheck()
  85. {
  86. $this->exec('routes check /articles/check');
  87. $this->assertExitCode(Shell::CODE_SUCCESS);
  88. $this->assertOutputContainsRow([
  89. '<info>Route name</info>',
  90. '<info>URI template</info>',
  91. '<info>Defaults</info>',
  92. ]);
  93. $this->assertOutputContainsRow([
  94. 'articles:_action',
  95. '/articles/check',
  96. '{"action":"check","controller":"Articles","pass":[],"plugin":null}',
  97. ]);
  98. }
  99. /**
  100. * Test checking an existing route with named route.
  101. *
  102. * @return void
  103. */
  104. public function testCheckWithNamedRoute()
  105. {
  106. $this->exec('routes check /tests/index');
  107. $this->assertExitCode(Shell::CODE_SUCCESS);
  108. $this->assertOutputContainsRow([
  109. '<info>Route name</info>',
  110. '<info>URI template</info>',
  111. '<info>Defaults</info>',
  112. ]);
  113. $this->assertOutputContainsRow([
  114. 'testName',
  115. '/tests/index',
  116. '{"_name":"testName","action":"index","controller":"Tests","pass":[],"plugin":null}',
  117. ]);
  118. }
  119. /**
  120. * Test checking an non-existing route.
  121. *
  122. * @return void
  123. */
  124. public function testCheckNotFound()
  125. {
  126. $this->exec('routes check /nope');
  127. $this->assertExitCode(Shell::CODE_ERROR);
  128. $this->assertErrorContains('did not match');
  129. }
  130. /**
  131. * Test checking an existing route with redirect route.
  132. *
  133. * @return void
  134. */
  135. public function testCheckWithRedirectRoute()
  136. {
  137. $this->exec('routes check /redirect');
  138. $this->assertExitCode(Shell::CODE_SUCCESS);
  139. $this->assertOutputContainsRow([
  140. '<info>URI template</info>',
  141. '<info>Redirect</info>',
  142. ]);
  143. $this->assertOutputContainsRow([
  144. '/redirect',
  145. 'http://example.com/test.html',
  146. ]);
  147. }
  148. /**
  149. * Test generating URLs
  150. *
  151. * @return void
  152. */
  153. public function testGenerateNoPassArgs()
  154. {
  155. $this->exec('routes generate controller:Articles action:index');
  156. $this->assertExitCode(Shell::CODE_SUCCESS);
  157. $this->assertOutputContains('> /articles/index');
  158. $this->assertErrorEmpty();
  159. }
  160. /**
  161. * Test generating URLs with passed arguments
  162. *
  163. * @return void
  164. */
  165. public function testGeneratePassedArguments()
  166. {
  167. $this->exec('routes generate controller:Articles action:view 2 3');
  168. $this->assertExitCode(Shell::CODE_SUCCESS);
  169. $this->assertOutputContains('> /articles/view/2/3');
  170. $this->assertErrorEmpty();
  171. }
  172. /**
  173. * Test generating URLs with bool params
  174. *
  175. * @return void
  176. */
  177. public function testGenerateBoolParams()
  178. {
  179. $this->exec('routes generate controller:Articles action:index _ssl:true _host:example.com');
  180. $this->assertExitCode(Shell::CODE_SUCCESS);
  181. $this->assertOutputContains('> https://example.com/articles/index');
  182. }
  183. /**
  184. * Test generating URLs
  185. *
  186. * @return void
  187. */
  188. public function testGenerateMissing()
  189. {
  190. $this->exec('routes generate controller:Derp');
  191. $this->assertExitCode(Shell::CODE_ERROR);
  192. $this->assertErrorContains('do not match');
  193. }
  194. }