Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move AugmentTags after CleanUnusedComponents #1630

Merged
merged 2 commits into from
Jul 16, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,10 @@ public function getProcessorPipeline(): Pipeline
new Processors\MergeJsonContent(),
new Processors\MergeXmlContent(),
new Processors\OperationId(),
new Processors\AugmentTags(),
new Processors\CleanUnmerged(),
new Processors\PathFilter(),
new Processors\CleanUnusedComponents(),
new Processors\AugmentTags(),
]);
}

Expand Down
25 changes: 25 additions & 0 deletions tests/Fixtures/SurplusTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);

/**
* @license Apache 2.0
*/

namespace OpenApi\Tests\Fixtures;

use OpenApi\Attributes as OAT;

#[OAT\OpenApi(
info: new OAT\Info(
title: 'test',
description: 'test',
version: '2.0.0'
),
)]
class SurplusTag
{
#[OAT\Get(path: '/world/', tags: ['tag world'], responses: [new OAT\Response(response: '200', description: 'success')])]
#[OAT\Get(path: '/hello/', tags: ['tag hello'], responses: [new OAT\Response(response: '200', description: 'success')])]
public function hello(): void
{
}
}
3 changes: 2 additions & 1 deletion tests/OpenApiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,12 @@ public static function processors(array $strip = []): array
return $processors;
}

public function analysisFromFixtures(array $files, array $processors = [], ?AnalyserInterface $analyzer = null): Analysis
public function analysisFromFixtures(array $files, array $processors = [], ?AnalyserInterface $analyzer = null, array $config = []): Analysis
{
$analysis = new Analysis([], $this->getContext());

(new Generator($this->getTrackingLogger()))
->setConfig($config)
->setAnalyser($analyzer ?: $this->getAnalyzer())
->setProcessorPipeline(new Pipeline($processors))
->generate($this->fixtures($files), $analysis, false);
Expand Down
28 changes: 28 additions & 0 deletions tests/Processors/AugmentTagsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);

/**
* @license Apache 2.0
*/

namespace OpenApi\Tests\Processors;

use OpenApi\Tests\OpenApiTestCase;

class AugmentTagsTest extends OpenApiTestCase
{
/**
* @requires PHP 8.1
*/
public function testAugmentTags(): void
{
$this->skipLegacy();

$config = [
'pathFilter' => ['paths' => ['#^/hello/#']],
'cleanUnusedComponents' => ['enabled' => true],
];
$analysis = $this->analysisFromFixtures(['SurplusTag.php'], static::processors(), null, $config);

$this->assertCount(1, $analysis->openapi->tags);
}
}
15 changes: 7 additions & 8 deletions tests/Processors/CleanUnusedComponentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,28 @@
namespace OpenApi\Tests\Processors;

use OpenApi\Generator;
use OpenApi\Processors\CleanUnusedComponents;
use OpenApi\Tests\OpenApiTestCase;

class CleanUnusedComponentsTest extends OpenApiTestCase
{
public static function countCases(): iterable
{
$defaultProcessors = static::processors([CleanUnusedComponents::class]);
$configEnable = ['cleanUnusedComponents' => ['enabled' => true]];

return [
'var-default' => [$defaultProcessors, 'UsingVar.php', 2, 5],
'var-clean' => [array_merge($defaultProcessors, [new CleanUnusedComponents(true)]), 'UsingVar.php', 0, 2],
'unreferenced-default' => [$defaultProcessors, 'Unreferenced.php', 2, 11],
'unreferenced-clean' => [array_merge($defaultProcessors, [new CleanUnusedComponents(true)]), 'Unreferenced.php', 0, 5],
'var-default' => [[], 'UsingVar.php', 2, 5],
'var-clean' => [$configEnable, 'UsingVar.php', 0, 2],
'unreferenced-default' => [[], 'Unreferenced.php', 2, 11],
'unreferenced-clean' => [$configEnable, 'Unreferenced.php', 0, 5],
];
}

/**
* @dataProvider countCases
*/
public function testCounts(array $processors, string $fixture, int $expectedSchemaCount, int $expectedAnnotationCount): void
public function testCounts(array $config, string $fixture, int $expectedSchemaCount, int $expectedAnnotationCount): void
{
$analysis = $this->analysisFromFixtures([$fixture], $processors);
$analysis = $this->analysisFromFixtures([$fixture], static::processors(), null, $config);

if ($expectedSchemaCount === 0) {
$this->assertTrue(Generator::isDefault($analysis->openapi->components->schemas));
Expand Down