Sfoglia il codice sorgente

Merge branch 'master' into feat/natsort

Shalvah 6 anni fa
parent
commit
68507895e0

+ 2 - 2
config/apidoc.php

@@ -81,12 +81,12 @@ return [
                  * Specify headers to be added to the example requests
                  */
                 'headers' => [
-                    // 'Authorization' => 'Bearer: {token}',
+                    // 'Authorization' => 'Bearer {token}',
                     // 'Api-Version' => 'v2',
                 ],
 
                 /*
-                 * If no @response or @transformer declaratons are found for the route,
+                 * If no @response or @transformer declarations are found for the route,
                  * we'll try to get a sample response by attempting an API call.
                  * Configure the settings for the API call here,
                  */

+ 9 - 0
src/Commands/GenerateDocumentation.php

@@ -3,6 +3,7 @@
 namespace Mpociot\ApiDoc\Commands;
 
 use ReflectionClass;
+use ReflectionException;
 use Illuminate\Routing\Route;
 use Illuminate\Console\Command;
 use Mpociot\Reflection\DocBlock;
@@ -215,13 +216,21 @@ class GenerateDocumentation extends Command
     /**
      * @param $route
      *
+     * @throws ReflectionException
+     *
      * @return bool
      */
     private function isRouteVisibleForDocumentation($route)
     {
         list($class, $method) = explode('@', $route);
         $reflection = new ReflectionClass($class);
+
+        if (! $reflection->hasMethod($method)) {
+            return false;
+        }
+
         $comment = $reflection->getMethod($method)->getDocComment();
+
         if ($comment) {
             $phpdoc = new DocBlock($comment);
 

+ 6 - 3
src/Tools/ResponseStrategies/ResponseFileStrategy.php

@@ -32,9 +32,12 @@ class ResponseFileStrategy
      */
     protected function getFileResponses(array $tags)
     {
-        $responseFileTags = array_filter($tags, function ($tag) {
-            return $tag instanceof Tag && strtolower($tag->getName()) === 'responsefile';
-        });
+        // avoid "holes" in the keys of the filtered array, by using array_values on the filtered array
+        $responseFileTags = array_values(
+            array_filter($tags, function ($tag) {
+                return $tag instanceof Tag && strtolower($tag->getName()) === 'responsefile';
+            })
+        );
 
         if (empty($responseFileTags)) {
             return;

+ 5 - 3
src/Tools/ResponseStrategies/ResponseTagStrategy.php

@@ -32,9 +32,11 @@ class ResponseTagStrategy
      */
     protected function getDocBlockResponses(array $tags)
     {
-        $responseTags = array_filter($tags, function ($tag) {
-            return $tag instanceof Tag && strtolower($tag->getName()) === 'response';
-        });
+        $responseTags = array_values(
+            array_filter($tags, function ($tag) {
+                return $tag instanceof Tag && strtolower($tag->getName()) === 'response';
+            })
+        );
 
         if (empty($responseTags)) {
             return;

+ 5 - 3
src/Tools/ResponseStrategies/TransformerTagsStrategy.php

@@ -127,9 +127,11 @@ class TransformerTagsStrategy
      */
     private function getTransformerTag(array $tags)
     {
-        $transFormerTags = array_filter($tags, function ($tag) {
-            return ($tag instanceof Tag) && in_array(strtolower($tag->getName()), ['transformer', 'transformercollection']);
-        });
+        $transFormerTags = array_values(
+            array_filter($tags, function ($tag) {
+                return ($tag instanceof Tag) && in_array(strtolower($tag->getName()), ['transformer', 'transformercollection']);
+            })
+        );
 
         return array_first($transFormerTags);
     }

+ 20 - 0
tests/Fixtures/TestPartialResourceController.php

@@ -0,0 +1,20 @@
+<?php
+
+namespace Mpociot\ApiDoc\Tests\Fixtures;
+
+class TestPartialResourceController
+{
+    /**
+     * @group Group A
+     */
+    public function index()
+    {
+    }
+
+    /**
+     * @group Group B
+     */
+    public function update()
+    {
+    }
+}

+ 22 - 0
tests/GenerateDocumentationTest.php

@@ -2,6 +2,7 @@
 
 namespace Mpociot\ApiDoc\Tests;
 
+use ReflectionException;
 use RecursiveIteratorIterator;
 use RecursiveDirectoryIterator;
 use Orchestra\Testbench\TestCase;
@@ -12,6 +13,7 @@ use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider;
 use Illuminate\Support\Facades\Route as RouteFacade;
 use Mpociot\ApiDoc\Tests\Fixtures\TestResourceController;
 use Mpociot\ApiDoc\Tests\Fixtures\TestNaturalSortController;
+use Mpociot\ApiDoc\Tests\Fixtures\TestPartialResourceController;
 
 class GenerateDocumentationTest extends TestCase
 {
@@ -279,6 +281,26 @@ class GenerateDocumentationTest extends TestCase
         $this->assertTrue(
             $firstGroup1Occurrence < $firstGroup2Occurrence && $firstGroup2Occurrence < $firstGroup10Occurrence
         );
+  
+    /** @test */
+    public function supports_partial_resource_controller()
+    {
+        RouteFacade::resource('/api/partial', TestPartialResourceController::class);
+
+        config(['apidoc.routes.0.prefixes' => ['api/*']]);
+
+        $thrownException = null;
+
+        try {
+            $this->artisan('apidoc:generate');
+        } catch (ReflectionException $e) {
+            $thrownException = $e;
+        }
+
+        $this->assertNull($thrownException);
+        $generatedMarkdown = file_get_contents(__DIR__.'/../public/docs/source/index.md');
+        $this->assertContains('Group A', $generatedMarkdown);
+        $this->assertContains('Group B', $generatedMarkdown);
     }
 
     /**