RoutesShellTest.php 5.4 KB

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