From ac9404ac12b4c45924b1d42d9005b3ee743cc9b4 Mon Sep 17 00:00:00 2001 From: shalvah Date: Sat, 25 Jun 2022 04:37:49 +0200 Subject: [PATCH] Add tests for laravel type --- .gitignore | 1 + src/Writing/Writer.php | 15 +++++++--- tests/GenerateDocumentation/OutputTest.php | 35 +++++++++++++++------- tests/TestHelpers.php | 2 +- 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index b13a58da..18395254 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ composer.dingo.lock public/ resources/docs/ resources/views/scribe/ +.scribe/ tests/public/ .idea/ coverage.xml diff --git a/src/Writing/Writer.php b/src/Writing/Writer.php index 19ee68e3..16a7e5d8 100644 --- a/src/Writing/Writer.php +++ b/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 @@ protected function runAfterGeneratingHook() } } + protected function getLaravelTypeOutputPath(string $docsName): ?string + { + if ($this->isStatic) return null; + + return config('view.paths.0', "resources/views")."/$docsName"; + } + } diff --git a/tests/GenerateDocumentation/OutputTest.php b/tests/GenerateDocumentation/OutputTest.php index a02d3675..12d057c4 100644 --- a/tests/GenerateDocumentation/OutputTest.php +++ b/tests/GenerateDocumentation/OutputTest.php @@ -4,7 +4,7 @@ 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 @@ protected function defineEnvironment($app) ]); } - /** @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 @@ public function generates_laravel_type_output() $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 @@ protected function htmlOutputPath(): string protected function bladeOutputPath(): string { - return 'resources/views/scribe/index.blade.php'; + return View::getFinder()->find('scribe/index'); } } diff --git a/tests/TestHelpers.php b/tests/TestHelpers.php index 58910701..33742afb 100644 --- a/tests/TestHelpers.php +++ b/tests/TestHelpers.php @@ -23,6 +23,6 @@ public function artisan($command, $parameters = []) protected function generate(array $flags = []): mixed { - return $this->artisan('scribe:generate', $flags); + return $this->artisan('scribe:generate', array_merge(['--no-upgrade-check' => true], $flags)); } }