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