Skip to content

Commit

Permalink
do not hide Exceptions from custom handlers but correctly handle null
Browse files Browse the repository at this point in the history
  • Loading branch information
Hikariii committed Mar 13, 2020
1 parent 0e28668 commit 61eb3d7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/GraphNavigator/SerializationGraphNavigator.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,11 @@ public function accept($data, ?array $type = null)
if (null !== $handler = $this->handlerRegistry->getHandler(GraphNavigatorInterface::DIRECTION_SERIALIZATION, $type['name'], $this->format)) {
try {
$rs = \call_user_func($handler, $this->visitor, $data, $type, $this->context);
} finally {
} catch (NotAcceptableException $e) {
$this->context->stopVisiting($data);
throw $e;
}
$this->context->stopVisiting($data);

return $rs;
}
Expand Down
31 changes: 29 additions & 2 deletions tests/Serializer/GraphNavigatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,42 @@ public function testNavigatorChangeTypeOnSerialization()

$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);

$navigator->accept($object, null);
}

public function testExposeAcceptHandlerExceptionOnSerialization()
{
$object = new SerializableClass();
$typeName = 'JsonSerializable';
$msg = 'Useful serialization error with relevant context information';

$handler = static function ($visitor, $data, array $type, SerializationContext $context) use ($msg) {
$context->startVisiting(new \stdClass());
throw new \RuntimeException($msg);
};
$this->handlerRegistry->registerHandler(GraphNavigatorInterface::DIRECTION_SERIALIZATION, $typeName, TestSubscribingHandler::FORMAT, $handler);

$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);

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage($msg);
$navigator->accept($object, ['name' => $typeName, 'params' => []]);
}

protected function setUp(): void
{
$this->deserializationVisitor = $this->getMockBuilder(DeserializationVisitorInterface::class)->getMock();
$this->serializationVisitor = $this->getMockBuilder(SerializationVisitorInterface::class)->getMock();

$this->context = $this->getMockBuilder(SerializationContext::class)
->enableOriginalConstructor()
->setMethodsExcept(['getExclusionStrategy'])
->setMethodsExcept(['getExclusionStrategy', 'initialize', 'startVisiting', 'stopVisiting'])
->getMock();

$this->deserializationContext = $this->getMockBuilder(DeserializationContext::class)
Expand Down Expand Up @@ -168,11 +193,13 @@ class SerializableClass

class TestSubscribingHandler implements SubscribingHandlerInterface
{
public const FORMAT = 'foo';

public static function getSubscribingMethods()
{
return [[
'type' => 'JsonSerializable',
'format' => 'foo',
'format' => self::FORMAT,
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
'method' => 'serialize',
],
Expand Down

0 comments on commit 61eb3d7

Please sign in to comment.