From f92d5b44b812f65e70fa5ae45ceae260018c7fb7 Mon Sep 17 00:00:00 2001 From: Thao Nguyen Date: Sun, 19 Jun 2016 17:22:53 +0700 Subject: [PATCH 1/2] Skip model serialization if transformer binding is available [ci skip] [skip ci] --- src/Http/Response.php | 9 ++- src/Routing/Router.php | 10 +-- tests/DispatcherTest.php | 76 ++++++++++++++++++++++- tests/Http/ResponseTest.php | 36 +++++++++++ tests/Routing/Adapter/BaseAdapterTest.php | 5 ++ tests/Routing/Adapter/LaravelTest.php | 1 - 6 files changed, 129 insertions(+), 8 deletions(-) diff --git a/src/Http/Response.php b/src/Http/Response.php index 40f1adaff..9dda56f08 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -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); } /** @@ -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; diff --git a/src/Routing/Router.php b/src/Routing/Router.php index 676e04bc0..a0bf8ab5c 100644 --- a/src/Routing/Router.php +++ b/src/Routing/Router.php @@ -600,10 +600,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) { diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index eb06e4083..c0befb30e 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -22,6 +22,26 @@ 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; @@ -88,7 +108,9 @@ public function testInternalRequests() public function testInternalRequestWithVersionAndParameters() { $this->router->version('v1', function () { - $this->router->get('test', function () { return 'test'; }); + $this->router->get('test', function () { + return 'test'; + }); }); $this->assertEquals('test', $this->dispatcher->version('v1')->with(['foo' => 'bar'])->get('test')); @@ -366,4 +388,56 @@ public function testUsingRequestFacadeDoesNotCacheRequestInstance() $this->assertEquals('bar', $response); $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()); + } } diff --git a/tests/Http/ResponseTest.php b/tests/Http/ResponseTest.php index 480079509..9e010760b 100644 --- a/tests/Http/ResponseTest.php +++ b/tests/Http/ResponseTest.php @@ -92,4 +92,40 @@ public function testChangingResponseHeadersWithEvents() $this->events->forget('Dingo\Api\Event\ResponseIsMorphing'); } + + 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()); + } } diff --git a/tests/Routing/Adapter/BaseAdapterTest.php b/tests/Routing/Adapter/BaseAdapterTest.php index f2a878033..30c9cf51b 100644 --- a/tests/Routing/Adapter/BaseAdapterTest.php +++ b/tests/Routing/Adapter/BaseAdapterTest.php @@ -10,6 +10,11 @@ abstract class BaseAdapterTest extends PHPUnit_Framework_TestCase { + /** + * @var Router + */ + protected $router; + public function setUp() { $this->container = $this->getContainerInstance(); diff --git a/tests/Routing/Adapter/LaravelTest.php b/tests/Routing/Adapter/LaravelTest.php index 4c94f9a6a..5e4afd4fa 100644 --- a/tests/Routing/Adapter/LaravelTest.php +++ b/tests/Routing/Adapter/LaravelTest.php @@ -6,7 +6,6 @@ use Illuminate\Events\Dispatcher; use Illuminate\Container\Container; use Dingo\Api\Routing\Adapter\Laravel; -use Illuminate\Routing\RouteCollection; class LaravelTest extends BaseAdapterTest { From 146046f7a0b1888979f2ec53e6a3a0161591e05d Mon Sep 17 00:00:00 2001 From: Thao Nguyen Date: Wed, 15 Feb 2017 09:13:45 +0000 Subject: [PATCH 2/2] Apply fixes from StyleCI --- src/Auth/Provider/Basic.php | 2 +- src/Auth/Provider/JWT.php | 2 +- src/Auth/Provider/OAuth2.php | 2 +- src/Contract/Auth/Provider.php | 2 +- src/Dispatcher.php | 2 +- src/Http/Response.php | 2 +- src/Http/Response/Factory.php | 2 +- src/Provider/HttpServiceProvider.php | 2 +- src/Routing/Adapter/Lumen.php | 2 +- src/Routing/Router.php | 2 +- tests/DispatcherTest.php | 6 +++--- tests/Exception/HandlerTest.php | 4 ++-- tests/Http/Middleware/RequestTest.php | 2 +- tests/Stubs/AuthorizationProviderStub.php | 2 +- tests/Stubs/TransformerStub.php | 2 +- tests/Transformer/FactoryTest.php | 2 +- 16 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/Auth/Provider/Basic.php b/src/Auth/Provider/Basic.php index d8c8c09e2..423d2c690 100644 --- a/src/Auth/Provider/Basic.php +++ b/src/Auth/Provider/Basic.php @@ -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; diff --git a/src/Auth/Provider/JWT.php b/src/Auth/Provider/JWT.php index fa6b17441..d45a93d04 100644 --- a/src/Auth/Provider/JWT.php +++ b/src/Auth/Provider/JWT.php @@ -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; diff --git a/src/Auth/Provider/OAuth2.php b/src/Auth/Provider/OAuth2.php index 3aec99460..aeebcd7c0 100644 --- a/src/Auth/Provider/OAuth2.php +++ b/src/Auth/Provider/OAuth2.php @@ -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; diff --git a/src/Contract/Auth/Provider.php b/src/Contract/Auth/Provider.php index 9bc5e19cd..d1fdafdda 100644 --- a/src/Contract/Auth/Provider.php +++ b/src/Contract/Auth/Provider.php @@ -2,8 +2,8 @@ namespace Dingo\Api\Contract\Auth; -use Illuminate\Http\Request; use Dingo\Api\Routing\Route; +use Illuminate\Http\Request; interface Provider { diff --git a/src/Dispatcher.php b/src/Dispatcher.php index aee347e1d..4021f2781 100644 --- a/src/Dispatcher.php +++ b/src/Dispatcher.php @@ -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; diff --git a/src/Http/Response.php b/src/Http/Response.php index 637df7dad..f67ba18e0 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -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; diff --git a/src/Http/Response/Factory.php b/src/Http/Response/Factory.php index 24b03f06c..5a3e50b3b 100644 --- a/src/Http/Response/Factory.php +++ b/src/Http/Response/Factory.php @@ -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 diff --git a/src/Provider/HttpServiceProvider.php b/src/Provider/HttpServiceProvider.php index fcf1c3e50..621403d8a 100644 --- a/src/Provider/HttpServiceProvider.php +++ b/src/Provider/HttpServiceProvider.php @@ -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; diff --git a/src/Routing/Adapter/Lumen.php b/src/Routing/Adapter/Lumen.php index df65f3929..f34775abb 100644 --- a/src/Routing/Adapter/Lumen.php +++ b/src/Routing/Adapter/Lumen.php @@ -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; diff --git a/src/Routing/Router.php b/src/Routing/Router.php index 0288d3625..45b365665 100644 --- a/src/Routing/Router.php +++ b/src/Routing/Router.php @@ -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; diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index a769dc6d1..ba2d26b03 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -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; @@ -16,8 +15,9 @@ 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; @@ -389,7 +389,7 @@ public function testUsingRequestFacadeDoesNotCacheRequestInstance() $this->assertSame('bar', $response); $this->assertNull(RequestFacade::input('foo')); } - + /** * Test model serialization with transformer binding. */ diff --git a/tests/Exception/HandlerTest.php b/tests/Exception/HandlerTest.php index 38412f238..f2459f765 100644 --- a/tests/Exception/HandlerTest.php +++ b/tests/Exception/HandlerTest.php @@ -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; diff --git a/tests/Http/Middleware/RequestTest.php b/tests/Http/Middleware/RequestTest.php index 65450dd50..fc31f8001 100644 --- a/tests/Http/Middleware/RequestTest.php +++ b/tests/Http/Middleware/RequestTest.php @@ -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; diff --git a/tests/Stubs/AuthorizationProviderStub.php b/tests/Stubs/AuthorizationProviderStub.php index a40593c68..2aff73dd8 100644 --- a/tests/Stubs/AuthorizationProviderStub.php +++ b/tests/Stubs/AuthorizationProviderStub.php @@ -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 diff --git a/tests/Stubs/TransformerStub.php b/tests/Stubs/TransformerStub.php index f042ee841..873ed3338 100644 --- a/tests/Stubs/TransformerStub.php +++ b/tests/Stubs/TransformerStub.php @@ -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 diff --git a/tests/Transformer/FactoryTest.php b/tests/Transformer/FactoryTest.php index 6ec48e631..f3ca8798a 100644 --- a/tests/Transformer/FactoryTest.php +++ b/tests/Transformer/FactoryTest.php @@ -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;