Quellcode durchsuchen

Add test for multi-docs

shalvah vor 2 Jahren
Ursprung
Commit
379b6f69c3

+ 3 - 8
src/Commands/GenerateDocumentation.php

@@ -90,12 +90,8 @@ class GenerateDocumentation extends Command
         c::bootstrapOutput($this->output);
 
         $this->configName = $this->option('config');
-        if ($this->configName !== 'scribe') {
-            $configPath = config_path($this->configName) . ".php";
-            if (!file_exists($configPath)) {
-                c::error("The specified config file doesn't exist: {$configPath}.\n");
-                exit(1);
-            }
+        if (!config($this->configName)) {
+            throw new \InvalidArgumentException("The specified config (config/{$this->configName}.php) doesn't exist.");
         }
 
         $this->docConfig = new DocumentationConfig(config($this->configName));
@@ -108,8 +104,7 @@ class GenerateDocumentation extends Command
         $this->shouldExtract = !$this->option('no-extraction');
 
         if ($this->forcing && !$this->shouldExtract) {
-            c::error("Can't use --force and --no-extraction together.\n");
-            exit(1);
+            throw new \InvalidArgumentException("Can't use --force and --no-extraction together.");
         }
 
         // Reset this map (useful for tests)

+ 1 - 1
src/GroupedEndpoints/GroupedEndpointsFromApp.php

@@ -76,7 +76,7 @@ class GroupedEndpointsFromApp implements GroupedEndpointsContract
             $groups = Camel::loadEndpointsIntoGroups(static::$camelDir);
         }
 
-        $routes = $routeMatcher->getRoutes($this->docConfig->get('routes'), $this->docConfig->get('router'));
+        $routes = $routeMatcher->getRoutes($this->docConfig->get('routes', []), $this->docConfig->get('router'));
         $endpoints = $this->extractEndpointsInfoFromLaravelApp($routes, $cachedEndpoints, $latestEndpointsData, $groups);
         $groupedEndpoints = Camel::groupEndpoints($endpoints, $this->endpointGroupIndexes, $this->docConfig->get('groups.order', []));
         $this->writeEndpointsToDisk($groupedEndpoints);

+ 28 - 6
tests/GenerateDocumentation/OutputTest.php

@@ -67,7 +67,6 @@ class OutputTest extends BaseLaravelTest
     public function generates_laravel_type_output()
     {
         RouteFacade::post('/api/withQueryParameters', [TestController::class, 'withQueryParameters']);
-        config(['scribe.type' => 'laravel']);
         config(['scribe.postman.enabled' => true]);
         config(['scribe.openapi.enabled' => true]);
 
@@ -84,14 +83,36 @@ class OutputTest extends BaseLaravelTest
         $response = $this->get('/apidocs.openapi');
         $response->assertStatus(200);
 
-        config(['scribe.laravel.add_routes' => false]);
-        config(['scribe.laravel.docs_url' => '/apidocs']);
-
         unlink($this->postmanOutputPath(true));
         unlink($this->openapiOutputPath(true));
         unlink($this->bladeOutputPath());
     }
 
+    /** @test */
+    public function supports_multi_docs_in_laravel_type_output()
+    {
+        RouteFacade::post('/api/withQueryParameters', [TestController::class, 'withQueryParameters']);
+        config(['scribe_admin' => config('scribe')]);
+        $title = "The Real Admin API";
+        config(['scribe_admin.title' => $title]);
+        config(['scribe_admin.type' => 'laravel']);
+        config(['scribe_admin.postman.enabled' => true]);
+        config(['scribe_admin.openapi.enabled' => true]);
+
+        $this->generate(["--config" => "scribe_admin"]);
+
+        $paths = collect([
+            Storage::disk('local')->path('scribe_admin/collection.json'),
+            Storage::disk('local')->path('scribe_admin/openapi.yaml'),
+            View::getFinder()->find('scribe_admin/index'),
+        ]);
+        $paths->each(fn($path) => $this->assertFileContainsString($path, $title));
+        $paths->each(fn($path) => unlink($path));
+
+        $this->assertDirectoryExists(".scribe_admin");
+        Utils::deleteDirectoryAndContents(".scribe_admin");
+    }
+
     /** @test */
     public function generated_postman_collection_file_is_correct()
     {
@@ -348,7 +369,8 @@ class OutputTest extends BaseLaravelTest
     /** @test */
     public function generates_correct_url_params_from_non_resource_routes_and_model_binding()
     {
-        RouteFacade::get('posts/{post}/users', function(TestPost $post) {});
+        RouteFacade::get('posts/{post}/users', function (TestPost $post) {
+        });
 
         config(['scribe.routes.0.match.prefixes' => ['*']]);
         config(['scribe.routes.0.apply.response_calls.methods' => []]);
@@ -363,7 +385,7 @@ class OutputTest extends BaseLaravelTest
     public function generates_from_camel_dir_if_noExtraction_flag_is_set()
     {
         config(['scribe.routes.0.exclude' => ['*']]);
-        Utils::copyDirectory(__DIR__.'/../Fixtures/.scribe', '.scribe');
+        Utils::copyDirectory(__DIR__ . '/../Fixtures/.scribe', '.scribe');
 
         $output = $this->generate(['--no-extraction' => true]);
 

+ 7 - 0
tests/TestHelpers.php

@@ -27,4 +27,11 @@ trait TestHelpers
             'scribe:generate', array_merge(['--no-upgrade-check' => true], $flags)
         );
     }
+
+    protected function assertFileContainsString(string $filePath, string $string)
+    {
+        $this->assertFileExists($filePath);
+        $fileContents = file_get_contents($filePath);
+        $this->assertStringContainsString($string, $fileContents);
+    }
 }