Skip to content

Commit a53464f

Browse files
ewgRaAmrouche Hamza
authored andcommitted
throw NotFound exception when use plain identifiers
1 parent decc69f commit a53464f

File tree

4 files changed

+126
-12
lines changed

4 files changed

+126
-12
lines changed

src/Api/IriPlainIdentifierAwareConverterInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@
1414
namespace ApiPlatform\Core\Api;
1515

1616
use ApiPlatform\Core\Exception\InvalidArgumentException;
17-
use ApiPlatform\Core\Exception\ItemNotFoundException;
18-
use ApiPlatform\Core\Exception\RuntimeException;
1917

2018
/**
21-
* Converts a plain identifier to an IRI
19+
* Converts a plain identifier to an IRI.
2220
*
2321
* @author Hamza Amrouche <[email protected]>
2422
*/

src/Bridge/Symfony/Routing/IriConverter.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515

1616
use ApiPlatform\Core\Api\IdentifiersExtractor;
1717
use ApiPlatform\Core\Api\IdentifiersExtractorInterface;
18-
use ApiPlatform\Core\Api\IriConverterInterface;
1918
use ApiPlatform\Core\Api\IriPlainIdentifierAwareConverterInterface;
2019
use ApiPlatform\Core\Api\OperationType;
21-
use ApiPlatform\Core\Api\PlainIdentifierConverterInterface;
2220
use ApiPlatform\Core\Api\UrlGeneratorInterface;
2321
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
2422
use ApiPlatform\Core\Exception\InvalidArgumentException;

src/Serializer/AbstractItemNormalizer.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use ApiPlatform\Core\Api\IriConverterInterface;
1717
use ApiPlatform\Core\Api\IriPlainIdentifierAwareConverterInterface;
1818
use ApiPlatform\Core\Api\OperationType;
19-
use ApiPlatform\Core\Api\PlainIdentifierConverterInterface;
2019
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
2120
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
2221
use ApiPlatform\Core\Exception\InvalidArgumentException;
@@ -44,7 +43,7 @@ abstract class AbstractItemNormalizer extends AbstractObjectNormalizer
4443
protected $propertyNameCollectionFactory;
4544
protected $propertyMetadataFactory;
4645
/**
47-
* @var IriPlainIdentifierAwareConverterInterface|IriConverterInterface $iriConverter
46+
* @var IriPlainIdentifierAwareConverterInterface|IriConverterInterface
4847
*/
4948
protected $iriConverter;
5049
protected $resourceClassResolver;

tests/Serializer/AbstractItemNormalizerTest.php

Lines changed: 124 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515

1616
use ApiPlatform\Core\Api\IriConverterInterface;
1717
use ApiPlatform\Core\Api\IriPlainIdentifierAwareConverterInterface;
18-
use ApiPlatform\Core\Api\PlainIdentifierConverterInterface;
1918
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
2019
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
2120
use ApiPlatform\Core\Exception\InvalidArgumentException;
21+
use ApiPlatform\Core\Exception\ItemNotFoundException;
2222
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
2323
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
2424
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
@@ -289,6 +289,73 @@ public function testDenormalize()
289289
], Dummy::class);
290290
}
291291

292+
/**
293+
* @expectedException \ApiPlatform\Core\Exception\InvalidArgumentException
294+
* @expectedExceptionMessage NotFound exception
295+
*/
296+
public function testDenormalizeNotFound()
297+
{
298+
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
299+
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn(
300+
new PropertyNameCollection(['name', 'relatedDummy', 'relatedDummies'])
301+
)->shouldBeCalled();
302+
303+
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
304+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn(
305+
new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), '', false, true)
306+
)->shouldBeCalled();
307+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn(
308+
new PropertyMetadata(
309+
new Type(Type::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class),
310+
'',
311+
false,
312+
true,
313+
false,
314+
false
315+
)
316+
)->shouldBeCalled();
317+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', [])->willReturn(
318+
new PropertyMetadata(
319+
new Type(Type::BUILTIN_TYPE_OBJECT,
320+
false,
321+
ArrayCollection::class,
322+
true,
323+
new Type(Type::BUILTIN_TYPE_INT),
324+
new Type(Type::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class)
325+
),
326+
'',
327+
false,
328+
true,
329+
false,
330+
false
331+
)
332+
)->shouldBeCalled();
333+
334+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
335+
$iriConverterProphecy->getItemFromIri('/dummies/1', Argument::type('array'))->willThrow(new ItemNotFoundException('NotFound exception'))->shouldBeCalled();
336+
337+
$propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
338+
339+
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
340+
341+
$serializerProphecy = $this->prophesize(SerializerInterface::class);
342+
$serializerProphecy->willImplement(NormalizerInterface::class);
343+
344+
$normalizer = $this->getMockForAbstractClass(AbstractItemNormalizer::class, [
345+
$propertyNameCollectionFactoryProphecy->reveal(),
346+
$propertyMetadataFactoryProphecy->reveal(),
347+
$iriConverterProphecy->reveal(),
348+
$resourceClassResolverProphecy->reveal(),
349+
$propertyAccessorProphecy->reveal(),
350+
]);
351+
$normalizer->setSerializer($serializerProphecy->reveal());
352+
353+
$normalizer->denormalize([
354+
'name' => 'foo',
355+
'relatedDummy' => '/dummies/1',
356+
], Dummy::class);
357+
}
358+
292359
public function testDenormalizeWritableLinks()
293360
{
294361
$relatedDummy1 = new RelatedDummy();
@@ -683,8 +750,60 @@ public function testDenormalizeRelationWithPlainId()
683750
$serializerProphecy = $this->prophesize(SerializerInterface::class);
684751
$serializerProphecy->willImplement(DenormalizerInterface::class);
685752

686-
$itemDataProviderProphecy = $this->prophesize(ItemDataProviderInterface::class);
687-
$itemDataProviderProphecy->getItem(RelatedDummy::class, 1, null, Argument::type('array'))->shouldNotBeCalled();
753+
$normalizer = $this->getMockForAbstractClass(AbstractItemNormalizer::class, [
754+
$propertyNameCollectionFactoryProphecy->reveal(),
755+
$propertyMetadataFactoryProphecy->reveal(),
756+
$iriConverterProphecy->reveal(),
757+
$resourceClassResolverProphecy->reveal(),
758+
$propertyAccessorProphecy->reveal(),
759+
null,
760+
null,
761+
null,
762+
true,
763+
]);
764+
$normalizer->setSerializer($serializerProphecy->reveal());
765+
766+
$normalizer->denormalize(['relatedDummy' => 1], Dummy::class, 'jsonld');
767+
}
768+
769+
/**
770+
* @expectedException \ApiPlatform\Core\Exception\InvalidArgumentException
771+
* @expectedExceptionMessage Item with id "1" not found for class "ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedDummy".
772+
*/
773+
public function testDenormalizeNotFoundRelationWithPlainId()
774+
{
775+
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
776+
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn(
777+
new PropertyNameCollection(['relatedDummy'])
778+
)->shouldBeCalled();
779+
780+
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
781+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn(
782+
new PropertyMetadata(
783+
new Type(Type::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class),
784+
'',
785+
false,
786+
true,
787+
false,
788+
false
789+
)
790+
)->shouldBeCalled();
791+
792+
$iriConverterProphecy = $this->prophesize(IriPlainIdentifierAwareConverterInterface::class);
793+
794+
$getItemFromIriSecondArgCallback = function ($arg) {
795+
return is_array($arg) && isset($arg['fetch_data']) && true === $arg['fetch_data'];
796+
};
797+
798+
$iriConverterProphecy->getItemFromIri('/related_dummies/1', Argument::that($getItemFromIriSecondArgCallback))->shouldBeCalled()->willThrow(new ItemNotFoundException('Item with id "1" not found for class "ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedDummy".'));
799+
$iriConverterProphecy->getIriFromPlainIdentifier('1', RelatedDummy::class)->shouldBeCalled()->willReturn('/related_dummies/1');
800+
$propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
801+
802+
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
803+
$resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true)->shouldBeCalled();
804+
805+
$serializerProphecy = $this->prophesize(SerializerInterface::class);
806+
$serializerProphecy->willImplement(DenormalizerInterface::class);
688807

689808
$normalizer = $this->getMockForAbstractClass(AbstractItemNormalizer::class, [
690809
$propertyNameCollectionFactoryProphecy->reveal(),
@@ -695,7 +814,7 @@ public function testDenormalizeRelationWithPlainId()
695814
null,
696815
null,
697816
null,
698-
true
817+
true,
699818
]);
700819
$normalizer->setSerializer($serializerProphecy->reveal());
701820

@@ -725,7 +844,7 @@ public function testTriggerDeprecation()
725844
null,
726845
null,
727846
$itemDataProviderProphecy->reveal(),
728-
true
847+
true,
729848
]);
730849
$normalizer->setSerializer($serializerProphecy->reveal());
731850
}

0 commit comments

Comments
 (0)