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

Skip model serialization if transformer is available #1109

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/Auth/Provider/Basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Dingo\Api\Auth\Provider;

use Illuminate\Http\Request;
use Dingo\Api\Routing\Route;
use Illuminate\Http\Request;
use Illuminate\Auth\AuthManager;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

Expand Down
2 changes: 1 addition & 1 deletion src/Auth/Provider/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use Exception;
use Tymon\JWTAuth\JWTAuth;
use Illuminate\Http\Request;
use Dingo\Api\Routing\Route;
use Illuminate\Http\Request;
use Tymon\JWTAuth\Exceptions\JWTException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

Expand Down
2 changes: 1 addition & 1 deletion src/Auth/Provider/OAuth2.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Dingo\Api\Auth\Provider;

use Exception;
use Illuminate\Http\Request;
use Dingo\Api\Routing\Route;
use Illuminate\Http\Request;
use League\OAuth2\Server\ResourceServer;
use League\OAuth2\Server\Entity\AccessTokenEntity;
use League\OAuth2\Server\Exception\OAuthException;
Expand Down
2 changes: 1 addition & 1 deletion src/Contract/Auth/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Dingo\Api\Contract\Auth;

use Illuminate\Http\Request;
use Dingo\Api\Routing\Route;
use Illuminate\Http\Request;

interface Provider
{
Expand Down
2 changes: 1 addition & 1 deletion src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use Dingo\Api\Auth\Auth;
use Dingo\Api\Routing\Router;
use Illuminate\Container\Container;
use Dingo\Api\Http\InternalRequest;
use Illuminate\Container\Container;
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Cookie;
use Dingo\Api\Exception\InternalHttpException;
Expand Down
11 changes: 8 additions & 3 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Response as IlluminateResponse;
use Illuminate\Events\Dispatcher as EventDispatcher;
use Illuminate\Database\Eloquent\Model as EloquentModel;
use Dingo\Api\Transformer\Factory as TransformerFactory;
use Illuminate\Database\Eloquent\Model as EloquentModel;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;

Expand Down Expand Up @@ -65,9 +65,8 @@ class Response extends IlluminateResponse
*/
public function __construct($content, $status = 200, $headers = [], Binding $binding = null)
{
parent::__construct($content, $status, $headers);

$this->binding = $binding;
parent::__construct($content, $status, $headers);
}

/**
Expand Down Expand Up @@ -178,6 +177,12 @@ public function setContent($content)
// case we'll simply leave the content as null and set the original
// content value and continue.
try {
if ($this->binding) {
$this->original = $this->content = $content;

return $this;
}

return parent::setContent($content);
} catch (UnexpectedValueException $exception) {
$this->original = $content;
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Response/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Illuminate\Support\Str;
use Dingo\Api\Http\Response;
use Illuminate\Support\Collection;
use Dingo\Api\Transformer\Factory as TransformerFactory;
use Illuminate\Contracts\Pagination\Paginator;
use Dingo\Api\Transformer\Factory as TransformerFactory;
use Symfony\Component\HttpKernel\Exception\HttpException;

class Factory
Expand Down
2 changes: 1 addition & 1 deletion src/Provider/HttpServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use Dingo\Api\Auth\Auth;
use Dingo\Api\Routing\Router;
use Dingo\Api\Http\Validation;
use Dingo\Api\Http\Middleware;
use Dingo\Api\Http\Validation;
use Dingo\Api\Transformer\Factory;
use Dingo\Api\Http\RequestValidator;
use Dingo\Api\Http\RateLimit\Handler;
Expand Down
2 changes: 1 addition & 1 deletion src/Routing/Adapter/Lumen.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use FastRoute\Dispatcher;
use FastRoute\RouteParser;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use FastRoute\DataGenerator;
use Illuminate\Http\Request;
use FastRoute\RouteCollector;
use Laravel\Lumen\Application;
use Dingo\Api\Contract\Routing\Adapter;
Expand Down
12 changes: 7 additions & 5 deletions src/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use Closure;
use Exception;
use RuntimeException;
use Dingo\Api\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Dingo\Api\Http\Request;
use Dingo\Api\Http\Response;
use Illuminate\Http\JsonResponse;
use Dingo\Api\Http\InternalRequest;
Expand Down Expand Up @@ -539,10 +539,12 @@ public function dispatch(Request $request)
*/
protected function prepareResponse($response, Request $request, $format)
{
if ($response instanceof IlluminateResponse) {
$response = Response::makeFromExisting($response);
} elseif ($response instanceof JsonResponse) {
$response = Response::makeFromJson($response);
if (! ($response instanceof Response)) {
if ($response instanceof IlluminateResponse) {
$response = Response::makeFromExisting($response);
} elseif ($response instanceof JsonResponse) {
$response = Response::makeFromJson($response);
}
}

if ($response instanceof Response) {
Expand Down
76 changes: 74 additions & 2 deletions tests/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Dingo\Api\Tests;

use Dingo\Api\Exception\ValidationHttpException;
use Mockery as m;
use Dingo\Api\Http;
use Dingo\Api\Auth\Auth;
Expand All @@ -16,13 +15,34 @@
use Dingo\Api\Tests\Stubs\MiddlewareStub;
use Dingo\Api\Tests\Stubs\TransformerStub;
use Dingo\Api\Tests\Stubs\RoutingAdapterStub;
use Dingo\Api\Tests\Stubs\UserTransformerStub;
use Dingo\Api\Exception\InternalHttpException;
use Dingo\Api\Tests\Stubs\UserTransformerStub;
use Dingo\Api\Exception\ValidationHttpException;
use Dingo\Api\Transformer\Factory as TransformerFactory;
use Illuminate\Support\Facades\Request as RequestFacade;

class DispatcherTest extends PHPUnit_Framework_TestCase
{
/**
* @var Container
*/
protected $container;

/**
* @var TransformerFactory
*/
protected $transformerFactory;

/**
* @var Dispatcher
*/
protected $dispatcher;

/**
* @var Router;
*/
protected $router;

public function setUp()
{
$this->container = new Container;
Expand Down Expand Up @@ -370,6 +390,58 @@ public function testUsingRequestFacadeDoesNotCacheRequestInstance()
$this->assertNull(RequestFacade::input('foo'));
}

/**
* Test model serialization with transformer binding.
*/
public function testModelSerializationWithTransformers()
{
$modelMock = $this
->getMockBuilder('\Illuminate\Database\Eloquent\Model')
->getMock();
$modelMock
->method('getTable')
->willReturn('test');

$modelMock
->expects($this->never())
->method('toJson');
$modelMock
->expects($this->never())
->method('toArray');

$transformerMock = $this
->getMockBuilder('\League\Fractal\TransformerAbstract')
->setMethods(['transform'])
->disableOriginalConstructor()
->getMock();
$transformerMock
->expects($this->once())
->method('transform')
->willReturn(['data' => 'test']);

$bindingMock = $this
->getMockBuilder('\Dingo\Api\Transformer\Binding')
->disableOriginalConstructor()
->getMock();
$bindingMock
->method('resolveTransformer')
->willReturn($transformerMock);

$this->router->version('v1', function () use ($modelMock, $bindingMock) {
$this->router->get('foo', function () use ($modelMock, $bindingMock) {
return new Http\Response($modelMock, 200, [], $bindingMock);
});
});

$this->transformerFactory->register(get_class($modelMock), $transformerMock);

$response = $this->dispatcher->raw()->get('foo');

$this->assertInstanceOf('Dingo\Api\Http\Response', $response);
$this->assertEquals('{"data":"test"}', $response->getContent());
$this->assertEquals($modelMock, $response->getOriginalContent());
}

public function testRedirectResponseThrowsException()
{
$this->router->version('v1', function () {
Expand Down
4 changes: 2 additions & 2 deletions tests/Exception/HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Dingo\Api\Tests\Exception;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Mockery as m;
use RuntimeException;
use Illuminate\Http\Response;
use PHPUnit_Framework_TestCase;
use Dingo\Api\Exception\Handler;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Dingo\Api\Http\Request as ApiRequest;
use Dingo\Api\Exception\ResourceException;
use Symfony\Component\HttpKernel\Exception\HttpException;
Expand Down
2 changes: 1 addition & 1 deletion tests/Http/Middleware/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
use Dingo\Api\Http\Validation;
use PHPUnit_Framework_TestCase;
use Dingo\Api\Exception\Handler;
use Dingo\Api\Http\RequestValidator;
use Dingo\Api\Http\Validation\Accept;
use Dingo\Api\Http\Validation\Domain;
use Dingo\Api\Http\Validation\Prefix;
use Dingo\Api\Http\RequestValidator;
use Dingo\Api\Tests\Stubs\ApplicationStub;
use Dingo\Api\Http\Parser\Accept as AcceptParser;
use Illuminate\Http\Request as IlluminateRequest;
Expand Down
36 changes: 36 additions & 0 deletions tests/Http/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,40 @@ public function testChangingResponseHeadersWithEvents()

$this->events->forget(ResponseIsMorphing::class);
}

public function testResponseConstructWithTransformerBinding()
{
$modelMock = $this
->getMockBuilder('\Illuminate\Database\Eloquent\Model')
->getMock();
$modelMock->expects($this->never())
->method('toJson')
->willReturn(['data' => 'test']);

$modelMock->expects($this->never())
->method('toArray')
->willReturn(['data' => 'test']);

$bindingMock = $this
->getMockBuilder('\Dingo\Api\Transformer\Binding')
->disableOriginalConstructor()
->getMock();

$response = new Response($modelMock, 200, [], $bindingMock);
$this->assertSame($modelMock, $response->getOriginalContent());
}

public function testResponseConstructWithOutTransformerBinding()
{
$modelMock = $this
->getMockBuilder('\Illuminate\Database\Eloquent\Model')
->getMock();
$modelMock->expects($this->once())
->method('toJson')
->willReturn('{}');

$response = new Response($modelMock, 200, []);
$this->assertSame($modelMock, $response->getOriginalContent());
$this->assertEquals('{}', $response->getContent());
}
}
5 changes: 5 additions & 0 deletions tests/Routing/Adapter/BaseAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

abstract class BaseAdapterTest extends PHPUnit_Framework_TestCase
{
/**
* @var Router
*/
protected $router;

public function setUp()
{
$this->container = $this->getContainerInstance();
Expand Down
2 changes: 1 addition & 1 deletion tests/Stubs/AuthorizationProviderStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Dingo\Api\Tests\Stubs;

use Illuminate\Http\Request;
use Dingo\Api\Routing\Route;
use Illuminate\Http\Request;
use Dingo\Api\Auth\Provider\Authorization;

class AuthorizationProviderStub extends Authorization
Expand Down
2 changes: 1 addition & 1 deletion tests/Stubs/TransformerStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Dingo\Api\Tests\Stubs;

use Dingo\Api\Http\Request;
use Illuminate\Support\Collection;
use Dingo\Api\Transformer\Binding;
use Illuminate\Support\Collection;
use Dingo\Api\Contract\Transformer\Adapter;

class TransformerStub implements Adapter
Expand Down
2 changes: 1 addition & 1 deletion tests/Transformer/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use PHPUnit_Framework_TestCase;
use Dingo\Api\Transformer\Factory;
use Illuminate\Support\Collection;
use Illuminate\Container\Container;
use Dingo\Api\Tests\Stubs\UserStub;
use Illuminate\Container\Container;
use Dingo\Api\Tests\Stubs\TransformerStub;
use Dingo\Api\Tests\Stubs\UserTransformerStub;

Expand Down