Completed
Push — master ( 132b95...1b4be3 )
by Richard
9s
created

ApplicationTester::getInput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
use Symfony\Component\Console\Application;
4
use Symfony\Component\Console\Input\StringInput;
5
use Symfony\Component\Console\Output\StreamOutput;
6
7
/**
8
 * A console application tester heavily inspired by/proudly stolen from \Symfony\Component\Console\Tester\ApplicationTester.
9
 */
10
class ApplicationTester
11
{
12
    /**
13
     * @var Application $application
14
     */
15
    private $application;
16
17
    /**
18
     * @var StringInput $input
19
     */
20
    private $input;
21
22
    /**
23
     * @var StreamOutput $output
24
     */
25
    private $output;
26
27
    /**
28
     * @var resource $inputStream
29
     */
30
    private $inputStream;
31
32
    /**
33
     * @param Application $application
34
     */
35
    public function __construct(Application $application)
36
    {
37
        $this->application = $application;
38
    }
39
40
    /**
41
     * @param string $input
42
     * @param array  $options
43
     *
44
     * @return integer
45
     */
46
    public function run($input, array $options = array())
47
    {
48
        if (isset($options['interactive']) && $options['interactive']) {
49
            $this->input = new InteractiveStringInput($input);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \InteractiveStringInput($input) of type object<InteractiveStringInput> is incompatible with the declared type object<Symfony\Component...sole\Input\StringInput> of property $input.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
50
        } else {
51
            $this->input = new StringInput($input);
52
            $this->input->setInteractive(false);
53
        }
54
55
        $this->output = new StreamOutput(fopen('php://memory', 'w', false));
56
        if (isset($options['decorated'])) {
57
            $this->output->setDecorated($options['decorated']);
58
        }
59
        if (isset($options['verbosity'])) {
60
            $this->output->setVerbosity($options['verbosity']);
61
        }
62
63
        $inputStream = $this->getInputStream();
64
        rewind($inputStream);
65
        $this->application->getHelperSet()
66
            ->get('question')
67
            ->setInputStream($inputStream);
68
69
        return $this->application->run($this->input, $this->output);
70
    }
71
72
    /**
73
     * @param boolean
74
     *
75
     * @return string
76
     */
77
    public function getDisplay($normalize = false)
78
    {
79
        rewind($this->output->getStream());
80
81
        $display = stream_get_contents($this->output->getStream());
82
83
        if ($normalize) {
84
            $display = str_replace(PHP_EOL, "\n", $display);
85
        }
86
87
        return $display;
88
    }
89
90
    /**
91
     * @return InputInterface
92
     */
93
    public function getInput()
94
    {
95
        return $this->input;
96
    }
97
98
    /**
99
     * @return OutputInterface
100
     */
101
    public function getOutput()
102
    {
103
        return $this->output;
104
    }
105
106
    /**
107
     * @param string $input
108
     */
109
    public function putToInputStream($input)
110
    {
111
        fputs($this->getInputStream(), $input);
112
    }
113
114
    /**
115
     * @return resource
116
     */
117
    private function getInputStream()
118
    {
119
        if (null === $this->inputStream) {
120
            $this->inputStream = fopen('php://memory', 'r+', false);
121
        }
122
123
        return $this->inputStream;
124
    }
125
}
126