ConsoleIntegrationTestCaseTest.php 4.2 KB

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