RoutesShellTest.php 6.0 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\Routing\Router;
  17. use Cake\Shell\RoutesShell;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * RoutesShellTest
  21. */
  22. class RoutesShellTest extends TestCase
  23. {
  24. /**
  25. * setUp method
  26. *
  27. * @return void
  28. */
  29. public function setUp()
  30. {
  31. parent::setUp();
  32. $this->io = $this->getMockBuilder('Cake\Console\ConsoleIo')
  33. ->setMethods(['helper', 'out', 'err'])
  34. ->getMock();
  35. $this->table = $this->getMockBuilder('Cake\Shell\Helper\TableHelper')
  36. ->setConstructorArgs([$this->io])
  37. ->getMock();
  38. $this->io->expects($this->any())
  39. ->method('helper')
  40. ->with('table')
  41. ->will($this->returnValue($this->table));
  42. $this->shell = new RoutesShell($this->io);
  43. Router::connect('/articles/:action/*', ['controller' => 'Articles']);
  44. Router::connect('/bake/:controller/:action', ['plugin' => 'Bake']);
  45. Router::connect('/tests/:action/*', ['controller' => 'Tests'], ['_name' => 'testName']);
  46. }
  47. /**
  48. * tearDown
  49. *
  50. * @return void
  51. */
  52. public function tearDown()
  53. {
  54. parent::tearDown();
  55. Router::reload();
  56. unset($this->io, $this->shell);
  57. }
  58. /**
  59. * Test checking an non-existing route.
  60. *
  61. * @return void
  62. */
  63. public function testMain()
  64. {
  65. $this->table->expects($this->once())
  66. ->method('output')
  67. ->with(
  68. $this->logicalAnd(
  69. $this->contains(['Route name', 'URI template', 'Defaults']),
  70. $this->contains([
  71. 'articles:_action',
  72. '/articles/:action/*',
  73. '{"controller":"Articles","action":"index","plugin":null}'
  74. ]),
  75. $this->contains([
  76. 'bake._controller:_action',
  77. '/bake/:controller/:action',
  78. '{"plugin":"Bake","action":"index"}',
  79. ]),
  80. $this->contains([
  81. 'testName',
  82. '/tests/:action/*',
  83. '{"controller":"Tests","action":"index","plugin":null}'
  84. ])
  85. )
  86. );
  87. $this->shell->main();
  88. }
  89. /**
  90. * Test checking an existing route.
  91. *
  92. * @return void
  93. */
  94. public function testCheck()
  95. {
  96. $this->table->expects($this->once())
  97. ->method('output')
  98. ->with(
  99. $this->logicalAnd(
  100. $this->contains(['Route name', 'URI template', 'Defaults']),
  101. $this->contains([
  102. 'articles:_action',
  103. '/articles/index',
  104. '{"action":"index","pass":[],"controller":"Articles","plugin":null}'
  105. ])
  106. )
  107. );
  108. $this->shell->check('/articles/index');
  109. }
  110. /**
  111. * Test checking an existing route with named route.
  112. *
  113. * @return void
  114. */
  115. public function testCheckWithNamedRoute()
  116. {
  117. $this->table->expects($this->once())
  118. ->method('output')
  119. ->with(
  120. $this->logicalAnd(
  121. $this->contains(['Route name', 'URI template', 'Defaults']),
  122. $this->contains([
  123. 'testName',
  124. '/tests/index',
  125. '{"action":"index","pass":[],"controller":"Tests","plugin":null}'
  126. ])
  127. )
  128. );
  129. $this->shell->check('/tests/index');
  130. }
  131. /**
  132. * Test checking an non-existing route.
  133. *
  134. * @return void
  135. */
  136. public function testCheckNotFound()
  137. {
  138. $this->io->expects($this->at(0))
  139. ->method('err')
  140. ->with($this->stringContains('did not match'));
  141. $this->shell->check('/nope');
  142. }
  143. /**
  144. * Test generating URLs
  145. *
  146. * @return void
  147. */
  148. public function testGenerate()
  149. {
  150. $this->io->expects($this->never())
  151. ->method('err');
  152. $this->io->expects($this->at(0))
  153. ->method('out')
  154. ->with($this->stringContains('> /articles/index'));
  155. $this->io->expects($this->at(2))
  156. ->method('out')
  157. ->with($this->stringContains('> /articles/view/2/3'));
  158. $this->shell->args = ['controller:Articles', 'action:index'];
  159. $this->shell->generate();
  160. $this->shell->args = ['controller:Articles', 'action:view', '2', '3'];
  161. $this->shell->generate();
  162. }
  163. /**
  164. * Test generating URLs with bool params
  165. *
  166. * @return void
  167. */
  168. public function testGenerateBoolParams()
  169. {
  170. $this->io->expects($this->never())
  171. ->method('err');
  172. $this->io->expects($this->at(0))
  173. ->method('out')
  174. ->with($this->stringContains('> https://example.com/articles/index'));
  175. $this->shell->args = ['_ssl:true', '_host:example.com', 'controller:Articles', 'action:index'];
  176. $this->shell->generate();
  177. }
  178. /**
  179. * Test generating URLs
  180. *
  181. * @return void
  182. */
  183. public function testGenerateMissing()
  184. {
  185. $this->io->expects($this->at(0))
  186. ->method('err')
  187. ->with($this->stringContains('do not match'));
  188. $this->shell->args = ['controller:Derp'];
  189. $this->shell->generate();
  190. }
  191. }