Shalvah 2 months ago
parent
commit
72f68dbe17

+ 10 - 1
src/Commands/GenerateDocumentation.php

@@ -3,6 +3,7 @@
 namespace Knuckles\Scribe\Commands;
 
 use Illuminate\Console\Command;
+use Illuminate\Foundation\Application;
 use Illuminate\Support\Arr;
 use Illuminate\Support\Facades\URL;
 use Illuminate\Support\Str;
@@ -119,7 +120,15 @@ class GenerateDocumentation extends Command
 
         // Force root URL so it works in Postman collection
         $baseUrl = $this->docConfig->get('base_url') ?? config('app.url');
-        URL::forceRootUrl($baseUrl);
+
+        /* @phpstan-ignore-next-line */
+        if (version_compare(Application::VERSION, '11.0', '>=')) {
+            // Renamed in Laravel 11
+            URL::useOrigin($baseUrl);
+        } else {
+            /* @phpstan-ignore-next-line */
+            URL::forceRootUrl($baseUrl);
+        }
 
         $this->forcing = $this->option('force');
         $this->shouldExtract = !$this->option('no-extraction');

+ 4 - 2
src/Extracting/InstantiatesExampleModels.php

@@ -30,9 +30,11 @@ trait InstantiatesExampleModels
 
         if ($type == null) {
             $parameter = Arr::first($transformationMethod->getParameters());
-            if ($parameter->hasType() && !$parameter->getType()->isBuiltin() && class_exists($parameter->getType()->getName())) {
+            $parameterType = $parameter->hasType() ? $parameter->getType() : null;
+            if ($parameterType instanceof \ReflectionNamedType &&
+                !$parameterType->isBuiltin() && class_exists($parameterType->getName())) {
                 // Ladies and gentlemen, we have a type!
-                $type = $parameter->getType()->getName();
+                $type = $parameterType->getName();
             }
         }
         if ($type == null) {

+ 1 - 0
src/Extracting/Strategies/GetFromFormRequestBase.php

@@ -65,6 +65,7 @@ class GetFromFormRequestBase extends Strategy
         if (method_exists($formRequest, 'validator')) {
             $validationFactory = app(ValidationFactory::class);
 
+            // @phpstan-ignore-next-line
             return app()->call([$formRequest, 'validator'], [$validationFactory])
                 ->getRules();
         } elseif (method_exists($formRequest, 'rules')) {

+ 1 - 5
src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php

@@ -34,11 +34,7 @@ class GetFromLaravelAPI extends Strategy
         }
 
         $parameters = $this->inferBetterTypesAndExamplesForEloquentUrlParameters($parameters, $endpointData);
-
-        if (version_compare(PHP_VERSION, '8.1.0', '>=')) {
-            $parameters = $this->inferBetterTypesAndExamplesForEnumUrlParameters($parameters, $endpointData);
-        }
-
+        $parameters = $this->inferBetterTypesAndExamplesForEnumUrlParameters($parameters, $endpointData);
         $parameters = $this->setTypesAndExamplesForOthers($parameters, $endpointData);
 
         return $parameters;

+ 9 - 26
src/Tools/Utils.php

@@ -118,36 +118,18 @@ class Utils
 
     public static function deleteDirectoryAndContents(string $dir, ?string $workingDir = null): void
     {
-        if (class_exists(LocalFilesystemAdapter::class)) {
-            // Flysystem 2+
-            $workingDir ??= getcwd();
-            $adapter = new LocalFilesystemAdapter($workingDir);
-            $fs = new Filesystem($adapter);
-            $dir = str_replace($workingDir, '', $dir);
-            $fs->deleteDirectory($dir);
-        } else {
-            // v1
-            $adapter = new \League\Flysystem\Adapter\Local($workingDir ?: getcwd());
-            $fs = new Filesystem($adapter);
-            $dir = str_replace($adapter->getPathPrefix(), '', $dir);
-            $fs->deleteDir($dir);
-        }
+        $workingDir ??= getcwd();
+        $adapter = new LocalFilesystemAdapter($workingDir);
+        $fs = new Filesystem($adapter);
+        $dir = str_replace($workingDir, '', $dir);
+        $fs->deleteDirectory($dir);
     }
 
     public static function listDirectoryContents(string $dir)
     {
-        if (class_exists(LocalFilesystemAdapter::class)) {
-            // Flysystem 2+
-            $adapter = new LocalFilesystemAdapter(getcwd());
-            $fs = new Filesystem($adapter);
-            return $fs->listContents($dir);
-        } else {
-            // v1
-            $adapter = new \League\Flysystem\Adapter\Local(getcwd()); // @phpstan-ignore-line
-            $fs = new Filesystem($adapter); // @phpstan-ignore-line
-            $dir = str_replace($adapter->getPathPrefix(), '', $dir); // @phpstan-ignore-line
-            return $fs->listContents($dir);
-        }
+        $adapter = new LocalFilesystemAdapter(getcwd());
+        $fs = new Filesystem($adapter);
+        return $fs->listContents($dir);
     }
 
     public static function copyDirectory(string $src, string $dest): void
@@ -342,6 +324,7 @@ class Utils
 
         $translation = trans($key, $replace);
 
+        /* @phpstan-ignore-next-line */
         if ($translation === $key || $translation === null) {
             $translation = trans($key, $replace, 'en');
         }

+ 1 - 3
tests/Fixtures/TestUserApiResource.php

@@ -35,9 +35,7 @@ class TestUserApiResource extends JsonResource
             }),
         ];
 
-        if (version_compare(Application::VERSION, '9', '>=')) {
-            $result['children_count'] = $this->whenCounted('children_count');
-        }
+        $result['children_count'] = $this->whenCounted('children_count');
 
         if ($this['state1'] && $this['random-state']) {
             $result['state1'] = $this['state1'];

+ 29 - 21
tests/GenerateDocumentation/OutputTest.php

@@ -117,6 +117,27 @@ class OutputTest extends BaseLaravelTest
         Utils::deleteDirectoryAndContents($intermediateOutputDirectory ?: ".{$configName}");
     }
 
+    /** @test */
+    public function generates_and_adds_routes()
+    {
+        RouteFacade::post('/api/withBodyParameters', [TestController::class, 'withBodyParameters']);
+
+        $this->setConfig([
+            'type' => 'laravel',
+            'laravel.add_routes' => true,
+            'postman.enabled' => true,
+            'openapi.enabled' => true,
+        ]);
+        $this->generate();
+
+        $response = $this->get('/docs');
+        $response->assertStatus(200);
+        $response = $this->get('/docs.postman');
+        $response->assertStatus(200);
+        $response = $this->get('/docs.openapi');
+        $response->assertStatus(200);
+    }
+
     /** @test */
     public function generated_postman_collection_file_is_correct()
     {
@@ -156,30 +177,17 @@ class OutputTest extends BaseLaravelTest
         $generatedCollection['info']['_postman_id'] = '';
         $fixtureCollection = json_decode(file_get_contents(__DIR__ . '/../Fixtures/collection.json'), true);
 
+        // Laravel 11 began adding CORS headers by default
+        foreach ($generatedCollection["item"] as &$group) {
+            foreach ($group["item"] as &$endpoint) {
+                foreach ($endpoint["response"] as &$response) {
+                    $response["header"] = array_filter($response["header"], fn ($header) => $header["key"] !== "access-control-allow-origin");
+                }
+            }
+        }
         $this->assertEquals($fixtureCollection, $generatedCollection);
     }
 
-    /** @test */
-    public function generates_and_adds_routes()
-    {
-        RouteFacade::post('/api/withBodyParameters', [TestController::class, 'withBodyParameters']);
-
-        $this->setConfig([
-            'type' => 'laravel',
-            'laravel.add_routes' => true,
-            'postman.enabled' => true,
-            'openapi.enabled' => true,
-        ]);
-        $this->generate();
-
-        $response = $this->get('/docs');
-        $response->assertStatus(200);
-        $response = $this->get('/docs.postman');
-        $response->assertStatus(200);
-        $response = $this->get('/docs.openapi');
-        $response->assertStatus(200);
-    }
-
     /** @test */
     public function generated_openapi_spec_file_is_correct()
     {

+ 6 - 12
tests/Strategies/Responses/UseApiResourceTagsTest.php

@@ -833,18 +833,12 @@ class UseApiResourceTagsTest extends BaseLaravelTest
                         "prev" => null,
                         "next" => "/?cursor={$nextCursor}",
                     ],
-                    "meta" => match (version_compare(Application::VERSION, '9.0', '>=')) {
-                        false => [
-                            "path" => '/',
-                            'per_page' => '1',
-                        ],
-                        true => [
-                            "path" => '/',
-                            'per_page' => 1,
-                            'next_cursor' => $nextCursor,
-                            'prev_cursor' => null,
-                        ]
-                    },
+                    "meta" => [
+                        "path" => '/',
+                        'per_page' => 1,
+                        'next_cursor' => $nextCursor,
+                        'prev_cursor' => null,
+                    ],
                 ]),
             ],
         ], $results);

+ 6 - 16
tests/Strategies/Responses/UseResponseAttributesTest.php

@@ -270,18 +270,12 @@ class UseResponseAttributesTest extends BaseLaravelTest
                         "prev" => null,
                         "next" => "/?cursor={$nextCursor}",
                     ],
-                    "meta" => match (version_compare(Application::VERSION, '9.0', '>=')) {
-                        false => [
-                            "path" => '/',
-                            'per_page' => 1,
-                        ],
-                        true => [
-                            "path" => '/',
-                            'per_page' => 1,
-                            'next_cursor' => $nextCursor,
-                            'prev_cursor' => null,
-                        ]
-                    },
+                    "meta" => [
+                        "path" => '/',
+                        'per_page' => 1,
+                        'next_cursor' => $nextCursor,
+                        'prev_cursor' => null,
+                    ]
                 ]),
             ],
         ], $results);
@@ -332,10 +326,6 @@ class UseResponseAttributesTest extends BaseLaravelTest
     /** @test */
     public function can_parse_apiresource_attributes_and_load_children_and_children_count_using_factory_create()
     {
-        if (version_compare(Application::VERSION, '9', '<')) {
-            $this->markTestSkipped('The whenCounted method in JsonResource requires Laravel 9 or higher.');
-        }
-
         Schema::create('test_users', function (Blueprint $table) {
             $table->id();
             $table->string('first_name');

+ 8 - 10
tests/Unit/ValidationRuleParsingTest.php

@@ -459,16 +459,14 @@ class ValidationRuleParsingTest extends BaseLaravelTest
             ['unsupported_param' => ['description' => $description]],
             ['description' => "$description."],
         ];
-        if (version_compare(Application::VERSION, '8.53', '>=')) {
-            yield 'accepted_if' => [
-                ['accepted_if_param' => 'accepted_if:another_field,a_value'],
-                [],
-                [
-                    'type' => 'boolean',
-                    'description' => "Must be accepted when <code>another_field</code> is <code>a_value</code>.",
-                ],
-            ];
-        }
+        yield 'accepted_if' => [
+            ['accepted_if_param' => 'accepted_if:another_field,a_value'],
+            [],
+            [
+                'type' => 'boolean',
+                'description' => "Must be accepted when <code>another_field</code> is <code>a_value</code>.",
+            ],
+        ];
     }
 
     /** @test */