From 68e948f2fe662e37737b175bc15ef7cc8f9a343d Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 3 Mar 2026 19:47:18 +0100 Subject: [PATCH 1/4] Improve error message for failed steps --- src/Context/Support.php | 17 +++++++++++++++++ src/Context/WhenStepDefinitions.php | 22 +++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/Context/Support.php b/src/Context/Support.php index c68e2879c..43ce10fe7 100644 --- a/src/Context/Support.php +++ b/src/Context/Support.php @@ -123,6 +123,23 @@ protected function check_string( $output, $expected, $action, $message = false, $message = $output; } + $action_message = ''; + switch ( $action ) { + case 'be': + $action_message = 'Output does not exactly match expected string:'; + break; + case 'contain': + $action_message = 'Output does not contain expected string:'; + break; + case 'not contain': + $action_message = 'Output unexpectedly contains string:'; + break; + } + + if ( ! empty( $action_message ) ) { + $message .= "\n\n" . $action_message . "\n" . $expected; + } + $diff = $this->generate_diff( $expected, rtrim( $output, "\n" ) ); if ( ! empty( $diff ) ) { $message .= "\n\n" . $diff; diff --git a/src/Context/WhenStepDefinitions.php b/src/Context/WhenStepDefinitions.php index 2de8b3e2e..842b1f604 100644 --- a/src/Context/WhenStepDefinitions.php +++ b/src/Context/WhenStepDefinitions.php @@ -19,7 +19,27 @@ public function wpcli_tests_invoke_proc( $proc, $mode ) { ); $method = $map[ $mode ]; - return $proc->$method(); + try { + return $proc->$method(); + } catch ( \RuntimeException $e ) { + if ( 'run' === $mode ) { + $message = $e->getMessage(); + $status = 'unknown'; + if ( preg_match( '/exit status: (\d+)$/', $message, $matches ) ) { + $status = $matches[1]; + } + + if ( '0' === $status ) { + $message .= "\n\nThe command unexpectedly produced STDERR output. If you expect a non-zero exit status or STDERR output, use `When I try [...]`. Else, this may be a bug in your code or test."; + } else { + $message .= "\n\nThe command unexpectedly exited with status {$status}. If you expect a non-zero exit status, use `When I try [...]`. Else, this may be a bug in your code or test."; + } + + throw new \RuntimeException( $message, $e->getCode(), $e ); + } + + throw $e; + } } /** From d62184875f6499586d08bec075c1212ec2609a82 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 3 Mar 2026 20:57:29 +0100 Subject: [PATCH 2/4] Lint fixes --- src/Context/Support.php | 6 +++--- src/PHPStan/ParseUrlFunctionDynamicReturnTypeExtension.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Context/Support.php b/src/Context/Support.php index 43ce10fe7..cbd843788 100644 --- a/src/Context/Support.php +++ b/src/Context/Support.php @@ -134,11 +134,11 @@ protected function check_string( $output, $expected, $action, $message = false, case 'not contain': $action_message = 'Output unexpectedly contains string:'; break; + default: + throw new \Behat\Behat\Tester\Exception\PendingException(); } - if ( ! empty( $action_message ) ) { - $message .= "\n\n" . $action_message . "\n" . $expected; - } + $message .= "\n\n" . $action_message . "\n" . $expected; $diff = $this->generate_diff( $expected, rtrim( $output, "\n" ) ); if ( ! empty( $diff ) ) { diff --git a/src/PHPStan/ParseUrlFunctionDynamicReturnTypeExtension.php b/src/PHPStan/ParseUrlFunctionDynamicReturnTypeExtension.php index 55fef8974..a875ecb42 100644 --- a/src/PHPStan/ParseUrlFunctionDynamicReturnTypeExtension.php +++ b/src/PHPStan/ParseUrlFunctionDynamicReturnTypeExtension.php @@ -81,7 +81,7 @@ public function getTypeFromFunctionCall( FunctionReflection $functionReflection, $types = []; foreach ( $urlType->getConstantStrings() as $constantString ) { try { - // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged $result = @parse_url( $constantString->getValue(), $componentType->getValue() ); } catch ( \Error $e ) { $types[] = new ConstantBooleanType( false ); From 0c73cea25d78e58e0bed917711649b67326a8c97 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 3 Mar 2026 20:58:54 +0100 Subject: [PATCH 3/4] Update src/Context/WhenStepDefinitions.php --- src/Context/WhenStepDefinitions.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Context/WhenStepDefinitions.php b/src/Context/WhenStepDefinitions.php index 842b1f604..7e1c0dbf8 100644 --- a/src/Context/WhenStepDefinitions.php +++ b/src/Context/WhenStepDefinitions.php @@ -24,10 +24,7 @@ public function wpcli_tests_invoke_proc( $proc, $mode ) { } catch ( \RuntimeException $e ) { if ( 'run' === $mode ) { $message = $e->getMessage(); - $status = 'unknown'; - if ( preg_match( '/exit status: (\d+)$/', $message, $matches ) ) { - $status = $matches[1]; - } + $status = preg_match( '/exit status: (\d+)$/', $message, $matches ) ? $matches[1] : 'unknown'; if ( '0' === $status ) { $message .= "\n\nThe command unexpectedly produced STDERR output. If you expect a non-zero exit status or STDERR output, use `When I try [...]`. Else, this may be a bug in your code or test."; From 806e03a769a4d6d38798c4198b1041804f68a775 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 3 Mar 2026 21:10:11 +0100 Subject: [PATCH 4/4] Lint fix --- src/Context/WhenStepDefinitions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Context/WhenStepDefinitions.php b/src/Context/WhenStepDefinitions.php index 7e1c0dbf8..e5a6c1237 100644 --- a/src/Context/WhenStepDefinitions.php +++ b/src/Context/WhenStepDefinitions.php @@ -24,7 +24,7 @@ public function wpcli_tests_invoke_proc( $proc, $mode ) { } catch ( \RuntimeException $e ) { if ( 'run' === $mode ) { $message = $e->getMessage(); - $status = preg_match( '/exit status: (\d+)$/', $message, $matches ) ? $matches[1] : 'unknown'; + $status = preg_match( '/exit status: (\d+)$/', $message, $matches ) ? $matches[1] : 'unknown'; if ( '0' === $status ) { $message .= "\n\nThe command unexpectedly produced STDERR output. If you expect a non-zero exit status or STDERR output, use `When I try [...]`. Else, this may be a bug in your code or test.";