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