-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathApplicationTester.php
More file actions
158 lines (134 loc) · 4.05 KB
/
ApplicationTester.php
File metadata and controls
158 lines (134 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/*
* This file is part of the PHPCR Shell package
*
* (c) Daniel Leech <daniel@dantleech.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace PHPCR\Shell\Test;
use PHPCR\Shell\Console\Application\ShellApplication;
use PHPCR\Shell\Console\Input\StringInput;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\StreamOutput;
/**
* Eases the testing of console applications.
*
* When testing an application, don't forget to disable the auto exit flag:
*
* $application = new Application();
* $application->setAutoExit(false);
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ApplicationTester
{
private $application;
private $shellApplication;
private $input;
private $output;
private $lastExitCode;
/**
* Constructor.
*
* @param Application $application An Application instance to test.
*/
public function __construct(Application $application, ?ShellApplication $shellApplication = null)
{
$this->output = new StreamOutput(fopen('php://memory', 'w', false));
$this->application = $application;
$this->shellApplication = $shellApplication;
}
/**
* Executes the application.
*
* Available options:
*
* * interactive: Sets the input interactive flag
* * decorated: Sets the output decorated flag
* * verbosity: Sets the output verbosity flag
*
* @param array $input An array of arguments and options
* @param array $options An array of options
*
* @return int The command exit code
*/
public function run(array $input, $options = [])
{
$this->input = new ArrayInput($input);
if (isset($options['decorated'])) {
$this->output->setDecorated($options['decorated']);
}
if (isset($options['verbosity'])) {
$this->output->setVerbosity($options['verbosity']);
}
$this->application->setAutoExit(false);
if ($this->shellApplication) {
$this->shellApplication->setAutoExit(false);
}
$this->application->setCatchExceptions(false);
return $this->application->run($this->input, $this->output);
}
/**
* Gets the display returned by the last execution of the application.
*
* @param bool $normalize Whether to normalize end of lines to \n or not
*
* @return string The display
*/
public function getDisplay($normalize = false)
{
rewind($this->output->getStream());
$display = stream_get_contents($this->output->getStream());
if ($normalize) {
$display = str_replace(PHP_EOL, "\n", $display);
}
return $display;
}
public function getLastLine()
{
$display = trim($this->getDisplay());
$lines = explode("\n", $display);
if ($lines) {
return end($lines);
}
return $display;
}
/**
* Gets the input instance used by the last execution of the application.
*
* @return InputInterface The current input instance
*/
public function getInput()
{
return $this->input;
}
/**
* Gets the output instance used by the last execution of the application.
*
* @return OutputInterface The current output instance
*/
public function getOutput()
{
return $this->output;
}
public function runShellCommand($command)
{
if ($this->shellApplication) {
$ret = $this->shellApplication->run(new StringInput($command), $this->output);
} else {
$ret = $this->application->run(new StringInput($command), $this->output);
}
$this->lastExitCode = $ret;
return $ret;
}
public function getLastExitCode()
{
return $this->lastExitCode;
}
}