ApplicationTester.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Output\StreamOutput;
  16. /**
  17. * Eases the testing of console applications.
  18. *
  19. * When testing an application, don't forget to disable the auto exit flag:
  20. *
  21. * $application = new Application();
  22. * $application->setAutoExit(false);
  23. *
  24. * @author Fabien Potencier <fabien@symfony.com>
  25. */
  26. class ApplicationTester
  27. {
  28. private $application;
  29. private $input;
  30. private $output;
  31. /**
  32. * Constructor.
  33. *
  34. * @param Application $application An Application instance to test.
  35. */
  36. public function __construct(Application $application)
  37. {
  38. $this->application = $application;
  39. }
  40. /**
  41. * Executes the application.
  42. *
  43. * Available options:
  44. *
  45. * * interactive: Sets the input interactive flag
  46. * * decorated: Sets the output decorated flag
  47. * * verbosity: Sets the output verbosity flag
  48. *
  49. * @param array $input An array of arguments and options
  50. * @param array $options An array of options
  51. *
  52. * @return integer The command exit code
  53. */
  54. public function run(array $input, $options = array())
  55. {
  56. $this->input = new ArrayInput($input);
  57. if (isset($options['interactive'])) {
  58. $this->input->setInteractive($options['interactive']);
  59. }
  60. $this->output = new StreamOutput(fopen('php://memory', 'w', false));
  61. if (isset($options['decorated'])) {
  62. $this->output->setDecorated($options['decorated']);
  63. }
  64. if (isset($options['verbosity'])) {
  65. $this->output->setVerbosity($options['verbosity']);
  66. }
  67. return $this->application->run($this->input, $this->output);
  68. }
  69. /**
  70. * Gets the display returned by the last execution of the application.
  71. *
  72. * @param Boolean $normalize Whether to normalize end of lines to \n or not
  73. *
  74. * @return string The display
  75. */
  76. public function getDisplay($normalize = false)
  77. {
  78. rewind($this->output->getStream());
  79. $display = stream_get_contents($this->output->getStream());
  80. if ($normalize) {
  81. $display = str_replace(PHP_EOL, "\n", $display);
  82. }
  83. return $display;
  84. }
  85. /**
  86. * Gets the input instance used by the last execution of the application.
  87. *
  88. * @return InputInterface The current input instance
  89. */
  90. public function getInput()
  91. {
  92. return $this->input;
  93. }
  94. /**
  95. * Gets the output instance used by the last execution of the application.
  96. *
  97. * @return OutputInterface The current output instance
  98. */
  99. public function getOutput()
  100. {
  101. return $this->output;
  102. }
  103. }