16
16
use Symfony \Component \Console \Command \Command ;
17
17
use Symfony \Component \Console \Exception \RunCommandFailedException ;
18
18
use Symfony \Component \Console \Input \InputInterface ;
19
+ use Symfony \Component \Console \Input \InputOption ;
19
20
use Symfony \Component \Console \Messenger \RunCommandMessage ;
20
21
use Symfony \Component \Console \Messenger \RunCommandMessageHandler ;
21
22
use Symfony \Component \Console \Output \OutputInterface ;
@@ -55,13 +56,31 @@ public function testExecutesCommandThatThrowsException()
55
56
public function testExecutesCommandThatCatchesThrownException ()
56
57
{
57
58
$ handler = new RunCommandMessageHandler ($ this ->createApplicationWithCommand ());
58
- $ context = $ handler (new RunCommandMessage ('test:command --throw -v ' , catchExceptions: true ));
59
+ $ context = $ handler (new RunCommandMessage ('test:command --throw -v ' , throwOnFailure: false , catchExceptions: true ));
59
60
60
61
$ this ->assertSame (1 , $ context ->exitCode );
61
62
$ this ->assertStringContainsString ('[RuntimeException] ' , $ context ->output );
62
63
$ this ->assertStringContainsString ('exception message ' , $ context ->output );
63
64
}
64
65
66
+ public function testThrowOnNonSuccess ()
67
+ {
68
+ $ handler = new RunCommandMessageHandler ($ this ->createApplicationWithCommand ());
69
+
70
+ try {
71
+ $ handler (new RunCommandMessage ('test:command --exit=1 ' ));
72
+ } catch (RunCommandFailedException $ e ) {
73
+ $ this ->assertSame (1 , $ e ->context ->exitCode );
74
+ $ this ->assertStringContainsString ('some message ' , $ e ->context ->output );
75
+ $ this ->assertSame ('Command "test:command --exit=1" exited with code "1". ' , $ e ->getMessage ());
76
+ $ this ->assertNull ($ e ->getPrevious ());
77
+
78
+ return ;
79
+ }
80
+
81
+ $ this ->fail ('Exception not thrown. ' );
82
+ }
83
+
65
84
private function createApplicationWithCommand (): Application
66
85
{
67
86
$ application = new Application ();
@@ -73,6 +92,7 @@ public function configure(): void
73
92
$ this
74
93
->setName ('test:command ' )
75
94
->addOption ('throw ' )
95
+ ->addOption ('exit ' , null , InputOption::VALUE_REQUIRED , 0 )
76
96
;
77
97
}
78
98
@@ -84,7 +104,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
84
104
throw new \RuntimeException ('exception message ' );
85
105
}
86
106
87
- return Command:: SUCCESS ;
107
+ return ( int ) $ input -> getOption ( ' exit ' ) ;
88
108
}
89
109
},
90
110
]);
0 commit comments