From 6de400a2c54e47a06b8936f86b5a5b6da7c1c65a Mon Sep 17 00:00:00 2001 From: Bob van de Vijver Date: Sun, 12 Jul 2020 14:50:55 +0200 Subject: [PATCH] Add tests for skippable handler implementation --- tests/Serializer/GraphNavigatorTest.php | 93 ++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/tests/Serializer/GraphNavigatorTest.php b/tests/Serializer/GraphNavigatorTest.php index 33de10008..bb6f0f53e 100644 --- a/tests/Serializer/GraphNavigatorTest.php +++ b/tests/Serializer/GraphNavigatorTest.php @@ -8,9 +8,12 @@ use JMS\Serializer\Accessor\DefaultAccessorStrategy; use JMS\Serializer\Construction\ObjectConstructorInterface; use JMS\Serializer\Construction\UnserializeObjectConstructor; +use JMS\Serializer\Context; use JMS\Serializer\DeserializationContext; use JMS\Serializer\EventDispatcher\EventDispatcher; +use JMS\Serializer\Exception\NotAcceptableException; use JMS\Serializer\Exception\RuntimeException; +use JMS\Serializer\Exception\SkipHandlerException; use JMS\Serializer\Exclusion\ExclusionStrategyInterface; use JMS\Serializer\GraphNavigator\DeserializationGraphNavigator; use JMS\Serializer\GraphNavigator\SerializationGraphNavigator; @@ -22,6 +25,7 @@ use JMS\Serializer\SerializationContext; use JMS\Serializer\Visitor\DeserializationVisitorInterface; use JMS\Serializer\Visitor\SerializationVisitorInterface; +use JMS\Serializer\VisitorInterface; use Metadata\MetadataFactory; use PHPUnit\Framework\TestCase; @@ -157,6 +161,54 @@ public function testExposeAcceptHandlerExceptionOnSerialization() $navigator->accept($object, ['name' => $typeName, 'params' => []]); } + public function testHandlerIsExecutedOnSerialization() + { + $object = new SerializableClass(); + $this->handlerRegistry->registerSubscribingHandler(new TestSubscribingHandler()); + + $this->context->method('getFormat')->willReturn(TestSubscribingHandler::FORMAT); + + $navigator = new SerializationGraphNavigator($this->metadataFactory, $this->handlerRegistry, $this->accessor, $this->dispatcher); + $navigator->initialize($this->serializationVisitor, $this->context); + $this->context->initialize(TestSubscribingHandler::FORMAT, $this->serializationVisitor, $navigator, $this->metadataFactory); + + $rt = $navigator->accept($object, null); + $this->assertEquals('foobar', $rt); + } + + /** + * @doesNotPerformAssertions + */ + public function testFilterableHandlerIsSkippedOnSerialization() + { + $object = new SerializableClass(); + $this->handlerRegistry->registerSubscribingHandler(new TestSkippableSubscribingHandler()); + + $this->context->method('getFormat')->willReturn(TestSkippableSubscribingHandler::FORMAT); + + $navigator = new SerializationGraphNavigator($this->metadataFactory, $this->handlerRegistry, $this->accessor, $this->dispatcher); + $navigator->initialize($this->serializationVisitor, $this->context); + $this->context->initialize(TestSkippableSubscribingHandler::FORMAT, $this->serializationVisitor, $navigator, $this->metadataFactory); + + $navigator->accept($object, null); + } + + public function testFilterableHandlerIsNotSkippedOnSerialization() + { + $object = new SerializableClass(); + $this->handlerRegistry->registerSubscribingHandler(new TestSkippableSubscribingHandler(false)); + + $this->context->method('getFormat')->willReturn(TestSkippableSubscribingHandler::FORMAT); + + $navigator = new SerializationGraphNavigator($this->metadataFactory, $this->handlerRegistry, $this->accessor, $this->dispatcher); + $navigator->initialize($this->serializationVisitor, $this->context); + $this->context->initialize(TestSkippableSubscribingHandler::FORMAT, $this->serializationVisitor, $navigator, $this->metadataFactory); + + $this->expectException(NotAcceptableException::class); + $this->expectExceptionMessage(TestSkippableSubscribingHandler::EX_MSG); + $navigator->accept($object, null); + } + /** * @doesNotPerformAssertions */ @@ -213,11 +265,50 @@ public static function getSubscribingMethods() { return [ [ - 'type' => 'JsonSerializable', + 'type' => SerializableClass::class, + 'format' => self::FORMAT, + 'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, + 'method' => 'serialize', + ], + ]; + } + + public function serialize(VisitorInterface $visitor, $userData, array $type, Context $context) + { + return 'foobar'; + } +} + +class TestSkippableSubscribingHandler implements SubscribingHandlerInterface +{ + public const FORMAT = 'foo'; + public const EX_MSG = 'This method should be skipped!'; + + private $shouldSkip; + + public function __construct(bool $shouldSkip = true) + { + $this->shouldSkip = $shouldSkip; + } + + public static function getSubscribingMethods() + { + return [ + [ + 'type' => SerializableClass::class, 'format' => self::FORMAT, 'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, 'method' => 'serialize', ], ]; } + + public function serialize(VisitorInterface $visitor, $userData, array $type, Context $context) + { + if ($this->shouldSkip){ + throw new SkipHandlerException(); + } + + throw new NotAcceptableException(self::EX_MSG); + } }