@@ -215,7 +215,21 @@ 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_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+ }
230+ }
231+ }
232+ throw new Exception ( $ message );
219233 }
220234 }
221235
@@ -246,7 +260,19 @@ public function then_stdout_should_be_a_json_array_containing( PyStringNode $exp
246260
247261 $ missing = array_diff ( $ expected_values , $ actual_values );
248262 if ( ! empty ( $ missing ) ) {
249- throw new Exception ( $ this ->result );
263+ $ message = (string ) $ this ->result ;
264+ // Pretty print JSON arrays for better diff readability.
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+ }
273+ }
274+ }
275+ throw new Exception ( $ message );
250276 }
251277 }
252278
@@ -269,19 +295,29 @@ public function then_stdout_should_be_csv_containing( TableNode $expected ): voi
269295 $ output = $ this ->result ->stdout ;
270296
271297 $ expected_rows = $ expected ->getRows ();
272- foreach ( $ expected as &$ row ) {
298+ foreach ( $ expected_rows as &$ row ) {
273299 foreach ( $ row as &$ value ) {
274300 $ value = $ this ->replace_variables ( $ value );
275301 }
276302 }
277303
278304 if ( ! $ this ->check_that_csv_string_contains_values ( $ output , $ expected_rows ) ) {
279- throw new Exception ( $ this ->result );
305+ $ message = (string ) $ this ->result ;
306+ // Convert expected rows to CSV format for diff.
307+ $ expected_csv = '' ;
308+ foreach ( $ expected_rows as $ row ) {
309+ $ expected_csv .= implode ( ', ' , array_map ( 'trim ' , $ row ) ) . "\n" ;
310+ }
311+ $ diff = $ this ->generate_diff ( trim ( $ expected_csv ), trim ( $ output ) );
312+ if ( ! empty ( $ diff ) ) {
313+ $ message .= "\n\n" . $ diff ;
314+ }
315+ throw new Exception ( $ message );
280316 }
281317 }
282318
283319 /**
284- * Expect STDOUT to be YAML containig certain content.
320+ * Expect STDOUT to be YAML containing certain content.
285321 *
286322 * ```
287323 * Scenario: My example scenario
@@ -303,7 +339,12 @@ public function then_stdout_should_be_yaml_containing( PyStringNode $expected ):
303339 $ expected = $ this ->replace_variables ( (string ) $ expected );
304340
305341 if ( ! $ this ->check_that_yaml_string_contains_yaml_string ( $ output , $ expected ) ) {
306- throw new Exception ( $ this ->result );
342+ $ message = (string ) $ this ->result ;
343+ $ diff = $ this ->generate_diff ( $ expected , $ output );
344+ if ( ! empty ( $ diff ) ) {
345+ $ message .= "\n\n" . $ diff ;
346+ }
347+ throw new Exception ( $ message );
307348 }
308349 }
309350
0 commit comments