shalvah 5 anni fa
parent
commit
f0f2aea049

+ 31 - 0
tests/Fixtures/TestController.php

@@ -136,6 +136,37 @@ class TestController extends Controller
         return '';
     }
 
+    /**
+     * @apiResource \Mpociot\ApiDoc\Tests\Fixtures\TestUserApiResource
+     * @apiResourceModel \Mpociot\ApiDoc\Tests\Fixtures\TestUser
+     */
+    public function withEloquentApiResource()
+    {
+        return new TestUserApiResource(factory(TestUser::class)->make(['id' => 0]));
+    }
+
+    /**
+     * @group Other😎
+     *
+     * @apiResourceCollection Mpociot\ApiDoc\Tests\Fixtures\TestUserApiResource
+     * @apiResourceModel Mpociot\ApiDoc\Tests\Fixtures\TestUser
+     */
+    public function withEloquentApiResourceCollection()
+    {
+        return TestUserApiResource::collection(factory(TestUser::class)->make(['id' => 0]));
+    }
+
+    /**
+     * @group Other😎
+     *
+     * @apiResourceCollection Mpociot\ApiDoc\Tests\Fixtures\TestUserApiResourceCollection
+     * @apiResourceModel Mpociot\ApiDoc\Tests\Fixtures\TestUser
+     */
+    public function withEloquentApiResourceCollectionClass()
+    {
+        return new TestUserApiResourceCollection(factory(TestUser::class)->make(['id' => 0]));
+    }
+
     public function checkCustomHeaders(Request $request)
     {
         return $request->headers->all();

+ 7 - 0
tests/Fixtures/TestUser.php

@@ -0,0 +1,7 @@
+<?php
+
+namespace Mpociot\ApiDoc\Tests\Fixtures;
+
+use Illuminate\Database\Eloquent\Model;
+
+class TestUser extends Model {}

+ 24 - 0
tests/Fixtures/TestUserApiResource.php

@@ -0,0 +1,24 @@
+<?php
+
+namespace Mpociot\ApiDoc\Tests\Fixtures;
+
+use Illuminate\Http\Resources\Json\JsonResource;
+use Illuminate\Http\Resources\Json\ResourceCollection;
+
+class TestUserApiResource extends JsonResource
+{
+    /**
+     * Transform the resource into an array.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @return array
+     */
+    public function toArray($request)
+    {
+        return [
+            'id' => $this->id,
+            'name' => $this->first_name." ".$this->last_name,
+            'email' => $this->email,
+        ];
+    }
+}

+ 25 - 0
tests/Fixtures/TestUserApiResourceCollection.php

@@ -0,0 +1,25 @@
+<?php
+
+namespace Mpociot\ApiDoc\Tests\Fixtures;
+
+use Illuminate\Http\Resources\Json\ResourceCollection;
+
+class TestUserApiResourceCollection extends ResourceCollection
+{
+    /**
+     * Transform the resource into an array.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @return array
+     */
+    public function toArray($request)
+    {
+        return [
+            'data' => $this->collection,
+            'links' => [
+                'self' => 'link-value',
+            ],
+        ];
+    }
+}
+

+ 87 - 0
tests/Fixtures/collection.json

@@ -231,6 +231,33 @@
                         "response": []
                     }
                 },
+                {
+                    "name": "http:\/\/localhost\/api\/withEloquentApiResource",
+                    "request": {
+                        "url": "http:\/\/localhost\/api\/withEloquentApiResource",
+                        "method": "GET",
+                        "header": [
+                            {
+                                "key": "Authorization",
+                                "value": "customAuthToken"
+                            },
+                            {
+                                "key": "Custom-Header",
+                                "value": "NotSoCustom"
+                            },
+                            {
+                                "key": "Accept",
+                                "value": "application\/json"
+                            }
+                        ],
+                        "body": {
+                            "mode": "formdata",
+                            "formdata": []
+                        },
+                        "description": "",
+                        "response": []
+                    }
+                },
                 {
                     "name": "http:\/\/localhost\/api\/withMultipleResponseTagsAndStatusCode",
                     "request": {
@@ -259,6 +286,66 @@
                     }
                 }
             ]
+        },
+        {
+            "name": "Other\ud83d\ude0e",
+            "description": "",
+            "item": [
+                {
+                    "name": "http:\/\/localhost\/api\/withEloquentApiResourceCollectionClass",
+                    "request": {
+                        "url": "http:\/\/localhost\/api\/withEloquentApiResourceCollectionClass",
+                        "method": "GET",
+                        "header": [
+                            {
+                                "key": "Authorization",
+                                "value": "customAuthToken"
+                            },
+                            {
+                                "key": "Custom-Header",
+                                "value": "NotSoCustom"
+                            },
+                            {
+                                "key": "Accept",
+                                "value": "application\/json"
+                            }
+                        ],
+                        "body": {
+                            "mode": "formdata",
+                            "formdata": []
+                        },
+                        "description": "",
+                        "response": []
+                    }
+                },
+                {
+                    "name": "http:\/\/localhost\/api\/echoesUrlParameters\/{param}-{param2}\/{param3?}",
+                    "request": {
+                        "url": "http:\/\/localhost\/api\/echoesUrlParameters\/{param}-{param2}\/{param3?}?something=consequatur",
+                        "method": "GET",
+                        "header": [
+                            {
+                                "key": "Authorization",
+                                "value": "customAuthToken"
+                            },
+                            {
+                                "key": "Custom-Header",
+                                "value": "NotSoCustom"
+                            },
+                            {
+                                "key": "Accept",
+                                "value": "application\/json"
+                            }
+                        ],
+                        "body": {
+                            "mode": "formdata",
+                            "formdata": []
+                        },
+                        "description": "",
+                        "response": []
+                    }
+                }
+            ]
         }
     ]
 }

+ 117 - 0
tests/Fixtures/index.md

@@ -323,6 +323,56 @@ null
 
 <!-- END_5c08cc4d72b6e5830f6814c64086e197 -->
 
+<!-- START_16ec1c69d4877579438d48a8ad8dc778 -->
+## api/withEloquentApiResource
+> Example request:
+
+```bash
+curl -X GET \
+    -G "http://localhost/api/withEloquentApiResource" \
+    -H "Authorization: customAuthToken" \
+    -H "Custom-Header: NotSoCustom"
+```
+
+```javascript
+const url = new URL(
+    "http://localhost/api/withEloquentApiResource"
+);
+
+let headers = {
+    "Authorization": "customAuthToken",
+    "Custom-Header": "NotSoCustom",
+    "Accept": "application/json",
+    "Content-Type": "application/json",
+};
+
+fetch(url, {
+    method: "GET",
+    headers: headers,
+})
+    .then(response => response.json())
+    .then(json => console.log(json));
+```
+
+
+> Example response (200):
+
+```json
+{
+    "data": {
+        "id": 0,
+        "name": "Tested Again",
+        "email": "a@b.com"
+    }
+}
+```
+
+### HTTP Request
+`GET api/withEloquentApiResource`
+
+
+<!-- END_16ec1c69d4877579438d48a8ad8dc778 -->
+
 <!-- START_55f321056bfc0de7269ac70e24eb84be -->
 ## api/withMultipleResponseTagsAndStatusCode
 > Example request:
@@ -384,6 +434,73 @@ fetch(url, {
 #Other😎
 
 
+<!-- START_c41db6ac2b427d8c29802195746cd91e -->
+## api/withEloquentApiResourceCollectionClass
+> Example request:
+
+```bash
+curl -X GET \
+    -G "http://localhost/api/withEloquentApiResourceCollectionClass" \
+    -H "Authorization: customAuthToken" \
+    -H "Custom-Header: NotSoCustom"
+```
+
+```javascript
+const url = new URL(
+    "http://localhost/api/withEloquentApiResourceCollectionClass"
+);
+
+let headers = {
+    "Authorization": "customAuthToken",
+    "Custom-Header": "NotSoCustom",
+    "Accept": "application/json",
+    "Content-Type": "application/json",
+};
+
+fetch(url, {
+    method: "GET",
+    headers: headers,
+})
+    .then(response => response.json())
+    .then(json => console.log(json));
+```
+
+
+> Example response (200):
+
+```json
+{
+    "data": [
+        {
+            "id": 4,
+            "name": "Tested Again",
+            "email": "a@b.com"
+        },
+        {
+            "id": 4,
+            "name": "Tested Again",
+            "email": "a@b.com"
+        }
+    ],
+    "links": {
+        "self": "link-value"
+    }
+}
+```
+> Example response (500):
+
+```json
+{
+    "message": "Server Error"
+}
+```
+
+### HTTP Request
+`GET api/withEloquentApiResourceCollectionClass`
+
+
+<!-- END_c41db6ac2b427d8c29802195746cd91e -->
+
 <!-- START_33e62c07bc6d7286628b18c0e046ebea -->
 ## api/echoesUrlParameters/{param}-{param2}/{param3?}
 > Example request:

+ 24 - 3
tests/GenerateDocumentationTest.php

@@ -2,6 +2,7 @@
 
 namespace Mpociot\ApiDoc\Tests;
 
+use Mpociot\ApiDoc\Tests\Fixtures\TestUser;
 use ReflectionException;
 use Illuminate\Support\Str;
 use Mpociot\ApiDoc\Tools\Utils;
@@ -19,6 +20,22 @@ class GenerateDocumentationTest extends TestCase
 {
     use TestHelpers;
 
+    protected function setUp(): void
+    {
+        parent::setUp();
+
+
+        $factory = app(\Illuminate\Database\Eloquent\Factory::class);
+        $factory->define(TestUser::class, function () {
+            return [
+                'id' => 4,
+                'first_name' => "Tested",
+                'last_name' => "Again",
+                'email' => "a@b.com",
+            ];
+        });
+    }
+
     public function tearDown(): void
     {
         Utils::deleteDirectoryAndContents('/public/docs');
@@ -168,6 +185,8 @@ class GenerateDocumentationTest extends TestCase
         RouteFacade::get('/api/withBodyParameters', TestController::class.'@withBodyParameters');
         RouteFacade::get('/api/withQueryParameters', TestController::class.'@withQueryParameters');
         RouteFacade::get('/api/withAuthTag', TestController::class.'@withAuthenticatedTag');
+        RouteFacade::get('/api/withEloquentApiResource', [TestController::class, 'withEloquentApiResource']);
+        RouteFacade::get('/api/withEloquentApiResourceCollectionClass', [TestController::class, 'withEloquentApiResourceCollectionClass']);
         RouteFacade::post('/api/withMultipleResponseTagsAndStatusCode', [TestController::class, 'withMultipleResponseTagsAndStatusCode']);
         RouteFacade::get('/api/echoesUrlParameters/{param}-{param2}/{param3?}', [TestController::class, 'echoesUrlParameters']);
 
@@ -180,7 +199,7 @@ class GenerateDocumentationTest extends TestCase
                 'Custom-Header' => 'NotSoCustom',
             ],
         ]);
-        $this->artisan('apidoc:generate');
+        $this->artisan('apidoc:generate -v');
 
         $generatedMarkdown = __DIR__.'/../public/docs/source/index.md';
         $compareMarkdown = __DIR__.'/../public/docs/source/.compare.md';
@@ -213,13 +232,15 @@ class GenerateDocumentationTest extends TestCase
 
     /** @test */
     public function generated_postman_collection_file_is_correct()
-    {
-        RouteFacade::get('/api/withDescription', TestController::class.'@withEndpointDescription');
+    {        RouteFacade::get('/api/withDescription', [TestController::class, 'withEndpointDescription']);
         RouteFacade::get('/api/withResponseTag', TestController::class.'@withResponseTag');
         RouteFacade::get('/api/withBodyParameters', TestController::class.'@withBodyParameters');
         RouteFacade::get('/api/withQueryParameters', TestController::class.'@withQueryParameters');
         RouteFacade::get('/api/withAuthTag', TestController::class.'@withAuthenticatedTag');
+        RouteFacade::get('/api/withEloquentApiResource', [TestController::class, 'withEloquentApiResource']);
+        RouteFacade::get('/api/withEloquentApiResourceCollectionClass', [TestController::class, 'withEloquentApiResourceCollectionClass']);
         RouteFacade::post('/api/withMultipleResponseTagsAndStatusCode', [TestController::class, 'withMultipleResponseTagsAndStatusCode']);
+        RouteFacade::get('/api/echoesUrlParameters/{param}-{param2}/{param3?}', [TestController::class, 'echoesUrlParameters']);
 
         // We want to have the same values for params each time
         config(['apidoc.faker_seed' => 1234]);

+ 107 - 6
tests/Unit/GeneratorTestCase.php

@@ -5,6 +5,7 @@
 namespace Mpociot\ApiDoc\Tests\Unit;
 
 use Illuminate\Support\Arr;
+use Mpociot\ApiDoc\Tests\Fixtures\TestUser;
 use Orchestra\Testbench\TestCase;
 use Mpociot\ApiDoc\Tools\Generator;
 use Mpociot\ApiDoc\Tools\DocumentationConfig;
@@ -32,6 +33,7 @@ abstract class GeneratorTestCase extends TestCase
             'responses' => [
                 \Mpociot\ApiDoc\Strategies\Responses\UseResponseTag::class,
                 \Mpociot\ApiDoc\Strategies\Responses\UseResponseFileTag::class,
+                \Mpociot\ApiDoc\Strategies\Responses\UseApiResourceTags::class,
                 \Mpociot\ApiDoc\Strategies\Responses\UseTransformerTags::class,
                 \Mpociot\ApiDoc\Strategies\Responses\ResponseCalls::class,
             ],
@@ -54,6 +56,15 @@ abstract class GeneratorTestCase extends TestCase
     {
         parent::setUp();
 
+        $factory = app(\Illuminate\Database\Eloquent\Factory::class);
+        $factory->define(TestUser::class, function () {
+            return [
+                'id' => 4,
+                'first_name' => "Tested",
+                'last_name' => "Again",
+                'email' => "a@b.com",
+            ];
+        });
         $this->generator = new Generator(new DocumentationConfig($this->config));
     }
 
@@ -378,6 +389,96 @@ abstract class GeneratorTestCase extends TestCase
         $this->assertSame(['DELETE'], $parsed['methods']);
     }
 
+    /** @test */
+    public function can_parse_apiresource_tags()
+    {
+        $route = $this->createRoute('POST', '/withEloquentApiResource', 'withEloquentApiResource');
+
+        $config = $this->config;
+        $config['strategies']['responses'] = [\Mpociot\ApiDoc\Strategies\Responses\UseApiResourceTags::class,];
+        $generator = new Generator(new DocumentationConfig($config));
+        $parsed = $this->generator->processRoute($route);
+
+        $response = Arr::first($parsed['response']);
+        $this->assertTrue(is_array($parsed));
+        $this->assertArrayHasKey('showresponse', $parsed);
+        $this->assertTrue($parsed['showresponse']);
+        $this->assertTrue(is_array($response));
+        $this->assertEquals(200, $response['status']);
+        $this->assertArraySubset([
+            'id' => 4,
+            'name' => "Tested Again",
+            'email' => "a@b.com",
+        ], json_decode($response['content'], true));
+    }
+
+    /** @test */
+    public function can_parse_apiresourcecollection_tags()
+    {
+        $route = $this->createRoute('POST', '/withEloquentApiResourceCollection', 'withEloquentApiResourceCollection');
+
+        $config = $this->config;
+        $config['strategies']['responses'] = [\Mpociot\ApiDoc\Strategies\Responses\UseApiResourceTags::class,];
+        $generator = new Generator(new DocumentationConfig($config));
+        $parsed = $this->generator->processRoute($route);
+
+        $response = Arr::first($parsed['response']);
+        $this->assertTrue(is_array($parsed));
+        $this->assertArrayHasKey('showresponse', $parsed);
+        $this->assertTrue($parsed['showresponse']);
+        $this->assertTrue(is_array($response));
+        $this->assertEquals(200, $response['status']);
+        $content = json_decode($response['content'], true);
+        $this->assertIsArray($content);
+        $this->assertArraySubset([
+            'id' => 4,
+            'name' => "Tested Again",
+            'email' => "a@b.com",
+        ], $content[0]);
+        $this->assertArraySubset([
+            'id' => 4,
+            'name' => "Tested Again",
+            'email' => "a@b.com",
+        ], $content[1]);
+    }
+
+    /** @test */
+    public function can_parse_apiresourcecollection_tags_with_collection_class()
+    {
+        $route = $this->createRoute('POST', '/withEloquentApiResourceCollectionClass', 'withEloquentApiResourceCollectionClass');
+
+        $config = $this->config;
+        $config['strategies']['responses'] = [\Mpociot\ApiDoc\Strategies\Responses\UseApiResourceTags::class,];
+        $generator = new Generator(new DocumentationConfig($config));
+        $parsed = $this->generator->processRoute($route);
+
+        $response = Arr::first($parsed['response']);
+        $this->assertTrue(is_array($parsed));
+        $this->assertArrayHasKey('showresponse', $parsed);
+        $this->assertTrue($parsed['showresponse']);
+        $this->assertTrue(is_array($response));
+        $this->assertEquals(200, $response['status']);
+        $content = json_decode($response['content'], true);
+        $this->assertIsArray($content);
+        $this->assertArraySubset([
+                "data" => [
+                    [
+                        'id' => 4,
+                        'name' => "Tested Again",
+                        'email' => "a@b.com",
+                    ],
+                    [
+                        'id' => 4,
+                        'name' => "Tested Again",
+                        'email' => "a@b.com",
+                    ],
+                ],
+                "links" => [
+                    "self" => "link-value",
+                ],
+        ], $content);
+    }
+
     /** @test */
     public function can_parse_response_tag()
     {
@@ -516,7 +617,7 @@ abstract class GeneratorTestCase extends TestCase
         $this->assertEquals(200, $response['status']);
         $this->assertSame(
             $response['content'],
-            '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'.
+            '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},' .
             '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
         );
     }
@@ -535,7 +636,7 @@ abstract class GeneratorTestCase extends TestCase
         $this->assertEquals(200, $response['status']);
         $this->assertSame(
             $response['content'],
-            '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'.
+            '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},' .
             '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
         );
     }
@@ -637,7 +738,7 @@ abstract class GeneratorTestCase extends TestCase
     public function can_parse_response_file_tag()
     {
         // copy file to storage
-        $filePath = __DIR__.'/../Fixtures/response_test.json';
+        $filePath = __DIR__ . '/../Fixtures/response_test.json';
         $fixtureFileJson = file_get_contents($filePath);
         copy($filePath, storage_path('response_test.json'));
 
@@ -662,7 +763,7 @@ abstract class GeneratorTestCase extends TestCase
     public function can_add_or_replace_key_value_pair_in_response_file()
     {
         // copy file to storage
-        $filePath = __DIR__.'/../Fixtures/response_test.json';
+        $filePath = __DIR__ . '/../Fixtures/response_test.json';
         $fixtureFileJson = file_get_contents($filePath);
         copy($filePath, storage_path('response_test.json'));
 
@@ -687,10 +788,10 @@ abstract class GeneratorTestCase extends TestCase
     public function can_parse_multiple_response_file_tags_with_status_codes()
     {
         // copy file to storage
-        $successFilePath = __DIR__.'/../Fixtures/response_test.json';
+        $successFilePath = __DIR__ . '/../Fixtures/response_test.json';
         $successFixtureFileJson = file_get_contents($successFilePath);
         copy($successFilePath, storage_path('response_test.json'));
-        $errorFilePath = __DIR__.'/../Fixtures/response_error_test.json';
+        $errorFilePath = __DIR__ . '/../Fixtures/response_error_test.json';
         $errorFixtureFileJson = file_get_contents($errorFilePath);
         copy($errorFilePath, storage_path('response_error_test.json'));