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.5] Ensure Arrayable objects are also morphed by the Response. #17868

Merged
merged 1 commit into from
Feb 10, 2017
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
6 changes: 5 additions & 1 deletion src/Illuminate/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ArrayObject;
use JsonSerializable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Renderable;
use Symfony\Component\HttpFoundation\Response as BaseResponse;

Expand Down Expand Up @@ -49,7 +50,8 @@ public function setContent($content)
*/
protected function shouldBeJson($content)
{
return $content instanceof Jsonable ||
return $content instanceof Arrayable ||
$content instanceof Jsonable ||
$content instanceof ArrayObject ||
$content instanceof JsonSerializable ||
is_array($content);
Expand All @@ -65,6 +67,8 @@ protected function morphToJson($content)
{
if ($content instanceof Jsonable) {
return $content->toJson();
} elseif ($content instanceof Arrayable) {
return json_encode($content->toArray());
}

return json_encode($content);
Expand Down
33 changes: 32 additions & 1 deletion tests/Http/HttpResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace Illuminate\Tests\Http;

use Mockery as m;
use JsonSerializable;
use Illuminate\Http\Request;
use PHPUnit\Framework\TestCase;
use Illuminate\Http\RedirectResponse;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Arrayable;

class HttpResponseTest extends TestCase
{
Expand All @@ -17,10 +19,18 @@ public function tearDown()

public function testJsonResponsesAreConvertedAndHeadersAreSet()
{
$response = new \Illuminate\Http\Response(new ArrayableStub);
$this->assertEquals('{"foo":"bar"}', $response->getContent());
$this->assertEquals('application/json', $response->headers->get('Content-Type'));

$response = new \Illuminate\Http\Response(new JsonableStub);
$this->assertEquals('foo', $response->getContent());
$this->assertEquals('application/json', $response->headers->get('Content-Type'));

$response = new \Illuminate\Http\Response(new ArrayableAndJsonableStub);
$this->assertEquals('{"foo":"bar"}', $response->getContent());
$this->assertEquals('application/json', $response->headers->get('Content-Type'));

$response = new \Illuminate\Http\Response();
$response->setContent(['foo' => 'bar']);
$this->assertEquals('{"foo":"bar"}', $response->getContent());
Expand Down Expand Up @@ -151,6 +161,27 @@ public function testMagicCallException()
}
}

class ArrayableStub implements Arrayable
{
public function toArray()
{
return ['foo' => 'bar'];
}
}

class ArrayableAndJsonableStub implements Arrayable, Jsonable
{
public function toJson($options = 0)
{
return '{"foo":"bar"}';
}

public function toArray()
{
return [];
}
}

class JsonableStub implements Jsonable
{
public function toJson($options = 0)
Expand All @@ -159,7 +190,7 @@ public function toJson($options = 0)
}
}

class JsonSerializableStub implements \JsonSerializable
class JsonSerializableStub implements JsonSerializable
{
public function jsonSerialize()
{
Expand Down