From 17ffc76919085f80691e420719f80fe6c0bcadce Mon Sep 17 00:00:00 2001 From: DerManoMann Date: Thu, 11 Jun 2026 12:40:22 +1200 Subject: [PATCH 1/2] Isolate instatiation of `Generator` and `GeneratorFactory` to minimize code duplication --- src/Generator.php | 7 ++++- src/GeneratorFactory.php | 21 +++++++++++++++ tests/Unit/GeneratorTest.php | 52 ++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) 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..c0fe529 100644 --- a/tests/Unit/GeneratorTest.php +++ b/tests/Unit/GeneratorTest.php @@ -7,9 +7,12 @@ use L5Swagger\Generator; use L5Swagger\GeneratorFactory; use L5Swagger\L5SwaggerServiceProvider; +use L5Swagger\ConfigFactory; +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,55 @@ 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 */ From 49f912a7e78b237a02f206a057faa223d67a3acc Mon Sep 17 00:00:00 2001 From: DerManoMann Date: Thu, 11 Jun 2026 13:03:46 +1200 Subject: [PATCH 2/2] CS --- tests/Unit/GeneratorTest.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/Unit/GeneratorTest.php b/tests/Unit/GeneratorTest.php index c0fe529..45c9096 100644 --- a/tests/Unit/GeneratorTest.php +++ b/tests/Unit/GeneratorTest.php @@ -3,11 +3,11 @@ 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\ConfigFactory; use L5Swagger\SecurityDefinitions; use OpenApi\Analysers\AttributeAnnotationFactory; use OpenApi\Analysers\DocBlockAnnotationFactory; @@ -243,7 +243,8 @@ public function testCanGenerateWithCustomGeneratorFactory(): void $customFactoryCalled = false; - $factory = new class($app->make(ConfigFactory::class)) extends GeneratorFactory { + $factory = new class($app->make(ConfigFactory::class)) extends GeneratorFactory + { protected function createGenerator( array $paths, array $constants, @@ -251,7 +252,8 @@ protected function createGenerator( SecurityDefinitions $security, array $scanOptions ): Generator { - return new class($paths, $constants, $yamlCopyRequired, $security, $scanOptions) extends Generator { + return new class($paths, $constants, $yamlCopyRequired, $security, $scanOptions) extends Generator + { protected function newOpenApiGenerator(): OpenApiGenerator { return new OpenApiGenerator();