|
30 | 30 | use PHPUnit\Framework\TestCase; |
31 | 31 | use Psr\Container\ContainerInterface; |
32 | 32 | use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; |
| 33 | +use Symfony\Component\TypeInfo\Type; |
33 | 34 |
|
34 | 35 | class ParameterResourceMetadataCollectionFactoryTest extends TestCase |
35 | 36 | { |
@@ -278,6 +279,110 @@ public function testNestedPropertyWithNameConverter(): void |
278 | 279 | $this->assertSame('related.nested', $searchNestedParam->getProperty()); |
279 | 280 | $this->assertSame('search[related.nested]', $searchNestedParam->getKey()); |
280 | 281 | } |
| 282 | + |
| 283 | + private function createNestedPropertyFactory(): ParameterResourceMetadataCollectionFactory |
| 284 | + { |
| 285 | + $nameCollection = $this->createStub(PropertyNameCollectionFactoryInterface::class); |
| 286 | + $nameCollection->method('create')->willReturn(new PropertyNameCollection(['id', 'name'])); |
| 287 | + |
| 288 | + $propertyMetadata = $this->createStub(PropertyMetadataFactoryInterface::class); |
| 289 | + $propertyMetadata->method('create')->willReturnCallback( |
| 290 | + static function (string $class, string $property): ApiProperty { |
| 291 | + if (NestedTestOrder::class === $class && 'product' === $property) { |
| 292 | + return new ApiProperty(readable: true, nativeType: Type::object(NestedTestProduct::class)); |
| 293 | + } |
| 294 | + if (NestedTestProduct::class === $class && 'productVariations' === $property) { |
| 295 | + return new ApiProperty(readable: true, nativeType: Type::list(Type::object(NestedTestVariation::class))); |
| 296 | + } |
| 297 | + |
| 298 | + return new ApiProperty(readable: true); |
| 299 | + } |
| 300 | + ); |
| 301 | + |
| 302 | + $filterLocator = $this->createStub(ContainerInterface::class); |
| 303 | + $filterLocator->method('has')->willReturn(false); |
| 304 | + |
| 305 | + return new ParameterResourceMetadataCollectionFactory( |
| 306 | + $nameCollection, |
| 307 | + $propertyMetadata, |
| 308 | + new AttributesResourceMetadataCollectionFactory(), |
| 309 | + $filterLocator, |
| 310 | + new CamelCaseToSnakeCaseNameConverter(), |
| 311 | + ); |
| 312 | + } |
| 313 | + |
| 314 | + public function testNestedPropertyInfoOnSingularProperty(): void |
| 315 | + { |
| 316 | + $factory = $this->createNestedPropertyFactory(); |
| 317 | + $collection = $factory->create(NestedTestOrder::class); |
| 318 | + $operation = $collection->getOperation(forceCollection: true); |
| 319 | + $parameters = $operation->getParameters(); |
| 320 | + |
| 321 | + $param = $parameters->get('product.name'); |
| 322 | + $this->assertNotNull($param, 'Parameter product.name should exist'); |
| 323 | + |
| 324 | + $extra = $param->getExtraProperties(); |
| 325 | + $this->assertArrayHasKey('nested_property_info', $extra); |
| 326 | + |
| 327 | + $info = $extra['nested_property_info']; |
| 328 | + $this->assertSame(['product'], $info['relation_segments']); |
| 329 | + $this->assertSame(['product'], $info['converted_relation_segments']); |
| 330 | + $this->assertSame('name', $info['leaf_property']); |
| 331 | + $this->assertSame(NestedTestProduct::class, $info['leaf_class']); |
| 332 | + $this->assertSame([NestedTestOrder::class], $info['relation_classes']); |
| 333 | + } |
| 334 | + |
| 335 | + public function testNestedPropertyInfoOnDeeplyNestedProperty(): void |
| 336 | + { |
| 337 | + $factory = $this->createNestedPropertyFactory(); |
| 338 | + $collection = $factory->create(NestedTestOrder::class); |
| 339 | + $operation = $collection->getOperation('deep_collection'); |
| 340 | + $parameters = $operation->getParameters(); |
| 341 | + |
| 342 | + $param = $parameters->get('product.productVariations.variantName'); |
| 343 | + $this->assertNotNull($param, 'Parameter product.productVariations.variantName should exist'); |
| 344 | + |
| 345 | + $extra = $param->getExtraProperties(); |
| 346 | + $this->assertArrayHasKey('nested_property_info', $extra); |
| 347 | + |
| 348 | + $info = $extra['nested_property_info']; |
| 349 | + $this->assertSame(['product', 'productVariations'], $info['relation_segments']); |
| 350 | + $this->assertSame(['product', 'product_variations'], $info['converted_relation_segments']); |
| 351 | + $this->assertSame('variant_name', $info['leaf_property']); |
| 352 | + $this->assertSame(NestedTestVariation::class, $info['leaf_class']); |
| 353 | + $this->assertSame([NestedTestOrder::class, NestedTestProduct::class], $info['relation_classes']); |
| 354 | + } |
| 355 | + |
| 356 | + public function testNestedPropertyInfoOnExpandedPlaceholderParameter(): void |
| 357 | + { |
| 358 | + $factory = $this->createNestedPropertyFactory(); |
| 359 | + $collection = $factory->create(NestedTestOrder::class); |
| 360 | + $operation = $collection->getOperation('search_collection'); |
| 361 | + $parameters = $operation->getParameters(); |
| 362 | + |
| 363 | + $searchProductName = $parameters->get('search[product.name]'); |
| 364 | + $this->assertNotNull($searchProductName, 'Parameter search[product.name] should exist'); |
| 365 | + |
| 366 | + $extra = $searchProductName->getExtraProperties(); |
| 367 | + $this->assertArrayHasKey('nested_property_info', $extra); |
| 368 | + |
| 369 | + $info = $extra['nested_property_info']; |
| 370 | + $this->assertSame(['product'], $info['relation_segments']); |
| 371 | + $this->assertSame('name', $info['leaf_property']); |
| 372 | + $this->assertSame(NestedTestProduct::class, $info['leaf_class']); |
| 373 | + } |
| 374 | + |
| 375 | + public function testSimplePropertyHasNoNestedPropertyInfo(): void |
| 376 | + { |
| 377 | + $factory = $this->createNestedPropertyFactory(); |
| 378 | + $collection = $factory->create(NestedTestOrder::class); |
| 379 | + $operation = $collection->getOperation(forceCollection: true); |
| 380 | + $parameters = $operation->getParameters(); |
| 381 | + |
| 382 | + $param = $parameters->get('name'); |
| 383 | + $this->assertNotNull($param); |
| 384 | + $this->assertArrayNotHasKey('nested_property_info', $param->getExtraProperties()); |
| 385 | + } |
281 | 386 | } |
282 | 387 |
|
283 | 388 | #[ApiResource( |
@@ -319,3 +424,52 @@ class HasNestedParameterAttribute |
319 | 424 | public $name; |
320 | 425 | public $related; |
321 | 426 | } |
| 427 | + |
| 428 | +#[ApiResource( |
| 429 | + operations: [ |
| 430 | + new GetCollection( |
| 431 | + parameters: [ |
| 432 | + 'name' => new QueryParameter(), |
| 433 | + 'product.name' => new QueryParameter(property: 'product.name'), |
| 434 | + ] |
| 435 | + ), |
| 436 | + new GetCollection( |
| 437 | + uriTemplate: '/nested_test_orders/deep', |
| 438 | + name: 'deep_collection', |
| 439 | + parameters: [ |
| 440 | + 'product.productVariations.variantName' => new QueryParameter(property: 'product.productVariations.variantName'), |
| 441 | + ] |
| 442 | + ), |
| 443 | + new GetCollection( |
| 444 | + uriTemplate: '/nested_test_orders/search', |
| 445 | + name: 'search_collection', |
| 446 | + parameters: [ |
| 447 | + 'search[:property]' => new QueryParameter( |
| 448 | + properties: ['name', 'product.name'] |
| 449 | + ), |
| 450 | + ] |
| 451 | + ), |
| 452 | + ] |
| 453 | +)] |
| 454 | +class NestedTestOrder |
| 455 | +{ |
| 456 | + public ?int $id = null; |
| 457 | + public ?string $name = null; |
| 458 | + public ?NestedTestProduct $product = null; |
| 459 | +} |
| 460 | + |
| 461 | +#[ApiResource] |
| 462 | +class NestedTestProduct |
| 463 | +{ |
| 464 | + public ?int $id = null; |
| 465 | + public ?string $name = null; |
| 466 | + /** @var NestedTestVariation[] */ |
| 467 | + public array $productVariations = []; |
| 468 | +} |
| 469 | + |
| 470 | +#[ApiResource] |
| 471 | +class NestedTestVariation |
| 472 | +{ |
| 473 | + public ?int $id = null; |
| 474 | + public ?string $variantName = null; |
| 475 | +} |
0 commit comments