|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Patchlevel\Hydrator\Tests\Unit\Extension\Upcast; |
| 6 | + |
| 7 | +use Patchlevel\Hydrator\CoreExtension; |
| 8 | +use Patchlevel\Hydrator\Cryptography\PayloadCryptographer; |
| 9 | +use Patchlevel\Hydrator\Extension\Cryptography\Cryptographer; |
| 10 | +use Patchlevel\Hydrator\Extension\Cryptography\CryptographyExtension; |
| 11 | +use Patchlevel\Hydrator\Extension\Cryptography\CryptographyMiddleware; |
| 12 | +use Patchlevel\Hydrator\Extension\Cryptography\LegacyCryptographyDecryptMiddleware; |
| 13 | +use Patchlevel\Hydrator\Extension\Upcast\CallbackUpcaster; |
| 14 | +use Patchlevel\Hydrator\Extension\Upcast\UpcastExtension; |
| 15 | +use Patchlevel\Hydrator\Extension\Upcast\UpcastMiddleware; |
| 16 | +use Patchlevel\Hydrator\StackHydratorBuilder; |
| 17 | +use Patchlevel\Hydrator\Tests\Unit\Extension\Upcast\Fixture\UpcastFixture; |
| 18 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +use function assert; |
| 22 | +use function is_string; |
| 23 | + |
| 24 | +#[CoversClass(UpcastExtension::class)] |
| 25 | +final class UpcastExtensionTest extends TestCase |
| 26 | +{ |
| 27 | + public function testIntegration(): void |
| 28 | + { |
| 29 | + $hydrator = (new StackHydratorBuilder()) |
| 30 | + ->useExtension(new CoreExtension()) |
| 31 | + ->useExtension(new UpcastExtension( |
| 32 | + afterCryptography: [ |
| 33 | + CallbackUpcaster::forClass( |
| 34 | + UpcastFixture::class, |
| 35 | + static function (array $data): array { |
| 36 | + $firstName = $data['firstName'] ?? ''; |
| 37 | + $lastName = $data['lastName'] ?? ''; |
| 38 | + assert(is_string($firstName)); |
| 39 | + assert(is_string($lastName)); |
| 40 | + |
| 41 | + $data['name'] = $firstName . ' ' . $lastName; |
| 42 | + |
| 43 | + return $data; |
| 44 | + }, |
| 45 | + ), |
| 46 | + ], |
| 47 | + )) |
| 48 | + ->build(); |
| 49 | + |
| 50 | + $object = $hydrator->hydrate(UpcastFixture::class, ['firstName' => 'Jane', 'lastName' => 'Doe']); |
| 51 | + |
| 52 | + self::assertSame('Jane Doe', $object->name); |
| 53 | + } |
| 54 | + |
| 55 | + public function testConfigureAroundCryptography(): void |
| 56 | + { |
| 57 | + $beforeCryptographyUpcaster = CallbackUpcaster::forClass( |
| 58 | + UpcastFixture::class, |
| 59 | + static fn (array $data): array => $data, |
| 60 | + ); |
| 61 | + $afterCryptographyUpcaster = CallbackUpcaster::forClass( |
| 62 | + UpcastFixture::class, |
| 63 | + static fn (array $data): array => $data, |
| 64 | + ); |
| 65 | + |
| 66 | + $builder = new StackHydratorBuilder(); |
| 67 | + $builder->useExtension(new UpcastExtension( |
| 68 | + beforeCryptography: [$beforeCryptographyUpcaster], |
| 69 | + afterCryptography: [$afterCryptographyUpcaster], |
| 70 | + )); |
| 71 | + $builder->useExtension(new CryptographyExtension($this->createMock(Cryptographer::class))); |
| 72 | + |
| 73 | + $middlewares = $builder->middlewares(); |
| 74 | + |
| 75 | + self::assertCount(3, $middlewares); |
| 76 | + self::assertInstanceOf(UpcastMiddleware::class, $middlewares[0]); |
| 77 | + self::assertInstanceOf(CryptographyMiddleware::class, $middlewares[1]); |
| 78 | + self::assertInstanceOf(UpcastMiddleware::class, $middlewares[2]); |
| 79 | + } |
| 80 | + |
| 81 | + public function testConfigureAroundLegacyCryptography(): void |
| 82 | + { |
| 83 | + $beforeCryptographyUpcaster = CallbackUpcaster::forClass( |
| 84 | + UpcastFixture::class, |
| 85 | + static fn (array $data): array => $data, |
| 86 | + ); |
| 87 | + $afterCryptographyUpcaster = CallbackUpcaster::forClass( |
| 88 | + UpcastFixture::class, |
| 89 | + static fn (array $data): array => $data, |
| 90 | + ); |
| 91 | + |
| 92 | + $builder = new StackHydratorBuilder(); |
| 93 | + $builder->useExtension(new UpcastExtension( |
| 94 | + beforeCryptography: [$beforeCryptographyUpcaster], |
| 95 | + afterCryptography: [$afterCryptographyUpcaster], |
| 96 | + )); |
| 97 | + $builder->useExtension(new CryptographyExtension( |
| 98 | + $this->createMock(Cryptographer::class), |
| 99 | + $this->createMock(PayloadCryptographer::class), |
| 100 | + )); |
| 101 | + |
| 102 | + $middlewares = $builder->middlewares(); |
| 103 | + |
| 104 | + self::assertCount(4, $middlewares); |
| 105 | + self::assertInstanceOf(UpcastMiddleware::class, $middlewares[0]); |
| 106 | + self::assertInstanceOf(LegacyCryptographyDecryptMiddleware::class, $middlewares[1]); |
| 107 | + self::assertInstanceOf(CryptographyMiddleware::class, $middlewares[2]); |
| 108 | + self::assertInstanceOf(UpcastMiddleware::class, $middlewares[3]); |
| 109 | + } |
| 110 | +} |
0 commit comments