Skip to content

Commit

Permalink
Add tests for skippable handler implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
bobvandevijver committed Jul 12, 2020
1 parent eb5289f commit 6de400a
Showing 1 changed file with 92 additions and 1 deletion.
93 changes: 92 additions & 1 deletion tests/Serializer/GraphNavigatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 6de400a

Please sign in to comment.