Skip to content

Commit

Permalink
[5.7] Add tests for decodeResponseJson (#26741)
Browse files Browse the repository at this point in the history
* Add test to prevent assertJsonStructure from breaking

This test will prevent assertJsonStructure from breaking when an array with numeric keys which don't start from 0 is encoded to json and passed through it.

See #26677 for more info.

* Add test to expect exception message from decodeResponseJson

This test will ensure that the exception message is thrown when invalid JSON is being returned from the route.

See #26684
  • Loading branch information
driesvints authored and taylorotwell committed Dec 4, 2018
1 parent d5d50a8 commit 609c768
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions tests/Foundation/FoundationTestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ public function testAssertJsonWithArray()
$response->assertJson($resource->jsonSerialize());
}

public function testAssertJsonWithNull()
{
$response = TestResponse::fromBaseResponse(new Response(null));

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Invalid JSON was returned from the route.');

$resource = new JsonSerializableSingleResourceStub;

$response->assertJson($resource->jsonSerialize());
}

public function testAssertJsonWithMixed()
{
$response = TestResponse::fromBaseResponse(new Response(new JsonSerializableMixedResourcesStub));
Expand Down Expand Up @@ -224,6 +236,9 @@ public function testAssertJsonStructure()
// Wildcard (repeating structure)
$response->assertJsonStructure(['bars' => ['*' => ['bar', 'foo']]]);

// Wildcard (numeric keys)
$response->assertJsonStructure(['numeric_keys' => ['*' => ['bar', 'foo']]]);

// Nested after wildcard
$response->assertJsonStructure(['baz' => ['*' => ['foo', 'bar' => ['foo', 'bar']]]]);

Expand Down Expand Up @@ -395,17 +410,17 @@ class JsonSerializableMixedResourcesStub implements JsonSerializable
public function jsonSerialize()
{
return [
'foo' => 'bar',
'foo' => 'bar',
'foobar' => [
'foobar_foo' => 'foo',
'foobar_bar' => 'bar',
],
'bars' => [
'bars' => [
['bar' => 'foo 0', 'foo' => 'bar 0'],
['bar' => 'foo 1', 'foo' => 'bar 1'],
['bar' => 'foo 2', 'foo' => 'bar 2'],
],
'baz' => [
'baz' => [
['foo' => 'bar 0', 'bar' => ['foo' => 'bar 0', 'bar' => 'foo 0']],
['foo' => 'bar 1', 'bar' => ['foo' => 'bar 1', 'bar' => 'foo 1']],
],
Expand All @@ -414,6 +429,11 @@ public function jsonSerialize()
['bar' => ['bar' => 'foo 0', 'bar' => 'foo 0']],
['bar' => ['foo' => 'bar 0', 'bar' => 'foo 0', 'rab' => 'rab 0']],
],
'numeric_keys' => [
2 => ['bar' => 'foo 0', 'foo' => 'bar 0'],
3 => ['bar' => 'foo 1', 'foo' => 'bar 1'],
4 => ['bar' => 'foo 2', 'foo' => 'bar 2'],
],
];
}
}
Expand Down

0 comments on commit 609c768

Please sign in to comment.