ConsoleIntegrationTestTraitTest.php 6.8 KB

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