Skip to content

Commit

Permalink
Merge pull request #8 from goodwix/json-odm-exception
Browse files Browse the repository at this point in the history
feature: serializer exception in ODMType wrapped into JsonOdmException
  • Loading branch information
strider2038 committed Sep 5, 2019
2 parents 9041319 + 0c41591 commit 9bde679
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/Exception/JsonOdmException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/*
* This file is part of Goodwix Doctrine JSON ODM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Goodwix\DoctrineJsonOdm\Exception;

class JsonOdmException extends \Exception
{
}
26 changes: 24 additions & 2 deletions src/Type/ODMType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\JsonType;
use Goodwix\DoctrineJsonOdm\Exception\JsonOdmException;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\SerializerInterface;

class ODMType extends JsonType
Expand Down Expand Up @@ -75,26 +77,46 @@ public function getName(): string

/**
* {@inheritdoc}
*
* @throws JsonOdmException
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
{
if (null === $value) {
return null;
}

return $this->getSerializer()->serialize($value, $this->format);
try {
$value = $this->getSerializer()->serialize($value, $this->format);
} catch (ExceptionInterface $exception) {
$message = sprintf('Serialization exception occurred for class "%s".', $this->getEntityClass());

throw new JsonOdmException($message, 0, $exception);
}

return $value;
}

/**
* {@inheritdoc}
*
* @throws JsonOdmException
*/
public function convertToPHPValue($value, AbstractPlatform $platform): ?object
{
if (null === $value || '' === $value) {
return null;
}

return $this->getSerializer()->deserialize($value, $this->getEntityClass(), $this->format);
try {
$value = $this->getSerializer()->deserialize($value, $this->getEntityClass(), $this->format);
} catch (ExceptionInterface $exception) {
$message = sprintf('Deserialization exception occurred for class "%s".', $this->getEntityClass());

throw new JsonOdmException($message, 0, $exception);
}

return $value;
}

public static function registerODMType(string $entityClass, SerializerInterface $serializer): void
Expand Down
69 changes: 69 additions & 0 deletions tests/Unit/Type/ODMTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
namespace Goodwix\DoctrineJsonOdm\Tests\Unit\Type;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Goodwix\DoctrineJsonOdm\Exception\JsonOdmException;
use Goodwix\DoctrineJsonOdm\Tests\Resources\ODM\DummyODM;
use Goodwix\DoctrineJsonOdm\Tests\Resources\ODMInterface\DummyODMInterface;
use Goodwix\DoctrineJsonOdm\Type\ODMType;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\SerializerInterface;

class ODMTypeTest extends TestCase
Expand Down Expand Up @@ -125,6 +127,28 @@ public function convertToDatabaseValue_object_objectSerializedAndReturned(): voi
$this->assertSame(self::SERIALIZED_VALUE, $value);
}

/** @test */
public function convertToDatabaseValue_serializerException_exceptionConvertedToJsonOdmException(): void
{
$type = $this->createODMType();
$type->setEntityClass(self::ENTITY_CLASS);
$type->setSerializer($this->serializer);
$object = new DummyODM();
$serializerException = $this->givenSerializerException();
$this->givenSerialize_serialize_throwsException($serializerException);
$exception = null;

try {
$type->convertToDatabaseValue($object, $this->platform);
} catch (\Throwable $exception) {
}

$this->assertSerializer_serialize_wasCalledOnceWithObjectAndFormat($object, self::JSON_FORMAT);
$this->assertInstanceOf(JsonOdmException::class, $exception);
$this->assertSame('Serialization exception occurred for class "entity_class".', $exception->getMessage());
$this->assertSame($serializerException, $exception->getPrevious());
}

/** @test */
public function convertToDatabaseValue_null_nullReturned(): void
{
Expand Down Expand Up @@ -154,6 +178,31 @@ public function convertToPHPValue_nonEmptyString_deserializedObjectReturned(): v
$this->assertSame($object, $value);
}

/** @test */
public function convertToPHPValue_serializerException_exceptionConvertedToJsonOdmException(): void
{
$type = $this->createODMType();
$type->setSerializer($this->serializer);
$type->setEntityClass(self::ENTITY_CLASS);
$serializerException = $this->givenSerializerException();
$this->givenSerializer_deserialize_throwsException($serializerException);
$exception = null;

try {
$type->convertToPHPValue(self::SERIALIZED_VALUE, $this->platform);
} catch (\Throwable $exception) {
}

$this->assertSerializer_deserialize_wasCalledOnceWithValueAndTypeAndFormat(
self::SERIALIZED_VALUE,
self::ENTITY_CLASS,
self::JSON_FORMAT
);
$this->assertInstanceOf(JsonOdmException::class, $exception);
$this->assertSame('Deserialization exception occurred for class "entity_class".', $exception->getMessage());
$this->assertSame($serializerException, $exception->getPrevious());
}

/**
* @test
* @dataProvider emptyValueProvider
Expand Down Expand Up @@ -232,6 +281,13 @@ private function givenSerialize_serialize_returnsValue($value): void
->thenReturn($value);
}

private function givenSerialize_serialize_throwsException(\Throwable $exception): void
{
\Phake::when($this->serializer)
->serialize(\Phake::anyParameters())
->thenThrow($exception);
}

private function assertSerializer_deserialize_wasCalledOnceWithValueAndTypeAndFormat(
string $value,
string $type,
Expand All @@ -251,4 +307,17 @@ private function givenSerializer_deserialize_returnsObject(): DummyODM

return $object;
}

private function givenSerializer_deserialize_throwsException(\Throwable $exception): void
{
\Phake::when($this->serializer)
->deserialize(\Phake::anyParameters())
->thenThrow($exception);
}

private function givenSerializerException(): ExceptionInterface
{
return new class() extends \Exception implements ExceptionInterface {
};
}
}

0 comments on commit 9bde679

Please sign in to comment.