Skip to content

Commit

Permalink
use traits in get set method normalizer test
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Apr 11, 2019
1 parent e8eae77 commit 4fb71a7
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 62 deletions.
Expand Up @@ -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;
}
}
Expand Up @@ -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;
Expand Down
Expand Up @@ -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;
Expand All @@ -32,17 +33,21 @@
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;

class GetSetMethodNormalizerTest extends TestCase
{
use CallbacksTestTrait;
use CircularReferenceTestTrait;
use ConstructorArgumentsTestTrait;
use GroupsTestTrait;
use IgnoredAttributesTestTrait;
use MaxDepthTestTrait;
use ObjectToPopulateTestTrait;
use TypeEnforcementTestTrait;
Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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]);
Expand All @@ -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()
{
Expand Down

0 comments on commit 4fb71a7

Please sign in to comment.