ApplicationTester.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Tester;
  11. use Symfony\Component\Console\Application;
  12. use Symfony\Component\Console\Input\ArrayInput;
  13. /**
  14. * Eases the testing of console applications.
  15. *
  16. * When testing an application, don't forget to disable the auto exit flag:
  17. *
  18. * $application = new Application();
  19. * $application->setAutoExit(false);
  20. *
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. */
  23. class ApplicationTester
  24. {
  25. use TesterTrait;
  26. private $application;
  27. private $input;
  28. private $statusCode;
  29. public function __construct(Application $application)
  30. {
  31. $this->application = $application;
  32. }
  33. /**
  34. * Executes the application.
  35. *
  36. * Available options:
  37. *
  38. * * interactive: Sets the input interactive flag
  39. * * decorated: Sets the output decorated flag
  40. * * verbosity: Sets the output verbosity flag
  41. * * capture_stderr_separately: Make output of stdOut and stdErr separately available
  42. *
  43. * @param array $input An array of arguments and options
  44. * @param array $options An array of options
  45. *
  46. * @return int The command exit code
  47. */
  48. public function run(array $input, $options = [])
  49. {
  50. $this->input = new ArrayInput($input);
  51. if (isset($options['interactive'])) {
  52. $this->input->setInteractive($options['interactive']);
  53. }
  54. $shellInteractive = getenv('SHELL_INTERACTIVE');
  55. if ($this->inputs) {
  56. $this->input->setStream(self::createStream($this->inputs));
  57. putenv('SHELL_INTERACTIVE=1');
  58. }
  59. $this->initOutput($options);
  60. $this->statusCode = $this->application->run($this->input, $this->output);
  61. putenv($shellInteractive ? "SHELL_INTERACTIVE=$shellInteractive" : 'SHELL_INTERACTIVE');
  62. return $this->statusCode;
  63. }
  64. }