Skip to content

Commit 4a9ccc9

Browse files
author
James Judd
committed
add unit tests to illustrate JSON assertion issues
1 parent 8af242b commit 4a9ccc9

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

tests/Foundation/FoundationTestResponseTest.php

Lines changed: 29 additions & 0 deletions
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)