RoutesShellTest.php 5.1 KB

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