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 5ce167bcf..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; @@ -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/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 dce9fe7c3..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; @@ -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) { diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index 97ec05ce6..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,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; @@ -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 () { 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/Http/ResponseTest.php b/tests/Http/ResponseTest.php index 89ec3abd6..b707b6493 100644 --- a/tests/Http/ResponseTest.php +++ b/tests/Http/ResponseTest.php @@ -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()); + } } diff --git a/tests/Routing/Adapter/BaseAdapterTest.php b/tests/Routing/Adapter/BaseAdapterTest.php index 32fd805f0..5124a85e2 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/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;