Skip to content

Commit

Permalink
Add custom exception on Serialization and ExpressionLanguage errors
Browse files Browse the repository at this point in the history
  • Loading branch information
micoli committed Jul 22, 2023
1 parent 66b002d commit 2729ec7
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Exception/ElqlException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

use LogicException;

class ElqlException extends LogicException
abstract class ElqlException extends LogicException
{
}
9 changes: 9 additions & 0 deletions src/Exception/ExpressionLanguageException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Micoli\Elql\Exception;

final class ExpressionLanguageException extends ElqlException
{
}
2 changes: 1 addition & 1 deletion src/Exception/NonUniqueException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

namespace Micoli\Elql\Exception;

class NonUniqueException extends ElqlException
final class NonUniqueException extends ElqlException
{
}
9 changes: 9 additions & 0 deletions src/Exception/SerializerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Micoli\Elql\Exception;

final class SerializerException extends ElqlException
{
}
8 changes: 7 additions & 1 deletion src/ExpressionLanguage/ExpressionLanguageEvaluator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Micoli\Elql\ExpressionLanguage;

use Micoli\Elql\Exception\ExpressionLanguageException;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\ExpressionLanguage\SyntaxError;

class ExpressionLanguageEvaluator implements ExpressionLanguageEvaluatorInterface
{
Expand All @@ -26,6 +28,10 @@ public function __construct(

public function evaluate(string $expression, mixed $record, array $parameters = []): mixed
{
return $this->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);
}
}
}
19 changes: 12 additions & 7 deletions src/Persister/FilePersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Expand Down
39 changes: 39 additions & 0 deletions tests/ExpressionLanguage/ExpressionLanguageEvaluatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Micoli\Elql\Tests\ExpressionLanguage;

use Micoli\Elql\Exception\ExpressionLanguageException;
use Micoli\Elql\ExpressionLanguage\ExpressionLanguageEvaluator;
use Micoli\Elql\Tests\AbstractTestCase;
use Micoli\Elql\Tests\Fixtures\Baz;

/**
* @internal
*
* @psalm-suppress PropertyNotSetInConstructor
*/
class ExpressionLanguageEvaluatorTest extends AbstractTestCase
{
/**
* @test
*/
public function ItShouldEvaluateExpression(): void
{
$evaluator = new ExpressionLanguageEvaluator();
$baz = new Baz(1, 'a', 'b');
self::assertSame('Ac', $evaluator->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']);
}
}
41 changes: 40 additions & 1 deletion tests/Persister/FilePersisterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
<<<BAZ
-
id: 2
firstName: b
lastName: b
-
id: 3
firstName: c
lastName: c
BAZ
);
self::assertCount(2, $this->persister->getRecords(Baz::class)->data);
}

public function testItShouldThrowCustomExceptionIfReadFailure(): void
{
file_put_contents(
$this->databaseDir . '/b_a_z.yaml',
<<<BAZ
-
id: "2"
firstName: b
lastName: b
-
id: 3
fairstName: c
lastName: c
BAZ
);
self::expectException(SerializerException::class);
self::assertCount(2, $this->persister->getRecords(Baz::class)->data);
}
}

0 comments on commit 2729ec7

Please sign in to comment.