RoutesShellTest.php 4.0 KB

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