@@ -215,7 +215,17 @@ public function then_stdout_should_be_json_containing( PyStringNode $expected ):
215215 $ expected = $ this ->replace_variables ( (string ) $ expected );
216216
217217 if ( ! $ this ->check_that_json_string_contains_json_string ( $ output , $ expected ) ) {
218- throw new Exception ( $ this ->result );
218+ $ message = (string ) $ this ->result ;
219+ // 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 ;
226+ }
227+ }
228+ throw new Exception ( $ message );
219229 }
220230 }
221231
@@ -246,7 +256,17 @@ public function then_stdout_should_be_a_json_array_containing( PyStringNode $exp
246256
247257 $ missing = array_diff ( $ expected_values , $ actual_values );
248258 if ( ! empty ( $ missing ) ) {
249- throw new Exception ( $ this ->result );
259+ $ message = (string ) $ this ->result ;
260+ // 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 ;
267+ }
268+ }
269+ throw new Exception ( $ message );
250270 }
251271 }
252272
@@ -276,7 +296,17 @@ public function then_stdout_should_be_csv_containing( TableNode $expected ): voi
276296 }
277297
278298 if ( ! $ this ->check_that_csv_string_contains_values ( $ output , $ expected_rows ) ) {
279- throw new Exception ( $ this ->result );
299+ $ message = (string ) $ this ->result ;
300+ // Convert expected rows to CSV format for diff.
301+ $ expected_csv = '' ;
302+ foreach ( $ expected_rows as $ row ) {
303+ $ expected_csv .= implode ( ', ' , array_map ( 'trim ' , $ row ) ) . "\n" ;
304+ }
305+ $ diff = $ this ->generate_diff ( trim ( $ expected_csv ), trim ( $ output ) );
306+ if ( ! empty ( $ diff ) ) {
307+ $ message .= "\n\n" . $ diff ;
308+ }
309+ throw new Exception ( $ message );
280310 }
281311 }
282312
@@ -303,7 +333,12 @@ public function then_stdout_should_be_yaml_containing( PyStringNode $expected ):
303333 $ expected = $ this ->replace_variables ( (string ) $ expected );
304334
305335 if ( ! $ this ->check_that_yaml_string_contains_yaml_string ( $ output , $ expected ) ) {
306- throw new Exception ( $ this ->result );
336+ $ message = (string ) $ this ->result ;
337+ $ diff = $ this ->generate_diff ( $ expected , $ output );
338+ if ( ! empty ( $ diff ) ) {
339+ $ message .= "\n\n" . $ diff ;
340+ }
341+ throw new Exception ( $ message );
307342 }
308343 }
309344
0 commit comments