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
|
|
View Code Duplication |
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); |
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
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.