Ver Fonte

More tests

shalvah há 2 anos atrás
pai
commit
136ce719ee

+ 11 - 11
src/Extracting/Strategies/ResponseFields/GetFromResponseFieldTag.php

@@ -4,6 +4,7 @@ namespace Knuckles\Scribe\Extracting\Strategies\ResponseFields;
 
 use Knuckles\Scribe\Extracting\Shared\ResponseFieldTools;
 use Knuckles\Scribe\Extracting\Strategies\GetFieldsFromTagStrategy;
+use Mpociot\Reflection\DocBlock;
 
 class GetFromResponseFieldTag extends GetFieldsFromTagStrategy
 {
@@ -42,21 +43,20 @@ class GetFromResponseFieldTag extends GetFieldsFromTagStrategy
         return $data;
     }
 
-    /**
-     * Get api resource tag.
-     *
-     * @param Tag[] $tags
-     *
-     * @return Tag|null
-     */
-    public function getApiResourceTag(array $tags): ?Tag
+    public function getFromTags(array $tagsOnMethod, array $tagsOnClass = []): array
     {
         $apiResourceTags = array_values(
-            array_filter($tags, function ($tag) {
-                return ($tag instanceof Tag) && in_array(strtolower($tag->getName()), ['apiresource', 'apiresourcecollection']);
+            array_filter($tagsOnMethod, function ($tag) {
+                return in_array(strtolower($tag->getName()), ['apiresource', 'apiresourcecollection']);
             })
         );
 
-        return empty($apiResourceTags) ? null : $apiResourceTags[0];
+        if ($apiResourceTags && !empty($className = $apiResourceTags[0]->getContent())) {
+            $method = u::getReflectedRouteMethod([$className, 'toArray']);
+            $docBlock = new DocBlock($method->getDocComment() ?: '');
+            $tagsOnApiResource = $docBlock->getTags();
+        }
+
+        return parent::getFromTags(array_merge($tagsOnMethod, $tagsOnApiResource ?? []), $tagsOnClass);
     }
 }

+ 20 - 11
src/Writing/Writer.php

@@ -54,7 +54,7 @@ class Writer
 
         $this->laravelAssetsPath = $this->config->get('laravel.assets_directory')
             ? '/' . $this->config->get('laravel.assets_directory')
-            : '/vendor/scribe';
+            : "/vendor/$this->docsName";
     }
 
     /**
@@ -87,12 +87,11 @@ class Writer
                 file_put_contents($collectionPath, $collection);
             } else {
                 Storage::disk('local')->put("{$this->docsName}/collection.json", $collection);
-                $collectionPath = "storage/app/{$this->docsName}/collection.json";
+                $collectionPath = Storage::disk('local')->path("$this->docsName/collection.json");
             }
 
-            c::success("Wrote Postman collection to: {$collectionPath}");
-            $this->generatedFiles['postman'] = realpath($collectionPath)
-                ?: Storage::disk('local')->path('scribe/collection.json');
+            c::success("Wrote Postman collection to: {$this->makePathFriendly($collectionPath)}");
+            $this->generatedFiles['postman'] = realpath($collectionPath);
         }
     }
 
@@ -107,12 +106,11 @@ class Writer
                 file_put_contents($specPath, $spec);
             } else {
                 Storage::disk('local')->put("{$this->docsName}/openapi.yaml", $spec);
-                $specPath = "storage/app/{$this->docsName}/openapi.yaml";
+                $specPath = Storage::disk('local')->path("$this->docsName/openapi.yaml");
             }
 
-            c::success("Wrote OpenAPI specification to: {$specPath}");
-            $this->generatedFiles['openapi'] = realpath($specPath)
-                ?: Storage::disk('local')->path('scribe/openapi.yaml');
+            c::success("Wrote OpenAPI specification to: {$this->makePathFriendly($specPath)}");
+            $this->generatedFiles['openapi'] = realpath($specPath);
         }
     }
 
@@ -208,10 +206,10 @@ class Writer
             $assetsOutputPath = $outputPath;
         } else {
             $outputPath = rtrim($this->laravelTypeOutputPath, '/') . '/';
-            c::success("Wrote Blade docs to: $outputPath");
+            c::success("Wrote Blade docs to: ". $this->makePathFriendly($outputPath));
             $this->generatedFiles['blade'] = realpath("{$outputPath}index.blade.php");
             $assetsOutputPath = app()->get('path.public') . $this->laravelAssetsPath . '/';
-            c::success("Wrote Laravel assets to: " . realpath($assetsOutputPath));
+            c::success("Wrote Laravel assets to: " . $this->makePathFriendly($assetsOutputPath));
         }
         $this->generatedFiles['assets']['js'] = realpath("{$assetsOutputPath}js");
         $this->generatedFiles['assets']['css'] = realpath("{$assetsOutputPath}css");
@@ -233,4 +231,15 @@ class Writer
         return config('view.paths.0', function_exists('base_path') ? base_path("resources/views") : "resources/views")."/$this->docsName";
     }
 
+    /**
+     * Turn a path from (possibly) C:\projects\myapp\resources\views
+     * or /projects/myapp/resources/views  to resources/views ie:
+     * - make it relative to PWD
+     * - normalise all slashes to forward slashes
+     */
+    protected function makePathFriendly(string $path): string
+    {
+        return str_replace("\\", "/", str_replace(getcwd() . DIRECTORY_SEPARATOR, "", $path));
+    }
+
 }

+ 13 - 5
tests/GenerateDocumentation/BehavioursTest.php

@@ -187,12 +187,20 @@ class BehavioursTest extends BaseLaravelTest
         Utils::deleteDirectoryAndContents('static/');
     }
 
-    protected function generateAndExpectConsoleOutput(string ...$expectedOutput): void
+    /** @test */
+    public function checks_for_upgrades_after_run_unless_disabled()
     {
-        $output = $this->generate();
+        file_put_contents("config/scribe_test.php", str_replace("'logo' => false,", "", file_get_contents("config/scribe.php")));
+        config(["scribe_test" => require "config/scribe_test.php"]);
+
+        $output = $this->artisan('scribe:generate', ['--config' => 'scribe_test']);
+        $this->assertStringContainsString("Checking for any pending upgrades to your config file...", $output);
+        $this->assertStringContainsString("`logo` will be added", $output);
+
+        $output = $this->artisan('scribe:generate', ['--config' => 'scribe_test', '--no-upgrade-check' => true]);
+        $this->assertStringNotContainsString("Checking for any pending upgrades to your config file...", $output);
 
-        foreach ($expectedOutput as $expected) {
-            $this->assertStringContainsString($expected, $output);
-        }
+        unlink("config/scribe_test.php");
+        Utils::deleteDirectoryAndContents(".scribe_test");
     }
 }

+ 27 - 4
tests/GenerateDocumentation/OutputTest.php

@@ -113,7 +113,12 @@ class OutputTest extends BaseLaravelTest
         config(['scribe.postman.enabled' => true]);
         config(['scribe.openapi.enabled' => true]);
 
-        $this->generate();
+        $this->generateAndExpectConsoleOutput(
+            "Wrote Blade docs to: vendor/orchestra/testbench-core/laravel/resources/views/scribe",
+            "Wrote Laravel assets to: vendor/orchestra/testbench-core/laravel/public/vendor/scribe",
+            "Wrote Postman collection to: vendor/orchestra/testbench-core/laravel/storage/app/scribe/collection.json",
+            "Wrote OpenAPI specification to: vendor/orchestra/testbench-core/laravel/storage/app/scribe/openapi.yaml",
+        );
 
         $this->assertFileExists($this->postmanOutputPath(true));
         $this->assertFileExists($this->openapiOutputPath(true));
@@ -142,7 +147,19 @@ class OutputTest extends BaseLaravelTest
         config(['scribe_admin.postman.enabled' => true]);
         config(['scribe_admin.openapi.enabled' => true]);
 
-        $this->generate(["--config" => "scribe_admin"]);
+        $output = $this->generate(["--config" => "scribe_admin"]);
+        $this->assertStringContainsString(
+            "Wrote Blade docs to: vendor/orchestra/testbench-core/laravel/resources/views/scribe_admin", $output
+        );
+        $this->assertStringContainsString(
+            "Wrote Laravel assets to: vendor/orchestra/testbench-core/laravel/public/vendor/scribe_admin", $output
+        );
+        $this->assertStringContainsString(
+            "Wrote Postman collection to: vendor/orchestra/testbench-core/laravel/storage/app/scribe_admin/collection.json", $output
+        );
+        $this->assertStringContainsString(
+            "Wrote OpenAPI specification to: vendor/orchestra/testbench-core/laravel/storage/app/scribe_admin/openapi.yaml", $output
+        );
 
         $paths = collect([
             Storage::disk('local')->path('scribe_admin/collection.json'),
@@ -177,7 +194,10 @@ class OutputTest extends BaseLaravelTest
         ]);
         config(['scribe.postman.enabled' => true]);
 
-        $this->generate();
+        $this->generateAndExpectConsoleOutput(
+            "Wrote HTML docs and assets to: public/docs/",
+            "Wrote Postman collection to: public/docs/collection.json"
+        );
 
         $generatedCollection = json_decode(file_get_contents($this->postmanOutputPath()), true);
         // The Postman ID varies from call to call; erase it to make the test data reproducible.
@@ -207,7 +227,10 @@ class OutputTest extends BaseLaravelTest
             ],
         ]);
 
-        $this->generate();
+        $this->generateAndExpectConsoleOutput(
+            "Wrote HTML docs and assets to: public/docs/",
+            "Wrote OpenAPI specification to: public/docs/openapi.yaml"
+        );
 
         $generatedSpec = Yaml::parseFile($this->openapiOutputPath());
         $fixtureSpec = Yaml::parseFile(__DIR__ . '/../Fixtures/openapi.yaml');

+ 9 - 0
tests/TestHelpers.php

@@ -28,6 +28,15 @@ trait TestHelpers
         );
     }
 
+    protected function generateAndExpectConsoleOutput(string ...$expectedOutput): void
+    {
+        $output = $this->generate();
+
+        foreach ($expectedOutput as $expected) {
+            $this->assertStringContainsString($expected, $output);
+        }
+    }
+
     protected function assertFileContainsString(string $filePath, string $string)
     {
         $this->assertFileExists($filePath);