Skip to content

Commit

Permalink
Add tests for laravel type
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Jun 25, 2022
1 parent d582cc5 commit ac9404a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ composer.dingo.lock
public/
resources/docs/
resources/views/scribe/
.scribe/
tests/public/
.idea/
coverage.xml
Expand Down
15 changes: 11 additions & 4 deletions src/Writing/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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')
Expand Down Expand Up @@ -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";
}

}
35 changes: 24 additions & 11 deletions tests/GenerateDocumentation/OutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
Expand All @@ -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());
Expand Down Expand Up @@ -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');
}
}
2 changes: 1 addition & 1 deletion tests/TestHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit ac9404a

Please sign in to comment.