From 34e2c3ceb34c9b3b21b2cd820d24c89e084d844c Mon Sep 17 00:00:00 2001 From: Martin Bastien Date: Tue, 13 Oct 2015 12:56:13 -0400 Subject: [PATCH 1/2] Add attributes for request and response Signed-off-by: Martin Bastien --- src/Annotation/Request.php | 5 +++++ src/Annotation/Response.php | 5 +++++ src/Blueprint.php | 17 +++++++++++++---- tests/BlueprintTest.php | 20 ++++++++++++++++++++ tests/Stubs/UserPhotosResourceStub.php | 4 ++++ tests/Stubs/UsersResourceStub.php | 4 ++++ 6 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/Annotation/Request.php b/src/Annotation/Request.php index 8184920..0eade41 100644 --- a/src/Annotation/Request.php +++ b/src/Annotation/Request.php @@ -26,4 +26,9 @@ class Request * @var array */ public $headers = []; + + /** + * @var array + */ + public $attributes; } diff --git a/src/Annotation/Response.php b/src/Annotation/Response.php index 5e8be8a..c677277 100644 --- a/src/Annotation/Response.php +++ b/src/Annotation/Response.php @@ -26,4 +26,9 @@ class Response * @var array */ public $headers = []; + + /** + * @var array + */ + public $attributes; } diff --git a/src/Blueprint.php b/src/Blueprint.php index a790c68..be986b0 100644 --- a/src/Blueprint.php +++ b/src/Blueprint.php @@ -170,16 +170,17 @@ protected function generateContentsFromResources(Collection $resources, $name) * * @param string $contents * @param \Illuminate\Support\Collection $attributes + * @param int $indent * * @return void */ - protected function appendAttributes(&$contents, Collection $attributes) + protected function appendAttributes(&$contents, Collection $attributes, $indent = 0) { - $this->appendSection($contents, 'Attributes'); + $this->appendSection($contents, 'Attributes', $indent); - $attributes->each(function ($attribute) use (&$contents) { + $attributes->each(function ($attribute) use (&$contents, $indent) { $contents .= $this->line(); - $contents .= $this->tab(); + $contents .= $this->tab(1 + $indent); $contents .= sprintf('+ %s', $attribute->identifier); if ($attribute->sample) { @@ -251,6 +252,10 @@ protected function appendResponse(&$contents, Annotation\Response $response) $this->appendHeaders($contents, $request->headers); } + if (isset($response->attributes)) { + $this->appendAttributes($contents, collect($response->attributes), 1); + } + if (isset($response->body)) { $this->appendBody($contents, $this->prepareBody($response->body, $response->contentType)); } @@ -278,6 +283,10 @@ protected function appendRequest(&$contents, $request) $this->appendHeaders($contents, $request->headers); } + if (isset($request->attributes)) { + $this->appendAttributes($contents, collect($request->attributes), 1); + } + if (isset($request->body)) { $this->appendBody($contents, $this->prepareBody($request->body, $request->contentType)); } diff --git a/tests/BlueprintTest.php b/tests/BlueprintTest.php index 2cd6e00..6521a0a 100644 --- a/tests/BlueprintTest.php +++ b/tests/BlueprintTest.php @@ -70,6 +70,11 @@ public function testGeneratingBlueprintForSingleResource() Create a new user. + Request (application/json) + + + Attributes + + name: jason (string, required) - The user name + + email: jason@jason.com (string, required) - The user email + + password: 1234567 (string, required) - The user password + Body { @@ -164,6 +169,11 @@ public function testGeneratingBlueprintForMultipleResourcesWithVersionOne() Create a new user. + Request (application/json) + + + Attributes + + name: jason (string, required) - The user name + + email: jason@jason.com (string, required) - The user email + + password: 1234567 (string, required) - The user password + Body { @@ -318,6 +328,11 @@ public function testGeneratingBlueprintForMultipleResourcesWithVersionTwo() Create a new user. + Request (application/json) + + + Attributes + + name: jason (string, required) - The user name + + email: jason@jason.com (string, required) - The user email + + password: 1234567 (string, required) - The user password + Body { @@ -383,6 +398,11 @@ public function testGeneratingBlueprintForMultipleResourcesWithVersionTwo() + photoId (integer, required) - ID of photo to show. + Response 200 (application/json) + + + Attributes + + id: 1 (number, optional) - The photo id + + name: photo (string, optional) - The photo name + + src: path/to/cool/photo.jpg (string, optional) - The photo path + Body { diff --git a/tests/Stubs/UserPhotosResourceStub.php b/tests/Stubs/UserPhotosResourceStub.php index 0051a7a..2b9c014 100644 --- a/tests/Stubs/UserPhotosResourceStub.php +++ b/tests/Stubs/UserPhotosResourceStub.php @@ -50,6 +50,10 @@ public function index($userId) * "id": 1, * "name": "photo", * "src": "path/to/cool/photo.jpg" + * }, attributes={ + * @Attribute("id", type="number", description="The photo id", sample="1"), + * @Attribute("name", type="string", description="The photo name", sample="photo"), + * @Attribute("src", type="string", description="The photo path", sample="path/to/cool/photo.jpg") * }), * @Response(404, body={"message": "Photo could not be found."}) * }) diff --git a/tests/Stubs/UsersResourceStub.php b/tests/Stubs/UsersResourceStub.php index f32311b..648d25f 100644 --- a/tests/Stubs/UsersResourceStub.php +++ b/tests/Stubs/UsersResourceStub.php @@ -56,6 +56,10 @@ public function show($id) * "name": "jason", * "email": "jason@jason.com", * "password": "1234567" + * }, attributes={ + * @Attribute("name", type="string", description="The user name", sample="jason", required=true), + * @Attribute("email", type="string", description="The user email", sample="jason@jason.com", required=true), + * @Attribute("password", type="string", description="The user password", sample="1234567", required=true) * }), * @Response(200, body={"id": 10, "name": "jason", "email": "jason@jason.com"}), * @Response(422, body={ From 613b6b4d2ba59d509b3cecb6785fd8bfc09925b6 Mon Sep 17 00:00:00 2001 From: Martin Bastien Date: Tue, 13 Oct 2015 14:27:18 -0400 Subject: [PATCH 2/2] fix test --- tests/Stubs/UsersResourceStub.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Stubs/UsersResourceStub.php b/tests/Stubs/UsersResourceStub.php index 648d25f..a939037 100644 --- a/tests/Stubs/UsersResourceStub.php +++ b/tests/Stubs/UsersResourceStub.php @@ -52,7 +52,7 @@ public function show($id) * * @Post("/") * @Transaction({ - * @Request({ + * @Request(body={ * "name": "jason", * "email": "jason@jason.com", * "password": "1234567"