Skip to content

Commit

Permalink
add aggregate id normalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Dec 29, 2023
1 parent 009a3d9 commit 293c694
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/Serializer/Normalizer/AggregateIdNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Serializer\Normalizer;

use Attribute;
use Cspray\Phinal\AllowInheritance;
use Patchlevel\EventSourcing\Aggregate\AggregateRootId;
use Patchlevel\Hydrator\Normalizer\InvalidArgument;
use Patchlevel\Hydrator\Normalizer\Normalizer;

use function is_string;

#[Attribute(Attribute::TARGET_PROPERTY)]
#[AllowInheritance('you can make specific normalizers for different classes')]
class AggregateIdNormalizer implements Normalizer
{
public function __construct(
/** @var class-string<AggregateRootId> */
private readonly string $aggregateIdClass,
) {
}

public function normalize(mixed $value): string|null
{
if ($value === null) {
return null;
}

if (!$value instanceof AggregateRootId) {
throw InvalidArgument::withWrongType($this->aggregateIdClass, $value);
}

return $value->toString();
}

public function denormalize(mixed $value): AggregateRootId|null
{
if ($value === null) {
return null;
}

if (!is_string($value)) {
throw InvalidArgument::withWrongType('string', $value);
}

$class = $this->aggregateIdClass;

return $class::fromString($value);
}
}
61 changes: 61 additions & 0 deletions tests/Unit/Serializer/Normalizer/AggregateIdNormalizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Serializer\Normalizer;

use Attribute;
use Patchlevel\EventSourcing\Aggregate\BasicAggregateRootId;
use Patchlevel\EventSourcing\Aggregate\UuidAggregateRootId;
use Patchlevel\EventSourcing\Serializer\Normalizer\AggregateIdNormalizer;
use Patchlevel\Hydrator\Normalizer\InvalidArgument;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Ramsey\Uuid\Exception\InvalidUuidStringException;

#[Attribute(Attribute::TARGET_PROPERTY)]
final class AggregateIdNormalizerTest extends TestCase
{
use ProphecyTrait;

public function testNormalizeWithNull(): void
{
$normalizer = new AggregateIdNormalizer(BasicAggregateRootId::class);
$this->assertEquals(null, $normalizer->normalize(null));
}

public function testDenormalizeWithNull(): void
{
$normalizer = new AggregateIdNormalizer(BasicAggregateRootId::class);
$this->assertEquals(null, $normalizer->denormalize(null));
}

public function testNormalizeWithInvalidArgument(): void
{
$this->expectException(InvalidArgument::class);
$this->expectExceptionMessage('type "Patchlevel\EventSourcing\Aggregate\BasicAggregateRootId" was expected but "string" was passed.');

$normalizer = new AggregateIdNormalizer(BasicAggregateRootId::class);
$normalizer->normalize('foo');
}

public function testDenormalizeWithInvalidArgument(): void
{
$this->expectException(InvalidUuidStringException::class);

$normalizer = new AggregateIdNormalizer(UuidAggregateRootId::class);
$normalizer->denormalize('foo');
}

public function testNormalizeWithValue(): void
{
$normalizer = new AggregateIdNormalizer(BasicAggregateRootId::class);
$this->assertEquals('foo', $normalizer->normalize(new BasicAggregateRootId('foo')));
}

public function testDenormalizeWithValue(): void
{
$normalizer = new AggregateIdNormalizer(BasicAggregateRootId::class);
$this->assertEquals(new BasicAggregateRootId('foo'), $normalizer->denormalize('foo'));
}
}

0 comments on commit 293c694

Please sign in to comment.