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