RoutesShellTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. }
  45. /**
  46. * tearDown
  47. *
  48. * @return void
  49. */
  50. public function tearDown()
  51. {
  52. parent::tearDown();
  53. Router::reload();
  54. unset($this->io, $this->shell);
  55. }
  56. /**
  57. * Test checking an non-existing route.
  58. *
  59. * @return void
  60. */
  61. public function testMain()
  62. {
  63. $this->table->expects($this->once())
  64. ->method('output')
  65. ->with(
  66. $this->logicalAnd(
  67. $this->contains(['Route name', 'URI template', 'Defaults']),
  68. $this->contains([
  69. 'articles:_action',
  70. '/articles/:action/*',
  71. '{"controller":"Articles","action":"index","plugin":null}'
  72. ])
  73. )
  74. );
  75. $this->shell->main();
  76. }
  77. /**
  78. * Test checking an existing route.
  79. *
  80. * @return void
  81. */
  82. public function testCheck()
  83. {
  84. $this->table->expects($this->once())
  85. ->method('output')
  86. ->with(
  87. $this->logicalAnd(
  88. $this->contains(['Route name', 'URI template', 'Defaults']),
  89. $this->contains([
  90. '',
  91. '/articles/index',
  92. '{"action":"index","pass":[],"controller":"Articles","plugin":null}'
  93. ])
  94. )
  95. );
  96. $this->shell->check('/articles/index');
  97. }
  98. /**
  99. * Test checking an non-existing route.
  100. *
  101. * @return void
  102. */
  103. public function testCheckNotFound()
  104. {
  105. $this->io->expects($this->at(0))
  106. ->method('err')
  107. ->with($this->stringContains('did not match'));
  108. $this->shell->check('/nope');
  109. }
  110. /**
  111. * Test generating URLs
  112. *
  113. * @return void
  114. */
  115. public function testGenerate()
  116. {
  117. $this->io->expects($this->never())
  118. ->method('err');
  119. $this->io->expects($this->at(0))
  120. ->method('out')
  121. ->with($this->stringContains('> /articles/index'));
  122. $this->io->expects($this->at(1))
  123. ->method('out')
  124. ->with($this->stringContains('> /articles/view/2/3'));
  125. $this->shell->args = ['controller:Articles', 'action:index'];
  126. $this->shell->generate();
  127. $this->shell->args = ['controller:Articles', 'action:view', '2', '3'];
  128. $this->shell->generate();
  129. }
  130. /**
  131. * Test generating URLs
  132. *
  133. * @return void
  134. */
  135. public function testGenerateMissing()
  136. {
  137. $this->io->expects($this->at(0))
  138. ->method('err')
  139. ->with($this->stringContains('do not match'));
  140. $this->shell->args = ['controller:Derp'];
  141. $this->shell->generate();
  142. }
  143. }