|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OpenSolid\Api\Tests\OpenApi\Processor; |
| 6 | + |
| 7 | +use OpenApi\Analysis; |
| 8 | +use OpenApi\Attributes as OAT; |
| 9 | +use OpenApi\Context; |
| 10 | +use OpenSolid\Api\OpenApi\Processor\RemoveScannedQueryParameters; |
| 11 | +use PHPUnit\Framework\Attributes\Test; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +class RemoveScannedQueryParametersTest extends TestCase |
| 15 | +{ |
| 16 | + private RemoveScannedQueryParameters $processor; |
| 17 | + |
| 18 | + protected function setUp(): void |
| 19 | + { |
| 20 | + $this->processor = new RemoveScannedQueryParameters(); |
| 21 | + } |
| 22 | + |
| 23 | + #[Test] |
| 24 | + public function itRemovesScannedQueryParametersWithReflectionPropertyContext(): void |
| 25 | + { |
| 26 | + // Simulate swagger-php scanning: QueryParameter annotations with nested=false |
| 27 | + // (exactly as AttributeAnnotationFactory does for property-level attributes) |
| 28 | + $scannedAnnotations = []; |
| 29 | + $class = new \ReflectionClass(ScannedParams::class); |
| 30 | + foreach ($class->getProperties() as $property) { |
| 31 | + $attrs = $property->getAttributes(OAT\QueryParameter::class, \ReflectionAttribute::IS_INSTANCEOF); |
| 32 | + if ($attrs !== []) { |
| 33 | + $scanned = $attrs[0]->newInstance(); |
| 34 | + $scanned->_context = new Context([ |
| 35 | + 'nested' => false, |
| 36 | + 'property' => $property->getName(), |
| 37 | + 'reflector' => $property, |
| 38 | + ]); |
| 39 | + $scannedAnnotations[] = $scanned; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + $analysis = new Analysis($scannedAnnotations, new Context()); |
| 44 | + |
| 45 | + self::assertNotEmpty($analysis->getAnnotationsOfType(OAT\QueryParameter::class)); |
| 46 | + |
| 47 | + ($this->processor)($analysis); |
| 48 | + |
| 49 | + self::assertEmpty($analysis->getAnnotationsOfType(OAT\QueryParameter::class)); |
| 50 | + } |
| 51 | + |
| 52 | + #[Test] |
| 53 | + public function itKeepsNestedQueryParameters(): void |
| 54 | + { |
| 55 | + // Simulate a QueryParameter created by AugmentQueryParameterSets (nested=true) |
| 56 | + $property = (new \ReflectionClass(ScannedParams::class))->getProperty('name'); |
| 57 | + $queryParam = new OAT\QueryParameter(description: 'Filter by name'); |
| 58 | + $queryParam->_context = new Context([ |
| 59 | + 'nested' => true, |
| 60 | + 'property' => 'name', |
| 61 | + 'reflector' => $property, |
| 62 | + ]); |
| 63 | + |
| 64 | + $analysis = new Analysis([$queryParam], new Context()); |
| 65 | + |
| 66 | + ($this->processor)($analysis); |
| 67 | + |
| 68 | + self::assertCount(1, $analysis->getAnnotationsOfType(OAT\QueryParameter::class)); |
| 69 | + } |
| 70 | + |
| 71 | + #[Test] |
| 72 | + public function itRemovesDuplicateScannedParametersAcrossClasses(): void |
| 73 | + { |
| 74 | + // Two different classes both with an 'externalId' parameter — the real-world scenario |
| 75 | + $scannedAnnotations = []; |
| 76 | + foreach ([ScannedParams::class, OtherScannedParams::class] as $className) { |
| 77 | + $class = new \ReflectionClass($className); |
| 78 | + foreach ($class->getProperties() as $property) { |
| 79 | + $attrs = $property->getAttributes(OAT\QueryParameter::class, \ReflectionAttribute::IS_INSTANCEOF); |
| 80 | + if ($attrs !== []) { |
| 81 | + $scanned = $attrs[0]->newInstance(); |
| 82 | + $scanned->_context = new Context([ |
| 83 | + 'nested' => false, |
| 84 | + 'property' => $property->getName(), |
| 85 | + 'reflector' => $property, |
| 86 | + ]); |
| 87 | + $scannedAnnotations[] = $scanned; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + $analysis = new Analysis($scannedAnnotations, new Context()); |
| 93 | + |
| 94 | + // Both classes contribute annotations, including duplicate 'externalId' |
| 95 | + self::assertGreaterThan(2, count($analysis->getAnnotationsOfType(OAT\QueryParameter::class))); |
| 96 | + |
| 97 | + ($this->processor)($analysis); |
| 98 | + |
| 99 | + self::assertEmpty($analysis->getAnnotationsOfType(OAT\QueryParameter::class)); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// Test fixtures |
| 104 | + |
| 105 | +class ScannedParams |
| 106 | +{ |
| 107 | + #[OAT\QueryParameter(description: 'Filter by name')] |
| 108 | + public ?string $name = null; |
| 109 | + |
| 110 | + #[OAT\QueryParameter(description: 'Filter by external ID')] |
| 111 | + public ?string $externalId = null; |
| 112 | +} |
| 113 | + |
| 114 | +class OtherScannedParams |
| 115 | +{ |
| 116 | + #[OAT\QueryParameter(description: 'Filter by external ID')] |
| 117 | + public ?string $externalId = null; |
| 118 | + |
| 119 | + #[OAT\QueryParameter(description: 'Filter by status')] |
| 120 | + public ?string $status = null; |
| 121 | +} |
0 commit comments