15
15
16
16
use ApiPlatform \Core \Api \IriConverterInterface ;
17
17
use ApiPlatform \Core \Api \IriPlainIdentifierAwareConverterInterface ;
18
- use ApiPlatform \Core \Api \PlainIdentifierConverterInterface ;
19
18
use ApiPlatform \Core \Api \ResourceClassResolverInterface ;
20
19
use ApiPlatform \Core \DataProvider \ItemDataProviderInterface ;
21
20
use ApiPlatform \Core \Exception \InvalidArgumentException ;
21
+ use ApiPlatform \Core \Exception \ItemNotFoundException ;
22
22
use ApiPlatform \Core \Metadata \Property \Factory \PropertyMetadataFactoryInterface ;
23
23
use ApiPlatform \Core \Metadata \Property \Factory \PropertyNameCollectionFactoryInterface ;
24
24
use ApiPlatform \Core \Metadata \Property \PropertyMetadata ;
@@ -289,6 +289,73 @@ public function testDenormalize()
289
289
], Dummy::class);
290
290
}
291
291
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
+
292
359
public function testDenormalizeWritableLinks ()
293
360
{
294
361
$ relatedDummy1 = new RelatedDummy ();
@@ -683,8 +750,60 @@ public function testDenormalizeRelationWithPlainId()
683
750
$ serializerProphecy = $ this ->prophesize (SerializerInterface::class);
684
751
$ serializerProphecy ->willImplement (DenormalizerInterface::class);
685
752
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);
688
807
689
808
$ normalizer = $ this ->getMockForAbstractClass (AbstractItemNormalizer::class, [
690
809
$ propertyNameCollectionFactoryProphecy ->reveal (),
@@ -695,7 +814,7 @@ public function testDenormalizeRelationWithPlainId()
695
814
null ,
696
815
null ,
697
816
null ,
698
- true
817
+ true ,
699
818
]);
700
819
$ normalizer ->setSerializer ($ serializerProphecy ->reveal ());
701
820
@@ -725,7 +844,7 @@ public function testTriggerDeprecation()
725
844
null ,
726
845
null ,
727
846
$ itemDataProviderProphecy ->reveal (),
728
- true
847
+ true ,
729
848
]);
730
849
$ normalizer ->setSerializer ($ serializerProphecy ->reveal ());
731
850
}
0 commit comments