Skip to content

Commit ecf0f15

Browse files
committed
Merge branch '5.6' of https://github.com/Juddling/framework into Juddling-5.6
2 parents 89fc860 + 85dbe29 commit ecf0f15

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

src/Illuminate/Foundation/Testing/TestResponse.php

+26-5
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
*

tests/Foundation/FoundationTestResponseTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,16 @@ public function testAssertJsonFragment()
174174
$response->assertJsonFragment(['foobar' => ['foobar_foo' => 'foo', 'foobar_bar' => 'bar']]);
175175

176176
$response->assertJsonFragment(['foo' => 'bar 0', 'bar' => ['foo' => 'bar 0', 'bar' => 'foo 0']]);
177+
178+
$response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceWithIntegersStub()));
179+
180+
$response->assertJsonFragment(['id' => 10]);
181+
182+
try {
183+
$response->assertJsonFragment(['id' => 1]);
184+
$this->fail('Asserting id => 1, existsing in JsonSerializableSingleResourceWithIntegersStub should fail');
185+
} catch (\PHPUnit\Framework\ExpectationFailedException $e) {
186+
}
177187
}
178188

179189
public function testAssertJsonStructure()
@@ -217,6 +227,13 @@ public function testAssertJsonCount()
217227
$response->assertJsonCount(4);
218228
}
219229

230+
public function testAssertJsonMissing()
231+
{
232+
$response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceWithIntegersStub));
233+
234+
$response->assertJsonMissing(['id' => 2]);
235+
}
236+
220237
public function testAssertJsonMissingValidationErrors()
221238
{
222239
$baseResponse = tap(new Response, function ($response) {
@@ -330,3 +347,15 @@ public function jsonSerialize()
330347
];
331348
}
332349
}
350+
351+
class JsonSerializableSingleResourceWithIntegersStub implements JsonSerializable
352+
{
353+
public function jsonSerialize()
354+
{
355+
return [
356+
['id' => 10, 'foo' => 'bar'],
357+
['id' => 20, 'foo' => 'bar'],
358+
['id' => 30, 'foo' => 'bar'],
359+
];
360+
}
361+
}

0 commit comments

Comments
 (0)