Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/Metadata/Resource/Factory/ExtractorResourceMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,23 @@ private function handleNotFound(?ResourceMetadata $parentPropertyMetadata, strin
}

/**
* Creates a new instance of metadata if the property is not already set.
* Update resource metadata if new config is given.
*/
private function update(ResourceMetadata $resourceMetadata, array $metadata): ResourceMetadata
{
foreach (['shortName', 'description', 'iri', 'itemOperations', 'collectionOperations', 'subresourceOperations', 'graphql', 'attributes'] as $property) {
if (null === $metadata[$property] || null !== $resourceMetadata->{'get'.ucfirst($property)}()) {
$propertyMetadata = $metadata[$property];
$parentPropertyMetadata = $resourceMetadata->{'get'.ucfirst($property)}();

if (null === $propertyMetadata) {
continue;
}

$resourceMetadata = $resourceMetadata->{'with'.ucfirst($property)}($metadata[$property]);
if (\is_array($parentPropertyMetadata) && \is_array($propertyMetadata)) {
$propertyMetadata = array_merge($parentPropertyMetadata, $propertyMetadata);
}

$resourceMetadata = $resourceMetadata->{'with'.ucfirst($property)}($propertyMetadata);
}

return $resourceMetadata;
Expand Down
1 change: 1 addition & 0 deletions tests/Fixtures/FileConfigurations/resources.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<collectionOperation name="my_collection_op">
<attribute name="method">POST</attribute>
<attribute name="path">the/collection/path</attribute>
<attribute name="status">201</attribute>
</collectionOperation>
</collectionOperations>
<subresourceOperations>
Expand Down
1 change: 1 addition & 0 deletions tests/Fixtures/FileConfigurations/resources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ resources:
my_collection_op:
method: 'POST'
path: 'the/collection/path'
status: '201'
subresourceOperations:
my_collection_subresource:
path: 'the/subresource/path'
Expand Down
21 changes: 21 additions & 0 deletions tests/Fixtures/FileConfigurations/resourcesoverride.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8" ?>

<resources xmlns="https://api-platform.com/schema/metadata"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://api-platform.com/schema/metadata
https://api-platform.com/schema/metadata/metadata-2.0.xsd">
<resource class="ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\FileConfigDummy" description="New description">
<itemOperations>
<itemOperation name="my_op_name">
<attribute name="method">GET</attribute>
<attribute name="path">the/path</attribute>
</itemOperation>
</itemOperations>
<collectionOperations>
<collectionOperation name="my_collection_op">
<attribute name="method">POST</attribute>
<attribute name="status">200</attribute>
</collectionOperation>
</collectionOperations>
</resource>
</resources>
11 changes: 11 additions & 0 deletions tests/Fixtures/FileConfigurations/resourcesoverride.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
resources:
'ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\FileConfigDummy':
description: New description
itemOperations:
my_op_name:
method: 'GET'
path: 'the/path'
collectionOperations:
my_collection_op:
method: 'POST'
status: '200'
1 change: 1 addition & 0 deletions tests/Fixtures/FileConfigurations/single_resource.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
my_collection_op:
method: 'POST'
path: 'the/collection/path'
status: '201'
subresourceOperations:
my_collection_subresource:
path: 'the/subresource/path'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,23 @@ public function testXmlExistingParentResourceMetadataFactory(ResourceMetadata $e
$this->assertEquals($expectedResourceMetadata, $resourceMetadata);
}

/**
* @dataProvider overrideOperationsResourceMetadataProvider
*/
public function testXmlOverrideExistingParentResourceMetadataFactory(ResourceMetadata $expectedResourceMetadata)
{
$configPath = __DIR__.'/../../../Fixtures/FileConfigurations/resourcesoverride.xml';

$decorated = $this->prophesize(ResourceMetadataFactoryInterface::class);
$decorated->create(FileConfigDummy::class)->willReturn($this->resourceMetadataProvider()[0][0])->shouldBeCalled();

$resourceMetadataFactory = new ExtractorResourceMetadataFactory(new XmlExtractor([$configPath]), $decorated->reveal());

$resourceMetadata = $resourceMetadataFactory->create(FileConfigDummy::class);

$this->assertEquals($expectedResourceMetadata, $resourceMetadata);
}

/**
* @dataProvider resourceMetadataProvider
*/
Expand Down Expand Up @@ -251,6 +268,23 @@ public function testYamlExistingParentResourceMetadataFactory(ResourceMetadata $
$this->assertEquals($expectedResourceMetadata, $resourceMetadata);
}

/**
* @dataProvider overrideOperationsResourceMetadataProvider
*/
public function testYamlOverrideExistingParentResourceMetadataFactory(ResourceMetadata $expectedResourceMetadata)
{
$configPath = __DIR__.'/../../../Fixtures/FileConfigurations/resourcesoverride.yml';

$decorated = $this->prophesize(ResourceMetadataFactoryInterface::class);
$decorated->create(FileConfigDummy::class)->willReturn($this->resourceMetadataProvider()[0][0])->shouldBeCalled();

$resourceMetadataFactory = new ExtractorResourceMetadataFactory(new YamlExtractor([$configPath]), $decorated->reveal());

$resourceMetadata = $resourceMetadataFactory->create(FileConfigDummy::class);

$this->assertEquals($expectedResourceMetadata, $resourceMetadata);
}

public function testCreateWithMalformedYaml()
{
$this->expectException(InvalidArgumentException::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function resourceMetadataProvider()
'my_other_op_name' => ['method' => 'POST'],
],
'collectionOperations' => [
'my_collection_op' => ['method' => 'POST', 'path' => 'the/collection/path'],
'my_collection_op' => ['method' => 'POST', 'path' => 'the/collection/path', 'status' => '201'],
],
'subresourceOperations' => [
'my_collection_subresource' => ['path' => 'the/subresource/path'],
Expand Down Expand Up @@ -111,4 +111,22 @@ public function legacyOperationsResourceMetadataProvider()

return [[$resourceMetadata]];
}

public function overrideOperationsResourceMetadataProvider()
{
$resourceMetadata = $this->resourceMetadataProvider()[0][0];

$resourceMetadata = $resourceMetadata->withDescription('New description');

$resourceMetadata = $resourceMetadata->withItemOperations([
'my_op_name' => ['method' => 'GET', 'path' => 'the/path'],
'my_other_op_name' => ['method' => 'POST'],
]);

$resourceMetadata = $resourceMetadata->withCollectionOperations([
'my_collection_op' => ['method' => 'POST', 'status' => '200'],
]);

return [[$resourceMetadata]];
}
}