Browse Source

Add tests for laravel type

shalvah 2 years ago
parent
commit
ac9404ac12
4 changed files with 37 additions and 16 deletions
  1. 1 0
      .gitignore
  2. 11 4
      src/Writing/Writer.php
  3. 24 11
      tests/GenerateDocumentation/OutputTest.php
  4. 1 1
      tests/TestHelpers.php

+ 1 - 0
.gitignore

@@ -6,6 +6,7 @@ composer.dingo.lock
 public/
 resources/docs/
 resources/views/scribe/
+.scribe/
 tests/public/
 .idea/
 coverage.xml

+ 11 - 4
src/Writing/Writer.php

@@ -17,9 +17,9 @@ class Writer
 
     private string $markdownOutputPath;
 
-    private string $staticTypeOutputPath;
+    private ?string $staticTypeOutputPath;
 
-    private string $laravelTypeOutputPath;
+    private ?string $laravelTypeOutputPath;
     protected array $generatedFiles = [
         'postman' => null,
         'openapi' => null,
@@ -36,12 +36,12 @@ class Writer
 
     public function __construct(DocumentationConfig $config = null, $docsName = 'scribe')
     {
-        $this->markdownOutputPath = ".{$docsName}"; //.scribe by default
-        $this->laravelTypeOutputPath = "resources/views/$docsName";
         // If no config is injected, pull from global. Makes testing easier.
         $this->config = $config ?: new DocumentationConfig(config($docsName));
 
         $this->isStatic = $this->config->get('type') === 'static';
+        $this->markdownOutputPath = ".{$docsName}"; //.scribe by default
+        $this->laravelTypeOutputPath = $this->getLaravelTypeOutputPath($docsName);
         $this->staticTypeOutputPath = rtrim($this->config->get('static.output_path', 'public/docs'), '/');
 
         $this->laravelAssetsPath = $this->config->get('laravel.assets_directory')
@@ -218,4 +218,11 @@ class Writer
         }
     }
 
+    protected function getLaravelTypeOutputPath(string $docsName): ?string
+    {
+        if ($this->isStatic) return null;
+
+        return config('view.paths.0', "resources/views")."/$docsName";
+    }
+
 }

+ 24 - 11
tests/GenerateDocumentation/OutputTest.php

@@ -4,7 +4,7 @@ namespace Knuckles\Scribe\Tests\GenerateDocumentation;
 
 use Illuminate\Support\Facades\Route as RouteFacade;
 use Illuminate\Support\Facades\Storage;
-use Knuckles\Scribe\Scribe;
+use Illuminate\Support\Facades\View;
 use Knuckles\Scribe\Tests\BaseLaravelTest;
 use Knuckles\Scribe\Tests\Fixtures\TestController;
 use Knuckles\Scribe\Tests\Fixtures\TestGroupController;
@@ -58,17 +58,20 @@ class OutputTest extends BaseLaravelTest
         ]);
     }
 
-    /** @test */
+    protected function usesLaravelTypeDocs($app)
+    {
+        $app['config']->set('scribe.type', 'laravel');
+        $app['config']->set('scribe.laravel.add_routes', true);
+        $app['config']->set('scribe.laravel.docs_url', '/apidocs');
+    }
+
+    /**
+     * @test
+     * @define-env usesLaravelTypeDocs
+     */
     public function generates_laravel_type_output()
     {
-        RouteFacade::post('/api/withBodyParametersAsArray', [TestController::class, 'withBodyParametersAsArray']);
-        RouteFacade::post('/api/withFormDataParams', [TestController::class, 'withFormDataParams']);
-        RouteFacade::post('/api/withBodyParameters', [TestController::class, 'withBodyParameters']);
-        RouteFacade::get('/api/withQueryParameters', [TestController::class, 'withQueryParameters']);
-        RouteFacade::get('/api/withAuthTag', [TestController::class, 'withAuthenticatedTag']);
-        RouteFacade::get('/api/echoesUrlParameters/{param}/{param2}/{param3?}/{param4?}', [TestController::class, 'echoesUrlParameters']);
-        config(['scribe.title' => 'GREAT API!']);
-        config(['scribe.auth.enabled' => true]);
+        RouteFacade::post('/api/withQueryParameters', [TestController::class, 'withQueryParameters']);
         config(['scribe.type' => 'laravel']);
         config(['scribe.postman.enabled' => true]);
         config(['scribe.openapi.enabled' => true]);
@@ -79,6 +82,16 @@ class OutputTest extends BaseLaravelTest
         $this->assertFileExists($this->openapiOutputPath(true));
         $this->assertFileExists($this->bladeOutputPath());
 
+        $response = $this->get('/apidocs/');
+        $response->assertStatus(200);
+        $response = $this->get('/apidocs.postman');
+        $response->assertStatus(200);
+        $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());
@@ -402,6 +415,6 @@ class OutputTest extends BaseLaravelTest
 
     protected function bladeOutputPath(): string
     {
-        return 'resources/views/scribe/index.blade.php';
+        return View::getFinder()->find('scribe/index');
     }
 }

+ 1 - 1
tests/TestHelpers.php

@@ -23,6 +23,6 @@ trait TestHelpers
 
     protected function generate(array $flags = []): mixed
     {
-        return $this->artisan('scribe:generate', $flags);
+        return $this->artisan('scribe:generate', array_merge(['--no-upgrade-check' => true], $flags));
     }
 }