|
31 | 31 | use Patchlevel\EventSourcing\Subscription\Store\InMemorySubscriptionStore; |
32 | 32 | use Patchlevel\EventSourcing\Subscription\Subscriber\MetadataSubscriberAccessorRepository; |
33 | 33 | use Patchlevel\EventSourcing\Tests\DbalManager; |
| 34 | +use Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\Command\AdjustStockForProduct; |
34 | 35 | use Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\Command\ChangeProfileName; |
35 | 36 | use Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\Command\CreateProfile; |
| 37 | +use Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\Command\DecreaseStockForProduct; |
36 | 38 | use Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\Events\NameChanged; |
37 | 39 | use Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\Events\ProfileCreated; |
38 | 40 | use Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\MessageDecorator\FooMessageDecorator; |
@@ -392,4 +394,50 @@ public function testQueryBus(): void |
392 | 394 |
|
393 | 395 | self::assertSame('John Doe', $result); |
394 | 396 | } |
| 397 | + |
| 398 | + public function testAggregateInitialization(): void |
| 399 | + { |
| 400 | + $store = new DoctrineDbalStore( |
| 401 | + $this->connection, |
| 402 | + DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']), |
| 403 | + DefaultHeadersSerializer::createFromPaths([ |
| 404 | + __DIR__ . '/Header', |
| 405 | + ]), |
| 406 | + ); |
| 407 | + |
| 408 | + $aggregateRootRegistry = new AggregateRootRegistry(['stock' => Stock::class]); |
| 409 | + |
| 410 | + $manager = new DefaultRepositoryManager( |
| 411 | + $aggregateRootRegistry, |
| 412 | + $store, |
| 413 | + null, |
| 414 | + new DefaultSnapshotStore(['default' => new InMemorySnapshotAdapter()]), |
| 415 | + new FooMessageDecorator(), |
| 416 | + ); |
| 417 | + |
| 418 | + $commandBus = SyncCommandBus::createForAggregateHandlers( |
| 419 | + $aggregateRootRegistry, |
| 420 | + $manager, |
| 421 | + ); |
| 422 | + |
| 423 | + $schemaDirector = new DoctrineSchemaDirector( |
| 424 | + $this->connection, |
| 425 | + $store, |
| 426 | + ); |
| 427 | + |
| 428 | + $schemaDirector->create(); |
| 429 | + |
| 430 | + $stockId = StockId::create(); |
| 431 | + $productId = ProductId::generate(); |
| 432 | + |
| 433 | + $commandBus->dispatch(new AdjustStockForProduct($stockId, $productId, 5)); |
| 434 | + $commandBus->dispatch(new DecreaseStockForProduct($stockId, $productId, 3)); |
| 435 | + |
| 436 | + $repository = $manager->get(Stock::class); |
| 437 | + $stock = $repository->load($stockId); |
| 438 | + |
| 439 | + self::assertEquals($stockId, $stock->aggregateRootId()); |
| 440 | + self::assertSame(3, $stock->playhead()); |
| 441 | + self::assertSame(2, $stock->stockFor($productId)); |
| 442 | + } |
395 | 443 | } |
0 commit comments