@@ -977,15 +977,28 @@ public function testRunDispatchesAllEventsWithException()
977
977
$ this ->assertContains ('before.foo.caught.after. ' , $ tester ->getDisplay ());
978
978
}
979
979
980
- public function testRunWithError ()
980
+ public function testRunDispatchesAllEventsWithExceptionInListener ()
981
981
{
982
- if (method_exists ($ this , 'expectException ' )) {
983
- $ this ->expectException ('Exception ' );
984
- $ this ->expectExceptionMessage ('dymerr ' );
985
- } else {
986
- $ this ->setExpectedException ('Exception ' , 'dymerr ' );
987
- }
982
+ $ dispatcher = $ this ->getDispatcher ();
983
+ $ dispatcher ->addListener ('console.command ' , function () {
984
+ throw new \RuntimeException ('foo ' );
985
+ });
986
+
987
+ $ application = new Application ();
988
+ $ application ->setDispatcher ($ dispatcher );
989
+ $ application ->setAutoExit (false );
988
990
991
+ $ application ->register ('foo ' )->setCode (function (InputInterface $ input , OutputInterface $ output ) {
992
+ $ output ->write ('foo. ' );
993
+ });
994
+
995
+ $ tester = new ApplicationTester ($ application );
996
+ $ tester ->run (array ('command ' => 'foo ' ));
997
+ $ this ->assertContains ('before.caught.after. ' , $ tester ->getDisplay ());
998
+ }
999
+
1000
+ public function testRunWithError ()
1001
+ {
989
1002
$ application = new Application ();
990
1003
$ application ->setAutoExit (false );
991
1004
$ application ->setCatchExceptions (false );
@@ -997,7 +1010,13 @@ public function testRunWithError()
997
1010
});
998
1011
999
1012
$ tester = new ApplicationTester ($ application );
1000
- $ tester ->run (array ('command ' => 'dym ' ));
1013
+
1014
+ try {
1015
+ $ tester ->run (array ('command ' => 'dym ' ));
1016
+ $ this ->fail ('Error expected. ' );
1017
+ } catch (\Error $ e ) {
1018
+ $ this ->assertSame ('dymerr ' , $ e ->getMessage ());
1019
+ }
1001
1020
}
1002
1021
1003
1022
/**
@@ -1087,32 +1106,6 @@ public function testTerminalDimensions()
1087
1106
$ this ->assertSame (array ($ width , 80 ), $ application ->getTerminalDimensions ());
1088
1107
}
1089
1108
1090
- protected function getDispatcher ($ skipCommand = false )
1091
- {
1092
- $ dispatcher = new EventDispatcher ();
1093
- $ dispatcher ->addListener ('console.command ' , function (ConsoleCommandEvent $ event ) use ($ skipCommand ) {
1094
- $ event ->getOutput ()->write ('before. ' );
1095
-
1096
- if ($ skipCommand ) {
1097
- $ event ->disableCommand ();
1098
- }
1099
- });
1100
- $ dispatcher ->addListener ('console.terminate ' , function (ConsoleTerminateEvent $ event ) use ($ skipCommand ) {
1101
- $ event ->getOutput ()->writeln ('after. ' );
1102
-
1103
- if (!$ skipCommand ) {
1104
- $ event ->setExitCode (113 );
1105
- }
1106
- });
1107
- $ dispatcher ->addListener ('console.exception ' , function (ConsoleExceptionEvent $ event ) {
1108
- $ event ->getOutput ()->write ('caught. ' );
1109
-
1110
- $ event ->setException (new \LogicException ('caught. ' , $ event ->getExitCode (), $ event ->getException ()));
1111
- });
1112
-
1113
- return $ dispatcher ;
1114
- }
1115
-
1116
1109
public function testSetRunCustomDefaultCommand ()
1117
1110
{
1118
1111
$ command = new \FooCommand ();
@@ -1151,6 +1144,32 @@ public function testCanCheckIfTerminalIsInteractive()
1151
1144
$ inputStream = $ application ->getHelperSet ()->get ('question ' )->getInputStream ();
1152
1145
$ this ->assertEquals ($ tester ->getInput ()->isInteractive (), @posix_isatty ($ inputStream ));
1153
1146
}
1147
+
1148
+ protected function getDispatcher ($ skipCommand = false )
1149
+ {
1150
+ $ dispatcher = new EventDispatcher ();
1151
+ $ dispatcher ->addListener ('console.command ' , function (ConsoleCommandEvent $ event ) use ($ skipCommand ) {
1152
+ $ event ->getOutput ()->write ('before. ' );
1153
+
1154
+ if ($ skipCommand ) {
1155
+ $ event ->disableCommand ();
1156
+ }
1157
+ });
1158
+ $ dispatcher ->addListener ('console.terminate ' , function (ConsoleTerminateEvent $ event ) use ($ skipCommand ) {
1159
+ $ event ->getOutput ()->writeln ('after. ' );
1160
+
1161
+ if (!$ skipCommand ) {
1162
+ $ event ->setExitCode (113 );
1163
+ }
1164
+ });
1165
+ $ dispatcher ->addListener ('console.exception ' , function (ConsoleExceptionEvent $ event ) {
1166
+ $ event ->getOutput ()->write ('caught. ' );
1167
+
1168
+ $ event ->setException (new \LogicException ('caught. ' , $ event ->getExitCode (), $ event ->getException ()));
1169
+ });
1170
+
1171
+ return $ dispatcher ;
1172
+ }
1154
1173
}
1155
1174
1156
1175
class CustomApplication extends Application
0 commit comments