Skip to content

Commit fc3150c

Browse files
author
James Judd
committed
implement bug fix by making assertion functions search for characters following specified value
1 parent 4a9ccc9 commit fc3150c

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

src/Illuminate/Foundation/Testing/TestResponse.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -466,12 +466,12 @@ public function assertJsonFragment(array $data)
466466
));
467467

468468
foreach (Arr::sortRecursive($data) as $key => $value) {
469-
$expected = substr(json_encode([$key => $value]), 1, -1);
469+
$expected = $this->jsonSearchStrings($key, $value);
470470

471471
PHPUnit::assertTrue(
472472
Str::contains($actual, $expected),
473473
'Unable to find JSON fragment: '.PHP_EOL.PHP_EOL.
474-
"[{$expected}]".PHP_EOL.PHP_EOL.
474+
"[". json_encode([$key => $value]) ."]".PHP_EOL.PHP_EOL.
475475
'within'.PHP_EOL.PHP_EOL.
476476
"[{$actual}]."
477477
);
@@ -498,12 +498,12 @@ public function assertJsonMissing(array $data, $exact = false)
498498
));
499499

500500
foreach (Arr::sortRecursive($data) as $key => $value) {
501-
$unexpected = substr(json_encode([$key => $value]), 1, -1);
501+
$unexpected = $this->jsonSearchStrings($key, $value);
502502

503503
PHPUnit::assertFalse(
504504
Str::contains($actual, $unexpected),
505505
'Found unexpected JSON fragment: '.PHP_EOL.PHP_EOL.
506-
"[{$unexpected}]".PHP_EOL.PHP_EOL.
506+
"[".json_encode([$key => $value])."]".PHP_EOL.PHP_EOL.
507507
'within'.PHP_EOL.PHP_EOL.
508508
"[{$actual}]."
509509
);
@@ -525,7 +525,7 @@ public function assertJsonMissingExact(array $data)
525525
));
526526

527527
foreach (Arr::sortRecursive($data) as $key => $value) {
528-
$unexpected = substr(json_encode([$key => $value]), 1, -1);
528+
$unexpected = $this->jsonSearchStrings($key, $value);
529529

530530
if (! Str::contains($actual, $unexpected)) {
531531
return $this;
@@ -540,6 +540,27 @@ public function assertJsonMissingExact(array $data)
540540
);
541541
}
542542

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+
543564
/**
544565
* Assert that the response has a given JSON structure.
545566
*

0 commit comments

Comments
 (0)