RoutesShellTest.php 5.4 KB

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