Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.3] Fix see json to not throw an error #16350

Merged
merged 1 commit into from
Nov 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
{
}
}