RoutesCommandTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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\Command\Command;
  18. use Cake\Routing\Router;
  19. use Cake\TestSuite\ConsoleIntegrationTestTrait;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * RoutesCommandTest
  23. */
  24. class RoutesCommandTest extends TestCase
  25. {
  26. use ConsoleIntegrationTestTrait;
  27. /**
  28. * setUp method
  29. *
  30. * @return void
  31. */
  32. public function setUp(): void
  33. {
  34. parent::setUp();
  35. $this->setAppNamespace();
  36. $this->useCommandRunner();
  37. }
  38. /**
  39. * tearDown
  40. *
  41. * @return void
  42. */
  43. public function tearDown(): void
  44. {
  45. parent::tearDown();
  46. Router::reload();
  47. }
  48. /**
  49. * Ensure help for `routes` works
  50. *
  51. * @return void
  52. */
  53. public function testRouteListHelp()
  54. {
  55. $this->exec('routes -h');
  56. $this->assertExitCode(Command::CODE_SUCCESS);
  57. $this->assertOutputContains('list of routes');
  58. $this->assertErrorEmpty();
  59. }
  60. /**
  61. * Test checking an non-existing route.
  62. *
  63. * @return void
  64. */
  65. public function testRouteList()
  66. {
  67. $this->exec('routes');
  68. $this->assertExitCode(Command::CODE_SUCCESS);
  69. $this->assertOutputContainsRow([
  70. '<info>Route name</info>',
  71. '<info>URI template</info>',
  72. '<info>Defaults</info>',
  73. ]);
  74. $this->assertOutputContainsRow([
  75. 'articles:_action',
  76. '/app/articles/:action/*',
  77. '{"action":"index","controller":"Articles","plugin":null}',
  78. ]);
  79. $this->assertOutputContainsRow([
  80. 'bake._controller:_action',
  81. '/bake/:controller/:action',
  82. '{"action":"index","plugin":"Bake"}',
  83. ]);
  84. $this->assertOutputContainsRow([
  85. 'testName',
  86. '/app/tests/:action/*',
  87. '{"action":"index","controller":"Tests","plugin":null}',
  88. ]);
  89. }
  90. /**
  91. * Ensure help for `routes` works
  92. *
  93. * @return void
  94. */
  95. public function testCheckHelp()
  96. {
  97. $this->exec('routes check -h');
  98. $this->assertExitCode(Command::CODE_SUCCESS);
  99. $this->assertOutputContains('Check a URL');
  100. $this->assertErrorEmpty();
  101. }
  102. /**
  103. * Ensure routes check with no input
  104. *
  105. * @return void
  106. */
  107. public function testCheckNoInput()
  108. {
  109. $this->exec('routes check');
  110. $this->assertExitCode(Command::CODE_ERROR);
  111. $this->assertErrorContains('`url` argument is required');
  112. }
  113. /**
  114. * Test checking an existing route.
  115. *
  116. * @return void
  117. */
  118. public function testCheck()
  119. {
  120. $this->exec('routes check /app/articles/check');
  121. $this->assertExitCode(Command::CODE_SUCCESS);
  122. $this->assertOutputContainsRow([
  123. '<info>Route name</info>',
  124. '<info>URI template</info>',
  125. '<info>Defaults</info>',
  126. ]);
  127. $this->assertOutputContainsRow([
  128. 'articles:_action',
  129. '/app/articles/check',
  130. '{"action":"check","controller":"Articles","pass":[],"plugin":null}',
  131. ]);
  132. }
  133. /**
  134. * Test checking an existing route with named route.
  135. *
  136. * @return void
  137. */
  138. public function testCheckWithNamedRoute()
  139. {
  140. $this->exec('routes check /app/tests/index');
  141. $this->assertExitCode(Command::CODE_SUCCESS);
  142. $this->assertOutputContainsRow([
  143. '<info>Route name</info>',
  144. '<info>URI template</info>',
  145. '<info>Defaults</info>',
  146. ]);
  147. $this->assertOutputContainsRow([
  148. 'testName',
  149. '/app/tests/index',
  150. '{"_name":"testName","action":"index","controller":"Tests","pass":[],"plugin":null}',
  151. ]);
  152. }
  153. /**
  154. * Test checking an non-existing route.
  155. *
  156. * @return void
  157. */
  158. public function testCheckNotFound()
  159. {
  160. $this->exec('routes check /nope');
  161. $this->assertExitCode(Command::CODE_ERROR);
  162. $this->assertErrorContains('did not match');
  163. }
  164. /**
  165. * Ensure help for `routes` works
  166. *
  167. * @return void
  168. */
  169. public function testGenerareHelp()
  170. {
  171. $this->exec('routes generate -h');
  172. $this->assertExitCode(Command::CODE_SUCCESS);
  173. $this->assertOutputContains('Check a routing array');
  174. $this->assertErrorEmpty();
  175. }
  176. /**
  177. * Test generating URLs
  178. *
  179. * @return void
  180. */
  181. public function testGenerateNoPassArgs()
  182. {
  183. $this->exec('routes generate controller:Articles action:index');
  184. $this->assertExitCode(Command::CODE_SUCCESS);
  185. $this->assertOutputContains('> /app/articles');
  186. $this->assertErrorEmpty();
  187. }
  188. /**
  189. * Test generating URLs with passed arguments
  190. *
  191. * @return void
  192. */
  193. public function testGeneratePassedArguments()
  194. {
  195. $this->exec('routes generate controller:Articles action:view 2 3');
  196. $this->assertExitCode(Command::CODE_SUCCESS);
  197. $this->assertOutputContains('> /app/articles/view/2/3');
  198. $this->assertErrorEmpty();
  199. }
  200. /**
  201. * Test generating URLs with bool params
  202. *
  203. * @return void
  204. */
  205. public function testGenerateBoolParams()
  206. {
  207. $this->exec('routes generate controller:Articles action:index _ssl:true _host:example.com');
  208. $this->assertExitCode(Command::CODE_SUCCESS);
  209. $this->assertOutputContains('> https://example.com/app/articles');
  210. }
  211. /**
  212. * Test generating URLs
  213. *
  214. * @return void
  215. */
  216. public function testGenerateMissing()
  217. {
  218. $this->exec('routes generate plugin:Derp controller:Derp');
  219. $this->assertExitCode(Command::CODE_ERROR);
  220. $this->assertErrorContains('do not match');
  221. }
  222. }