Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 50d6a38

Browse filesBrowse files
committed
extract tests for groups context option
1 parent 07b2102 commit 50d6a38
Copy full SHA for 50d6a38

File tree

5 files changed

+115
-158
lines changed
Filter options

5 files changed

+115
-158
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
+5-1Lines 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.
+82Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
31+
$this->assertEquals([
32+
'bar' => 'bar',
33+
], $normalizer->normalize($obj, null, ['groups' => ['c']]));
34+
35+
$this->assertEquals([
36+
'symfony' => 'symfony',
37+
'foo' => 'foo',
38+
'fooBar' => 'fooBar',
39+
'bar' => 'bar',
40+
'kevin' => 'kevin',
41+
'coopTilleuls' => 'coopTilleuls',
42+
], $normalizer->normalize($obj, null, ['groups' => ['a', 'c']]));
43+
}
44+
45+
public function testGroupsDenormalize()
46+
{
47+
$normalizer = $this->getDenormalizerForGroups();
48+
49+
$obj = new GroupDummy();
50+
$obj->setFoo('foo');
51+
52+
$data = ['foo' => 'foo', 'bar' => 'bar'];
53+
54+
$normalized = $normalizer->denormalize(
55+
$data,
56+
GroupDummy::class,
57+
null,
58+
['groups' => ['a']]
59+
);
60+
$this->assertEquals($obj, $normalized);
61+
62+
$obj->setBar('bar');
63+
64+
$normalized = $normalizer->denormalize(
65+
$data,
66+
GroupDummy::class,
67+
null,
68+
['groups' => ['a', 'b']]
69+
);
70+
$this->assertEquals($obj, $normalized);
71+
}
72+
73+
public function testNormalizeNoPropertyInGroup()
74+
{
75+
$normalizer = $this->getNormalizerForGroups();
76+
77+
$obj = new GroupDummy();
78+
$obj->setFoo('foo');
79+
80+
$this->assertEquals([], $normalizer->normalize($obj, null, ['groups' => ['notExist']]));
81+
}
82+
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php
+8-48Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
1717
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
1818
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
19+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
1920
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
2021
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2122
use Symfony\Component\Serializer\Serializer;
@@ -24,9 +25,12 @@
2425
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
2526
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
2627
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
28+
use Symfony\Component\Serializer\Tests\Normalizer\Features\GroupsTestTrait;
2729

2830
class GetSetMethodNormalizerTest extends TestCase
2931
{
32+
use GroupsTestTrait;
33+
3034
/**
3135
* @var GetSetMethodNormalizer
3236
*/
@@ -185,62 +189,18 @@ public function testConstructorWArgWithPrivateMutator()
185189
$this->assertEquals('bar', $obj->getFoo());
186190
}
187191

188-
public function testGroupsNormalize()
192+
protected function getNormalizerForGroups(): NormalizerInterface
189193
{
190194
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
191-
$this->normalizer = new GetSetMethodNormalizer($classMetadataFactory);
192-
$this->normalizer->setSerializer($this->serializer);
193195

194-
$obj = new GroupDummy();
195-
$obj->setFoo('foo');
196-
$obj->setBar('bar');
197-
$obj->setFooBar('fooBar');
198-
$obj->setSymfony('symfony');
199-
$obj->setKevin('kevin');
200-
$obj->setCoopTilleuls('coopTilleuls');
201-
202-
$this->assertEquals([
203-
'bar' => 'bar',
204-
], $this->normalizer->normalize($obj, null, [GetSetMethodNormalizer::GROUPS => ['c']]));
205-
206-
$this->assertEquals([
207-
'symfony' => 'symfony',
208-
'foo' => 'foo',
209-
'fooBar' => 'fooBar',
210-
'bar' => 'bar',
211-
'kevin' => 'kevin',
212-
'coopTilleuls' => 'coopTilleuls',
213-
], $this->normalizer->normalize($obj, null, [GetSetMethodNormalizer::GROUPS => ['a', 'c']]));
196+
return new GetSetMethodNormalizer($classMetadataFactory);
214197
}
215198

216-
public function testGroupsDenormalize()
199+
protected function getDenormalizerForGroups(): DenormalizerInterface
217200
{
218201
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
219-
$this->normalizer = new GetSetMethodNormalizer($classMetadataFactory);
220-
$this->normalizer->setSerializer($this->serializer);
221202

222-
$obj = new GroupDummy();
223-
$obj->setFoo('foo');
224-
225-
$toNormalize = ['foo' => 'foo', 'bar' => 'bar'];
226-
227-
$normalized = $this->normalizer->denormalize(
228-
$toNormalize,
229-
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
230-
null,
231-
[GetSetMethodNormalizer::GROUPS => ['a']]
232-
);
233-
$this->assertEquals($obj, $normalized);
234-
235-
$obj->setBar('bar');
236-
237-
$normalized = $this->normalizer->denormalize(
238-
$toNormalize,
239-
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
240-
null,
241-
[GetSetMethodNormalizer::GROUPS => ['a', 'b']]
242-
);
243-
$this->assertEquals($obj, $normalized);
203+
return new GetSetMethodNormalizer($classMetadataFactory);
244204
}
245205

246206
public function testGroupsNormalizeWithNameConverter()

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php
+11-60Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php
+9-49Lines changed: 9 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
1717
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
1818
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
19+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
20+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
1921
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
2022
use Symfony\Component\Serializer\Serializer;
2123
use Symfony\Component\Serializer\SerializerInterface;
@@ -24,9 +26,12 @@
2426
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
2527
use Symfony\Component\Serializer\Tests\Fixtures\PropertyCircularReferenceDummy;
2628
use Symfony\Component\Serializer\Tests\Fixtures\PropertySiblingHolder;
29+
use Symfony\Component\Serializer\Tests\Normalizer\Features\GroupsTestTrait;
2730

2831
class PropertyNormalizerTest extends TestCase
2932
{
33+
use GroupsTestTrait;
34+
3035
/**
3136
* @var PropertyNormalizer
3237
*/
@@ -204,63 +209,18 @@ private function doTestIgnoredAttributes(bool $legacy = false)
204209
);
205210
}
206211

207-
public function testGroupsNormalize()
212+
protected function getNormalizerForGroups(): NormalizerInterface
208213
{
209214
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
210-
$this->normalizer = new PropertyNormalizer($classMetadataFactory);
211-
$this->normalizer->setSerializer($this->serializer);
212215

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']]));
216+
return new PropertyNormalizer($classMetadataFactory);
234217
}
235218

236-
public function testGroupsDenormalize()
219+
protected function getDenormalizerForGroups(): DenormalizerInterface
237220
{
238221
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
239-
$this->normalizer = new PropertyNormalizer($classMetadataFactory);
240-
$this->normalizer->setSerializer($this->serializer);
241222

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);
223+
return new PropertyNormalizer($classMetadataFactory);
264224
}
265225

266226
public function testGroupsNormalizeWithNameConverter()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.