From 4fb71a70c8029cbbd2a859a714de4c1bf178999e Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Thu, 11 Apr 2019 07:40:24 +0200 Subject: [PATCH] use traits in get set method normalizer test --- .../Tests/Normalizer/Features/ObjectInner.php | 20 +++ .../Tests/Normalizer/Features/ObjectOuter.php | 23 ++++ .../Normalizer/GetSetMethodNormalizerTest.php | 123 +++++++++--------- 3 files changed, 104 insertions(+), 62 deletions(-) diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectInner.php b/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectInner.php index be4f5485f244c..d4086e93e3ba5 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectInner.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectInner.php @@ -6,4 +6,24 @@ class ObjectInner { public $foo; public $bar; + + public function getBar() + { + return $this->bar; + } + + public function setBar($bar): void + { + $this->bar = $bar; + } + + public function getFoo() + { + return $this->foo; + } + + public function setFoo($foo): void + { + $this->foo = $foo; + } } diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectOuter.php b/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectOuter.php index 58d64165ee530..8193fa8ffe202 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectOuter.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectOuter.php @@ -14,6 +14,29 @@ class ObjectOuter */ private $inners; + public function getFoo() + { + return $this->foo; + } + + public function setFoo($foo): void + { + $this->foo = $foo; + } + + public function getBar() + { + return $this->bar; + } + + public function setBar($bar): void + { + $this->bar = $bar; + } + + /** + * @return ObjectInner + */ public function getInner() { return $this->inner; diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php index 2e972830068d3..a37c491d87dc4 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php @@ -16,6 +16,7 @@ use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor; use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor; use Symfony\Component\PropertyInfo\PropertyInfoExtractor; +use Symfony\Component\Serializer\Exception\CircularReferenceException; use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; @@ -32,8 +33,10 @@ use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder; use Symfony\Component\Serializer\Tests\Normalizer\Features\CallbacksObject; use Symfony\Component\Serializer\Tests\Normalizer\Features\CallbacksTestTrait; +use Symfony\Component\Serializer\Tests\Normalizer\Features\CircularReferenceTestTrait; use Symfony\Component\Serializer\Tests\Normalizer\Features\ConstructorArgumentsTestTrait; use Symfony\Component\Serializer\Tests\Normalizer\Features\GroupsTestTrait; +use Symfony\Component\Serializer\Tests\Normalizer\Features\IgnoredAttributesTestTrait; use Symfony\Component\Serializer\Tests\Normalizer\Features\MaxDepthTestTrait; use Symfony\Component\Serializer\Tests\Normalizer\Features\ObjectToPopulateTestTrait; use Symfony\Component\Serializer\Tests\Normalizer\Features\TypeEnforcementTestTrait; @@ -41,8 +44,10 @@ class GetSetMethodNormalizerTest extends TestCase { use CallbacksTestTrait; + use CircularReferenceTestTrait; use ConstructorArgumentsTestTrait; use GroupsTestTrait; + use IgnoredAttributesTestTrait; use MaxDepthTestTrait; use ObjectToPopulateTestTrait; use TypeEnforcementTestTrait; @@ -242,6 +247,48 @@ public function testLegacyCallbacks($callbacks, $value, $result) ); } + protected function getNormalizerForCircularReference(): GetSetMethodNormalizer + { + $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); + $normalizer = new GetSetMethodNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory)); + new Serializer([$normalizer]); + + return $normalizer; + } + + protected function getSelfReferencingModel() + { + return new CircularReferenceDummy(); + } + + public function testLegacyUnableToNormalizeCircularReference() + { + $this->normalizer->setCircularReferenceLimit(2); + $this->serializer = new Serializer([$this->normalizer]); + $this->normalizer->setSerializer($this->serializer); + + $obj = new CircularReferenceDummy(); + + $this->expectException(CircularReferenceException::class); + $this->normalizer->normalize($obj); + } + + public function testLegacyCircularReferenceHandler() + { + $handler = function ($obj) { + return \get_class($obj); + }; + + $this->normalizer->setCircularReferenceHandler($handler); + $this->serializer = new Serializer([$this->normalizer]); + $this->normalizer->setSerializer($this->serializer); + + $obj = new CircularReferenceDummy(); + + $expected = ['me' => CircularReferenceDummy::class]; + $this->assertEquals($expected, $this->normalizer->normalize($obj)); + } + protected function getDenormalizerForConstructArguments(): GetSetMethodNormalizer { $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); @@ -341,25 +388,28 @@ public function testRejectInvalidKey() $this->markTestSkipped('This test makes no sense with the GetSetMethodNormalizer'); } - - - - - - public function testIgnoredAttributes() + protected function getNormalizerForIgnoredAttributes(): GetSetMethodNormalizer { - $this->doTestIgnoredAttributes(); + $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); + $normalizer = new GetSetMethodNormalizer($classMetadataFactory, null, new PhpDocExtractor()); + new Serializer([$normalizer]); + + return $normalizer; } - public function testLegacyIgnoredAttributes() + protected function getDenormalizerForIgnoredAttributes(): GetSetMethodNormalizer { - $this->doTestIgnoredAttributes(true); + $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); + $normalizer = new GetSetMethodNormalizer($classMetadataFactory, null, new PhpDocExtractor()); + new Serializer([$normalizer]); + + return $normalizer; } - private function doTestIgnoredAttributes(bool $legacy = false) + public function testLegacyIgnoredAttributes() { $ignoredAttributes = ['foo', 'bar', 'baz', 'camelCase', 'object']; - $legacy ? $this->normalizer->setIgnoredAttributes($ignoredAttributes) : $this->createNormalizer([GetSetMethodNormalizer::IGNORED_ATTRIBUTES => $ignoredAttributes]); + $this->normalizer->setIgnoredAttributes($ignoredAttributes); $obj = new GetSetDummy(); $obj->setFoo('foo'); @@ -388,32 +438,6 @@ public function testUnableToNormalizeObjectAttribute() $this->normalizer->normalize($obj, 'any'); } - /** - * @expectedException \Symfony\Component\Serializer\Exception\CircularReferenceException - */ - public function testUnableToNormalizeCircularReference() - { - $this->doTestUnableToNormalizeCircularReference(); - } - - /** - * @expectedException \Symfony\Component\Serializer\Exception\CircularReferenceException - */ - public function testLegacyUnableToNormalizeCircularReference() - { - $this->doTestUnableToNormalizeCircularReference(true); - } - - private function doTestUnableToNormalizeCircularReference(bool $legacy = false) - { - $legacy ? $this->normalizer->setCircularReferenceLimit(2) : $this->createNormalizer([GetSetMethodNormalizer::CIRCULAR_REFERENCE_LIMIT => 2]); - $this->serializer = new Serializer([$this->normalizer]); - $this->normalizer->setSerializer($this->serializer); - - $obj = new CircularReferenceDummy(); - $this->normalizer->normalize($obj); - } - public function testSiblingReference() { $serializer = new Serializer([$this->normalizer]); @@ -429,31 +453,6 @@ public function testSiblingReference() $this->assertEquals($expected, $this->normalizer->normalize($siblingHolder)); } - public function testCircularReferenceHandler() - { - $this->doTestCircularReferenceHandler(); - } - - public function testLegacyCircularReferenceHandler() - { - $this->doTestCircularReferenceHandler(true); - } - - private function doTestCircularReferenceHandler(bool $legacy = false) - { - $handler = function ($obj) { - return \get_class($obj); - }; - - $legacy ? $this->normalizer->setCircularReferenceHandler($handler) : $this->createNormalizer([GetSetMethodNormalizer::CIRCULAR_REFERENCE_HANDLER => $handler]); - $this->serializer = new Serializer([$this->normalizer]); - $this->normalizer->setSerializer($this->serializer); - - $obj = new CircularReferenceDummy(); - - $expected = ['me' => 'Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy']; - $this->assertEquals($expected, $this->normalizer->normalize($obj)); - } public function testDenormalizeNonExistingAttribute() {