Keine Beschreibung

ApplicationTester.php 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. if ($this->inputs) {
  55. $this->input->setStream(self::createStream($this->inputs));
  56. }
  57. $this->initOutput($options);
  58. return $this->statusCode = $this->application->run($this->input, $this->output);
  59. }
  60. }