@@ -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