|
11 | 11 | use Patchlevel\Hydrator\ClassNotSupported; |
12 | 12 | use Patchlevel\Hydrator\Cryptography\PayloadCryptographer; |
13 | 13 | use Patchlevel\Hydrator\DenormalizationFailure; |
| 14 | +use Patchlevel\Hydrator\Event\PostExtract; |
| 15 | +use Patchlevel\Hydrator\Event\PreHydrate; |
14 | 16 | use Patchlevel\Hydrator\Metadata\AttributeMetadataFactory; |
15 | 17 | use Patchlevel\Hydrator\MetadataHydrator; |
16 | 18 | use Patchlevel\Hydrator\NormalizationFailure; |
|
37 | 39 | use Patchlevel\Hydrator\TypeMismatch; |
38 | 40 | use PHPUnit\Framework\TestCase; |
39 | 41 | use Prophecy\PhpUnit\ProphecyTrait; |
| 42 | +use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
40 | 43 |
|
41 | 44 | final class MetadataHydratorTest extends TestCase |
42 | 45 | { |
@@ -298,6 +301,76 @@ public function testEncrypt(): void |
298 | 301 | self::assertSame($encryptedPayload, $return); |
299 | 302 | } |
300 | 303 |
|
| 304 | + public function testPreHydrate(): void |
| 305 | + { |
| 306 | + $object = new ProfileCreated( |
| 307 | + ProfileId::fromString('1'), |
| 308 | + Email::fromString('info@patchlevel.de'), |
| 309 | + ); |
| 310 | + |
| 311 | + $payload = ['profileId' => '1', 'email' => 'info@patchlevel.de']; |
| 312 | + $encryptedPayload = ['profileId' => '1', 'email' => 'encrypted']; |
| 313 | + |
| 314 | + $metadataFactory = new AttributeMetadataFactory(); |
| 315 | + |
| 316 | + $event = new PreHydrate( |
| 317 | + $encryptedPayload, |
| 318 | + $metadataFactory->metadata(ProfileCreated::class), |
| 319 | + ); |
| 320 | + |
| 321 | + $eventReturn = new PreHydrate( |
| 322 | + $payload, |
| 323 | + $metadataFactory->metadata(ProfileCreated::class), |
| 324 | + ); |
| 325 | + |
| 326 | + $eventDispatcher = $this->prophesize(EventDispatcherInterface::class); |
| 327 | + $eventDispatcher |
| 328 | + ->dispatch($event) |
| 329 | + ->willReturn($eventReturn) |
| 330 | + ->shouldBeCalledOnce(); |
| 331 | + |
| 332 | + $hydrator = new MetadataHydrator($metadataFactory, eventDispatcher: $eventDispatcher->reveal()); |
| 333 | + |
| 334 | + $return = $hydrator->hydrate(ProfileCreated::class, $encryptedPayload); |
| 335 | + |
| 336 | + self::assertEquals($object, $return); |
| 337 | + } |
| 338 | + |
| 339 | + public function testPostExtract(): void |
| 340 | + { |
| 341 | + $object = new ProfileCreated( |
| 342 | + ProfileId::fromString('1'), |
| 343 | + Email::fromString('info@patchlevel.de'), |
| 344 | + ); |
| 345 | + |
| 346 | + $payload = ['profileId' => '1', 'email' => 'info@patchlevel.de']; |
| 347 | + $encryptedPayload = ['profileId' => '1', 'email' => 'encrypted']; |
| 348 | + |
| 349 | + $metadataFactory = new AttributeMetadataFactory(); |
| 350 | + |
| 351 | + $event = new PostExtract( |
| 352 | + $payload, |
| 353 | + $metadataFactory->metadata(ProfileCreated::class), |
| 354 | + ); |
| 355 | + |
| 356 | + $eventReturn = new PostExtract( |
| 357 | + $encryptedPayload, |
| 358 | + $metadataFactory->metadata(ProfileCreated::class), |
| 359 | + ); |
| 360 | + |
| 361 | + $eventDispatcher = $this->prophesize(EventDispatcherInterface::class); |
| 362 | + $eventDispatcher |
| 363 | + ->dispatch($event) |
| 364 | + ->willReturn($eventReturn) |
| 365 | + ->shouldBeCalledOnce(); |
| 366 | + |
| 367 | + $hydrator = new MetadataHydrator($metadataFactory, eventDispatcher: $eventDispatcher->reveal()); |
| 368 | + |
| 369 | + $return = $hydrator->extract($object); |
| 370 | + |
| 371 | + self::assertSame($encryptedPayload, $return); |
| 372 | + } |
| 373 | + |
301 | 374 | public function testHydrateWithNormalizerInBaseClass(): void |
302 | 375 | { |
303 | 376 | $expected = new NormalizerInBaseClassDefinedDto( |
|
0 commit comments