21
21
* Eases the testing of console commands.
22
22
*
23
23
* @author Fabien Potencier <[email protected] >
24
+ * @author Robin Chalas <[email protected] >
24
25
*/
25
26
class CommandTester
26
27
{
27
28
private $ command ;
28
29
private $ input ;
29
30
private $ output ;
31
+ private $ inputs = array ();
30
32
private $ statusCode ;
31
33
32
34
/**
@@ -39,46 +41,50 @@ public function __construct(Command $command)
39
41
$ this ->command = $ command ;
40
42
}
41
43
42
- /**
43
- * Executes the command.
44
- *
45
- * Available execution options:
46
- *
47
- * * interactive: Sets the input interactive flag
48
- * * decorated: Sets the output decorated flag
49
- * * verbosity: Sets the output verbosity flag
50
- *
51
- * @param array $input An array of command arguments and options
52
- * @param array $options An array of execution options
53
- *
54
- * @return int The command exit code
55
- */
56
- public function execute (array $ input , array $ options = array ())
57
- {
58
- // set the command name automatically if the application requires
59
- // this argument and no command name was passed
60
- if (!isset ($ input ['command ' ])
61
- && (null !== $ application = $ this ->command ->getApplication ())
62
- && $ application ->getDefinition ()->hasArgument ('command ' )
63
- ) {
64
- $ input = array_merge (array ('command ' => $ this ->command ->getName ()), $ input );
65
- }
66
-
67
- $ this ->input = new ArrayInput ($ input );
68
- if (isset ($ options ['interactive ' ])) {
69
- $ this ->input ->setInteractive ($ options ['interactive ' ]);
70
- }
71
-
72
- $ this ->output = new StreamOutput (fopen ('php://memory ' , 'w ' , false ));
73
- if (isset ($ options ['decorated ' ])) {
74
- $ this ->output ->setDecorated ($ options ['decorated ' ]);
75
- }
76
- if (isset ($ options ['verbosity ' ])) {
77
- $ this ->output ->setVerbosity ($ options ['verbosity ' ]);
78
- }
79
-
80
- return $ this ->statusCode = $ this ->command ->run ($ this ->input , $ this ->output );
81
- }
44
+ /**
45
+ * Executes the command.
46
+ *
47
+ * Available execution options:
48
+ *
49
+ * * interactive: Sets the input interactive flag
50
+ * * decorated: Sets the output decorated flag
51
+ * * verbosity: Sets the output verbosity flag
52
+ *
53
+ * @param array $input An array of command arguments and options
54
+ * @param array $options An array of execution options
55
+ *
56
+ * @return int The command exit code
57
+ */
58
+ public function execute (array $ input , array $ options = array ())
59
+ {
60
+ // set the command name automatically if the application requires
61
+ // this argument and no command name was passed
62
+ if (!isset ($ input ['command ' ])
63
+ && (null !== $ application = $ this ->command ->getApplication ())
64
+ && $ application ->getDefinition ()->hasArgument ('command ' )
65
+ ) {
66
+ $ input = array_merge (array ('command ' => $ this ->command ->getName ()), $ input );
67
+ }
68
+
69
+ $ this ->input = new ArrayInput ($ input );
70
+ if ($ this ->inputs ) {
71
+ $ this ->input ->setStream ($ this ->getInputStream ());
72
+ }
73
+
74
+ if (isset ($ options ['interactive ' ])) {
75
+ $ this ->input ->setInteractive ($ options ['interactive ' ]);
76
+ }
77
+
78
+ $ this ->output = new StreamOutput (fopen ('php://memory ' , 'w ' , false ));
79
+ if (isset ($ options ['decorated ' ])) {
80
+ $ this ->output ->setDecorated ($ options ['decorated ' ]);
81
+ }
82
+ if (isset ($ options ['verbosity ' ])) {
83
+ $ this ->output ->setVerbosity ($ options ['verbosity ' ]);
84
+ }
85
+
86
+ return $ this ->statusCode = $ this ->command ->run ($ this ->input , $ this ->output );
87
+ }
82
88
83
89
/**
84
90
* Gets the display returned by the last execution of the command.
@@ -129,4 +135,29 @@ public function getStatusCode()
129
135
{
130
136
return $ this ->statusCode ;
131
137
}
138
+
139
+ /**
140
+ * Sets the user inputs.
141
+ *
142
+ * @param array An array of strings representing each input
143
+ * passed to the command input stream.
144
+ *
145
+ * @return CommandTester
146
+ */
147
+ public function setInputs (array $ inputs )
148
+ {
149
+ $ this ->inputs = $ inputs ;
150
+
151
+ return $ this ;
152
+ }
153
+
154
+ private function getInputStream ()
155
+ {
156
+ $ stream = fopen ('php://memory ' , 'r+ ' , false );
157
+
158
+ fputs ($ stream , implode (PHP_EOL , $ this ->inputs ));
159
+ rewind ($ stream );
160
+
161
+ return $ stream ;
162
+ }
132
163
}
0 commit comments