ConsoleIntegrationTestCaseTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * CakePHP(tm) : 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. * @since 3.5.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\TestSuite;
  15. use Cake\Console\Shell;
  16. use Cake\Core\Configure;
  17. use Cake\TestSuite\ConsoleIntegrationTestCase;
  18. class ConsoleIntegrationTestCaseTest extends ConsoleIntegrationTestCase
  19. {
  20. /**
  21. * setUp
  22. *
  23. * @return void
  24. */
  25. public function setUp()
  26. {
  27. parent::setUp();
  28. Configure::write('App.namespace', 'TestApp');
  29. }
  30. /**
  31. * tests exec when using the command runner
  32. *
  33. * @return void
  34. */
  35. public function testExecWithCommandRunner()
  36. {
  37. $this->useCommandRunner();
  38. $this->exec('routes');
  39. $this->assertExitCode(Shell::CODE_SUCCESS);
  40. }
  41. /**
  42. * tests exec
  43. *
  44. * @return void
  45. */
  46. public function testExec()
  47. {
  48. $this->exec('');
  49. $this->assertOutputContains('Welcome to CakePHP');
  50. $this->assertExitCode(Shell::CODE_ERROR);
  51. }
  52. /**
  53. * tests that exec catches a StopException
  54. *
  55. * @return void
  56. */
  57. public function testExecShellWithStopException()
  58. {
  59. $this->exec('integration abort_shell');
  60. $this->assertExitCode(Shell::CODE_ERROR);
  61. $this->assertErrorContains('Shell aborted');
  62. }
  63. /**
  64. * tests that exec catches a StopException
  65. *
  66. * @return void
  67. */
  68. public function testExecCommandWithStopException()
  69. {
  70. $this->useCommandRunner();
  71. $this->exec('abort_command');
  72. $this->assertExitCode(Shell::CODE_ERROR);
  73. $this->assertErrorContains('Command aborted');
  74. }
  75. /**
  76. * tests a valid core command
  77. *
  78. * @return void
  79. */
  80. public function testExecCoreCommand()
  81. {
  82. $this->exec('routes');
  83. $this->assertExitCode(Shell::CODE_SUCCESS);
  84. }
  85. /**
  86. * tests exec with an arg and an option
  87. *
  88. * @return void
  89. */
  90. public function testExecWithArgsAndOption()
  91. {
  92. $this->exec('integration args_and_options arg --opt="some string"');
  93. $this->assertErrorEmpty();
  94. $this->assertOutputContains('arg: arg');
  95. $this->assertOutputContains('opt: some string');
  96. $this->assertExitCode(Shell::CODE_SUCCESS);
  97. }
  98. /**
  99. * tests exec with missing required argument
  100. *
  101. * @return void
  102. */
  103. public function testExecWithMissingRequiredArg()
  104. {
  105. $this->exec('integration args_and_options');
  106. $this->assertOutputEmpty();
  107. $this->assertErrorContains('Missing required arguments');
  108. $this->assertErrorContains('arg is required');
  109. $this->assertExitCode(Shell::CODE_ERROR);
  110. }
  111. /**
  112. * tests exec with input
  113. *
  114. * @return void
  115. */
  116. public function testExecWithInput()
  117. {
  118. $this->exec('integration bridge', ['javascript']);
  119. $this->assertErrorContains('No!');
  120. $this->assertExitCode(Shell::CODE_ERROR);
  121. }
  122. /**
  123. * tests exec with multiple inputs
  124. *
  125. * @return void
  126. */
  127. public function testExecWithMultipleInput()
  128. {
  129. $this->exec('integration bridge', ['cake', 'blue']);
  130. $this->assertOutputContains('You may pass');
  131. $this->assertExitCode(Shell::CODE_SUCCESS);
  132. }
  133. /**
  134. * tests assertOutputRegExp assertion
  135. *
  136. * @return void
  137. */
  138. public function testAssertOutputRegExp()
  139. {
  140. $this->exec('routes');
  141. $this->assertOutputRegExp('/^\+[\-\+]+\+$/m');
  142. }
  143. /**
  144. * tests assertErrorRegExp assertion
  145. *
  146. * @return void
  147. */
  148. public function testAssertErrorRegExp()
  149. {
  150. $this->exec('integration args_and_options');
  151. $this->assertErrorRegExp('/\<error\>(.+)\<\/error\>/');
  152. }
  153. /**
  154. * tests _commandStringToArgs
  155. *
  156. * @return void
  157. */
  158. public function testCommandStringToArgs()
  159. {
  160. $result = $this->_commandStringToArgs('command --something=nothing --with-spaces="quote me on that" \'quoted \"arg\"\'');
  161. $expected = [
  162. 'command',
  163. '--something=nothing',
  164. '--with-spaces=quote me on that',
  165. 'quoted \"arg\"',
  166. ];
  167. $this->assertSame($expected, $result);
  168. $json = json_encode(['key' => '"val"', 'this' => true]);
  169. $result = $this->_commandStringToArgs(" --json='$json'");
  170. $expected = [
  171. '--json=' . $json
  172. ];
  173. $this->assertSame($expected, $result);
  174. }
  175. }