Skip to content
This repository has been archived by the owner on Sep 22, 2024. It is now read-only.

Commit

Permalink
Use DefaultResponse as fallback response
Browse files Browse the repository at this point in the history
  • Loading branch information
pactode committed May 28, 2019
1 parent 41ee624 commit d0e25fe
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 43 deletions.
14 changes: 3 additions & 11 deletions src/Responses/CollectionResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,18 @@ class CollectionResponse extends Response
/** @var string */
protected $resourceClass;

/** @var int */
protected $statusCode = 200;

public function __construct(Collection $collection, ?string $resourceClass = null)
{
$this->collection = $collection;
$this->resourceClass = $resourceClass;
}

public function setStatusCode(int $statusCode): self
{
$this->statusCode = $statusCode;

return $this;
}

public function toResponse($request)
{
if (empty($this->resourceClass)) {
return $this->collection;
return (new DefaultResponse($this->collection))
->setStatusCode($this->statusCode)
->toResponse($request);
}

return $this->resourceClass::collection($this->collection)
Expand Down
10 changes: 0 additions & 10 deletions src/Responses/DefaultResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,11 @@ class DefaultResponse extends Response
/** @var mixed */
protected $data;

/** @var int */
protected $statusCode = 200;

public function __construct($data)
{
$this->data = $data;
}

public function setStatusCode(int $statusCode): self
{
$this->statusCode = $statusCode;

return $this;
}

public function toResponse($request)
{
return new JsonResponse($this->data, $this->statusCode);
Expand Down
7 changes: 0 additions & 7 deletions src/Responses/ModelResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ public function __construct(Model $model, ?string $resourceClass = null)
$this->resourceClass = $resourceClass;
}

public function setStatusCode(int $statusCode): self
{
$this->statusCode = $statusCode;

return $this;
}

public function toResponse($request)
{
// Respond appropriately to a deleted model
Expand Down
14 changes: 3 additions & 11 deletions src/Responses/PaginatorResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ class PaginatorResponse extends Response
/** @var string */
protected $resourceClass;

/** @var int */
protected $statusCode = 200;

public function __construct(
LengthAwarePaginator $paginator,
?string $resourceClass = null
Expand All @@ -23,17 +20,12 @@ public function __construct(
$this->resourceClass = $resourceClass;
}

public function setStatusCode(int $statusCode): self
{
$this->statusCode = $statusCode;

return $this;
}

public function toResponse($request)
{
if (empty($this->resourceClass)) {
return $this->paginator;
return (new DefaultResponse($this->paginator))
->setStatusCode($this->statusCode)
->toResponse($request);
}

return $this->resourceClass::collection($this->paginator)
Expand Down
9 changes: 9 additions & 0 deletions src/Responses/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,13 @@

abstract class Response implements Responsable
{
/** @var int */
protected $statusCode = 200;

public function setStatusCode(int $statusCode): self
{
$this->statusCode = $statusCode;

return $this;
}
}
21 changes: 19 additions & 2 deletions tests/Responses/CollectionResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,32 @@ public function it_returns_a_json_response_if_a_resource_is_provided()
$response = (new CollectionResponse($products, ProductResource::class))->toResponse(null);

$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertContainsOnlyInstancesOf(ProductResource::class, $response->original);
}

/** @test */
public function it_returns_the_collection_if_no_resource_is_provided()
public function it_returns_a_json_response_if_no_resource_is_provided()
{
$products = Product::all();

$response = (new CollectionResponse($products))->toResponse(null);

$this->assertInstanceOf(Collection::class, $response);
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertEquals(200, $response->status());
$this->assertInstanceOf(Collection::class, $response->original);
$this->assertContainsOnlyInstancesOf(Product::class, $response->original);
}

/** @test */
public function it_can_set_the_status_code()
{
$products = Product::all();

$response = (new CollectionResponse($products))
->setStatusCode(204)
->toResponse(null);

$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertEquals(204, $response->status());
}
}
7 changes: 5 additions & 2 deletions tests/Responses/PaginatorResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Signifly\Responder\Tests\Responses;

use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;
use Signifly\Responder\Tests\TestCase;
use Signifly\Responder\Tests\Models\Product;
use Signifly\Responder\Responses\PaginatorResponse;
Expand All @@ -19,15 +20,17 @@ public function it_returns_a_json_response_if_a_resource_is_provided()
$response = (new PaginatorResponse($products, ProductResource::class))->toResponse(null);

$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertInstanceOf(Collection::class, $response->original);
}

/** @test */
public function it_returns_a_model_if_no_resource_is_provided()
public function it_returns_a_json_response_if_no_resource_is_provided()
{
$products = Product::paginate(5);

$response = (new PaginatorResponse($products))->toResponse(null);

$this->assertInstanceOf(LengthAwarePaginator::class, $response);
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertInstanceOf(LengthAwarePaginator::class, $response->original);
}
}

0 comments on commit d0e25fe

Please sign in to comment.