Skip to content

Commit c73108e

Browse files
Copilotswissspidy
andcommitted
Address code review feedback - add error checking and type hints
Co-authored-by: swissspidy <[email protected]>
1 parent 6d5bc89 commit c73108e

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

src/Context/Support.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ protected function check_that_yaml_string_contains_yaml_string( $actual_yaml, $e
315315
* @param string $actual The actual string.
316316
* @return string The unified diff output.
317317
*/
318-
protected function generate_diff( $expected, $actual ): string {
318+
protected function generate_diff( string $expected, string $actual ): string {
319319
$builder = new UnifiedDiffOutputBuilder(
320320
"--- Expected\n+++ Actual\n",
321321
false

src/Context/ThenStepDefinitions.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,16 @@ public function then_stdout_should_be_json_containing( PyStringNode $expected ):
217217
if ( ! $this->check_that_json_string_contains_json_string( $output, $expected ) ) {
218218
$message = (string) $this->result;
219219
// Pretty print JSON for better diff readability.
220-
$expected_json = json_encode( json_decode( $expected ), JSON_PRETTY_PRINT );
221-
$actual_json = json_encode( json_decode( $output ), JSON_PRETTY_PRINT );
222-
if ( $expected_json && $actual_json ) {
223-
$diff = $this->generate_diff( $expected_json, $actual_json );
224-
if ( ! empty( $diff ) ) {
225-
$message .= "\n\n" . $diff;
220+
$expected_decoded = json_decode( $expected );
221+
$actual_decoded = json_decode( $output );
222+
if ( null !== $expected_decoded && null !== $actual_decoded ) {
223+
$expected_json = json_encode( $expected_decoded, JSON_PRETTY_PRINT );
224+
$actual_json = json_encode( $actual_decoded, JSON_PRETTY_PRINT );
225+
if ( false !== $expected_json && false !== $actual_json ) {
226+
$diff = $this->generate_diff( $expected_json, $actual_json );
227+
if ( ! empty( $diff ) ) {
228+
$message .= "\n\n" . $diff;
229+
}
226230
}
227231
}
228232
throw new Exception( $message );
@@ -258,12 +262,14 @@ public function then_stdout_should_be_a_json_array_containing( PyStringNode $exp
258262
if ( ! empty( $missing ) ) {
259263
$message = (string) $this->result;
260264
// Pretty print JSON arrays for better diff readability.
261-
$expected_json = json_encode( $expected_values, JSON_PRETTY_PRINT );
262-
$actual_json = json_encode( $actual_values, JSON_PRETTY_PRINT );
263-
if ( $expected_json && $actual_json ) {
264-
$diff = $this->generate_diff( $expected_json, $actual_json );
265-
if ( ! empty( $diff ) ) {
266-
$message .= "\n\n" . $diff;
265+
if ( null !== $expected_values && null !== $actual_values ) {
266+
$expected_json = json_encode( $expected_values, JSON_PRETTY_PRINT );
267+
$actual_json = json_encode( $actual_values, JSON_PRETTY_PRINT );
268+
if ( false !== $expected_json && false !== $actual_json ) {
269+
$diff = $this->generate_diff( $expected_json, $actual_json );
270+
if ( ! empty( $diff ) ) {
271+
$message .= "\n\n" . $diff;
272+
}
267273
}
268274
}
269275
throw new Exception( $message );

0 commit comments

Comments
 (0)