Skip to content

Commit

Permalink
Fix error when using seeJson and the response type is not JSON (#16350)
Browse files Browse the repository at this point in the history
An error is thrown when seeJson is called on a response but the response
is not JSON. Instead, seeJson now fails the test with an explanation
that the response was not JSON.
  • Loading branch information
andrewmh authored and taylorotwell committed Nov 11, 2016
1 parent 6337bf6 commit 0a9e23a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ protected function receiveJson(array $data = null)
public function seeJsonEquals(array $data)
{
$actual = json_encode(Arr::sortRecursive(
json_decode($this->response->getContent(), true)
(array) $this->decodeResponseJson()
));

$this->assertEquals(json_encode(Arr::sortRecursive($data)), $actual);
Expand All @@ -321,9 +321,7 @@ public function seeJson(array $data = null, $negate = false)
}

try {
$this->seeJsonEquals($data);

return $this;
return $this->seeJsonEquals($data);
} catch (PHPUnit_Framework_ExpectationFailedException $e) {
return $this->seeJsonContains($data, $negate);
}
Expand Down Expand Up @@ -354,7 +352,7 @@ public function seeJsonStructure(array $structure = null, $responseData = null)
}

if (! $responseData) {
$responseData = json_decode($this->response->getContent(), true);
$responseData = $this->decodeResponseJson();
}

foreach ($structure as $key => $value) {
Expand Down
43 changes: 40 additions & 3 deletions tests/Foundation/FoundationMakesHttpRequestsJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,54 @@ public function testSeeJsonStructure()
$this->response = new Illuminate\Http\Response(new JsonSerializableSingleResourceStub);
$this->seeJsonStructure(['*' => ['foo', 'bar', 'foobar']]);
}

public function testSeeJsonWithNonJson()
{
$this->response = new Illuminate\Http\Response(new NonJsonSerializableStub);

try {
$this->seeJson(['foo' => 'bad']);
} catch (PHPUnit_Framework_AssertionFailedError $e) {
$this->assertStringStartsWith('Invalid JSON', $e->getMessage());

return;
}

$this->fail('invalid JSON should fail seeJson');
}

public function testSeeJsonEqualsWithNonJson()
{
$this->response = new Illuminate\Http\Response(new NonJsonSerializableStub);

try {
$this->seeJsonEquals(['foo' => 'bad']);
} catch (PHPUnit_Framework_AssertionFailedError $e) {
$this->assertStringStartsWith('Invalid JSON', $e->getMessage());

return;
}

$this->fail('Invalid JSON should fail seeJsonEquals');
}
}

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 @@ -88,3 +118,10 @@ public function jsonSerialize()
];
}
}

class NonJsonSerializableStub implements JsonSerializable
{
public function jsonSerialize()
{
}
}

0 comments on commit 0a9e23a

Please sign in to comment.