diff --git a/src/Responses/CollectionResponse.php b/src/Responses/CollectionResponse.php index d26718a..f3559c4 100644 --- a/src/Responses/CollectionResponse.php +++ b/src/Responses/CollectionResponse.php @@ -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) diff --git a/src/Responses/DefaultResponse.php b/src/Responses/DefaultResponse.php index 37272e4..6af8206 100644 --- a/src/Responses/DefaultResponse.php +++ b/src/Responses/DefaultResponse.php @@ -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); diff --git a/src/Responses/ModelResponse.php b/src/Responses/ModelResponse.php index 480c0ce..e036155 100644 --- a/src/Responses/ModelResponse.php +++ b/src/Responses/ModelResponse.php @@ -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 diff --git a/src/Responses/PaginatorResponse.php b/src/Responses/PaginatorResponse.php index 4072788..e741487 100644 --- a/src/Responses/PaginatorResponse.php +++ b/src/Responses/PaginatorResponse.php @@ -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 @@ -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) diff --git a/src/Responses/Response.php b/src/Responses/Response.php index b0c4abe..3a9a5c2 100644 --- a/src/Responses/Response.php +++ b/src/Responses/Response.php @@ -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; + } } diff --git a/tests/Responses/CollectionResponseTest.php b/tests/Responses/CollectionResponseTest.php index 5ee5786..1bacd52 100644 --- a/tests/Responses/CollectionResponseTest.php +++ b/tests/Responses/CollectionResponseTest.php @@ -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()); } } diff --git a/tests/Responses/PaginatorResponseTest.php b/tests/Responses/PaginatorResponseTest.php index a19d448..3062ae9 100644 --- a/tests/Responses/PaginatorResponseTest.php +++ b/tests/Responses/PaginatorResponseTest.php @@ -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; @@ -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); } }