RoutesShellTest.php 5.5 KB

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