RoutesCommandTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP Project
  13. * @since 3.1.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Command;
  17. use Cake\Console\Command;
  18. use Cake\Routing\Router;
  19. use Cake\TestSuite\ConsoleIntegrationTestCase;
  20. /**
  21. * RoutesCommandTest
  22. */
  23. class RoutesCommandTest extends ConsoleIntegrationTestCase
  24. {
  25. /**
  26. * setUp method
  27. *
  28. * @return void
  29. */
  30. public function setUp(): void
  31. {
  32. parent::setUp();
  33. $this->setAppNamespace();
  34. $this->useCommandRunner();
  35. }
  36. /**
  37. * tearDown
  38. *
  39. * @return void
  40. */
  41. public function tearDown(): void
  42. {
  43. parent::tearDown();
  44. Router::reload();
  45. }
  46. /**
  47. * Ensure help for `routes` works
  48. *
  49. * @return void
  50. */
  51. public function testRouteListHelp()
  52. {
  53. $this->exec('routes -h');
  54. $this->assertExitCode(Command::CODE_SUCCESS);
  55. $this->assertOutputContains('list of routes');
  56. $this->assertErrorEmpty();
  57. }
  58. /**
  59. * Test checking an non-existing route.
  60. *
  61. * @return void
  62. */
  63. public function testRouteList()
  64. {
  65. $this->exec('routes');
  66. $this->assertExitCode(Command::CODE_SUCCESS);
  67. $this->assertOutputContainsRow([
  68. '<info>Route name</info>',
  69. '<info>URI template</info>',
  70. '<info>Defaults</info>',
  71. ]);
  72. $this->assertOutputContainsRow([
  73. 'articles:_action',
  74. '/app/articles/:action/*',
  75. '{"action":"index","controller":"Articles","plugin":null}',
  76. ]);
  77. $this->assertOutputContainsRow([
  78. 'bake._controller:_action',
  79. '/bake/:controller/:action',
  80. '{"action":"index","plugin":"Bake"}',
  81. ]);
  82. $this->assertOutputContainsRow([
  83. 'testName',
  84. '/app/tests/:action/*',
  85. '{"action":"index","controller":"Tests","plugin":null}',
  86. ]);
  87. }
  88. /**
  89. * Ensure help for `routes` works
  90. *
  91. * @return void
  92. */
  93. public function testCheckHelp()
  94. {
  95. $this->exec('routes check -h');
  96. $this->assertExitCode(Command::CODE_SUCCESS);
  97. $this->assertOutputContains('Check a URL');
  98. $this->assertErrorEmpty();
  99. }
  100. /**
  101. * Test checking an existing route.
  102. *
  103. * @return void
  104. */
  105. public function testCheck()
  106. {
  107. $this->exec('routes check /app/articles/check');
  108. $this->assertExitCode(Command::CODE_SUCCESS);
  109. $this->assertOutputContainsRow([
  110. '<info>Route name</info>',
  111. '<info>URI template</info>',
  112. '<info>Defaults</info>',
  113. ]);
  114. $this->assertOutputContainsRow([
  115. 'articles:_action',
  116. '/app/articles/check',
  117. '{"action":"check","controller":"Articles","pass":[],"plugin":null}',
  118. ]);
  119. }
  120. /**
  121. * Test checking an existing route with named route.
  122. *
  123. * @return void
  124. */
  125. public function testCheckWithNamedRoute()
  126. {
  127. $this->exec('routes check /app/tests/index');
  128. $this->assertExitCode(Command::CODE_SUCCESS);
  129. $this->assertOutputContainsRow([
  130. '<info>Route name</info>',
  131. '<info>URI template</info>',
  132. '<info>Defaults</info>',
  133. ]);
  134. $this->assertOutputContainsRow([
  135. 'testName',
  136. '/app/tests/index',
  137. '{"_name":"testName","action":"index","controller":"Tests","pass":[],"plugin":null}',
  138. ]);
  139. }
  140. /**
  141. * Test checking an non-existing route.
  142. *
  143. * @return void
  144. */
  145. public function testCheckNotFound()
  146. {
  147. $this->exec('routes check /nope');
  148. $this->assertExitCode(Command::CODE_ERROR);
  149. $this->assertErrorContains('did not match');
  150. }
  151. /**
  152. * Ensure help for `routes` works
  153. *
  154. * @return void
  155. */
  156. public function testGenerareHelp()
  157. {
  158. $this->exec('routes generate -h');
  159. $this->assertExitCode(Command::CODE_SUCCESS);
  160. $this->assertOutputContains('Check a routing array');
  161. $this->assertErrorEmpty();
  162. }
  163. /**
  164. * Test generating URLs
  165. *
  166. * @return void
  167. */
  168. public function testGenerateNoPassArgs()
  169. {
  170. $this->exec('routes generate controller:Articles action:index');
  171. $this->assertExitCode(Command::CODE_SUCCESS);
  172. $this->assertOutputContains('> /app/articles');
  173. $this->assertErrorEmpty();
  174. }
  175. /**
  176. * Test generating URLs with passed arguments
  177. *
  178. * @return void
  179. */
  180. public function testGeneratePassedArguments()
  181. {
  182. $this->exec('routes generate controller:Articles action:view 2 3');
  183. $this->assertExitCode(Command::CODE_SUCCESS);
  184. $this->assertOutputContains('> /app/articles/view/2/3');
  185. $this->assertErrorEmpty();
  186. }
  187. /**
  188. * Test generating URLs with bool params
  189. *
  190. * @return void
  191. */
  192. public function testGenerateBoolParams()
  193. {
  194. $this->exec('routes generate controller:Articles action:index _ssl:true _host:example.com');
  195. $this->assertExitCode(Command::CODE_SUCCESS);
  196. $this->assertOutputContains('> https://example.com/app/articles');
  197. }
  198. /**
  199. * Test generating URLs
  200. *
  201. * @return void
  202. */
  203. public function testGenerateMissing()
  204. {
  205. $this->exec('routes generate plugin:Derp controller:Derp');
  206. $this->assertExitCode(Command::CODE_ERROR);
  207. $this->assertErrorContains('do not match');
  208. }
  209. }