16
16
use ApiPlatform \Core \Api \IdentifiersExtractorInterface ;
17
17
use ApiPlatform \Core \Identifier \Normalizer \ChainIdentifierDenormalizer ;
18
18
use ApiPlatform \Core \Identifier \Normalizer \DateTimeIdentifierDenormalizer ;
19
+ use ApiPlatform \Core \Identifier \Normalizer \IntegerDenormalizer ;
19
20
use ApiPlatform \Core \Metadata \Property \Factory \PropertyMetadataFactoryInterface ;
20
21
use ApiPlatform \Core \Metadata \Property \PropertyMetadata ;
21
22
use PHPUnit \Framework \TestCase ;
@@ -31,22 +32,25 @@ public function testCompositeIdentifier()
31
32
$ identifier = 'a=1;c=2;d=2015-04-05 ' ;
32
33
$ class = 'Dummy ' ;
33
34
35
+ $ integerPropertyMetadata = (new PropertyMetadata ())->withIdentifier (true )->withType (new Type (Type::BUILTIN_TYPE_INT ));
34
36
$ identifierPropertyMetadata = (new PropertyMetadata ())->withIdentifier (true );
35
37
$ dateIdentifierPropertyMetadata = (new PropertyMetadata ())->withIdentifier (true )->withType (new Type (Type::BUILTIN_TYPE_OBJECT , false , \DateTime::class));
36
38
37
39
$ propertyMetadataFactory = $ this ->prophesize (PropertyMetadataFactoryInterface::class);
38
- $ propertyMetadataFactory ->create ($ class , 'a ' )->shouldBeCalled ()->willReturn ($ identifierPropertyMetadata );
40
+ $ propertyMetadataFactory ->create ($ class , 'a ' )->shouldBeCalled ()->willReturn ($ integerPropertyMetadata );
39
41
$ propertyMetadataFactory ->create ($ class , 'c ' )->shouldBeCalled ()->willReturn ($ identifierPropertyMetadata );
40
42
$ propertyMetadataFactory ->create ($ class , 'd ' )->shouldBeCalled ()->willReturn ($ dateIdentifierPropertyMetadata );
41
43
42
44
$ identifiersExtractor = $ this ->prophesize (IdentifiersExtractorInterface::class);
43
45
$ identifiersExtractor ->getIdentifiersFromResourceClass ($ class )->willReturn (['a ' , 'c ' , 'd ' ]);
44
46
45
- $ identifierDenormalizers = [new DateTimeIdentifierDenormalizer ()];
47
+ $ identifierDenormalizers = [new IntegerDenormalizer (), new DateTimeIdentifierDenormalizer ()];
46
48
47
49
$ identifierDenormalizer = new ChainIdentifierDenormalizer ($ identifiersExtractor ->reveal (), $ propertyMetadataFactory ->reveal (), $ identifierDenormalizers );
48
50
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 ' ]);
50
54
}
51
55
52
56
public function testSingleDateIdentifier ()
@@ -67,4 +71,23 @@ public function testSingleDateIdentifier()
67
71
68
72
$ this ->assertEquals ($ identifierDenormalizer ->denormalize ($ identifier , $ class ), ['funkyid ' => new \DateTime ('2015-04-05 ' )]);
69
73
}
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
+ }
70
93
}
0 commit comments