-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
009a3d9
commit 293c694
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
61
tests/Unit/Serializer/Normalizer/AggregateIdNormalizerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} |