RoutesShellTest.php 3.8 KB

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