Skip to content

Commit 6c0e55c

Browse files
committed
Prefix root resource route_prefix to sub-resources
Unit test also added
1 parent 91e5bb2 commit 6c0e55c

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/Operation/Factory/SubresourceOperationFactory.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,14 @@ private function computeSubresourceOperations(string $resourceClass, array &$tre
127127
$operation['operation_name']
128128
);
129129

130+
$prefix = trim(trim($rootResourceMetadata->getAttribute('route_prefix', '')), '/');
131+
if ('' !== $prefix) {
132+
$prefix .= '/';
133+
}
134+
130135
$operation['path'] = $subresourceOperation['path'] ?? sprintf(
131-
'/%s/{id}/%s%s',
136+
'/%s%s/{id}/%s%s',
137+
$prefix,
132138
$this->pathSegmentNameGenerator->getSegmentName($rootShortname, true),
133139
$this->pathSegmentNameGenerator->getSegmentName($operation['property'], $operation['collection']),
134140
self::FORMAT_SUFFIX

tests/Operation/Factory/SubresourceOperationFactoryTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,4 +607,49 @@ public function testCreateWithEndButNoCollection()
607607
] + SubresourceOperationFactory::ROUTE_OPTIONS,
608608
], $result);
609609
}
610+
611+
public function testCreateWithRootResourcePrefix()
612+
{
613+
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
614+
$resourceMetadataFactoryProphecy->create(RelatedDummyEntity::class)->shouldBeCalled()->willReturn(new ResourceMetadata('relatedDummyEntity'));
615+
$resourceMetadataFactoryProphecy->create(DummyEntity::class)->shouldBeCalled()->willReturn(new ResourceMetadata('dummyEntity', null, null, null, null, ['route_prefix' => 'root_resource_prefix']));
616+
617+
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
618+
$propertyNameCollectionFactoryProphecy->create(DummyEntity::class)->shouldBeCalled()->willReturn(new PropertyNameCollection(['subresource']));
619+
$propertyNameCollectionFactoryProphecy->create(RelatedDummyEntity::class)->shouldBeCalled()->willReturn(new PropertyNameCollection(['bar', 'anotherSubresource']));
620+
621+
$subresourceMetadataCollectionWithMaxDepth = (new PropertyMetadata())->withSubresource(new SubresourceMetadata(RelatedDummyEntity::class, false, 1));
622+
$anotherSubresourceMetadata = (new PropertyMetadata())->withSubresource(new SubresourceMetadata(DummyEntity::class, false));
623+
624+
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
625+
$propertyMetadataFactoryProphecy->create(DummyEntity::class, 'subresource')->shouldBeCalled()->willReturn($subresourceMetadataCollectionWithMaxDepth);
626+
$propertyMetadataFactoryProphecy->create(RelatedDummyEntity::class, 'bar')->shouldBeCalled()->willReturn(new PropertyMetadata());
627+
$propertyMetadataFactoryProphecy->create(RelatedDummyEntity::class, 'anotherSubresource')->shouldBeCalled()->willReturn($anotherSubresourceMetadata);
628+
629+
$pathSegmentNameGeneratorProphecy = $this->prophesize(PathSegmentNameGeneratorInterface::class);
630+
$pathSegmentNameGeneratorProphecy->getSegmentName('dummyEntity', true)->shouldBeCalled()->willReturn('dummy_entities');
631+
$pathSegmentNameGeneratorProphecy->getSegmentName('subresource', false)->shouldBeCalled()->willReturn('subresource');
632+
633+
$subresourceOperationFactory = new SubresourceOperationFactory(
634+
$resourceMetadataFactoryProphecy->reveal(),
635+
$propertyNameCollectionFactoryProphecy->reveal(),
636+
$propertyMetadataFactoryProphecy->reveal(),
637+
$pathSegmentNameGeneratorProphecy->reveal()
638+
);
639+
640+
$this->assertEquals([
641+
'api_dummy_entities_subresource_get_subresource' => [
642+
'property' => 'subresource',
643+
'collection' => false,
644+
'resource_class' => RelatedDummyEntity::class,
645+
'shortNames' => ['relatedDummyEntity', 'dummyEntity'],
646+
'identifiers' => [
647+
['id', DummyEntity::class, true],
648+
],
649+
'route_name' => 'api_dummy_entities_subresource_get_subresource',
650+
'path' => '/root_resource_prefix/dummy_entities/{id}/subresource.{_format}',
651+
'operation_name' => 'subresource_get_subresource',
652+
] + SubresourceOperationFactory::ROUTE_OPTIONS,
653+
], $subresourceOperationFactory->create(DummyEntity::class));
654+
}
610655
}

0 commit comments

Comments
 (0)