Skip to content

Commit 37adf7f

Browse files
committed
extract tests for groups context option
1 parent d3c6901 commit 37adf7f

File tree

5 files changed

+111
-158
lines changed

5 files changed

+111
-158
lines changed

src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
3535
/* constants to configure the context */
3636
const CIRCULAR_REFERENCE_LIMIT = 'circular_reference_limit';
3737
const OBJECT_TO_POPULATE = 'object_to_populate';
38-
const GROUPS = 'groups';
38+
39+
/**
40+
* Only (de)normalize attributes that are in the specified groups.
41+
*/
42+
public const GROUPS = 'groups';
3943

4044
/**
4145
* Limit (de)normalize to the specified names.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace Symfony\Component\Serializer\Tests\Normalizer\Features;
4+
5+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
6+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
7+
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
8+
9+
/**
10+
* Test AbstractNormalizer::GROUPS.
11+
*/
12+
trait GroupsTestTrait
13+
{
14+
abstract protected function getNormalizerForGroups(): NormalizerInterface;
15+
16+
abstract protected function getDenormalizerForGroups(): DenormalizerInterface;
17+
18+
public function testGroupsNormalize()
19+
{
20+
$normalizer = $this->getNormalizerForGroups();
21+
22+
$obj = new GroupDummy();
23+
$obj->setFoo('foo');
24+
$obj->setBar('bar');
25+
$obj->setFooBar('fooBar');
26+
$obj->setSymfony('symfony');
27+
$obj->setKevin('kevin');
28+
$obj->setCoopTilleuls('coopTilleuls');
29+
30+
$this->assertEquals([
31+
'bar' => 'bar',
32+
], $normalizer->normalize($obj, null, ['groups' => ['c']]));
33+
34+
$this->assertEquals([
35+
'symfony' => 'symfony',
36+
'foo' => 'foo',
37+
'fooBar' => 'fooBar',
38+
'bar' => 'bar',
39+
'kevin' => 'kevin',
40+
'coopTilleuls' => 'coopTilleuls',
41+
], $normalizer->normalize($obj, null, ['groups' => ['a', 'c']]));
42+
}
43+
44+
public function testGroupsDenormalize()
45+
{
46+
$normalizer = $this->getDenormalizerForGroups();
47+
48+
$obj = new GroupDummy();
49+
$obj->setFoo('foo');
50+
51+
$data = ['foo' => 'foo', 'bar' => 'bar'];
52+
53+
$normalized = $normalizer->denormalize(
54+
$data,
55+
GroupDummy::class,
56+
null,
57+
['groups' => ['a']]
58+
);
59+
$this->assertEquals($obj, $normalized);
60+
61+
$obj->setBar('bar');
62+
63+
$normalized = $normalizer->denormalize(
64+
$data,
65+
GroupDummy::class,
66+
null,
67+
['groups' => ['a', 'b']]
68+
);
69+
$this->assertEquals($obj, $normalized);
70+
}
71+
72+
public function testNormalizeNoPropertyInGroup()
73+
{
74+
$normalizer = $this->getNormalizerForGroups();
75+
76+
$obj = new GroupDummy();
77+
$obj->setFoo('foo');
78+
79+
$this->assertEquals([], $normalizer->normalize($obj, null, ['groups' => ['notExist']]));
80+
}
81+
}

src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@
2525
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
2626
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
2727
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
28+
use Symfony\Component\Serializer\Tests\Normalizer\Features\GroupsTestTrait;
2829

2930
class GetSetMethodNormalizerTest extends TestCase
3031
{
32+
use GroupsTestTrait;
33+
3134
/**
3235
* @var GetSetMethodNormalizer
3336
*/
@@ -202,62 +205,18 @@ public function testConstructorWArgWithPrivateMutator()
202205
$this->assertEquals('bar', $obj->getFoo());
203206
}
204207

205-
public function testGroupsNormalize()
208+
protected function getNormalizerForGroups(): GetSetMethodNormalizer
206209
{
207210
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
208-
$this->normalizer = new GetSetMethodNormalizer($classMetadataFactory);
209-
$this->normalizer->setSerializer($this->serializer);
210211

211-
$obj = new GroupDummy();
212-
$obj->setFoo('foo');
213-
$obj->setBar('bar');
214-
$obj->setFooBar('fooBar');
215-
$obj->setSymfony('symfony');
216-
$obj->setKevin('kevin');
217-
$obj->setCoopTilleuls('coopTilleuls');
218-
219-
$this->assertEquals([
220-
'bar' => 'bar',
221-
], $this->normalizer->normalize($obj, null, [GetSetMethodNormalizer::GROUPS => ['c']]));
222-
223-
$this->assertEquals([
224-
'symfony' => 'symfony',
225-
'foo' => 'foo',
226-
'fooBar' => 'fooBar',
227-
'bar' => 'bar',
228-
'kevin' => 'kevin',
229-
'coopTilleuls' => 'coopTilleuls',
230-
], $this->normalizer->normalize($obj, null, [GetSetMethodNormalizer::GROUPS => ['a', 'c']]));
212+
return new GetSetMethodNormalizer($classMetadataFactory);
231213
}
232214

233-
public function testGroupsDenormalize()
215+
protected function getDenormalizerForGroups(): GetSetMethodNormalizer
234216
{
235217
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
236-
$this->normalizer = new GetSetMethodNormalizer($classMetadataFactory);
237-
$this->normalizer->setSerializer($this->serializer);
238218

239-
$obj = new GroupDummy();
240-
$obj->setFoo('foo');
241-
242-
$toNormalize = ['foo' => 'foo', 'bar' => 'bar'];
243-
244-
$normalized = $this->normalizer->denormalize(
245-
$toNormalize,
246-
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
247-
null,
248-
[GetSetMethodNormalizer::GROUPS => ['a']]
249-
);
250-
$this->assertEquals($obj, $normalized);
251-
252-
$obj->setBar('bar');
253-
254-
$normalized = $this->normalizer->denormalize(
255-
$toNormalize,
256-
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
257-
null,
258-
[GetSetMethodNormalizer::GROUPS => ['a', 'b']]
259-
);
260-
$this->assertEquals($obj, $normalized);
219+
return new GetSetMethodNormalizer($classMetadataFactory);
261220
}
262221

263222
public function testGroupsNormalizeWithNameConverter()

src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 11 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
2525
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
2626
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
27+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
2728
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2829
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
2930
use Symfony\Component\Serializer\Serializer;
@@ -34,6 +35,7 @@
3435
use Symfony\Component\Serializer\Tests\Fixtures\NotSerializedConstructorArgumentDummy;
3536
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
3637
use Symfony\Component\Serializer\Tests\Normalizer\Features\AttributesTestTrait;
38+
use Symfony\Component\Serializer\Tests\Normalizer\Features\GroupsTestTrait;
3739
use Symfony\Component\Serializer\Tests\Normalizer\Features\IgnoredAttributesTestTrait;
3840

3941
/**
@@ -43,6 +45,7 @@ class ObjectNormalizerTest extends TestCase
4345
{
4446
use AttributesTestTrait;
4547
use IgnoredAttributesTestTrait;
48+
use GroupsTestTrait;
4649

4750
/**
4851
* @var ObjectNormalizer
@@ -266,74 +269,22 @@ public function testFillWithEmptyDataWhenMissingData()
266269
$this->assertEquals(new DummyValueObject(10, '', null), $result);
267270
}
268271

269-
public function testGroupsNormalize()
272+
protected function getNormalizerForGroups(): ObjectNormalizer
270273
{
271274
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
272-
$this->normalizer = new ObjectNormalizer($classMetadataFactory);
273-
$this->normalizer->setSerializer($this->serializer);
274-
275-
$obj = new GroupDummy();
276-
$obj->setFoo('foo');
277-
$obj->setBar('bar');
278-
$obj->setFooBar('fooBar');
279-
$obj->setSymfony('symfony');
280-
$obj->setKevin('kevin');
281-
$obj->setCoopTilleuls('coopTilleuls');
282-
283-
$this->assertEquals([
284-
'bar' => 'bar',
285-
], $this->normalizer->normalize($obj, null, [ObjectNormalizer::GROUPS => ['c']]));
286-
287-
$this->assertEquals([
288-
'symfony' => 'symfony',
289-
'foo' => 'foo',
290-
'fooBar' => 'fooBar',
291-
'bar' => 'bar',
292-
'kevin' => 'kevin',
293-
'coopTilleuls' => 'coopTilleuls',
294-
], $this->normalizer->normalize($obj, null, [ObjectNormalizer::GROUPS => ['a', 'c']]));
295-
}
296-
297-
public function testGroupsDenormalize()
298-
{
299-
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
300-
$this->normalizer = new ObjectNormalizer($classMetadataFactory);
301-
$this->normalizer->setSerializer($this->serializer);
302-
303-
$obj = new GroupDummy();
304-
$obj->setFoo('foo');
305-
306-
$toNormalize = ['foo' => 'foo', 'bar' => 'bar'];
307-
308-
$normalized = $this->normalizer->denormalize(
309-
$toNormalize,
310-
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
311-
null,
312-
[ObjectNormalizer::GROUPS => ['a']]
313-
);
314-
$this->assertEquals($obj, $normalized);
315-
316-
$obj->setBar('bar');
275+
$normalizer = new ObjectNormalizer($classMetadataFactory);
276+
// instantiate a serializer with the normalizer to handle normalizing recursive structures
277+
new Serializer([$normalizer]);
317278

318-
$normalized = $this->normalizer->denormalize(
319-
$toNormalize,
320-
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
321-
null,
322-
[ObjectNormalizer::GROUPS => ['a', 'b']]
323-
);
324-
$this->assertEquals($obj, $normalized);
279+
return $normalizer;
325280
}
326281

327-
public function testNormalizeNoPropertyInGroup()
282+
protected function getDenormalizerForGroups(): DenormalizerInterface
328283
{
329284
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
330-
$this->normalizer = new ObjectNormalizer($classMetadataFactory);
331-
$this->normalizer->setSerializer($this->serializer);
285+
$normalizer = new ObjectNormalizer($classMetadataFactory);
332286

333-
$obj = new GroupDummy();
334-
$obj->setFoo('foo');
335-
336-
$this->assertEquals([], $this->normalizer->normalize($obj, null, ['groups' => ['notExist']]));
287+
return $normalizer;
337288
}
338289

339290
public function testGroupsNormalizeWithNameConverter()

src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
2525
use Symfony\Component\Serializer\Tests\Fixtures\PropertyCircularReferenceDummy;
2626
use Symfony\Component\Serializer\Tests\Fixtures\PropertySiblingHolder;
27+
use Symfony\Component\Serializer\Tests\Normalizer\Features\GroupsTestTrait;
2728

2829
class PropertyNormalizerTest extends TestCase
2930
{
31+
use GroupsTestTrait;
32+
3033
/**
3134
* @var PropertyNormalizer
3235
*/
@@ -204,63 +207,18 @@ private function doTestIgnoredAttributes(bool $legacy = false)
204207
);
205208
}
206209

207-
public function testGroupsNormalize()
210+
protected function getNormalizerForGroups(): PropertyNormalizer
208211
{
209212
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
210-
$this->normalizer = new PropertyNormalizer($classMetadataFactory);
211-
$this->normalizer->setSerializer($this->serializer);
212213

213-
$obj = new GroupDummy();
214-
$obj->setFoo('foo');
215-
$obj->setBar('bar');
216-
$obj->setFooBar('fooBar');
217-
$obj->setSymfony('symfony');
218-
$obj->setKevin('kevin');
219-
$obj->setCoopTilleuls('coopTilleuls');
220-
221-
$this->assertEquals([
222-
'bar' => 'bar',
223-
], $this->normalizer->normalize($obj, null, [PropertyNormalizer::GROUPS => ['c']]));
224-
225-
// The PropertyNormalizer is also able to hydrate properties from parent classes
226-
$this->assertEquals([
227-
'symfony' => 'symfony',
228-
'foo' => 'foo',
229-
'fooBar' => 'fooBar',
230-
'bar' => 'bar',
231-
'kevin' => 'kevin',
232-
'coopTilleuls' => 'coopTilleuls',
233-
], $this->normalizer->normalize($obj, null, [PropertyNormalizer::GROUPS => ['a', 'c']]));
214+
return new PropertyNormalizer($classMetadataFactory);
234215
}
235216

236-
public function testGroupsDenormalize()
217+
protected function getDenormalizerForGroups(): PropertyNormalizer
237218
{
238219
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
239-
$this->normalizer = new PropertyNormalizer($classMetadataFactory);
240-
$this->normalizer->setSerializer($this->serializer);
241220

242-
$obj = new GroupDummy();
243-
$obj->setFoo('foo');
244-
245-
$toNormalize = ['foo' => 'foo', 'bar' => 'bar'];
246-
247-
$normalized = $this->normalizer->denormalize(
248-
$toNormalize,
249-
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
250-
null,
251-
[PropertyNormalizer::GROUPS => ['a']]
252-
);
253-
$this->assertEquals($obj, $normalized);
254-
255-
$obj->setBar('bar');
256-
257-
$normalized = $this->normalizer->denormalize(
258-
$toNormalize,
259-
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
260-
null,
261-
[PropertyNormalizer::GROUPS => ['a', 'b']]
262-
);
263-
$this->assertEquals($obj, $normalized);
221+
return new PropertyNormalizer($classMetadataFactory);
264222
}
265223

266224
public function testGroupsNormalizeWithNameConverter()

0 commit comments

Comments
 (0)