|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Patchlevel\Hydrator\Tests\Unit\Normalizer; |
| 6 | + |
| 7 | +use Patchlevel\Hydrator\Hydrator; |
| 8 | +use Patchlevel\Hydrator\Normalizer\InvalidArgument; |
| 9 | +use Patchlevel\Hydrator\Normalizer\MissingHydrator; |
| 10 | +use Patchlevel\Hydrator\Normalizer\ObjectMapNormalizer; |
| 11 | +use Patchlevel\Hydrator\Tests\Unit\Fixture\Email; |
| 12 | +use Patchlevel\Hydrator\Tests\Unit\Fixture\ProfileCreated; |
| 13 | +use Patchlevel\Hydrator\Tests\Unit\Fixture\ProfileId; |
| 14 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +use function serialize; |
| 18 | +use function unserialize; |
| 19 | + |
| 20 | +#[CoversClass(ObjectMapNormalizer::class)] |
| 21 | +final class ObjectMapNormalizerTest extends TestCase |
| 22 | +{ |
| 23 | + public function testNormalizeMissingHydrator(): void |
| 24 | + { |
| 25 | + $this->expectException(MissingHydrator::class); |
| 26 | + |
| 27 | + $normalizer = new ObjectMapNormalizer([ProfileCreated::class => 'created']); |
| 28 | + $this->assertEquals(null, $normalizer->normalize(null)); |
| 29 | + } |
| 30 | + |
| 31 | + public function testDenormalizeMissingHydrator(): void |
| 32 | + { |
| 33 | + $this->expectException(MissingHydrator::class); |
| 34 | + |
| 35 | + $normalizer = new ObjectMapNormalizer([ProfileCreated::class => 'created']); |
| 36 | + $this->assertEquals(null, $normalizer->denormalize(null)); |
| 37 | + } |
| 38 | + |
| 39 | + public function testNormalizeWithNull(): void |
| 40 | + { |
| 41 | + $hydrator = $this->createStub(Hydrator::class); |
| 42 | + |
| 43 | + $normalizer = new ObjectMapNormalizer([ProfileCreated::class => 'created']); |
| 44 | + $normalizer->setHydrator($hydrator); |
| 45 | + |
| 46 | + $this->assertEquals(null, $normalizer->normalize(null)); |
| 47 | + } |
| 48 | + |
| 49 | + public function testDenormalizeWithNull(): void |
| 50 | + { |
| 51 | + $hydrator = $this->createStub(Hydrator::class); |
| 52 | + |
| 53 | + $normalizer = new ObjectMapNormalizer([ProfileCreated::class => 'created']); |
| 54 | + $normalizer->setHydrator($hydrator); |
| 55 | + |
| 56 | + $this->assertEquals(null, $normalizer->denormalize(null)); |
| 57 | + } |
| 58 | + |
| 59 | + public function testNormalizeWithInvalidArgument(): void |
| 60 | + { |
| 61 | + $this->expectException(InvalidArgument::class); |
| 62 | + $this->expectExceptionMessage('type "Patchlevel\Hydrator\Tests\Unit\Fixture\ProfileCreated|null" was expected but "string" was passed.'); |
| 63 | + |
| 64 | + $hydrator = $this->createStub(Hydrator::class); |
| 65 | + |
| 66 | + $normalizer = new ObjectMapNormalizer([ProfileCreated::class => 'created']); |
| 67 | + $normalizer->setHydrator($hydrator); |
| 68 | + $normalizer->normalize('foo'); |
| 69 | + } |
| 70 | + |
| 71 | + public function testDenormalizeWithInvalidArgument(): void |
| 72 | + { |
| 73 | + $this->expectException(InvalidArgument::class); |
| 74 | + $this->expectExceptionMessage('array<string, mixed>|null" was expected but "string" was passed.'); |
| 75 | + |
| 76 | + $hydrator = $this->createStub(Hydrator::class); |
| 77 | + |
| 78 | + $normalizer = new ObjectMapNormalizer([ProfileCreated::class => 'created']); |
| 79 | + $normalizer->setHydrator($hydrator); |
| 80 | + $normalizer->denormalize('foo'); |
| 81 | + } |
| 82 | + |
| 83 | + public function testNormalizeWithValue(): void |
| 84 | + { |
| 85 | + $hydrator = $this->createMock(Hydrator::class); |
| 86 | + |
| 87 | + $event = new ProfileCreated( |
| 88 | + ProfileId::fromString('1'), |
| 89 | + Email::fromString('info@patchlevel.de'), |
| 90 | + ); |
| 91 | + |
| 92 | + $hydrator |
| 93 | + ->expects($this->once()) |
| 94 | + ->method('extract') |
| 95 | + ->with($event) |
| 96 | + ->willReturn(['profileId' => '1', 'email' => 'info@patchlevel.de']); |
| 97 | + |
| 98 | + $normalizer = new ObjectMapNormalizer([ProfileCreated::class => 'created']); |
| 99 | + $normalizer->setHydrator($hydrator); |
| 100 | + |
| 101 | + self::assertEquals( |
| 102 | + $normalizer->normalize($event), |
| 103 | + ['profileId' => '1', 'email' => 'info@patchlevel.de', '_type' => 'created'], |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + public function testDenormalizeWithValue(): void |
| 108 | + { |
| 109 | + $hydrator = $this->createMock(Hydrator::class); |
| 110 | + |
| 111 | + $expected = new ProfileCreated( |
| 112 | + ProfileId::fromString('1'), |
| 113 | + Email::fromString('info@patchlevel.de'), |
| 114 | + ); |
| 115 | + |
| 116 | + $hydrator |
| 117 | + ->expects($this->once()) |
| 118 | + ->method('hydrate') |
| 119 | + ->with(ProfileCreated::class, ['profileId' => '1', 'email' => 'info@patchlevel.de']) |
| 120 | + ->willReturn($expected); |
| 121 | + |
| 122 | + $normalizer = new ObjectMapNormalizer([ProfileCreated::class => 'created']); |
| 123 | + $normalizer->setHydrator($hydrator); |
| 124 | + |
| 125 | + $this->assertEquals( |
| 126 | + $expected, |
| 127 | + $normalizer->denormalize(['profileId' => '1', 'email' => 'info@patchlevel.de', '_type' => 'created']), |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + public function testSerialize(): void |
| 132 | + { |
| 133 | + $hydrator = $this->createStub(Hydrator::class); |
| 134 | + |
| 135 | + $normalizer = new ObjectMapNormalizer([ProfileCreated::class => 'created']); |
| 136 | + $normalizer->setHydrator($hydrator); |
| 137 | + |
| 138 | + $serialized = serialize($normalizer); |
| 139 | + $normalizer2 = unserialize($serialized); |
| 140 | + |
| 141 | + self::assertInstanceOf(ObjectMapNormalizer::class, $normalizer2); |
| 142 | + self::assertEquals(new ObjectMapNormalizer([ProfileCreated::class => 'created']), $normalizer2); |
| 143 | + } |
| 144 | +} |
0 commit comments