|
17 | 17 | use ApiPlatform\Hydra\Tests\Fixtures\Foo; |
18 | 18 | use ApiPlatform\JsonLd\ContextBuilder; |
19 | 19 | use ApiPlatform\JsonLd\ContextBuilderInterface; |
| 20 | +use ApiPlatform\Metadata\ApiResource; |
| 21 | +use ApiPlatform\Metadata\GetCollection; |
20 | 22 | use ApiPlatform\Metadata\IriConverterInterface; |
| 23 | +use ApiPlatform\Metadata\Operations; |
| 24 | +use ApiPlatform\Metadata\Post; |
| 25 | +use ApiPlatform\Metadata\Patch; |
| 26 | +use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
| 27 | +use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; |
21 | 28 | use ApiPlatform\Metadata\ResourceClassResolverInterface; |
22 | 29 | use ApiPlatform\Metadata\UrlGeneratorInterface; |
23 | 30 | use ApiPlatform\Serializer\AbstractItemNormalizer; |
@@ -445,4 +452,139 @@ public function testNormalizeResourceCollectionWithoutPrefix(): void |
445 | 452 | 'totalItems' => 2, |
446 | 453 | ], $actual); |
447 | 454 | } |
| 455 | + |
| 456 | + public function testNormalizeCollectionWithHydraOperations(): void |
| 457 | + { |
| 458 | + $fooOne = new Foo(); |
| 459 | + $fooOne->id = 1; |
| 460 | + $fooOne->bar = 'baz'; |
| 461 | + |
| 462 | + $data = [$fooOne]; |
| 463 | + |
| 464 | + $normalizedFooOne = [ |
| 465 | + '@id' => '/foos/1', |
| 466 | + '@type' => 'Foo', |
| 467 | + 'bar' => 'baz', |
| 468 | + ]; |
| 469 | + |
| 470 | + $contextBuilderProphecy = $this->prophesize(ContextBuilderInterface::class); |
| 471 | + $contextBuilderProphecy->getResourceContextUri(Foo::class)->willReturn('/contexts/Foo'); |
| 472 | + |
| 473 | + $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class); |
| 474 | + $resourceClassResolverProphecy->getResourceClass($data, Foo::class)->willReturn(Foo::class); |
| 475 | + |
| 476 | + $iriConverterProphecy = $this->prophesize(IriConverterInterface::class); |
| 477 | + $iriConverterProphecy->getIriFromResource(Foo::class, UrlGeneratorInterface::ABS_PATH, Argument::any(), Argument::any())->willReturn('/foos'); |
| 478 | + |
| 479 | + $resourceMetadataCollectionFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class); |
| 480 | + $resourceMetadataCollectionFactoryProphecy->create(Foo::class)->willReturn(new ResourceMetadataCollection('Foo', [ |
| 481 | + (new ApiResource()) |
| 482 | + ->withShortName('Foo') |
| 483 | + ->withOperations(new Operations(['get' => (new GetCollection())->withShortName('Foo'), 'post' => (new Post())->withShortName('Foo')])), |
| 484 | + ])); |
| 485 | + |
| 486 | + $delegateNormalizerProphecy = $this->prophesize(NormalizerInterface::class); |
| 487 | + $delegateNormalizerProphecy->normalize($fooOne, CollectionNormalizer::FORMAT, Argument::allOf( |
| 488 | + Argument::withEntry('resource_class', Foo::class), |
| 489 | + Argument::withEntry('api_sub_level', true) |
| 490 | + ))->willReturn($normalizedFooOne); |
| 491 | + |
| 492 | + $normalizer = new CollectionNormalizer( |
| 493 | + $contextBuilderProphecy->reveal(), |
| 494 | + $resourceClassResolverProphecy->reveal(), |
| 495 | + $iriConverterProphecy->reveal(), |
| 496 | + $resourceMetadataCollectionFactoryProphecy->reveal(), |
| 497 | + ['hydra_prefix' => false, 'hydra_operations' => true] |
| 498 | + ); |
| 499 | + $normalizer->setNormalizer($delegateNormalizerProphecy->reveal()); |
| 500 | + |
| 501 | + $actual = $normalizer->normalize($data, CollectionNormalizer::FORMAT, [ |
| 502 | + 'operation_name' => 'get', |
| 503 | + 'resource_class' => Foo::class, |
| 504 | + ]); |
| 505 | + |
| 506 | + $this->assertArrayHasKey('supportedOperation', $actual); |
| 507 | + $this->assertIsArray($actual['supportedOperation']); |
| 508 | + $this->assertNotEmpty($actual['supportedOperation']); |
| 509 | + |
| 510 | + $methods = array_map(fn($op) => $op['method'] ?? null, $actual['supportedOperation']); |
| 511 | + $this->assertContains('GET', $methods); |
| 512 | + $this->assertContains('POST', $methods); |
| 513 | + |
| 514 | + $this->assertArrayHasKey('@context', $actual); |
| 515 | + $this->assertArrayHasKey('@id', $actual); |
| 516 | + $this->assertArrayHasKey('@type', $actual); |
| 517 | + $this->assertArrayHasKey('member', $actual); |
| 518 | + $this->assertArrayHasKey('totalItems', $actual); |
| 519 | + } |
| 520 | + |
| 521 | + public function testNormalizeCollectionWithMultipleApiResourcesAndHydraOperations(): void |
| 522 | + { |
| 523 | + $fooOne = new Foo(); |
| 524 | + $fooOne->id = 1; |
| 525 | + $fooOne->bar = 'baz'; |
| 526 | + |
| 527 | + $data = [$fooOne]; |
| 528 | + |
| 529 | + $normalizedFooOne = [ |
| 530 | + '@id' => '/foos/1', |
| 531 | + '@type' => 'Foo', |
| 532 | + 'bar' => 'baz', |
| 533 | + ]; |
| 534 | + |
| 535 | + $contextBuilderProphecy = $this->prophesize(ContextBuilderInterface::class); |
| 536 | + $contextBuilderProphecy->getResourceContextUri(Foo::class)->willReturn('/contexts/Foo'); |
| 537 | + |
| 538 | + $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class); |
| 539 | + $resourceClassResolverProphecy->getResourceClass($data, Foo::class)->willReturn(Foo::class); |
| 540 | + |
| 541 | + $iriConverterProphecy = $this->prophesize(IriConverterInterface::class); |
| 542 | + $iriConverterProphecy->getIriFromResource(Foo::class, UrlGeneratorInterface::ABS_PATH, Argument::any(), Argument::any())->willReturn('/foos'); |
| 543 | + |
| 544 | + $resourceMetadataCollectionFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class); |
| 545 | + $resourceMetadataCollectionFactoryProphecy->create(Foo::class)->willReturn(new ResourceMetadataCollection('Foo', [ |
| 546 | + (new ApiResource()) |
| 547 | + ->withShortName('Foo') |
| 548 | + ->withOperations(new Operations(['get' => (new GetCollection())->withShortName('Foo')])), |
| 549 | + (new ApiResource()) |
| 550 | + ->withShortName('Foo') |
| 551 | + ->withOperations(new Operations(['post' => (new Post())->withShortName('Foo')])), |
| 552 | + ])); |
| 553 | + |
| 554 | + $delegateNormalizerProphecy = $this->prophesize(NormalizerInterface::class); |
| 555 | + $delegateNormalizerProphecy->normalize($fooOne, CollectionNormalizer::FORMAT, Argument::allOf( |
| 556 | + Argument::withEntry('resource_class', Foo::class), |
| 557 | + Argument::withEntry('api_sub_level', true) |
| 558 | + ))->willReturn($normalizedFooOne); |
| 559 | + |
| 560 | + $normalizer = new CollectionNormalizer( |
| 561 | + $contextBuilderProphecy->reveal(), |
| 562 | + $resourceClassResolverProphecy->reveal(), |
| 563 | + $iriConverterProphecy->reveal(), |
| 564 | + $resourceMetadataCollectionFactoryProphecy->reveal(), |
| 565 | + ['hydra_prefix' => false, 'hydra_operations' => true] |
| 566 | + ); |
| 567 | + $normalizer->setNormalizer($delegateNormalizerProphecy->reveal()); |
| 568 | + |
| 569 | + $actual = $normalizer->normalize($data, CollectionNormalizer::FORMAT, [ |
| 570 | + 'operation_name' => 'get', |
| 571 | + 'resource_class' => Foo::class, |
| 572 | + ]); |
| 573 | + |
| 574 | + $this->assertArrayHasKey('supportedOperation', $actual); |
| 575 | + $this->assertIsArray($actual['supportedOperation']); |
| 576 | + |
| 577 | + $this->assertCount(2, $actual['supportedOperation']); |
| 578 | + |
| 579 | + $methods = array_map(fn($op) => $op['method'] ?? null, $actual['supportedOperation']); |
| 580 | + $this->assertContains('GET', $methods); |
| 581 | + $this->assertContains('POST', $methods); |
| 582 | + |
| 583 | + $this->assertArrayHasKey('@context', $actual); |
| 584 | + $this->assertArrayHasKey('@id', $actual); |
| 585 | + $this->assertArrayHasKey('@type', $actual); |
| 586 | + $this->assertEquals('Collection', $actual['@type']); |
| 587 | + $this->assertArrayHasKey('member', $actual); |
| 588 | + $this->assertArrayHasKey('totalItems', $actual); |
| 589 | + } |
448 | 590 | } |
0 commit comments