TesterTrait.php 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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\Input\InputInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. use Symfony\Component\Console\Output\StreamOutput;
  14. /**
  15. * @author Amrouche Hamza <hamza.simperfit@gmail.com>
  16. *
  17. * @internal
  18. */
  19. trait TesterTrait
  20. {
  21. /** @var StreamOutput */
  22. private $output;
  23. private $inputs = array();
  24. /**
  25. * Gets the display returned by the last execution of the command or application.
  26. *
  27. * @param bool $normalize Whether to normalize end of lines to \n or not
  28. *
  29. * @return string The display
  30. */
  31. public function getDisplay($normalize = false)
  32. {
  33. rewind($this->output->getStream());
  34. $display = stream_get_contents($this->output->getStream());
  35. if ($normalize) {
  36. $display = str_replace(PHP_EOL, "\n", $display);
  37. }
  38. return $display;
  39. }
  40. /**
  41. * Gets the input instance used by the last execution of the command or application.
  42. *
  43. * @return InputInterface The current input instance
  44. */
  45. public function getInput()
  46. {
  47. return $this->input;
  48. }
  49. /**
  50. * Gets the output instance used by the last execution of the command or application.
  51. *
  52. * @return OutputInterface The current output instance
  53. */
  54. public function getOutput()
  55. {
  56. return $this->output;
  57. }
  58. /**
  59. * Gets the status code returned by the last execution of the command or application.
  60. *
  61. * @return int The status code
  62. */
  63. public function getStatusCode()
  64. {
  65. return $this->statusCode;
  66. }
  67. /**
  68. * Sets the user inputs.
  69. *
  70. * @param $inputs array An array of strings representing each input
  71. * passed to the command input stream
  72. *
  73. * @return self
  74. */
  75. public function setInputs(array $inputs)
  76. {
  77. $this->inputs = $inputs;
  78. return $this;
  79. }
  80. private static function createStream(array $inputs)
  81. {
  82. $stream = fopen('php://memory', 'r+', false);
  83. fwrite($stream, implode(PHP_EOL, $inputs));
  84. rewind($stream);
  85. return $stream;
  86. }
  87. }