22
22
use ApiPlatform \Core \Metadata \Property \PropertyMetadata ;
23
23
use ApiPlatform \Core \Metadata \Property \PropertyNameCollection ;
24
24
use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \Dummy ;
25
+ use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \RelatedDummy ;
25
26
use PHPUnit \Framework \TestCase ;
26
27
use Prophecy \Argument ;
28
+ use Symfony \Component \PropertyInfo \Type ;
29
+ use Symfony \Component \Serializer \NameConverter \NameConverterInterface ;
27
30
use Symfony \Component \Serializer \Normalizer \NormalizerInterface ;
28
31
use Symfony \Component \Serializer \SerializerInterface ;
29
32
@@ -35,25 +38,28 @@ class ItemNormalizerTest extends TestCase
35
38
/**
36
39
* @expectedException \ApiPlatform\Core\Exception\RuntimeException
37
40
*/
38
- public function testDonTSupportDenormalization ()
41
+ public function testDoesNotSupportDenormalization ()
39
42
{
40
43
$ propertyNameCollectionFactoryProphecy = $ this ->prophesize (PropertyNameCollectionFactoryInterface::class);
41
44
$ propertyMetadataFactoryProphecy = $ this ->prophesize (PropertyMetadataFactoryInterface::class);
42
45
$ iriConverterProphecy = $ this ->prophesize (IriConverterInterface::class);
43
46
$ resourceClassResolverProphecy = $ this ->prophesize (ResourceClassResolverInterface::class);
47
+ $ nameConverter = $ this ->prophesize (NameConverterInterface::class);
44
48
45
49
$ normalizer = new ItemNormalizer (
46
50
$ propertyNameCollectionFactoryProphecy ->reveal (),
47
51
$ propertyMetadataFactoryProphecy ->reveal (),
48
52
$ iriConverterProphecy ->reveal (),
49
- $ resourceClassResolverProphecy ->reveal ()
53
+ $ resourceClassResolverProphecy ->reveal (),
54
+ null ,
55
+ $ nameConverter ->reveal ()
50
56
);
51
57
52
58
$ this ->assertFalse ($ normalizer ->supportsDenormalization ('foo ' , ItemNormalizer::FORMAT ));
53
59
$ normalizer ->denormalize (['foo ' ], 'Foo ' );
54
60
}
55
61
56
- public function testSupportNormalization ()
62
+ public function testSupportsNormalization ()
57
63
{
58
64
$ std = new \stdClass ();
59
65
$ dummy = new Dummy ();
@@ -67,11 +73,15 @@ public function testSupportNormalization()
67
73
$ resourceClassResolverProphecy ->getResourceClass ($ dummy )->willReturn (Dummy::class)->shouldBeCalled ();
68
74
$ resourceClassResolverProphecy ->getResourceClass ($ std )->willThrow (new InvalidArgumentException ())->shouldBeCalled ();
69
75
76
+ $ nameConverter = $ this ->prophesize (NameConverterInterface::class);
77
+
70
78
$ normalizer = new ItemNormalizer (
71
79
$ propertyNameCollectionFactoryProphecy ->reveal (),
72
80
$ propertyMetadataFactoryProphecy ->reveal (),
73
81
$ iriConverterProphecy ->reveal (),
74
- $ resourceClassResolverProphecy ->reveal ()
82
+ $ resourceClassResolverProphecy ->reveal (),
83
+ null ,
84
+ $ nameConverter ->reveal ()
75
85
);
76
86
77
87
$ this ->assertTrue ($ normalizer ->supportsNormalization ($ dummy , 'jsonhal ' ));
@@ -81,40 +91,57 @@ public function testSupportNormalization()
81
91
82
92
public function testNormalize ()
83
93
{
94
+ $ relatedDummy = new RelatedDummy ();
84
95
$ dummy = new Dummy ();
85
96
$ dummy ->setName ('hello ' );
97
+ $ dummy ->setRelatedDummy ($ relatedDummy );
86
98
87
- $ propertyNameCollection = new PropertyNameCollection (['name ' ]);
99
+ $ propertyNameCollection = new PropertyNameCollection (['name ' , ' relatedDummy ' ]);
88
100
$ propertyNameCollectionFactoryProphecy = $ this ->prophesize (PropertyNameCollectionFactoryInterface::class);
89
101
$ propertyNameCollectionFactoryProphecy ->create (Dummy::class, [])->willReturn ($ propertyNameCollection )->shouldBeCalled ();
90
102
91
- $ propertyMetadataFactory = new PropertyMetadata (null , null , true );
92
103
$ propertyMetadataFactoryProphecy = $ this ->prophesize (PropertyMetadataFactoryInterface::class);
93
- $ propertyMetadataFactoryProphecy ->create (Dummy::class, 'name ' , [])->willReturn ($ propertyMetadataFactory )->shouldBeCalled ();
104
+ $ propertyMetadataFactoryProphecy ->create (Dummy::class, 'name ' , [])->willReturn (
105
+ new PropertyMetadata (new Type (Type::BUILTIN_TYPE_STRING ), '' , true )
106
+ )->shouldBeCalled ();
107
+ $ propertyMetadataFactoryProphecy ->create (Dummy::class, 'relatedDummy ' , [])->willReturn (
108
+ new PropertyMetadata (new Type (Type::BUILTIN_TYPE_OBJECT , false , RelatedDummy::class), '' , true , false , false )
109
+ )->shouldBeCalled ();
94
110
95
111
$ iriConverterProphecy = $ this ->prophesize (IriConverterInterface::class);
96
- $ iriConverterProphecy ->getIriFromItem ($ dummy )->willReturn ('/dummies/1988 ' )->shouldBeCalled ();
112
+ $ iriConverterProphecy ->getIriFromItem ($ dummy )->willReturn ('/dummies/1 ' )->shouldBeCalled ();
113
+ $ iriConverterProphecy ->getIriFromItem ($ relatedDummy )->willReturn ('/related-dummies/2 ' )->shouldBeCalled ();
97
114
98
115
$ resourceClassResolverProphecy = $ this ->prophesize (ResourceClassResolverInterface::class);
99
116
$ resourceClassResolverProphecy ->getResourceClass ($ dummy , null , true )->willReturn (Dummy::class)->shouldBeCalled ();
100
117
$ resourceClassResolverProphecy ->getResourceClass ($ dummy , Dummy::class, true )->willReturn (Dummy::class)->shouldBeCalled ();
118
+ $ resourceClassResolverProphecy ->isResourceClass (RelatedDummy::class)->willReturn (true )->shouldBeCalled ();
101
119
102
120
$ serializerProphecy = $ this ->prophesize (SerializerInterface::class);
103
121
$ serializerProphecy ->willImplement (NormalizerInterface::class);
104
122
$ serializerProphecy ->normalize ('hello ' , null , Argument::type ('array ' ))->willReturn ('hello ' )->shouldBeCalled ();
105
123
124
+ $ nameConverter = $ this ->prophesize (NameConverterInterface::class);
125
+ $ nameConverter ->normalize ('name ' )->shouldBeCalled ()->willReturn ('name ' );
126
+ $ nameConverter ->normalize ('relatedDummy ' )->shouldBeCalled ()->willReturn ('related_dummy ' );
127
+
106
128
$ normalizer = new ItemNormalizer (
107
129
$ propertyNameCollectionFactoryProphecy ->reveal (),
108
130
$ propertyMetadataFactoryProphecy ->reveal (),
109
131
$ iriConverterProphecy ->reveal (),
110
- $ resourceClassResolverProphecy ->reveal ()
132
+ $ resourceClassResolverProphecy ->reveal (),
133
+ null ,
134
+ $ nameConverter ->reveal ()
111
135
);
112
136
$ normalizer ->setSerializer ($ serializerProphecy ->reveal ());
113
137
114
138
$ expected = [
115
139
'_links ' => [
116
140
'self ' => [
117
- 'href ' => '/dummies/1988 ' ,
141
+ 'href ' => '/dummies/1 ' ,
142
+ ],
143
+ 'related_dummy ' => [
144
+ 'href ' => '/related-dummies/2 ' ,
118
145
],
119
146
],
120
147
'name ' => 'hello ' ,
@@ -124,40 +151,57 @@ public function testNormalize()
124
151
125
152
public function testNormalizeWithoutCache ()
126
153
{
154
+ $ relatedDummy = new RelatedDummy ();
127
155
$ dummy = new Dummy ();
128
156
$ dummy ->setName ('hello ' );
157
+ $ dummy ->setRelatedDummy ($ relatedDummy );
129
158
130
- $ propertyNameCollection = new PropertyNameCollection (['name ' ]);
159
+ $ propertyNameCollection = new PropertyNameCollection (['name ' , ' relatedDummy ' ]);
131
160
$ propertyNameCollectionFactoryProphecy = $ this ->prophesize (PropertyNameCollectionFactoryInterface::class);
132
161
$ propertyNameCollectionFactoryProphecy ->create (Dummy::class, [])->willReturn ($ propertyNameCollection )->shouldBeCalled ();
133
162
134
- $ propertyMetadataFactory = new PropertyMetadata (null , null , true );
135
163
$ propertyMetadataFactoryProphecy = $ this ->prophesize (PropertyMetadataFactoryInterface::class);
136
- $ propertyMetadataFactoryProphecy ->create (Dummy::class, 'name ' , [])->willReturn ($ propertyMetadataFactory )->shouldBeCalled ();
164
+ $ propertyMetadataFactoryProphecy ->create (Dummy::class, 'name ' , [])->willReturn (
165
+ new PropertyMetadata (new Type (Type::BUILTIN_TYPE_STRING ), '' , true )
166
+ )->shouldBeCalled ();
167
+ $ propertyMetadataFactoryProphecy ->create (Dummy::class, 'relatedDummy ' , [])->willReturn (
168
+ new PropertyMetadata (new Type (Type::BUILTIN_TYPE_OBJECT , false , RelatedDummy::class), '' , true , false , false )
169
+ )->shouldBeCalled ();
137
170
138
171
$ iriConverterProphecy = $ this ->prophesize (IriConverterInterface::class);
139
- $ iriConverterProphecy ->getIriFromItem ($ dummy )->willReturn ('/dummies/1988 ' )->shouldBeCalled ();
172
+ $ iriConverterProphecy ->getIriFromItem ($ dummy )->willReturn ('/dummies/1 ' )->shouldBeCalled ();
173
+ $ iriConverterProphecy ->getIriFromItem ($ relatedDummy )->willReturn ('/related-dummies/2 ' )->shouldBeCalled ();
140
174
141
175
$ resourceClassResolverProphecy = $ this ->prophesize (ResourceClassResolverInterface::class);
142
176
$ resourceClassResolverProphecy ->getResourceClass ($ dummy , null , true )->willReturn (Dummy::class)->shouldBeCalled ();
143
177
$ resourceClassResolverProphecy ->getResourceClass ($ dummy , Dummy::class, true )->willReturn (Dummy::class)->shouldBeCalled ();
178
+ $ resourceClassResolverProphecy ->isResourceClass (RelatedDummy::class)->willReturn (true )->shouldBeCalled ();
144
179
145
180
$ serializerProphecy = $ this ->prophesize (SerializerInterface::class);
146
181
$ serializerProphecy ->willImplement (NormalizerInterface::class);
147
182
$ serializerProphecy ->normalize ('hello ' , null , Argument::type ('array ' ))->willReturn ('hello ' )->shouldBeCalled ();
148
183
184
+ $ nameConverter = $ this ->prophesize (NameConverterInterface::class);
185
+ $ nameConverter ->normalize ('name ' )->shouldBeCalled ()->willReturn ('name ' );
186
+ $ nameConverter ->normalize ('relatedDummy ' )->shouldBeCalled ()->willReturn ('related_dummy ' );
187
+
149
188
$ normalizer = new ItemNormalizer (
150
189
$ propertyNameCollectionFactoryProphecy ->reveal (),
151
190
$ propertyMetadataFactoryProphecy ->reveal (),
152
191
$ iriConverterProphecy ->reveal (),
153
- $ resourceClassResolverProphecy ->reveal ()
192
+ $ resourceClassResolverProphecy ->reveal (),
193
+ null ,
194
+ $ nameConverter ->reveal ()
154
195
);
155
196
$ normalizer ->setSerializer ($ serializerProphecy ->reveal ());
156
197
157
198
$ expected = [
158
199
'_links ' => [
159
200
'self ' => [
160
- 'href ' => '/dummies/1988 ' ,
201
+ 'href ' => '/dummies/1 ' ,
202
+ ],
203
+ 'related_dummy ' => [
204
+ 'href ' => '/related-dummies/2 ' ,
161
205
],
162
206
],
163
207
'name ' => 'hello ' ,
0 commit comments