diff --git a/src/Generator.php b/src/Generator.php index 59c2db3..7b6e59f 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -184,9 +184,14 @@ protected function scanFilesForDocumentation(): self * * @return OpenApiGenerator */ + protected function newOpenApiGenerator(): OpenApiGenerator + { + return new OpenApiGenerator(); + } + protected function createOpenApiGenerator(): OpenApiGenerator { - $generator = new OpenApiGenerator(); + $generator = $this->newOpenApiGenerator(); if (! empty($this->scanOptions['default_processors_configuration']) && is_array($this->scanOptions['default_processors_configuration']) diff --git a/src/GeneratorFactory.php b/src/GeneratorFactory.php index 5c665c7..3c2c5db 100644 --- a/src/GeneratorFactory.php +++ b/src/GeneratorFactory.php @@ -32,6 +32,27 @@ public function make(string $documentation): Generator $security = new SecurityDefinitions($secSchemesConfig, $secConfig); + return $this->createGenerator( + $paths, + $constants, + $yamlCopyRequired, + $security, + $scanOptions + ); + } + + /** + * @param array $paths + * @param array $constants + * @param array $scanOptions + */ + protected function createGenerator( + array $paths, + array $constants, + bool $yamlCopyRequired, + SecurityDefinitions $security, + array $scanOptions + ): Generator { return new Generator( $paths, $constants, diff --git a/tests/Unit/GeneratorTest.php b/tests/Unit/GeneratorTest.php index 7ea0c83..45c9096 100644 --- a/tests/Unit/GeneratorTest.php +++ b/tests/Unit/GeneratorTest.php @@ -3,13 +3,16 @@ namespace Tests\Unit; use Illuminate\Http\Request; +use L5Swagger\ConfigFactory; use L5Swagger\Exceptions\L5SwaggerException; use L5Swagger\Generator; use L5Swagger\GeneratorFactory; use L5Swagger\L5SwaggerServiceProvider; +use L5Swagger\SecurityDefinitions; use OpenApi\Analysers\AttributeAnnotationFactory; use OpenApi\Analysers\DocBlockAnnotationFactory; use OpenApi\Analysers\ReflectionAnalyser; +use OpenApi\Generator as OpenApiGenerator; use OpenApi\OpenApiException; use OpenApi\Processors\AugmentParameters; use PHPUnit\Framework\Attributes\CoversClass; @@ -225,6 +228,57 @@ public function testCanGenerateWithScanOptions(): void ->assertStatus(200); } + /** + * @throws L5SwaggerException + */ + public function testCanGenerateWithCustomGeneratorFactory(): void + { + $app = $this->app; + + if (! $app instanceof \Illuminate\Foundation\Application) { + throw new \RuntimeException('Application is not set'); + } + + $this->setAnnotationsPath(); + + $customFactoryCalled = false; + + $factory = new class($app->make(ConfigFactory::class)) extends GeneratorFactory + { + protected function createGenerator( + array $paths, + array $constants, + bool $yamlCopyRequired, + SecurityDefinitions $security, + array $scanOptions + ): Generator { + return new class($paths, $constants, $yamlCopyRequired, $security, $scanOptions) extends Generator + { + protected function newOpenApiGenerator(): OpenApiGenerator + { + return new OpenApiGenerator(); + } + }; + } + }; + + $app->bind(Generator::class, function () use ($factory, &$customFactoryCalled) { + $customFactoryCalled = true; + + return $factory->make(config('l5-swagger.default')); + }); + + $generator = $app->make(Generator::class); + $generator->generateDocs(); + + $this->assertTrue($customFactoryCalled); + $this->assertFileExists($this->jsonDocsFile()); + + $this->get(route('l5-swagger.default.docs')) + ->assertSee('L5 Swagger') + ->assertStatus(200); + } + /** * @throws L5SwaggerException */