Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down
21 changes: 21 additions & 0 deletions src/GeneratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string,mixed> $paths
* @param array<string> $constants
* @param array<string,mixed> $scanOptions
*/
protected function createGenerator(
array $paths,
array $constants,
bool $yamlCopyRequired,
SecurityDefinitions $security,
array $scanOptions
): Generator {
return new Generator(
$paths,
$constants,
Expand Down
54 changes: 54 additions & 0 deletions tests/Unit/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down
Loading