Skip to content

Commit

Permalink
[Serializer][Minor] Fix exception message
Browse files Browse the repository at this point in the history
Wrong circular reference limit displayed.
  • Loading branch information
l-vo committed Oct 2, 2020
1 parent fb0298f commit 8e97a9c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Normalizer/AbstractNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ protected function handleCircularReference($object/*, string $format = null, arr
return $circularReferenceHandler($object, $format, $context);
}

throw new CircularReferenceException(sprintf('A circular reference has been detected when serializing the object of class "%s" (configured limit: %d).', \get_class($object), $this->circularReferenceLimit));
throw new CircularReferenceException(sprintf('A circular reference has been detected when serializing the object of class "%s" (configured limit: %d).', \get_class($object), $context[self::CIRCULAR_REFERENCE_LIMIT] ?? $this->defaultContext[self::CIRCULAR_REFERENCE_LIMIT] ?? $this->circularReferenceLimit));
}

/**
Expand Down
23 changes: 18 additions & 5 deletions Tests/Normalizer/Features/CircularReferenceTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,36 @@
*/
trait CircularReferenceTestTrait
{
abstract protected function getNormalizerForCircularReference(): NormalizerInterface;
abstract protected function getNormalizerForCircularReference(array $defaultContext): NormalizerInterface;

abstract protected function getSelfReferencingModel();

public function testUnableToNormalizeCircularReference()
public function provideUnableToNormalizeCircularReference(): array
{
$normalizer = $this->getNormalizerForCircularReference();
return [
[[], [], 1],
[['circular_reference_limit' => 2], [], 2],
[['circular_reference_limit' => 2], ['circular_reference_limit' => 3], 3],
];
}

/**
* @dataProvider provideUnableToNormalizeCircularReference
*/
public function testUnableToNormalizeCircularReference(array $defaultContext, array $context, int $expectedLimit)
{
$normalizer = $this->getNormalizerForCircularReference($defaultContext);

$obj = $this->getSelfReferencingModel();

$this->expectException(CircularReferenceException::class);
$normalizer->normalize($obj, null, ['circular_reference_limit' => 2]);
$this->expectExceptionMessage(sprintf('A circular reference has been detected when serializing the object of class "%s" (configured limit: %d).', \get_class($obj), $expectedLimit));
$normalizer->normalize($obj, null, $context);
}

public function testCircularReferenceHandler()
{
$normalizer = $this->getNormalizerForCircularReference();
$normalizer = $this->getNormalizerForCircularReference([]);

$obj = $this->getSelfReferencingModel();
$expected = ['me' => \get_class($obj)];
Expand Down
4 changes: 2 additions & 2 deletions Tests/Normalizer/GetSetMethodNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,10 @@ public function testLegacyCallbacks($callbacks, $value, $result)
);
}

protected function getNormalizerForCircularReference(): GetSetMethodNormalizer
protected function getNormalizerForCircularReference(array $defaultContext): GetSetMethodNormalizer
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$normalizer = new GetSetMethodNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory));
$normalizer = new GetSetMethodNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory), null, null, null, $defaultContext);
new Serializer([$normalizer]);

return $normalizer;
Expand Down
4 changes: 2 additions & 2 deletions Tests/Normalizer/ObjectNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,9 @@ public function testLegacyUncallableCallbacks($callbacks)

// circular reference

protected function getNormalizerForCircularReference(): ObjectNormalizer
protected function getNormalizerForCircularReference(array $defaultContext): ObjectNormalizer
{
$normalizer = new ObjectNormalizer();
$normalizer = new ObjectNormalizer(null, null, null, null, null, null, $defaultContext);
new Serializer([$normalizer]);

return $normalizer;
Expand Down
4 changes: 2 additions & 2 deletions Tests/Normalizer/PropertyNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ public function testLegacyUncallableCallbacks($callbacks)
$this->normalizer->setCallbacks($callbacks);
}

protected function getNormalizerForCircularReference(): PropertyNormalizer
protected function getNormalizerForCircularReference(array $defaultContext): PropertyNormalizer
{
$normalizer = new PropertyNormalizer();
$normalizer = new PropertyNormalizer(null, null, null, null, null, $defaultContext);
new Serializer([$normalizer]);

return $normalizer;
Expand Down

0 comments on commit 8e97a9c

Please sign in to comment.