1616use ApiPlatform \Core \Api \IdentifiersExtractorInterface ;
1717use ApiPlatform \Core \Identifier \Normalizer \ChainIdentifierDenormalizer ;
1818use ApiPlatform \Core \Identifier \Normalizer \DateTimeIdentifierDenormalizer ;
19+ use ApiPlatform \Core \Identifier \Normalizer \IntegerDenormalizer ;
1920use ApiPlatform \Core \Metadata \Property \Factory \PropertyMetadataFactoryInterface ;
2021use ApiPlatform \Core \Metadata \Property \PropertyMetadata ;
2122use PHPUnit \Framework \TestCase ;
@@ -31,22 +32,25 @@ public function testCompositeIdentifier()
3132 $ identifier = 'a=1;c=2;d=2015-04-05 ' ;
3233 $ class = 'Dummy ' ;
3334
35+ $ integerPropertyMetadata = (new PropertyMetadata ())->withIdentifier (true )->withType (new Type (Type::BUILTIN_TYPE_INT ));
3436 $ identifierPropertyMetadata = (new PropertyMetadata ())->withIdentifier (true );
3537 $ dateIdentifierPropertyMetadata = (new PropertyMetadata ())->withIdentifier (true )->withType (new Type (Type::BUILTIN_TYPE_OBJECT , false , \DateTime::class));
3638
3739 $ propertyMetadataFactory = $ this ->prophesize (PropertyMetadataFactoryInterface::class);
38- $ propertyMetadataFactory ->create ($ class , 'a ' )->shouldBeCalled ()->willReturn ($ identifierPropertyMetadata );
40+ $ propertyMetadataFactory ->create ($ class , 'a ' )->shouldBeCalled ()->willReturn ($ integerPropertyMetadata );
3941 $ propertyMetadataFactory ->create ($ class , 'c ' )->shouldBeCalled ()->willReturn ($ identifierPropertyMetadata );
4042 $ propertyMetadataFactory ->create ($ class , 'd ' )->shouldBeCalled ()->willReturn ($ dateIdentifierPropertyMetadata );
4143
4244 $ identifiersExtractor = $ this ->prophesize (IdentifiersExtractorInterface::class);
4345 $ identifiersExtractor ->getIdentifiersFromResourceClass ($ class )->willReturn (['a ' , 'c ' , 'd ' ]);
4446
45- $ identifierDenormalizers = [new DateTimeIdentifierDenormalizer ()];
47+ $ identifierDenormalizers = [new IntegerDenormalizer (), new DateTimeIdentifierDenormalizer ()];
4648
4749 $ identifierDenormalizer = new ChainIdentifierDenormalizer ($ identifiersExtractor ->reveal (), $ propertyMetadataFactory ->reveal (), $ identifierDenormalizers );
4850
49- $ this ->assertEquals ($ identifierDenormalizer ->denormalize ($ identifier , $ class ), ['a ' => '1 ' , 'c ' => '2 ' , 'd ' => new \DateTime ('2015-04-05 ' )]);
51+ $ result = $ identifierDenormalizer ->denormalize ($ identifier , $ class );
52+ $ this ->assertEquals (['a ' => 1 , 'c ' => '2 ' , 'd ' => new \DateTime ('2015-04-05 ' )], $ result );
53+ $ this ->assertSame (1 , $ result ['a ' ]);
5054 }
5155
5256 public function testSingleDateIdentifier ()
@@ -67,4 +71,23 @@ public function testSingleDateIdentifier()
6771
6872 $ this ->assertEquals ($ identifierDenormalizer ->denormalize ($ identifier , $ class ), ['funkyid ' => new \DateTime ('2015-04-05 ' )]);
6973 }
74+
75+ public function testIntegerIdentifier ()
76+ {
77+ $ identifier = '42 ' ;
78+ $ class = 'Dummy ' ;
79+
80+ $ integerIdentifierPropertyMetadata = (new PropertyMetadata ())->withIdentifier (true )->withType (new Type (Type::BUILTIN_TYPE_INT ));
81+
82+ $ propertyMetadataFactory = $ this ->prophesize (PropertyMetadataFactoryInterface::class);
83+ $ propertyMetadataFactory ->create ($ class , 'id ' )->shouldBeCalled ()->willReturn ($ integerIdentifierPropertyMetadata );
84+
85+ $ identifiersExtractor = $ this ->prophesize (IdentifiersExtractorInterface::class);
86+ $ identifiersExtractor ->getIdentifiersFromResourceClass ($ class )->willReturn (['id ' ]);
87+
88+ $ identifierDenormalizers = [new IntegerDenormalizer ()];
89+ $ identifierDenormalizer = new ChainIdentifierDenormalizer ($ identifiersExtractor ->reveal (), $ propertyMetadataFactory ->reveal (), $ identifierDenormalizers );
90+
91+ $ this ->assertSame (['id ' => 42 ], $ identifierDenormalizer ->denormalize ($ identifier , $ class ));
92+ }
7093}
0 commit comments