RoutesShellTest.php 4.0 KB

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