@@ -466,12 +466,12 @@ public function assertJsonFragment(array $data)
466
466
));
467
467
468
468
foreach (Arr::sortRecursive ($ data ) as $ key => $ value ) {
469
- $ expected = substr ( json_encode ([ $ key => $ value]), 1 , - 1 );
469
+ $ expected = $ this -> jsonSearchStrings ( $ key, $ value );
470
470
471
471
PHPUnit::assertTrue (
472
472
Str::contains ($ actual , $ expected ),
473
473
'Unable to find JSON fragment: ' .PHP_EOL .PHP_EOL .
474
- " [ { $ expected } ] " .PHP_EOL .PHP_EOL .
474
+ ' [ ' . json_encode ([ $ key => $ value ]). ' ] ' .PHP_EOL .PHP_EOL .
475
475
'within ' .PHP_EOL .PHP_EOL .
476
476
"[ {$ actual }]. "
477
477
);
@@ -498,12 +498,12 @@ public function assertJsonMissing(array $data, $exact = false)
498
498
));
499
499
500
500
foreach (Arr::sortRecursive ($ data ) as $ key => $ value ) {
501
- $ unexpected = substr ( json_encode ([ $ key => $ value]), 1 , - 1 );
501
+ $ unexpected = $ this -> jsonSearchStrings ( $ key, $ value );
502
502
503
503
PHPUnit::assertFalse (
504
504
Str::contains ($ actual , $ unexpected ),
505
505
'Found unexpected JSON fragment: ' .PHP_EOL .PHP_EOL .
506
- " [ { $ unexpected } ] " .PHP_EOL .PHP_EOL .
506
+ ' [ ' . json_encode ([ $ key => $ value ]). ' ] ' .PHP_EOL .PHP_EOL .
507
507
'within ' .PHP_EOL .PHP_EOL .
508
508
"[ {$ actual }]. "
509
509
);
@@ -525,7 +525,7 @@ public function assertJsonMissingExact(array $data)
525
525
));
526
526
527
527
foreach (Arr::sortRecursive ($ data ) as $ key => $ value ) {
528
- $ unexpected = substr ( json_encode ([ $ key => $ value]), 1 , - 1 );
528
+ $ unexpected = $ this -> jsonSearchStrings ( $ key, $ value );
529
529
530
530
if (! Str::contains ($ actual , $ unexpected )) {
531
531
return $ this ;
@@ -540,6 +540,27 @@ public function assertJsonMissingExact(array $data)
540
540
);
541
541
}
542
542
543
+ /**
544
+ * TestResponse::assertJsonFragment and TestResponse::assertJsonMissing search for
545
+ * JSON encoded strings in the Response. This function returns the set of strings to
546
+ * be searched for in the response, explicitly adding all of the possible characters
547
+ * which could follow $value in the response.
548
+ *
549
+ * @param $key
550
+ * @param $value
551
+ * @return array
552
+ */
553
+ protected function jsonSearchStrings ($ key , $ value )
554
+ {
555
+ $ needle = substr (json_encode ([$ key => $ value ]), 1 , -1 );
556
+
557
+ return [
558
+ $ needle .'] ' ,
559
+ $ needle .'} ' ,
560
+ $ needle .', ' ,
561
+ ];
562
+ }
563
+
543
564
/**
544
565
* Assert that the response has a given JSON structure.
545
566
*
0 commit comments