ApplicationTester.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. private $statusCode;
  32. /**
  33. * Constructor.
  34. *
  35. * @param Application $application An Application instance to test.
  36. */
  37. public function __construct(Application $application)
  38. {
  39. $this->application = $application;
  40. }
  41. /**
  42. * Executes the application.
  43. *
  44. * Available options:
  45. *
  46. * * interactive: Sets the input interactive flag
  47. * * decorated: Sets the output decorated flag
  48. * * verbosity: Sets the output verbosity flag
  49. *
  50. * @param array $input An array of arguments and options
  51. * @param array $options An array of options
  52. *
  53. * @return int The command exit code
  54. */
  55. public function run(array $input, $options = array())
  56. {
  57. $this->input = new ArrayInput($input);
  58. if (isset($options['interactive'])) {
  59. $this->input->setInteractive($options['interactive']);
  60. }
  61. $this->output = new StreamOutput(fopen('php://memory', 'w', false));
  62. if (isset($options['decorated'])) {
  63. $this->output->setDecorated($options['decorated']);
  64. }
  65. if (isset($options['verbosity'])) {
  66. $this->output->setVerbosity($options['verbosity']);
  67. }
  68. return $this->statusCode = $this->application->run($this->input, $this->output);
  69. }
  70. /**
  71. * Gets the display returned by the last execution of the application.
  72. *
  73. * @param bool $normalize Whether to normalize end of lines to \n or not
  74. *
  75. * @return string The display
  76. */
  77. public function getDisplay($normalize = false)
  78. {
  79. rewind($this->output->getStream());
  80. $display = stream_get_contents($this->output->getStream());
  81. if ($normalize) {
  82. $display = str_replace(PHP_EOL, "\n", $display);
  83. }
  84. return $display;
  85. }
  86. /**
  87. * Gets the input instance used by the last execution of the application.
  88. *
  89. * @return InputInterface The current input instance
  90. */
  91. public function getInput()
  92. {
  93. return $this->input;
  94. }
  95. /**
  96. * Gets the output instance used by the last execution of the application.
  97. *
  98. * @return OutputInterface The current output instance
  99. */
  100. public function getOutput()
  101. {
  102. return $this->output;
  103. }
  104. /**
  105. * Gets the status code returned by the last execution of the application.
  106. *
  107. * @return int The status code
  108. */
  109. public function getStatusCode()
  110. {
  111. return $this->statusCode;
  112. }
  113. }