19
19
use Symfony \Component \Console \EventListener \ExceptionListener ;
20
20
use Symfony \Component \Console \Input \ArgvInput ;
21
21
use Symfony \Component \Console \Input \ArrayInput ;
22
+ use Symfony \Component \Console \Input \Input ;
22
23
use Symfony \Component \Console \Input \StringInput ;
23
24
use Symfony \Component \Console \Input \InputInterface ;
24
25
use Symfony \Component \Console \Output \OutputInterface ;
@@ -36,10 +37,40 @@ public function testOnConsoleError()
36
37
->with ('Error thrown while running command "{command}". Message: "{message}" ' , array ('error ' => $ exception , 'command ' => 'test:run --foo=baz buzz ' , 'message ' => 'An error occurred ' ))
37
38
;
38
39
40
+ $ listener = new ExceptionListener ($ logger );
41
+ $ listener ->onConsoleError ($ this ->getConsoleErrorEvent ($ exception , new ArgvInput (array ('console.php ' , 'test:run ' , '--foo=baz ' , 'buzz ' )), 1 , new Command ('test:run ' )));
42
+ }
43
+
44
+ public function testOnConsoleErrorWithNoCommandCastsInputAsString ()
45
+ {
46
+ $ exception = new \RuntimeException ('An error occurred ' );
47
+
48
+ $ logger = $ this ->getLogger ();
49
+ $ logger
50
+ ->expects ($ this ->once ())
51
+ ->method ('error ' )
52
+ ->with ('Error thrown while running command "{command}". Message: "{message}" ' , array ('error ' => $ exception , 'command ' => '\'test:run \' --foo=baz buzz ' , 'message ' => 'An error occurred ' ))
53
+ ;
54
+
39
55
$ listener = new ExceptionListener ($ logger );
40
56
$ listener ->onConsoleError ($ this ->getConsoleErrorEvent ($ exception , new ArgvInput (array ('console.php ' , 'test:run ' , '--foo=baz ' , 'buzz ' )), 1 ));
41
57
}
42
58
59
+ public function testOnConsoleErrorWithNoCommandAndNoInputString ()
60
+ {
61
+ $ exception = new \RuntimeException ('An error occurred ' );
62
+
63
+ $ logger = $ this ->getLogger ();
64
+ $ logger
65
+ ->expects ($ this ->once ())
66
+ ->method ('error ' )
67
+ ->with ('An error occurred while using the console. Message: "{message}" ' , array ('error ' => $ exception , 'message ' => 'An error occurred ' ))
68
+ ;
69
+
70
+ $ listener = new ExceptionListener ($ logger );
71
+ $ listener ->onConsoleError ($ this ->getConsoleErrorEvent ($ exception , new NonStringInput (), 1 ));
72
+ }
73
+
43
74
public function testOnConsoleTerminateForNonZeroExitCodeWritesToLog ()
44
75
{
45
76
$ logger = $ this ->getLogger ();
@@ -109,9 +140,9 @@ private function getLogger()
109
140
return $ this ->getMockForAbstractClass (LoggerInterface::class);
110
141
}
111
142
112
- private function getConsoleErrorEvent (\Exception $ exception , InputInterface $ input , $ exitCode )
143
+ private function getConsoleErrorEvent (\Exception $ exception , InputInterface $ input , $ exitCode, Command $ command = null )
113
144
{
114
- return new ConsoleErrorEvent ($ input , $ this ->getOutput (), $ exception , $ exitCode , new Command ( ' test:run ' ) );
145
+ return new ConsoleErrorEvent ($ input , $ this ->getOutput (), $ exception , $ exitCode , $ command );
115
146
}
116
147
117
148
private function getConsoleTerminateEvent (InputInterface $ input , $ exitCode )
@@ -124,3 +155,22 @@ private function getOutput()
124
155
return $ this ->getMockBuilder (OutputInterface::class)->getMock ();
125
156
}
126
157
}
158
+
159
+ class NonStringInput extends Input
160
+ {
161
+ public function getFirstArgument ()
162
+ {
163
+ }
164
+
165
+ public function hasParameterOption ($ values , $ onlyParams = false )
166
+ {
167
+ }
168
+
169
+ public function getParameterOption ($ values , $ default = false , $ onlyParams = false )
170
+ {
171
+ }
172
+
173
+ public function parse ()
174
+ {
175
+ }
176
+ }
0 commit comments