From 0a9e23a2bfb5028c79b6ff26e89fc8b783b33c8a Mon Sep 17 00:00:00 2001 From: andrewmh Date: Fri, 11 Nov 2016 06:38:12 -0700 Subject: [PATCH] Fix error when using seeJson and the response type is not JSON (#16350) 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. --- .../Testing/Concerns/MakesHttpRequests.php | 8 ++-- .../FoundationMakesHttpRequestsJsonTest.php | 43 +++++++++++++++++-- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php b/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php index f600484783fa..70a893b6ee20 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php +++ b/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php @@ -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); @@ -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); } @@ -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) { diff --git a/tests/Foundation/FoundationMakesHttpRequestsJsonTest.php b/tests/Foundation/FoundationMakesHttpRequestsJsonTest.php index bc22d47065a4..9fd5d6785115 100644 --- a/tests/Foundation/FoundationMakesHttpRequestsJsonTest.php +++ b/tests/Foundation/FoundationMakesHttpRequestsJsonTest.php @@ -51,6 +51,36 @@ 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 @@ -58,17 +88,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']], ], @@ -88,3 +118,10 @@ public function jsonSerialize() ]; } } + +class NonJsonSerializableStub implements JsonSerializable +{ + public function jsonSerialize() + { + } +}