1616use Symfony \Component \Console \Tester \CommandTester ;
1717use PHPUnit \Framework \Assert ;
1818use Symfony \Component \Console \Exception \CommandNotFoundException ;
19+ use Symfony \Component \Console \Exception \ExceptionInterface ;
20+ use Throwable ;
1921
2022/**
2123 * Faux "PendingCommand" class for unit testing.
@@ -28,6 +30,11 @@ class Test_Command {
2830 */
2931 protected CommandTester $ tester ;
3032
33+ /**
34+ * Exception thrown during command execution.
35+ */
36+ protected Throwable $ exception ;
37+
3138 /**
3239 * Flag if the command has been executed.
3340 */
@@ -139,6 +146,10 @@ public function dd(): never {
139146 $ this ->run ();
140147 }
141148
149+ if ( isset ( $ this ->exception ) ) {
150+ dd ( $ this ->exception );
151+ }
152+
142153 dd ( $ this ->tester ->getDisplay () );
143154 }
144155
@@ -179,7 +190,7 @@ protected function verify_command(): void {
179190 }
180191
181192 /**
182- * Run the command.
193+ * Run the command and verify the expectations .
183194 */
184195 public function run (): static {
185196 $ this ->has_executed = true ;
@@ -188,8 +199,8 @@ public function run(): static {
188199 $ this ->tester = $ this ->app ->make (
189200 \Mantle \Framework \Console \Kernel::class
190201 )->test ( $ this ->command , $ this ->arguments );
191- } catch ( CommandNotFoundException ) {
192- $ this ->test -> fail ( " Command [ { $ this -> command } ] not found. " ) ;
202+ } catch ( Throwable $ e ) {
203+ $ this ->exception = $ e ;
193204 }
194205
195206 $ this ->verify_expectations ();
@@ -203,7 +214,7 @@ public function run(): static {
203214 protected function verify_expectations (): void {
204215 // Assert that the exit code matches the expected exit code.
205216 if ( null !== $ this ->expected_exit_code ) {
206- $ exit_code = $ this ->tester -> getStatusCode ();
217+ $ exit_code = $ this ->get_status_code ();
207218
208219 $ this ->test ->assertEquals (
209220 $ this ->expected_exit_code ,
@@ -213,29 +224,20 @@ protected function verify_expectations(): void {
213224 }
214225
215226 if ( ! empty ( $ this ->expected_output ) ) {
216- $ output = $ this ->tester -> getDisplay ();
227+ $ output = $ this ->get_output ();
217228
218229 foreach ( $ this ->expected_output as $ expected_output ) {
219230 $ this ->test ->assertStringContainsString ( $ expected_output , $ output );
220231 }
221232 }
222233
223234 if ( ! empty ( $ this ->unexpected_output ) ) {
224- $ output = $ this ->tester -> getDisplay ();
235+ $ output = $ this ->get_output ();
225236
226237 foreach ( $ this ->unexpected_output as $ unexpected_output ) {
227238 $ this ->test ->assertStringNotContainsString ( $ unexpected_output , $ output );
228239 }
229240 }
230-
231- // todo: add output substring assertions.
232- // if ( ! empty( $this->expected_output_substrings ) ) {
233- // $output = $this->tester->getDisplay();
234-
235- // foreach ( $this->expected_output_substrings as $expected_output_substring ) {
236- // $this->test->assertStringContainsString( $expected_output_substring, $output );
237- // }
238- // }
239241 }
240242
241243 /**
@@ -246,4 +248,26 @@ public function __destruct() {
246248 $ this ->run ();
247249 }
248250 }
251+
252+ /**
253+ * Retrieve the output of the command.
254+ */
255+ public function get_output (): string {
256+ return match ( true ) {
257+ isset ( $ this ->exception ) => $ this ->exception ->getMessage (),
258+ isset ( $ this ->tester ) => $ this ->tester ->getDisplay (),
259+ default => '' ,
260+ };
261+ }
262+
263+ /**
264+ * Retrieve the status code of the command.
265+ */
266+ public function get_status_code (): int {
267+ return match ( true ) {
268+ isset ( $ this ->exception ) => Command::FAILURE ,
269+ isset ( $ this ->tester ) => $ this ->tester ->getStatusCode (),
270+ default => Command::FAILURE ,
271+ };
272+ }
249273}
0 commit comments