|
15 | 15 |
|
16 | 16 | use ApiPlatform\Metadata\ApiProperty; |
17 | 17 | use ApiPlatform\Metadata\ApiResource; |
| 18 | +use ApiPlatform\Metadata\Exception\RuntimeException; |
18 | 19 | use ApiPlatform\Metadata\FilterInterface; |
19 | 20 | use ApiPlatform\Metadata\GetCollection; |
20 | 21 | use ApiPlatform\Metadata\Parameters; |
@@ -282,6 +283,193 @@ public function testParameterFactoryWithLimitedProperties(): void |
282 | 283 | $this->assertSame(['name'], $param->getProperties()); |
283 | 284 | } |
284 | 285 |
|
| 286 | + public function testQueryParameterFromPropertyAttributes(): void |
| 287 | + { |
| 288 | + $nameCollection = $this->createStub(PropertyNameCollectionFactoryInterface::class); |
| 289 | + $nameCollection->method('create')->willReturn(new PropertyNameCollection(['id', 'name', 'isActive'])); |
| 290 | + |
| 291 | + $propertyMetadata = $this->createStub(PropertyMetadataFactoryInterface::class); |
| 292 | + $propertyMetadata->method('create')->willReturn( |
| 293 | + new ApiProperty(readable: true), |
| 294 | + ); |
| 295 | + |
| 296 | + $filterLocator = $this->createStub(ContainerInterface::class); |
| 297 | + $filterLocator->method('has')->willReturn(false); |
| 298 | + |
| 299 | + $parameterFactory = new ParameterResourceMetadataCollectionFactory( |
| 300 | + $nameCollection, |
| 301 | + $propertyMetadata, |
| 302 | + new AttributesResourceMetadataCollectionFactory(), |
| 303 | + $filterLocator |
| 304 | + ); |
| 305 | + |
| 306 | + $resourceMetadataCollection = $parameterFactory->create(ParameterOnProperties::class); |
| 307 | + $operation = $resourceMetadataCollection->getOperation(forceCollection: true); |
| 308 | + $parameters = $operation->getParameters(); |
| 309 | + |
| 310 | + $this->assertInstanceOf(Parameters::class, $parameters); |
| 311 | + |
| 312 | + $this->assertTrue($parameters->has('search')); |
| 313 | + $searchParam = $parameters->get('search', QueryParameter::class); |
| 314 | + $this->assertInstanceOf(QueryParameter::class, $searchParam); |
| 315 | + $this->assertSame('search', $searchParam->getKey()); |
| 316 | + $this->assertSame('name', $searchParam->getProperty()); |
| 317 | + $this->assertSame('Search by name', $searchParam->getDescription()); |
| 318 | + |
| 319 | + $this->assertTrue($parameters->has('filter_active')); |
| 320 | + $filterParam = $parameters->get('filter_active', QueryParameter::class); |
| 321 | + $this->assertInstanceOf(QueryParameter::class, $filterParam); |
| 322 | + $this->assertSame('filter_active', $filterParam->getKey()); |
| 323 | + $this->assertSame('isActive', $filterParam->getProperty()); |
| 324 | + $this->assertSame('Filter by active status', $filterParam->getDescription()); |
| 325 | + } |
| 326 | + |
| 327 | + public function testQueryParameterFromPropertyAttributeThrowsExceptionWhenPropertyMismatch(): void |
| 328 | + { |
| 329 | + $this->expectException(RuntimeException::class); |
| 330 | + $this->expectExceptionMessage('Parameter attribute on property "name" must target itself or have no explicit property. Got "property: \'description\'" instead.'); |
| 331 | + |
| 332 | + $nameCollection = $this->createStub(PropertyNameCollectionFactoryInterface::class); |
| 333 | + $nameCollection->method('create')->willReturn(new PropertyNameCollection(['id', 'name', 'description'])); |
| 334 | + |
| 335 | + $propertyMetadata = $this->createStub(PropertyMetadataFactoryInterface::class); |
| 336 | + $propertyMetadata->method('create')->willReturn( |
| 337 | + new ApiProperty(readable: true), |
| 338 | + ); |
| 339 | + |
| 340 | + $filterLocator = $this->createStub(ContainerInterface::class); |
| 341 | + $filterLocator->method('has')->willReturn(false); |
| 342 | + |
| 343 | + $parameterFactory = new ParameterResourceMetadataCollectionFactory( |
| 344 | + $nameCollection, |
| 345 | + $propertyMetadata, |
| 346 | + new AttributesResourceMetadataCollectionFactory(), |
| 347 | + $filterLocator |
| 348 | + ); |
| 349 | + |
| 350 | + $parameterFactory->create(ParameterOnPropertiesMismatchPropertyException::class); |
| 351 | + } |
| 352 | + |
| 353 | + public function testQueryParameterFromPropertyAttributeThrowsExceptionWhenPropertiesMismatch(): void |
| 354 | + { |
| 355 | + $this->expectException(RuntimeException::class); |
| 356 | + $this->expectExceptionMessage('Parameter attribute on property "name" must target itself or have no explicit properties. Got "properties: [description]" instead.'); |
| 357 | + |
| 358 | + $nameCollection = $this->createStub(PropertyNameCollectionFactoryInterface::class); |
| 359 | + $nameCollection->method('create')->willReturn(new PropertyNameCollection(['id', 'name', 'description'])); |
| 360 | + |
| 361 | + $propertyMetadata = $this->createStub(PropertyMetadataFactoryInterface::class); |
| 362 | + $propertyMetadata->method('create')->willReturn( |
| 363 | + new ApiProperty(readable: true), |
| 364 | + ); |
| 365 | + |
| 366 | + $filterLocator = $this->createStub(ContainerInterface::class); |
| 367 | + $filterLocator->method('has')->willReturn(false); |
| 368 | + |
| 369 | + $parameterFactory = new ParameterResourceMetadataCollectionFactory( |
| 370 | + $nameCollection, |
| 371 | + $propertyMetadata, |
| 372 | + new AttributesResourceMetadataCollectionFactory(), |
| 373 | + $filterLocator |
| 374 | + ); |
| 375 | + |
| 376 | + $parameterFactory->create(ParameterOnPropertiesMismatchPropertiesException::class); |
| 377 | + } |
| 378 | + |
| 379 | + public function testQueryParameterFromPropertyAttributeThrowsExceptionWhenPropertiesHasMultipleWithoutSelf(): void |
| 380 | + { |
| 381 | + $this->expectException(RuntimeException::class); |
| 382 | + $this->expectExceptionMessage('Parameter attribute on property "name" must target itself or have no explicit properties. Got "properties: [description, active]" instead.'); |
| 383 | + |
| 384 | + $nameCollection = $this->createStub(PropertyNameCollectionFactoryInterface::class); |
| 385 | + $nameCollection->method('create')->willReturn(new PropertyNameCollection(['id', 'name', 'description', 'active'])); |
| 386 | + |
| 387 | + $propertyMetadata = $this->createStub(PropertyMetadataFactoryInterface::class); |
| 388 | + $propertyMetadata->method('create')->willReturn( |
| 389 | + new ApiProperty(readable: true), |
| 390 | + ); |
| 391 | + |
| 392 | + $filterLocator = $this->createStub(ContainerInterface::class); |
| 393 | + $filterLocator->method('has')->willReturn(false); |
| 394 | + |
| 395 | + $parameterFactory = new ParameterResourceMetadataCollectionFactory( |
| 396 | + $nameCollection, |
| 397 | + $propertyMetadata, |
| 398 | + new AttributesResourceMetadataCollectionFactory(), |
| 399 | + $filterLocator |
| 400 | + ); |
| 401 | + |
| 402 | + $parameterFactory->create(ParameterOnPropertiesMismatchMultiplePropertiesException::class); |
| 403 | + } |
| 404 | + |
| 405 | + public function testQueryParameterFromPropertyAttributePropertiesSingleCorrectProperty(): void |
| 406 | + { |
| 407 | + $nameCollection = $this->createStub(PropertyNameCollectionFactoryInterface::class); |
| 408 | + $nameCollection->method('create')->willReturn(new PropertyNameCollection(['id', 'name', 'description'])); |
| 409 | + |
| 410 | + $propertyMetadata = $this->createStub(PropertyMetadataFactoryInterface::class); |
| 411 | + $propertyMetadata->method('create')->willReturn( |
| 412 | + new ApiProperty(readable: true), |
| 413 | + ); |
| 414 | + |
| 415 | + $filterLocator = $this->createStub(ContainerInterface::class); |
| 416 | + $filterLocator->method('has')->willReturn(false); |
| 417 | + |
| 418 | + $parameterFactory = new ParameterResourceMetadataCollectionFactory( |
| 419 | + $nameCollection, |
| 420 | + $propertyMetadata, |
| 421 | + new AttributesResourceMetadataCollectionFactory(), |
| 422 | + $filterLocator |
| 423 | + ); |
| 424 | + |
| 425 | + $resourceMetadataCollection = $parameterFactory->create(ParameterOnPropertiesSingleCorrectProperty::class); |
| 426 | + $operation = $resourceMetadataCollection->getOperation(forceCollection: true); |
| 427 | + $parameters = $operation->getParameters(); |
| 428 | + |
| 429 | + $this->assertInstanceOf(Parameters::class, $parameters); |
| 430 | + |
| 431 | + $this->assertTrue($parameters->has('search')); |
| 432 | + $searchParam = $parameters->get('search', QueryParameter::class); |
| 433 | + $this->assertInstanceOf(QueryParameter::class, $searchParam); |
| 434 | + $this->assertSame('search', $searchParam->getKey()); |
| 435 | + $this->assertSame('name', $searchParam->getProperty()); |
| 436 | + $this->assertSame(['name'], $searchParam->getProperties()); |
| 437 | + } |
| 438 | + |
| 439 | + public function testQueryParameterFromPropertyAttributePropertiesHasMultipleIncludingSelf(): void |
| 440 | + { |
| 441 | + $nameCollection = $this->createStub(PropertyNameCollectionFactoryInterface::class); |
| 442 | + $nameCollection->method('create')->willReturn(new PropertyNameCollection(['id', 'name', 'description'])); |
| 443 | + |
| 444 | + $propertyMetadata = $this->createStub(PropertyMetadataFactoryInterface::class); |
| 445 | + $propertyMetadata->method('create')->willReturn( |
| 446 | + new ApiProperty(readable: true), |
| 447 | + ); |
| 448 | + |
| 449 | + $filterLocator = $this->createStub(ContainerInterface::class); |
| 450 | + $filterLocator->method('has')->willReturn(false); |
| 451 | + |
| 452 | + $parameterFactory = new ParameterResourceMetadataCollectionFactory( |
| 453 | + $nameCollection, |
| 454 | + $propertyMetadata, |
| 455 | + new AttributesResourceMetadataCollectionFactory(), |
| 456 | + $filterLocator |
| 457 | + ); |
| 458 | + |
| 459 | + $resourceMetadataCollection = $parameterFactory->create(ParameterOnPropertiesMultiplePropertiesIncludingSelf::class); |
| 460 | + $operation = $resourceMetadataCollection->getOperation(forceCollection: true); |
| 461 | + $parameters = $operation->getParameters(); |
| 462 | + |
| 463 | + $this->assertInstanceOf(Parameters::class, $parameters); |
| 464 | + |
| 465 | + $this->assertTrue($parameters->has('search')); |
| 466 | + $searchParam = $parameters->get('search', QueryParameter::class); |
| 467 | + $this->assertInstanceOf(QueryParameter::class, $searchParam); |
| 468 | + $this->assertSame('search', $searchParam->getKey()); |
| 469 | + $this->assertSame('name', $searchParam->getProperty()); |
| 470 | + $this->assertSame(['name'], $searchParam->getProperties()); |
| 471 | + } |
| 472 | + |
285 | 473 | public function testNestedPropertyWithNameConverter(): void |
286 | 474 | { |
287 | 475 | $nameCollection = $this->createStub(PropertyNameCollectionFactoryInterface::class); |
@@ -537,3 +725,60 @@ class NestedTestVariation |
537 | 725 | public ?int $id = null; |
538 | 726 | public ?string $variantName = null; |
539 | 727 | } |
| 728 | + |
| 729 | +#[ApiResource] |
| 730 | +class ParameterOnProperties |
| 731 | +{ |
| 732 | + #[QueryParameter(key: 'search', description: 'Search by name')] |
| 733 | + public string $name = ''; |
| 734 | + |
| 735 | + #[QueryParameter(key: 'filter_active', description: 'Filter by active status')] |
| 736 | + public bool $isActive = true; |
| 737 | +} |
| 738 | + |
| 739 | +#[ApiResource] |
| 740 | +class ParameterOnPropertiesMismatchPropertyException |
| 741 | +{ |
| 742 | + #[QueryParameter(key: 'search', property: 'description')] |
| 743 | + public string $name = ''; |
| 744 | + |
| 745 | + public string $description = ''; |
| 746 | +} |
| 747 | + |
| 748 | +#[ApiResource] |
| 749 | +class ParameterOnPropertiesMismatchPropertiesException |
| 750 | +{ |
| 751 | + #[QueryParameter(key: 'search', properties: ['description'])] |
| 752 | + public string $name = ''; |
| 753 | + |
| 754 | + public string $description = ''; |
| 755 | +} |
| 756 | + |
| 757 | +#[ApiResource] |
| 758 | +class ParameterOnPropertiesMismatchMultiplePropertiesException |
| 759 | +{ |
| 760 | + #[QueryParameter(key: 'search', properties: ['description', 'active'])] |
| 761 | + public string $name = ''; |
| 762 | + |
| 763 | + public string $description = ''; |
| 764 | + |
| 765 | + public bool $active = true; |
| 766 | +} |
| 767 | + |
| 768 | +#[ApiResource] |
| 769 | +class ParameterOnPropertiesSingleCorrectProperty |
| 770 | +{ |
| 771 | + #[QueryParameter(key: 'search', properties: ['name'])] |
| 772 | + public string $name = ''; |
| 773 | + |
| 774 | + public string $description = ''; |
| 775 | +} |
| 776 | + |
| 777 | +#[ApiResource] |
| 778 | +class ParameterOnPropertiesMultiplePropertiesIncludingSelf |
| 779 | +{ |
| 780 | + #[QueryParameter(key: 'search', properties: ['description', 'name'])] |
| 781 | + public string $name = ''; |
| 782 | + |
| 783 | + public string $description = ''; |
| 784 | +} |
0 commit comments