ConsoleIntegrationTestTraitTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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\Exception\ConsoleException;
  16. use Cake\Console\Shell;
  17. use Cake\Core\Configure;
  18. use Cake\TestSuite\ConsoleIntegrationTestCase;
  19. use Cake\TestSuite\Stub\MissingConsoleInputException;
  20. use PHPUnit\Framework\AssertionFailedError;
  21. class ConsoleIntegrationTestTraitTest extends ConsoleIntegrationTestCase
  22. {
  23. /**
  24. * setUp
  25. *
  26. * @return void
  27. */
  28. public function setUp()
  29. {
  30. parent::setUp();
  31. Configure::write('App.namespace', 'TestApp');
  32. }
  33. /**
  34. * tests exec when using the command runner
  35. *
  36. * @return void
  37. */
  38. public function testExecWithCommandRunner()
  39. {
  40. $this->useCommandRunner();
  41. $this->exec('routes');
  42. $this->assertExitCode(Shell::CODE_SUCCESS);
  43. $this->assertExitSuccess();
  44. }
  45. /**
  46. * tests exec
  47. *
  48. * @return void
  49. */
  50. public function testExec()
  51. {
  52. $this->exec('');
  53. $this->assertOutputContains('Welcome to CakePHP');
  54. $this->assertExitCode(Shell::CODE_ERROR);
  55. $this->assertExitError();
  56. }
  57. /**
  58. * tests that exec catches a StopException
  59. *
  60. * @return void
  61. */
  62. public function testExecShellWithStopException()
  63. {
  64. $this->exec('integration abort_shell');
  65. $this->assertExitCode(Shell::CODE_ERROR);
  66. $this->assertErrorContains('Shell aborted');
  67. }
  68. /**
  69. * tests that exec catches a StopException
  70. *
  71. * @return void
  72. */
  73. public function testExecCommandWithStopException()
  74. {
  75. $this->useCommandRunner();
  76. $this->exec('abort_command');
  77. $this->assertExitCode(127);
  78. $this->assertErrorContains('Command aborted');
  79. }
  80. /**
  81. * tests a valid core command
  82. *
  83. * @return void
  84. */
  85. public function testExecCoreCommand()
  86. {
  87. $this->exec('routes');
  88. $this->assertExitCode(Shell::CODE_SUCCESS);
  89. }
  90. /**
  91. * tests exec with an arg and an option
  92. *
  93. * @return void
  94. */
  95. public function testExecWithArgsAndOption()
  96. {
  97. $this->exec('integration args_and_options arg --opt="some string"');
  98. $this->assertErrorEmpty();
  99. $this->assertOutputContains('arg: arg');
  100. $this->assertOutputContains('opt: some string');
  101. $this->assertExitCode(Shell::CODE_SUCCESS);
  102. }
  103. /**
  104. * tests exec with missing required argument
  105. *
  106. * @return void
  107. */
  108. public function testExecWithMissingRequiredArg()
  109. {
  110. $this->exec('integration args_and_options');
  111. $this->assertOutputEmpty();
  112. $this->assertErrorContains('Missing required arguments');
  113. $this->assertErrorContains('arg is required');
  114. $this->assertExitCode(Shell::CODE_ERROR);
  115. }
  116. /**
  117. * tests exec with input
  118. *
  119. * @return void
  120. */
  121. public function testExecWithInput()
  122. {
  123. $this->exec('integration bridge', ['javascript']);
  124. $this->assertErrorContains('No!');
  125. $this->assertExitCode(Shell::CODE_ERROR);
  126. }
  127. /**
  128. * tests exec with fewer inputs than questions
  129. *
  130. * @return void
  131. */
  132. public function testExecWithMissingInput()
  133. {
  134. $this->expectException(MissingConsoleInputException::class);
  135. $this->expectExceptionMessage('no more input');
  136. $this->exec('integration bridge', ['cake']);
  137. }
  138. /**
  139. * tests exec with multiple inputs
  140. *
  141. * @return void
  142. */
  143. public function testExecWithMultipleInput()
  144. {
  145. $this->exec('integration bridge', ['cake', 'blue']);
  146. $this->assertOutputContains('You may pass');
  147. $this->assertExitCode(Shell::CODE_SUCCESS);
  148. }
  149. /**
  150. * tests assertOutputRegExp assertion
  151. *
  152. * @return void
  153. */
  154. public function testAssertOutputRegExp()
  155. {
  156. $this->exec('routes');
  157. $this->assertOutputRegExp('/^\+[\-\+]+\+$/m');
  158. }
  159. /**
  160. * tests assertErrorRegExp assertion
  161. *
  162. * @return void
  163. */
  164. public function testAssertErrorRegExp()
  165. {
  166. $this->exec('integration args_and_options');
  167. $this->assertErrorRegExp('/\<error\>(.+)\<\/error\>/');
  168. }
  169. /**
  170. * tests commandStringToArgs
  171. *
  172. * @return void
  173. */
  174. public function testCommandStringToArgs()
  175. {
  176. $result = $this->commandStringToArgs('command --something=nothing --with-spaces="quote me on that" \'quoted \"arg\"\'');
  177. $expected = [
  178. 'command',
  179. '--something=nothing',
  180. '--with-spaces=quote me on that',
  181. 'quoted \"arg\"',
  182. ];
  183. $this->assertSame($expected, $result);
  184. $json = json_encode(['key' => '"val"', 'this' => true]);
  185. $result = $this->commandStringToArgs(" --json='$json'");
  186. $expected = [
  187. '--json=' . $json,
  188. ];
  189. $this->assertSame($expected, $result);
  190. }
  191. /**
  192. * tests failure messages for assertions
  193. *
  194. * @param string $assertion Assertion method
  195. * @param string $message Expected failure message
  196. * @param string $command Command to test
  197. * @param mixed ...$rest
  198. * @dataProvider assertionFailureMessagesProvider
  199. */
  200. public function testAssertionFailureMessages($assertion, $message, $command, ...$rest)
  201. {
  202. $this->expectException(AssertionFailedError::class);
  203. $this->expectExceptionMessage($message);
  204. $this->exec($command);
  205. call_user_func_array([$this, $assertion], $rest);
  206. }
  207. /**
  208. * data provider for assertion failure messages
  209. *
  210. * @return array
  211. */
  212. public function assertionFailureMessagesProvider()
  213. {
  214. return [
  215. 'assertExitCode' => ['assertExitCode', 'Failed asserting that 1 matches exit code 0.', 'routes', Shell::CODE_ERROR],
  216. 'assertOutputEmpty' => ['assertOutputEmpty', 'Failed asserting that output is empty.', 'routes'],
  217. 'assertOutputContains' => ['assertOutputContains', 'Failed asserting that \'missing\' is in output.', 'routes', 'missing'],
  218. 'assertOutputNotContains' => ['assertOutputNotContains', 'Failed asserting that \'controller\' is not in output.', 'routes', 'controller'],
  219. 'assertOutputRegExp' => ['assertOutputRegExp', 'Failed asserting that `/missing/` PCRE pattern found in output.', 'routes', '/missing/'],
  220. 'assertOutputContainsRow' => ['assertOutputContainsRow', 'Failed asserting that `Array (...)` row was in output.', 'routes', ['test', 'missing']],
  221. 'assertErrorContains' => ['assertErrorContains', 'Failed asserting that \'test\' is in error output.', 'routes', 'test'],
  222. 'assertErrorRegExp' => ['assertErrorRegExp', 'Failed asserting that `/test/` PCRE pattern found in error output.', 'routes', '/test/'],
  223. 'assertErrorEmpty' => ['assertErrorEmpty', 'Failed asserting that error output is empty.', 'integration args_and_options'],
  224. ];
  225. }
  226. }