2020use Symfony \Component \Console \Messenger \RunCommandMessage ;
2121use Symfony \Component \Console \Messenger \RunCommandMessageHandler ;
2222use Symfony \Component \Console \Output \OutputInterface ;
23+ use Symfony \Component \Messenger \Exception \RecoverableExceptionInterface ;
24+ use Symfony \Component \Messenger \Exception \RecoverableMessageHandlingException ;
25+ use Symfony \Component \Messenger \Exception \UnrecoverableExceptionInterface ;
26+ use Symfony \Component \Messenger \Exception \UnrecoverableMessageHandlingException ;
2327
2428/**
2529 * @author Kevin Bond <[email protected] > @@ -81,6 +85,38 @@ public function testThrowOnNonSuccess()
8185 $ this ->fail ('Exception not thrown. ' );
8286 }
8387
88+ public function testExecutesCommandThatThrownUnrecoverableException ()
89+ {
90+ $ handler = new RunCommandMessageHandler ($ this ->createApplicationWithCommand ());
91+
92+ try {
93+ $ handler (new RunCommandMessage ('test:command --throw-unrecoverable ' ));
94+ } catch (UnrecoverableExceptionInterface $ e ) {
95+ $ this ->assertSame ('Unrecoverable exception message ' , $ e ->getMessage ());
96+ $ this ->assertNull ($ e ->getPrevious ());
97+
98+ return ;
99+ }
100+
101+ $ this ->fail ('Exception not thrown. ' );
102+ }
103+
104+ public function testExecutesCommandThatThrownRecoverableException ()
105+ {
106+ $ handler = new RunCommandMessageHandler ($ this ->createApplicationWithCommand ());
107+
108+ try {
109+ $ handler (new RunCommandMessage ('test:command --throw-recoverable ' ));
110+ } catch (RecoverableExceptionInterface $ e ) {
111+ $ this ->assertSame ('Recoverable exception message ' , $ e ->getMessage ());
112+ $ this ->assertNull ($ e ->getPrevious ());
113+
114+ return ;
115+ }
116+
117+ $ this ->fail ('Exception not thrown. ' );
118+ }
119+
84120 private function createApplicationWithCommand (): Application
85121 {
86122 $ application = new Application ();
@@ -92,6 +128,8 @@ public function configure(): void
92128 $ this
93129 ->setName ('test:command ' )
94130 ->addOption ('throw ' )
131+ ->addOption ('throw-unrecoverable ' )
132+ ->addOption ('throw-recoverable ' )
95133 ->addOption ('exit ' , null , InputOption::VALUE_REQUIRED , 0 )
96134 ;
97135 }
@@ -100,6 +138,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
100138 {
101139 $ output ->write ('some message ' );
102140
141+ if ($ input ->getOption ('throw-unrecoverable ' )) {
142+ throw new UnrecoverableMessageHandlingException ('Unrecoverable exception message ' );
143+ }
144+
145+ if ($ input ->getOption ('throw-recoverable ' )) {
146+ throw new RecoverableMessageHandlingException ('Recoverable exception message ' );
147+ }
148+
103149 if ($ input ->getOption ('throw ' )) {
104150 throw new \RuntimeException ('exception message ' );
105151 }
0 commit comments