Skip to content

Commit fd5d847

Browse files
committed
Fix up raw internal requests so that transformed data is properly returned.
Signed-off-by: Jason Lewis <[email protected]>
1 parent 73d920a commit fd5d847

File tree

7 files changed

+50
-22
lines changed

7 files changed

+50
-22
lines changed

src/Dispatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ protected function dispatch(InternalRequest $request)
510510
try {
511511
$this->container['request'] = $request;
512512

513-
$response = $this->router->dispatch($request, $this->raw);
513+
$response = $this->router->dispatch($request);
514514

515515
if (! $response->isSuccessful()) {
516516
throw new InternalHttpException($response);

src/Routing/Router.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -544,11 +544,10 @@ protected function formatPrefix($new, $old)
544544
* Dispatch a request via the adapter.
545545
*
546546
* @param \Dingo\Api\Http\Request $request
547-
* @param bool $raw
548547
*
549548
* @return \Dingo\Api\Http\Response
550549
*/
551-
public function dispatch(Request $request, $raw = false)
550+
public function dispatch(Request $request)
552551
{
553552
$this->currentRoute = null;
554553

@@ -568,7 +567,7 @@ public function dispatch(Request $request, $raw = false)
568567
$response = $this->exception->handle($exception);
569568
}
570569

571-
return $this->prepareResponse($response, $request, $accept['format'], $raw);
570+
return $this->prepareResponse($response, $request, $accept['format']);
572571
}
573572

574573
/**
@@ -581,7 +580,7 @@ public function dispatch(Request $request, $raw = false)
581580
*
582581
* @return \Dingo\Api\Http\Response
583582
*/
584-
protected function prepareResponse(IlluminateResponse $response, Request $request, $format, $raw = false)
583+
protected function prepareResponse(IlluminateResponse $response, Request $request, $format)
585584
{
586585
if (! $response instanceof Response) {
587586
$response = Response::makeFromExisting($response);
@@ -597,9 +596,7 @@ protected function prepareResponse(IlluminateResponse $response, Request $reques
597596
return $this->exception->handle($exception);
598597
}
599598

600-
if (! $raw) {
601-
$response = $response->morph($format);
602-
}
599+
$response = $response->morph($format);
603600

604601
if ($response->isSuccessful() && $this->requestIsConditional()) {
605602
if (! $response->headers->has('ETag')) {

tests/DispatcherTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@
1010
use Dingo\Api\Http\Response;
1111
use Dingo\Api\Routing\Router;
1212
use PHPUnit_Framework_TestCase;
13+
use Dingo\Api\Tests\Stubs\UserStub;
1314
use Illuminate\Container\Container;
1415
use Illuminate\Filesystem\Filesystem;
1516
use Dingo\Api\Tests\Stubs\MiddlewareStub;
17+
use Dingo\Api\Tests\Stubs\TransformerStub;
1618
use Dingo\Api\Tests\Stubs\RoutingAdapterStub;
19+
use Dingo\Api\Tests\Stubs\UserTransformerStub;
1720
use Dingo\Api\Exception\InternalHttpException;
21+
use Dingo\Api\Transformer\Factory as TransformerFactory;
1822
use Illuminate\Support\Facades\Request as RequestFacade;
1923

2024
class DispatcherTest extends PHPUnit_Framework_TestCase
@@ -26,6 +30,8 @@ public function setUp()
2630
$this->container['api.auth'] = new MiddlewareStub;
2731
$this->container['api.limiting'] = new MiddlewareStub;
2832

33+
$this->transformerFactory = new TransformerFactory($this->container, new TransformerStub);
34+
2935
$this->adapter = new RoutingAdapterStub;
3036
$this->exception = m::mock('Dingo\Api\Exception\Handler');
3137
$this->router = new Router($this->adapter, new Http\Parser\Accept('api', 'v1', 'json'), $this->exception, $this->container, null, null);
@@ -38,6 +44,7 @@ public function setUp()
3844
$this->dispatcher->setDefaultFormat('json');
3945

4046
Http\Response::setFormatters(['json' => new Http\Response\Format\Json]);
47+
Http\Response::setTransformer($this->transformerFactory);
4148
}
4249

4350
public function tearDown()
@@ -321,6 +328,25 @@ public function testRequestingRawResponse()
321328
$this->assertEquals(['foo' => 'bar'], $response->getOriginalContent());
322329
}
323330

331+
public function testRequestingRawResponseWithTransformers()
332+
{
333+
$instance = null;
334+
335+
$this->router->version('v1', function () use (&$instance) {
336+
$this->router->get('foo', function () use (&$instance) {
337+
return $instance = new UserStub('Jason');
338+
});
339+
});
340+
341+
$this->transformerFactory->register(UserStub::class, UserTransformerStub::class);
342+
343+
$response = $this->dispatcher->raw()->get('foo');
344+
345+
$this->assertInstanceOf('Dingo\Api\Http\Response', $response);
346+
$this->assertEquals('{"name":"Jason"}', $response->getContent());
347+
$this->assertEquals($instance, $response->getOriginalContent());
348+
}
349+
324350
public function testUsingRequestFacadeDoesNotCacheRequestInstance()
325351
{
326352
RequestFacade::setFacadeApplication($this->container);

tests/Http/Response/FactoryTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,24 @@ public function testMakingCollectionRegistersUnderlyingClassWithTransformer()
4646
{
4747
$this->transformer->shouldReceive('register')->twice()->with('Dingo\Api\Tests\Stubs\UserStub', 'test', [], null);
4848

49-
$this->assertInstanceOf('Illuminate\Support\Collection', $this->factory->collection(new Collection([new UserStub]), 'test')->getOriginalContent());
50-
$this->assertInstanceOf('Illuminate\Support\Collection', $this->factory->withCollection(new Collection([new UserStub]), 'test')->getOriginalContent());
49+
$this->assertInstanceOf('Illuminate\Support\Collection', $this->factory->collection(new Collection([new UserStub('Jason')]), 'test')->getOriginalContent());
50+
$this->assertInstanceOf('Illuminate\Support\Collection', $this->factory->withCollection(new Collection([new UserStub('Jason')]), 'test')->getOriginalContent());
5151
}
5252

5353
public function testMakingItemsRegistersClassWithTransformer()
5454
{
5555
$this->transformer->shouldReceive('register')->twice()->with('Dingo\Api\Tests\Stubs\UserStub', 'test', [], null);
5656

57-
$this->assertInstanceOf('Dingo\Api\Tests\Stubs\UserStub', $this->factory->item(new UserStub, 'test')->getOriginalContent());
58-
$this->assertInstanceOf('Dingo\Api\Tests\Stubs\UserStub', $this->factory->withItem(new UserStub, 'test')->getOriginalContent());
57+
$this->assertInstanceOf('Dingo\Api\Tests\Stubs\UserStub', $this->factory->item(new UserStub('Jason'), 'test')->getOriginalContent());
58+
$this->assertInstanceOf('Dingo\Api\Tests\Stubs\UserStub', $this->factory->withItem(new UserStub('Jason'), 'test')->getOriginalContent());
5959
}
6060

6161
public function testMakingPaginatorRegistersUnderlyingClassWithTransformer()
6262
{
6363
$this->transformer->shouldReceive('register')->twice()->with('Dingo\Api\Tests\Stubs\UserStub', 'test', [], null);
6464

65-
$this->assertInstanceOf('Illuminate\Pagination\Paginator', $this->factory->paginator(new Paginator([new UserStub], 1), 'test')->getOriginalContent());
66-
$this->assertInstanceOf('Illuminate\Pagination\Paginator', $this->factory->withPaginator(new Paginator([new UserStub], 1), 'test')->getOriginalContent());
65+
$this->assertInstanceOf('Illuminate\Pagination\Paginator', $this->factory->paginator(new Paginator([new UserStub('Jason')], 1), 'test')->getOriginalContent());
66+
$this->assertInstanceOf('Illuminate\Pagination\Paginator', $this->factory->withPaginator(new Paginator([new UserStub('Jason')], 1), 'test')->getOriginalContent());
6767
}
6868

6969
/**

tests/Stubs/UserStub.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,10 @@
44

55
class UserStub
66
{
7-
//
7+
public $name;
8+
9+
public function __construct($name)
10+
{
11+
$this->name = $name;
12+
}
813
}

tests/Stubs/UserTransformerStub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class UserTransformerStub
77
public function transform(UserStub $user)
88
{
99
return [
10-
'name' => 'Jason'
10+
'name' => $user->name
1111
];
1212
}
1313
}

tests/Transformer/FactoryTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ public function tearDown()
2929

3030
public function testResponseIsTransformable()
3131
{
32-
$this->assertFalse($this->factory->transformableResponse(new UserStub, new UserTransformerStub));
32+
$this->assertFalse($this->factory->transformableResponse(new UserStub('Jason'), new UserTransformerStub));
3333

3434
$this->factory->register('Dingo\Api\Tests\Stubs\UserStub', new UserTransformerStub);
3535

36-
$this->assertTrue($this->factory->transformableResponse(new UserStub, new UserTransformerStub));
36+
$this->assertTrue($this->factory->transformableResponse(new UserStub('Jason'), new UserTransformerStub));
3737
}
3838

3939
public function testRegisterParameterOrder()
@@ -70,7 +70,7 @@ public function testTransformingResponse()
7070
{
7171
$this->factory->register('Dingo\Api\Tests\Stubs\UserStub', new UserTransformerStub);
7272

73-
$response = $this->factory->transform(new UserStub);
73+
$response = $this->factory->transform(new UserStub('Jason'));
7474

7575
$this->assertEquals(['name' => 'Jason'], $response);
7676
}
@@ -79,9 +79,9 @@ public function testTransformingCollectionResponse()
7979
{
8080
$this->factory->register('Dingo\Api\Tests\Stubs\UserStub', new UserTransformerStub);
8181

82-
$response = $this->factory->transform(new Collection([new UserStub, new UserStub]));
82+
$response = $this->factory->transform(new Collection([new UserStub('Jason'), new UserStub('Bob')]));
8383

84-
$this->assertEquals([['name' => 'Jason'], ['name' => 'Jason']], $response);
84+
$this->assertEquals([['name' => 'Jason'], ['name' => 'Bob']], $response);
8585
}
8686

8787
/**
@@ -90,6 +90,6 @@ public function testTransformingCollectionResponse()
9090
*/
9191
public function testTransformingWithNoTransformerThrowsException()
9292
{
93-
$this->factory->transform(new UserStub);
93+
$this->factory->transform(new UserStub('Jason'));
9494
}
9595
}

0 commit comments

Comments
 (0)