ApplicationTester.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Symfony\Component\Console\Tester;
  3. use Symfony\Component\Console\Application;
  4. use Symfony\Component\Console\Input\ArrayInput;
  5. use Symfony\Component\Console\Output\StreamOutput;
  6. /*
  7. * This file is part of the Symfony framework.
  8. *
  9. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  10. *
  11. * This source file is subject to the MIT license that is bundled
  12. * with this source code in the file LICENSE.
  13. */
  14. /**
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. */
  17. class ApplicationTester
  18. {
  19. protected $application;
  20. protected $display;
  21. protected $input;
  22. protected $output;
  23. /**
  24. * Constructor.
  25. *
  26. * @param Application $application A Application instance to test.
  27. */
  28. public function __construct(Application $application)
  29. {
  30. $this->application = $application;
  31. }
  32. /**
  33. * Executes the application.
  34. *
  35. * Available options:
  36. *
  37. * * interactive: Sets the input interactive flag
  38. * * decorated: Sets the output decorated flag
  39. * * verbosity: Sets the output verbosity flag
  40. *
  41. * @param array $input An array of arguments and options
  42. * @param array $options An array of options
  43. */
  44. public function run(array $input, $options = array())
  45. {
  46. $this->input = new ArrayInput($input);
  47. if (isset($options['interactive'])) {
  48. $this->input->setInteractive($options['interactive']);
  49. }
  50. $this->output = new StreamOutput(fopen('php://memory', 'w', false));
  51. if (isset($options['decorated'])) {
  52. $this->output->setDecorated($options['decorated']);
  53. }
  54. if (isset($options['verbosity'])) {
  55. $this->output->setVerbosity($options['verbosity']);
  56. }
  57. $ret = $this->application->run($this->input, $this->output);
  58. rewind($this->output->getStream());
  59. return $this->display = stream_get_contents($this->output->getStream());
  60. }
  61. /**
  62. * Gets the display returned by the last execution of the application.
  63. *
  64. * @return string The display
  65. */
  66. public function getDisplay()
  67. {
  68. return $this->display;
  69. }
  70. /**
  71. * Gets the input instance used by the last execution of the application.
  72. *
  73. * @return InputInterface The current input instance
  74. */
  75. public function getInput()
  76. {
  77. return $this->input;
  78. }
  79. /**
  80. * Gets the output instance used by the last execution of the application.
  81. *
  82. * @return OutputInterface The current output instance
  83. */
  84. public function getOutput()
  85. {
  86. return $this->output;
  87. }
  88. }