ConsoleIntegrationTestCaseTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 a valid core command
  54. *
  55. * @return void
  56. */
  57. public function testExecCoreCommand()
  58. {
  59. $this->exec('routes');
  60. $this->assertExitCode(Shell::CODE_SUCCESS);
  61. }
  62. /**
  63. * tests exec with an arg and an option
  64. *
  65. * @return void
  66. */
  67. public function testExecWithArgsAndOption()
  68. {
  69. $this->exec('integration args_and_options arg --opt="some string"');
  70. $this->assertOutputContains('arg: arg');
  71. $this->assertOutputContains('opt: some string');
  72. $this->assertExitCode(Shell::CODE_SUCCESS);
  73. }
  74. /**
  75. * tests exec with missing required argument
  76. *
  77. * @return void
  78. */
  79. public function testExecWithMissingRequiredArg()
  80. {
  81. $this->exec('integration args_and_options');
  82. $this->assertErrorContains('Missing required arguments');
  83. $this->assertErrorContains('arg is required');
  84. $this->assertExitCode(Shell::CODE_ERROR);
  85. }
  86. /**
  87. * tests exec with input
  88. *
  89. * @return void
  90. */
  91. public function testExecWithInput()
  92. {
  93. $this->exec('integration bridge', ['javascript']);
  94. $this->assertErrorContains('No!');
  95. $this->assertExitCode(Shell::CODE_ERROR);
  96. }
  97. /**
  98. * tests exec with multiple inputs
  99. *
  100. * @return void
  101. */
  102. public function testExecWithMultipleInput()
  103. {
  104. $this->exec('integration bridge', ['cake', 'blue']);
  105. $this->assertOutputContains('You may pass');
  106. $this->assertExitCode(Shell::CODE_SUCCESS);
  107. }
  108. /**
  109. * tests assertOutputRegExp assertion
  110. *
  111. * @return void
  112. */
  113. public function testAssertOutputRegExp()
  114. {
  115. $this->exec('routes');
  116. $this->assertOutputRegExp('/^\+[\-\+]+\+$/m');
  117. }
  118. /**
  119. * tests assertErrorRegExp assertion
  120. *
  121. * @return void
  122. */
  123. public function testAssertErrorRegExp()
  124. {
  125. $this->exec('integration args_and_options');
  126. $this->assertErrorRegExp('/\<error\>(.+)\<\/error\>/');
  127. }
  128. /**
  129. * tests _commandStringToArgs
  130. *
  131. * @return void
  132. */
  133. public function testCommandStringToArgs()
  134. {
  135. $result = $this->_commandStringToArgs('command --something=nothing --with-spaces="quote me on that" \'quoted \"arg\"\'');
  136. $expected = [
  137. 'command',
  138. '--something=nothing',
  139. '--with-spaces=quote me on that',
  140. 'quoted \"arg\"',
  141. ];
  142. $this->assertSame($expected, $result);
  143. $json = json_encode(['key' => '"val"', 'this' => true]);
  144. $result = $this->_commandStringToArgs(" --json='$json'");
  145. $expected = [
  146. '--json=' . $json
  147. ];
  148. $this->assertSame($expected, $result);
  149. }
  150. }