|
17 | 17 | use ApiPlatform\Metadata\ApiResource; |
18 | 18 | use ApiPlatform\Metadata\Exception\RuntimeException; |
19 | 19 | use ApiPlatform\Metadata\FilterInterface; |
| 20 | +use ApiPlatform\Metadata\Get; |
20 | 21 | use ApiPlatform\Metadata\GetCollection; |
21 | 22 | use ApiPlatform\Metadata\HeaderParameter; |
22 | 23 | use ApiPlatform\Metadata\Parameters; |
@@ -658,6 +659,78 @@ public function testHeaderParameterFromPropertyAttributePropertiesHasMultipleInc |
658 | 659 | $this->assertSame(['authToken'], $authParam->getProperties()); |
659 | 660 | } |
660 | 661 |
|
| 662 | + public function testQueryParameterOnPropertiesWithOperations(): void |
| 663 | + { |
| 664 | + $nameCollection = $this->createStub(PropertyNameCollectionFactoryInterface::class); |
| 665 | + $nameCollection->method('create')->willReturn(new PropertyNameCollection(['id', 'name'])); |
| 666 | + |
| 667 | + $propertyMetadata = $this->createStub(PropertyMetadataFactoryInterface::class); |
| 668 | + $propertyMetadata->method('create')->willReturn( |
| 669 | + new ApiProperty(readable: true), |
| 670 | + ); |
| 671 | + |
| 672 | + $filterLocator = $this->createStub(ContainerInterface::class); |
| 673 | + $filterLocator->method('has')->willReturn(false); |
| 674 | + |
| 675 | + $parameterFactory = new ParameterResourceMetadataCollectionFactory( |
| 676 | + $nameCollection, |
| 677 | + $propertyMetadata, |
| 678 | + new AttributesResourceMetadataCollectionFactory(), |
| 679 | + $filterLocator |
| 680 | + ); |
| 681 | + |
| 682 | + $resourceMetadataCollection = $parameterFactory->create(QueryParameterOnPropertiesWithOperations::class); |
| 683 | + $operations = array_values(iterator_to_array($resourceMetadataCollection[0]->getOperations())); |
| 684 | + |
| 685 | + $collectionOperation = $operations[0]; |
| 686 | + $this->assertInstanceOf(GetCollection::class, $collectionOperation); |
| 687 | + $collectionParameters = $collectionOperation->getParameters(); |
| 688 | + $this->assertTrue($collectionParameters->has('search')); |
| 689 | + $this->assertFalse($collectionParameters->has('filter_id')); |
| 690 | + |
| 691 | + $getOperation = $operations[1]; |
| 692 | + $this->assertInstanceOf(Get::class, $getOperation); |
| 693 | + $getParameters = $getOperation->getParameters(); |
| 694 | + $this->assertTrue($getParameters->has('search')); |
| 695 | + $this->assertTrue($getParameters->has('filter_id')); |
| 696 | + } |
| 697 | + |
| 698 | + public function testHeaderParameterOnPropertiesWithOperations(): void |
| 699 | + { |
| 700 | + $nameCollection = $this->createStub(PropertyNameCollectionFactoryInterface::class); |
| 701 | + $nameCollection->method('create')->willReturn(new PropertyNameCollection(['id', 'authToken', 'apiKey'])); |
| 702 | + |
| 703 | + $propertyMetadata = $this->createStub(PropertyMetadataFactoryInterface::class); |
| 704 | + $propertyMetadata->method('create')->willReturn( |
| 705 | + new ApiProperty(readable: true), |
| 706 | + ); |
| 707 | + |
| 708 | + $filterLocator = $this->createStub(ContainerInterface::class); |
| 709 | + $filterLocator->method('has')->willReturn(false); |
| 710 | + |
| 711 | + $parameterFactory = new ParameterResourceMetadataCollectionFactory( |
| 712 | + $nameCollection, |
| 713 | + $propertyMetadata, |
| 714 | + new AttributesResourceMetadataCollectionFactory(), |
| 715 | + $filterLocator |
| 716 | + ); |
| 717 | + |
| 718 | + $resourceMetadataCollection = $parameterFactory->create(HeaderParameterOnPropertiesWithOperations::class); |
| 719 | + $operations = array_values(iterator_to_array($resourceMetadataCollection[0]->getOperations())); |
| 720 | + |
| 721 | + $collectionOperation = $operations[0]; |
| 722 | + $this->assertInstanceOf(GetCollection::class, $collectionOperation); |
| 723 | + $collectionParameters = $collectionOperation->getParameters(); |
| 724 | + $this->assertFalse($collectionParameters->has('X-Authorization', HeaderParameter::class)); |
| 725 | + $this->assertTrue($collectionParameters->has('X-API-Key', HeaderParameter::class)); |
| 726 | + |
| 727 | + $getOperation = $operations[1]; |
| 728 | + $this->assertInstanceOf(Get::class, $getOperation); |
| 729 | + $getParameters = $getOperation->getParameters(); |
| 730 | + $this->assertTrue($getParameters->has('X-Authorization', HeaderParameter::class)); |
| 731 | + $this->assertTrue($getParameters->has('X-API-Key', HeaderParameter::class)); |
| 732 | + } |
| 733 | + |
661 | 734 | public function testNestedPropertyWithNameConverter(): void |
662 | 735 | { |
663 | 736 | $nameCollection = $this->createStub(PropertyNameCollectionFactoryInterface::class); |
@@ -1027,3 +1100,33 @@ class HeaderParameterOnPropertiesMismatchMultiplePropertiesException |
1027 | 1100 |
|
1028 | 1101 | public string $token2 = ''; |
1029 | 1102 | } |
| 1103 | + |
| 1104 | +#[ApiResource( |
| 1105 | + operations: [ |
| 1106 | + new GetCollection(), |
| 1107 | + new Get(), |
| 1108 | + ] |
| 1109 | +)] |
| 1110 | +class QueryParameterOnPropertiesWithOperations |
| 1111 | +{ |
| 1112 | + #[QueryParameter(key: 'search', description: 'Search by name', operations: [new GetCollection(), new Get()])] |
| 1113 | + public string $name = ''; |
| 1114 | + |
| 1115 | + #[QueryParameter(key: 'filter_id', description: 'Filter by ID', operations: [new Get()])] |
| 1116 | + public int $id = 0; |
| 1117 | +} |
| 1118 | + |
| 1119 | +#[ApiResource( |
| 1120 | + operations: [ |
| 1121 | + new GetCollection(), |
| 1122 | + new Get(), |
| 1123 | + ] |
| 1124 | +)] |
| 1125 | +class HeaderParameterOnPropertiesWithOperations |
| 1126 | +{ |
| 1127 | + #[HeaderParameter(key: 'X-Authorization', description: 'Authorization header', operations: [new Get()])] |
| 1128 | + public string $authToken = ''; |
| 1129 | + |
| 1130 | + #[HeaderParameter(key: 'X-API-Key', description: 'API key header', operations: [new GetCollection(), new Get()])] |
| 1131 | + public string $apiKey = ''; |
| 1132 | +} |
0 commit comments