RoutesShellTest.php 5.1 KB

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