From 2729ec733ae81beb93266f7c1508a2e53500c4ea Mon Sep 17 00:00:00 2001 From: Olivier Michaud Date: Sat, 22 Jul 2023 12:00:37 +0200 Subject: [PATCH] Add custom exception on Serialization and ExpressionLanguage errors --- src/Exception/ElqlException.php | 2 +- src/Exception/ExpressionLanguageException.php | 9 ++++ src/Exception/NonUniqueException.php | 2 +- src/Exception/SerializerException.php | 9 ++++ .../ExpressionLanguageEvaluator.php | 8 +++- src/Persister/FilePersister.php | 19 +++++---- .../ExpressionLanguageEvaluatorTest.php | 39 ++++++++++++++++++ tests/Persister/FilePersisterTest.php | 41 ++++++++++++++++++- 8 files changed, 118 insertions(+), 11 deletions(-) create mode 100644 src/Exception/ExpressionLanguageException.php create mode 100644 src/Exception/SerializerException.php create mode 100644 tests/ExpressionLanguage/ExpressionLanguageEvaluatorTest.php diff --git a/src/Exception/ElqlException.php b/src/Exception/ElqlException.php index 2b5b756..52998a7 100644 --- a/src/Exception/ElqlException.php +++ b/src/Exception/ElqlException.php @@ -6,6 +6,6 @@ use LogicException; -class ElqlException extends LogicException +abstract class ElqlException extends LogicException { } diff --git a/src/Exception/ExpressionLanguageException.php b/src/Exception/ExpressionLanguageException.php new file mode 100644 index 0000000..6495f5f --- /dev/null +++ b/src/Exception/ExpressionLanguageException.php @@ -0,0 +1,9 @@ +expressionLanguage->evaluate($expression, [...$parameters, 'record' => $record]); + try { + return $this->expressionLanguage->evaluate($expression, [...$parameters, 'record' => $record]); + } catch (SyntaxError $exception) { + throw new ExpressionLanguageException($exception->getMessage(), previous: $exception); + } } } diff --git a/src/Persister/FilePersister.php b/src/Persister/FilePersister.php index 82090f9..dd1d6a8 100644 --- a/src/Persister/FilePersister.php +++ b/src/Persister/FilePersister.php @@ -6,12 +6,14 @@ use Doctrine\Common\Annotations\AnnotationReader; use Micoli\Elql\Encoder\YamlEncoder; +use Micoli\Elql\Exception\SerializerException; use Micoli\Elql\ExpressionLanguage\ExpressionLanguageEvaluator; use Micoli\Elql\ExpressionLanguage\ExpressionLanguageEvaluatorInterface; use Micoli\Elql\Metadata\MetadataManagerInterface; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor; use Symfony\Component\Serializer\Encoder\JsonEncoder; +use Symfony\Component\Serializer\Exception\ExceptionInterface; use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; @@ -74,13 +76,16 @@ public function getRecords(string $model): InMemoryTable return $this->records[$model]; } - - /** @var array $values */ - $values = $this->serializer->deserialize( - file_get_contents($filename), - $model . '[]', - $this->format, - ); + try { + /** @var array $values */ + $values = $this->serializer->deserialize( + file_get_contents($filename), + $model . '[]', + $this->format, + ); + } catch (ExceptionInterface $exception) { + throw new SerializerException($exception->getMessage(), previous: $exception); + } $this->records[$model] = new InMemoryTable($model, $values); return $this->records[$model]; diff --git a/tests/ExpressionLanguage/ExpressionLanguageEvaluatorTest.php b/tests/ExpressionLanguage/ExpressionLanguageEvaluatorTest.php new file mode 100644 index 0000000..3ef40c4 --- /dev/null +++ b/tests/ExpressionLanguage/ExpressionLanguageEvaluatorTest.php @@ -0,0 +1,39 @@ +evaluate('strtoupper(record.firstName) ~ test', $baz, ['test' => 'c'])); + } + + /** + * @test + */ + public function ItShouldGetCustomException(): void + { + $evaluator = new ExpressionLanguageEvaluator(); + $baz = new Baz(1, 'a', 'b'); + self::expectException(ExpressionLanguageException::class); + $evaluator->evaluate('strtoupper(record.firstName) AAAAAA', $baz, ['test' => 'c']); + } +} diff --git a/tests/Persister/FilePersisterTest.php b/tests/Persister/FilePersisterTest.php index b87893b..f17b70e 100644 --- a/tests/Persister/FilePersisterTest.php +++ b/tests/Persister/FilePersisterTest.php @@ -5,9 +5,11 @@ namespace Micoli\Elql\Tests\Persister; use Micoli\Elql\Encoder\YamlEncoder; +use Micoli\Elql\Exception\SerializerException; use Micoli\Elql\Metadata\MetadataManager; use Micoli\Elql\Persister\FilePersister; use Micoli\Elql\Tests\AbstractTestCase; +use Micoli\Elql\Tests\Fixtures\Baz; /** * @internal @@ -30,6 +32,43 @@ protected function setUp(): void public function testItShouldGetEmptydatabaseOnStartup(): void { - self::assertCount(0, $this->persister->getDatabase()); + self::assertCount(0, $this->persister->getRecords(Baz::class)->data); + } + + public function testItShouldGetNonEmptydatabaseOnStartup(): void + { + file_put_contents( + $this->databaseDir . '/b_a_z.yaml', + <<persister->getRecords(Baz::class)->data); + } + + public function testItShouldThrowCustomExceptionIfReadFailure(): void + { + file_put_contents( + $this->databaseDir . '/b_a_z.yaml', + <<persister->getRecords(Baz::class)->data); } }