|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Patchlevel\Hydrator\Tests\Unit\Extension\Lifecycle; |
| 6 | + |
| 7 | +use LogicException; |
| 8 | +use Patchlevel\Hydrator\Extension\Lifecycle\Attribute\PostExtract; |
| 9 | +use Patchlevel\Hydrator\Extension\Lifecycle\Attribute\PostHydrate; |
| 10 | +use Patchlevel\Hydrator\Extension\Lifecycle\Attribute\PreExtract; |
| 11 | +use Patchlevel\Hydrator\Extension\Lifecycle\Attribute\PreHydrate; |
| 12 | +use Patchlevel\Hydrator\Extension\Lifecycle\Lifecycle; |
| 13 | +use Patchlevel\Hydrator\Extension\Lifecycle\LifecycleMetadataEnricher; |
| 14 | +use Patchlevel\Hydrator\Metadata\AttributeMetadataFactory; |
| 15 | +use Patchlevel\Hydrator\Metadata\ClassMetadata; |
| 16 | +use Patchlevel\Hydrator\Tests\Unit\Fixture\LifecycleFixture; |
| 17 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +#[CoversClass(LifecycleMetadataEnricher::class)] |
| 21 | +final class LifecycleMetadataEnricherTest extends TestCase |
| 22 | +{ |
| 23 | + public function testEnrich(): void |
| 24 | + { |
| 25 | + $metadata = $this->metadata(LifecycleFixture::class); |
| 26 | + |
| 27 | + self::assertArrayHasKey(Lifecycle::class, $metadata->extras); |
| 28 | + $lifecycle = $metadata->extras[Lifecycle::class]; |
| 29 | + |
| 30 | + self::assertInstanceOf(Lifecycle::class, $lifecycle); |
| 31 | + self::assertSame('preHydrate', $lifecycle->preHydrate); |
| 32 | + self::assertSame('postHydrate', $lifecycle->postHydrate); |
| 33 | + self::assertSame('preExtract', $lifecycle->preExtract); |
| 34 | + self::assertSame('postExtract', $lifecycle->postExtract); |
| 35 | + } |
| 36 | + |
| 37 | + public function testNoLifecycleAttributes(): void |
| 38 | + { |
| 39 | + $object = new class { |
| 40 | + }; |
| 41 | + |
| 42 | + $metadata = $this->metadata($object::class); |
| 43 | + |
| 44 | + self::assertArrayNotHasKey(Lifecycle::class, $metadata->extras); |
| 45 | + } |
| 46 | + |
| 47 | + public function testNonStaticPreHydrate(): void |
| 48 | + { |
| 49 | + $object = new class { |
| 50 | + #[PreHydrate] |
| 51 | + public function preHydrate(): void |
| 52 | + { |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + $this->expectException(LogicException::class); |
| 57 | + $this->metadata($object::class); |
| 58 | + } |
| 59 | + |
| 60 | + public function testNonStaticPostHydrate(): void |
| 61 | + { |
| 62 | + $object = new class { |
| 63 | + #[PostHydrate] |
| 64 | + public function postHydrate(): void |
| 65 | + { |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + $this->expectException(LogicException::class); |
| 70 | + $this->metadata($object::class); |
| 71 | + } |
| 72 | + |
| 73 | + public function testNonStaticPreExtract(): void |
| 74 | + { |
| 75 | + $object = new class { |
| 76 | + #[PreExtract] |
| 77 | + public function preExtract(): void |
| 78 | + { |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + $this->expectException(LogicException::class); |
| 83 | + $this->metadata($object::class); |
| 84 | + } |
| 85 | + |
| 86 | + public function testNonStaticPostExtract(): void |
| 87 | + { |
| 88 | + $object = new class { |
| 89 | + #[PostExtract] |
| 90 | + public function postExtract(): void |
| 91 | + { |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | + $this->expectException(LogicException::class); |
| 96 | + $this->metadata($object::class); |
| 97 | + } |
| 98 | + |
| 99 | + /** @param class-string $class */ |
| 100 | + private function metadata(string $class): ClassMetadata |
| 101 | + { |
| 102 | + $metadata = (new AttributeMetadataFactory())->metadata($class); |
| 103 | + (new LifecycleMetadataEnricher())->enrich($metadata); |
| 104 | + |
| 105 | + return $metadata; |
| 106 | + } |
| 107 | +} |
0 commit comments