ConsoleIntegrationTestCase.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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\TestSuite;
  15. use Cake\Console\ConsoleInput;
  16. use Cake\Console\ConsoleIo;
  17. use Cake\TestSuite\Stub\ConsoleOutput;
  18. /**
  19. * A test case class intended to make integration tests of cake console commands
  20. * easier.
  21. */
  22. class ConsoleIntegrationTestCase extends TestCase
  23. {
  24. /**
  25. * Whether or not to use the CommandRunner
  26. *
  27. * @var bool
  28. */
  29. protected $_useCommandRunner = false;
  30. /**
  31. * Last exit code
  32. *
  33. * @var int
  34. */
  35. protected $_exitCode;
  36. /**
  37. * Console output stub
  38. *
  39. * @var ConsoleOutput
  40. */
  41. protected $_out;
  42. /**
  43. * Console error output stub
  44. *
  45. * @var ConsoleOutput
  46. */
  47. protected $_err;
  48. /**
  49. * Console input mock
  50. *
  51. * @var ConsoleInput
  52. */
  53. protected $_in;
  54. /**
  55. * Runs cli integration test
  56. *
  57. * @param string $command Command to run
  58. * @return void
  59. */
  60. public function cli($command)
  61. {
  62. $dispatcher = $this->_makeDispatcher("bin/cake $command");
  63. $this->_exitCode = $dispatcher->dispatch();
  64. }
  65. /**
  66. * tearDown
  67. *
  68. * @return void
  69. */
  70. public function tearDown()
  71. {
  72. parent::tearDown();
  73. $this->_exitCode = null;
  74. $this->_io = null;
  75. $this->_out = null;
  76. $this->_err = null;
  77. $this->_in = null;
  78. $this->_useCommandRunner = false;
  79. }
  80. /**
  81. * Set this test case to use the CommandRunner rather than the legacy
  82. * ShellDispatcher
  83. *
  84. * @return void
  85. */
  86. public function enableCommandRunner()
  87. {
  88. $this->_useCommandRunner = true;
  89. }
  90. /**
  91. * Asserts shell exited with the expected code
  92. *
  93. * @param int $expected Expected exit code
  94. * @return void
  95. */
  96. public function assertExitCode($expected)
  97. {
  98. $this->assertSame($this->_exitCode, $expected);
  99. }
  100. /**
  101. * Asserts `stdout` contains expected output
  102. *
  103. * @param string $expected Expected output
  104. * @return void
  105. */
  106. public function assertOutputContains($expected)
  107. {
  108. $output = implode(PHP_EOL, $this->_out->messages());
  109. $this->assertContains($expected, $output);
  110. }
  111. /**
  112. * Builds the appropriate command dispatcher
  113. *
  114. * @param string $command Command
  115. * @return LegacyShellDispatcher
  116. */
  117. protected function _makeDispatcher($command)
  118. {
  119. $args = $this->_commandStringToArgs($command);
  120. if ($this->_useCommandRunner) {
  121. // not implemented yet
  122. return;
  123. }
  124. $this->_out = new ConsoleOutput();
  125. $this->_err = new ConsoleOutput();
  126. $this->_in = $this->getMockBuilder(ConsoleInput::class)
  127. ->disableOriginalConstructor()
  128. ->setMethods(['read'])
  129. ->getMock();
  130. $io = new ConsoleIo($this->_out, $this->_err, $this->_in);
  131. return new LegacyShellDispatcher($args, true, $io);
  132. }
  133. /**
  134. * Dispatches the command string to a script that returns the argv array
  135. * parsed by PHP
  136. *
  137. * @param string $command Command string
  138. * @return array
  139. */
  140. protected function _commandStringToArgs($command)
  141. {
  142. $argvScript = CORE_TESTS . 'argv.php';
  143. $jsonArgv = shell_exec("$argvScript $command");
  144. $argv = json_decode($jsonArgv);
  145. array_shift($argv);
  146. return $argv;
  147. }
  148. }