|
6 | 6 |
|
7 | 7 | use Doctrine\DBAL\Connection; |
8 | 8 | use Patchlevel\EventSourcing\Cryptography\DoctrineCipherKeyStore; |
| 9 | +use Patchlevel\EventSourcing\Cryptography\ExtensionDoctrineCipherKeyStore; |
9 | 10 | use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootRegistry; |
| 11 | +use Patchlevel\EventSourcing\Metadata\Event\AttributeEventRegistryFactory; |
10 | 12 | use Patchlevel\EventSourcing\Repository\DefaultRepositoryManager; |
11 | 13 | use Patchlevel\EventSourcing\Schema\ChainDoctrineSchemaConfigurator; |
12 | 14 | use Patchlevel\EventSourcing\Schema\DoctrineSchemaDirector; |
|
19 | 21 | use Patchlevel\EventSourcing\Subscription\Subscriber\MetadataSubscriberAccessorRepository; |
20 | 22 | use Patchlevel\EventSourcing\Tests\DbalManager; |
21 | 23 | use Patchlevel\EventSourcing\Tests\Integration\PersonalData\Processor\DeletePersonalDataProcessor; |
| 24 | +use Patchlevel\Hydrator\CoreExtension; |
22 | 25 | use Patchlevel\Hydrator\Cryptography\PersonalDataPayloadCryptographer; |
| 26 | +use Patchlevel\Hydrator\Extension\Cryptography\BaseCryptographer; |
| 27 | +use Patchlevel\Hydrator\Extension\Cryptography\CryptographyExtension; |
| 28 | +use Patchlevel\Hydrator\StackHydratorBuilder; |
23 | 29 | use PHPUnit\Framework\Attributes\CoversNothing; |
24 | 30 | use PHPUnit\Framework\TestCase; |
25 | 31 |
|
@@ -232,4 +238,162 @@ public function testRemoveKeyWithEventAndSnapshot(): void |
232 | 238 | self::assertSame(2, $profile->playhead()); |
233 | 239 | self::assertSame('unknown', $profile->name()); |
234 | 240 | } |
| 241 | + |
| 242 | + public function testWithStackHydrator(): void |
| 243 | + { |
| 244 | + $cipherKeyStore = new ExtensionDoctrineCipherKeyStore($this->connection); |
| 245 | + $extension = new CryptographyExtension( |
| 246 | + BaseCryptographer::createWithOpenssl($cipherKeyStore), |
| 247 | + ); |
| 248 | + |
| 249 | + $eventSerializer = new DefaultEventSerializer( |
| 250 | + (new AttributeEventRegistryFactory())->create([__DIR__ . '/Events']), |
| 251 | + (new StackHydratorBuilder()) |
| 252 | + ->useExtension(new CoreExtension()) |
| 253 | + ->useExtension($extension) |
| 254 | + ->build(), |
| 255 | + ); |
| 256 | + |
| 257 | + $store = new DoctrineDbalStore( |
| 258 | + $this->connection, |
| 259 | + $eventSerializer, |
| 260 | + ); |
| 261 | + |
| 262 | + $manager = new DefaultRepositoryManager( |
| 263 | + new AggregateRootRegistry(['profile' => Profile::class]), |
| 264 | + $store, |
| 265 | + ); |
| 266 | + |
| 267 | + $repository = $manager->get(Profile::class); |
| 268 | + |
| 269 | + $schemaDirector = new DoctrineSchemaDirector( |
| 270 | + $this->connection, |
| 271 | + new ChainDoctrineSchemaConfigurator([ |
| 272 | + $store, |
| 273 | + $cipherKeyStore, |
| 274 | + ]), |
| 275 | + ); |
| 276 | + |
| 277 | + $schemaDirector->create(); |
| 278 | + |
| 279 | + $profileId = ProfileId::generate(); |
| 280 | + $profile = Profile::create($profileId, 'John'); |
| 281 | + |
| 282 | + $repository->save($profile); |
| 283 | + |
| 284 | + $profile = $repository->load($profileId); |
| 285 | + |
| 286 | + self::assertInstanceOf(Profile::class, $profile); |
| 287 | + self::assertEquals($profileId, $profile->aggregateRootId()); |
| 288 | + self::assertSame(1, $profile->playhead()); |
| 289 | + self::assertSame('John', $profile->name()); |
| 290 | + |
| 291 | + $result = $this->connection->fetchAllAssociative('SELECT * FROM eventstore'); |
| 292 | + |
| 293 | + self::assertCount(1, $result); |
| 294 | + self::assertArrayHasKey(0, $result); |
| 295 | + |
| 296 | + $row = $result[0]; |
| 297 | + |
| 298 | + self::assertStringNotContainsString('John', $row['payload']); |
| 299 | + } |
| 300 | + |
| 301 | + public function testWithStackHydratorWithLegacyFallback(): void |
| 302 | + { |
| 303 | + $extensionCipherKeyStore = new ExtensionDoctrineCipherKeyStore($this->connection); |
| 304 | + $legacyCipherKeyStore = new DoctrineCipherKeyStore($this->connection); |
| 305 | + |
| 306 | + $cryptographer = PersonalDataPayloadCryptographer::createWithOpenssl($legacyCipherKeyStore); |
| 307 | + |
| 308 | + $store = new DoctrineDbalStore( |
| 309 | + $this->connection, |
| 310 | + DefaultEventSerializer::createFromPaths([__DIR__ . '/Events'], cryptographer: $cryptographer), |
| 311 | + ); |
| 312 | + |
| 313 | + $manager = new DefaultRepositoryManager( |
| 314 | + new AggregateRootRegistry(['profile' => Profile::class]), |
| 315 | + $store, |
| 316 | + ); |
| 317 | + |
| 318 | + $repository = $manager->get(Profile::class); |
| 319 | + |
| 320 | + $schemaDirector = new DoctrineSchemaDirector( |
| 321 | + $this->connection, |
| 322 | + new ChainDoctrineSchemaConfigurator([ |
| 323 | + $store, |
| 324 | + $legacyCipherKeyStore, |
| 325 | + $extensionCipherKeyStore, |
| 326 | + ]), |
| 327 | + ); |
| 328 | + |
| 329 | + $schemaDirector->create(); |
| 330 | + |
| 331 | + $profileId = ProfileId::generate(); |
| 332 | + $profile = Profile::create($profileId, 'John'); |
| 333 | + |
| 334 | + $repository->save($profile); |
| 335 | + |
| 336 | + // switch to new hydrator |
| 337 | + |
| 338 | + $extension = new CryptographyExtension( |
| 339 | + BaseCryptographer::createWithOpenssl($extensionCipherKeyStore), |
| 340 | + PersonalDataPayloadCryptographer::createWithOpenssl($legacyCipherKeyStore), |
| 341 | + ); |
| 342 | + |
| 343 | + $eventSerializer = new DefaultEventSerializer( |
| 344 | + (new AttributeEventRegistryFactory())->create([__DIR__ . '/Events']), |
| 345 | + (new StackHydratorBuilder()) |
| 346 | + ->useExtension(new CoreExtension()) |
| 347 | + ->useExtension($extension) |
| 348 | + ->build(), |
| 349 | + ); |
| 350 | + |
| 351 | + $store = new DoctrineDbalStore( |
| 352 | + $this->connection, |
| 353 | + $eventSerializer, |
| 354 | + ); |
| 355 | + |
| 356 | + $manager = new DefaultRepositoryManager( |
| 357 | + new AggregateRootRegistry(['profile' => Profile::class]), |
| 358 | + $store, |
| 359 | + ); |
| 360 | + |
| 361 | + $repository = $manager->get(Profile::class); |
| 362 | + $profile = $repository->load($profileId); |
| 363 | + |
| 364 | + self::assertInstanceOf(Profile::class, $profile); |
| 365 | + self::assertEquals($profileId, $profile->aggregateRootId()); |
| 366 | + self::assertSame(1, $profile->playhead()); |
| 367 | + self::assertSame('John', $profile->name()); |
| 368 | + |
| 369 | + $result = $this->connection->fetchAllAssociative('SELECT * FROM eventstore'); |
| 370 | + |
| 371 | + self::assertCount(1, $result); |
| 372 | + self::assertArrayHasKey(0, $result); |
| 373 | + |
| 374 | + $row = $result[0]; |
| 375 | + |
| 376 | + self::assertStringNotContainsString('John', $row['payload']); |
| 377 | + |
| 378 | + $result = $this->connection->fetchAllAssociative('SELECT * FROM crypto_keys'); |
| 379 | + |
| 380 | + self::assertCount(1, $result); |
| 381 | + self::assertArrayHasKey(0, $result); |
| 382 | + |
| 383 | + $row = $result[0]; |
| 384 | + |
| 385 | + self::assertEquals($profileId->toString(), $row['subject_id']); |
| 386 | + |
| 387 | + $result = $this->connection->fetchAllAssociative('SELECT * FROM cryptography_keys'); |
| 388 | + |
| 389 | + self::assertCount(0, $result); |
| 390 | + |
| 391 | + $profile->changeName('John 2'); |
| 392 | + $repository->save($profile); |
| 393 | + |
| 394 | + $result = $this->connection->fetchAllAssociative('SELECT * FROM cryptography_keys'); |
| 395 | + |
| 396 | + self::assertCount(1, $result); |
| 397 | + self::assertEquals($profileId->toString(), $row['subject_id']); |
| 398 | + } |
235 | 399 | } |
0 commit comments