Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4fb71a7

Browse files
committed
use traits in get set method normalizer test
1 parent e8eae77 commit 4fb71a7

File tree

3 files changed

+104
-62
lines changed

3 files changed

+104
-62
lines changed

src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectInner.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,24 @@ class ObjectInner
66
{
77
public $foo;
88
public $bar;
9+
10+
public function getBar()
11+
{
12+
return $this->bar;
13+
}
14+
15+
public function setBar($bar): void
16+
{
17+
$this->bar = $bar;
18+
}
19+
20+
public function getFoo()
21+
{
22+
return $this->foo;
23+
}
24+
25+
public function setFoo($foo): void
26+
{
27+
$this->foo = $foo;
28+
}
929
}

src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectOuter.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,29 @@ class ObjectOuter
1414
*/
1515
private $inners;
1616

17+
public function getFoo()
18+
{
19+
return $this->foo;
20+
}
21+
22+
public function setFoo($foo): void
23+
{
24+
$this->foo = $foo;
25+
}
26+
27+
public function getBar()
28+
{
29+
return $this->bar;
30+
}
31+
32+
public function setBar($bar): void
33+
{
34+
$this->bar = $bar;
35+
}
36+
37+
/**
38+
* @return ObjectInner
39+
*/
1740
public function getInner()
1841
{
1942
return $this->inner;

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

Lines changed: 61 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
1717
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
1818
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
19+
use Symfony\Component\Serializer\Exception\CircularReferenceException;
1920
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
2021
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
2122
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
@@ -32,17 +33,21 @@
3233
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
3334
use Symfony\Component\Serializer\Tests\Normalizer\Features\CallbacksObject;
3435
use Symfony\Component\Serializer\Tests\Normalizer\Features\CallbacksTestTrait;
36+
use Symfony\Component\Serializer\Tests\Normalizer\Features\CircularReferenceTestTrait;
3537
use Symfony\Component\Serializer\Tests\Normalizer\Features\ConstructorArgumentsTestTrait;
3638
use Symfony\Component\Serializer\Tests\Normalizer\Features\GroupsTestTrait;
39+
use Symfony\Component\Serializer\Tests\Normalizer\Features\IgnoredAttributesTestTrait;
3740
use Symfony\Component\Serializer\Tests\Normalizer\Features\MaxDepthTestTrait;
3841
use Symfony\Component\Serializer\Tests\Normalizer\Features\ObjectToPopulateTestTrait;
3942
use Symfony\Component\Serializer\Tests\Normalizer\Features\TypeEnforcementTestTrait;
4043

4144
class GetSetMethodNormalizerTest extends TestCase
4245
{
4346
use CallbacksTestTrait;
47+
use CircularReferenceTestTrait;
4448
use ConstructorArgumentsTestTrait;
4549
use GroupsTestTrait;
50+
use IgnoredAttributesTestTrait;
4651
use MaxDepthTestTrait;
4752
use ObjectToPopulateTestTrait;
4853
use TypeEnforcementTestTrait;
@@ -242,6 +247,48 @@ public function testLegacyCallbacks($callbacks, $value, $result)
242247
);
243248
}
244249

250+
protected function getNormalizerForCircularReference(): GetSetMethodNormalizer
251+
{
252+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
253+
$normalizer = new GetSetMethodNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory));
254+
new Serializer([$normalizer]);
255+
256+
return $normalizer;
257+
}
258+
259+
protected function getSelfReferencingModel()
260+
{
261+
return new CircularReferenceDummy();
262+
}
263+
264+
public function testLegacyUnableToNormalizeCircularReference()
265+
{
266+
$this->normalizer->setCircularReferenceLimit(2);
267+
$this->serializer = new Serializer([$this->normalizer]);
268+
$this->normalizer->setSerializer($this->serializer);
269+
270+
$obj = new CircularReferenceDummy();
271+
272+
$this->expectException(CircularReferenceException::class);
273+
$this->normalizer->normalize($obj);
274+
}
275+
276+
public function testLegacyCircularReferenceHandler()
277+
{
278+
$handler = function ($obj) {
279+
return \get_class($obj);
280+
};
281+
282+
$this->normalizer->setCircularReferenceHandler($handler);
283+
$this->serializer = new Serializer([$this->normalizer]);
284+
$this->normalizer->setSerializer($this->serializer);
285+
286+
$obj = new CircularReferenceDummy();
287+
288+
$expected = ['me' => CircularReferenceDummy::class];
289+
$this->assertEquals($expected, $this->normalizer->normalize($obj));
290+
}
291+
245292
protected function getDenormalizerForConstructArguments(): GetSetMethodNormalizer
246293
{
247294
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
@@ -341,25 +388,28 @@ public function testRejectInvalidKey()
341388
$this->markTestSkipped('This test makes no sense with the GetSetMethodNormalizer');
342389
}
343390

344-
345-
346-
347-
348-
349-
public function testIgnoredAttributes()
391+
protected function getNormalizerForIgnoredAttributes(): GetSetMethodNormalizer
350392
{
351-
$this->doTestIgnoredAttributes();
393+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
394+
$normalizer = new GetSetMethodNormalizer($classMetadataFactory, null, new PhpDocExtractor());
395+
new Serializer([$normalizer]);
396+
397+
return $normalizer;
352398
}
353399

354-
public function testLegacyIgnoredAttributes()
400+
protected function getDenormalizerForIgnoredAttributes(): GetSetMethodNormalizer
355401
{
356-
$this->doTestIgnoredAttributes(true);
402+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
403+
$normalizer = new GetSetMethodNormalizer($classMetadataFactory, null, new PhpDocExtractor());
404+
new Serializer([$normalizer]);
405+
406+
return $normalizer;
357407
}
358408

359-
private function doTestIgnoredAttributes(bool $legacy = false)
409+
public function testLegacyIgnoredAttributes()
360410
{
361411
$ignoredAttributes = ['foo', 'bar', 'baz', 'camelCase', 'object'];
362-
$legacy ? $this->normalizer->setIgnoredAttributes($ignoredAttributes) : $this->createNormalizer([GetSetMethodNormalizer::IGNORED_ATTRIBUTES => $ignoredAttributes]);
412+
$this->normalizer->setIgnoredAttributes($ignoredAttributes);
363413

364414
$obj = new GetSetDummy();
365415
$obj->setFoo('foo');
@@ -388,32 +438,6 @@ public function testUnableToNormalizeObjectAttribute()
388438
$this->normalizer->normalize($obj, 'any');
389439
}
390440

391-
/**
392-
* @expectedException \Symfony\Component\Serializer\Exception\CircularReferenceException
393-
*/
394-
public function testUnableToNormalizeCircularReference()
395-
{
396-
$this->doTestUnableToNormalizeCircularReference();
397-
}
398-
399-
/**
400-
* @expectedException \Symfony\Component\Serializer\Exception\CircularReferenceException
401-
*/
402-
public function testLegacyUnableToNormalizeCircularReference()
403-
{
404-
$this->doTestUnableToNormalizeCircularReference(true);
405-
}
406-
407-
private function doTestUnableToNormalizeCircularReference(bool $legacy = false)
408-
{
409-
$legacy ? $this->normalizer->setCircularReferenceLimit(2) : $this->createNormalizer([GetSetMethodNormalizer::CIRCULAR_REFERENCE_LIMIT => 2]);
410-
$this->serializer = new Serializer([$this->normalizer]);
411-
$this->normalizer->setSerializer($this->serializer);
412-
413-
$obj = new CircularReferenceDummy();
414-
$this->normalizer->normalize($obj);
415-
}
416-
417441
public function testSiblingReference()
418442
{
419443
$serializer = new Serializer([$this->normalizer]);
@@ -429,31 +453,6 @@ public function testSiblingReference()
429453
$this->assertEquals($expected, $this->normalizer->normalize($siblingHolder));
430454
}
431455

432-
public function testCircularReferenceHandler()
433-
{
434-
$this->doTestCircularReferenceHandler();
435-
}
436-
437-
public function testLegacyCircularReferenceHandler()
438-
{
439-
$this->doTestCircularReferenceHandler(true);
440-
}
441-
442-
private function doTestCircularReferenceHandler(bool $legacy = false)
443-
{
444-
$handler = function ($obj) {
445-
return \get_class($obj);
446-
};
447-
448-
$legacy ? $this->normalizer->setCircularReferenceHandler($handler) : $this->createNormalizer([GetSetMethodNormalizer::CIRCULAR_REFERENCE_HANDLER => $handler]);
449-
$this->serializer = new Serializer([$this->normalizer]);
450-
$this->normalizer->setSerializer($this->serializer);
451-
452-
$obj = new CircularReferenceDummy();
453-
454-
$expected = ['me' => 'Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy'];
455-
$this->assertEquals($expected, $this->normalizer->normalize($obj));
456-
}
457456

458457
public function testDenormalizeNonExistingAttribute()
459458
{

0 commit comments

Comments
 (0)