-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add failing test with synchronous event sourced saga (#112)
* Add failing test with synchronous event sourced saga * Improve event handler routing --------- Co-authored-by: jlabedo <jean@needelp.com> Co-authored-by: Dariusz Gafka <dariuszgafka@gmail.com> Co-authored-by: Dariusz Gafka <dgafka.mail@gmail.com>
- Loading branch information
1 parent
92b3118
commit f640b60
Showing
10 changed files
with
235 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/PdoEventSourcing/tests/Fixture/BasketWithSynchronousEventDrivenSaga/Saga.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Test\Ecotone\EventSourcing\Fixture\BasketWithSynchronousEventDrivenSaga; | ||
|
||
use Ecotone\Modelling\Attribute\AggregateIdentifier; | ||
use Ecotone\Modelling\Attribute\EventHandler; | ||
use Ecotone\Modelling\Attribute\EventSourcingAggregate; | ||
use Ecotone\Modelling\Attribute\EventSourcingHandler; | ||
use Ecotone\Modelling\CommandBus; | ||
use Ecotone\Modelling\WithAggregateVersioning; | ||
use Test\Ecotone\EventSourcing\Fixture\Basket\Command\AddProduct; | ||
use Test\Ecotone\EventSourcing\Fixture\Basket\Event\BasketWasCreated; | ||
|
||
#[EventSourcingAggregate] | ||
class Saga | ||
{ | ||
use WithAggregateVersioning; | ||
#[AggregateIdentifier] | ||
private string $id; | ||
|
||
#[EventHandler] | ||
public static function start(BasketWasCreated $event): array | ||
{ | ||
return [new SagaStarted($event->getId())]; | ||
} | ||
|
||
#[EventHandler] | ||
public function whenSagaStarted(SagaStarted $event, CommandBus $commandBus): array | ||
{ | ||
if ($event->getId() === "1000") { | ||
$commandBus->send(new AddProduct($event->getId(), 'chocolate')); | ||
} | ||
|
||
return []; | ||
} | ||
|
||
#[EventSourcingHandler()] | ||
public function applySagaStarted(SagaStarted $event): void | ||
{ | ||
$this->id = $event->getId(); | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
...doEventSourcing/tests/Fixture/BasketWithSynchronousEventDrivenSaga/SagaEventConverter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Test\Ecotone\EventSourcing\Fixture\BasketWithSynchronousEventDrivenSaga; | ||
|
||
use Ecotone\Messaging\Attribute\Converter; | ||
|
||
class SagaEventConverter | ||
{ | ||
#[Converter] | ||
public function fromSagaStarted(SagaStarted $event): array | ||
{ | ||
return [ | ||
'id' => $event->getId(), | ||
]; | ||
} | ||
|
||
#[Converter] | ||
public function toTicketWasRegistered(array $event): SagaStarted | ||
{ | ||
return new SagaStarted($event['id']); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...es/PdoEventSourcing/tests/Fixture/BasketWithSynchronousEventDrivenSaga/SagaProjection.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Test\Ecotone\EventSourcing\Fixture\BasketWithSynchronousEventDrivenSaga; | ||
|
||
use Ecotone\EventSourcing\Attribute\Projection; | ||
use Ecotone\EventSourcing\Attribute\ProjectionInitialization; | ||
use Ecotone\Messaging\Support\Assert; | ||
use Ecotone\Modelling\Attribute\EventHandler; | ||
use Ecotone\Modelling\Attribute\QueryHandler; | ||
|
||
#[Projection(self::PROJECTION_NAME, Saga::class)] | ||
class SagaProjection | ||
{ | ||
public const PROJECTION_NAME = 'saga_projection'; | ||
private bool $isInitialized = false; | ||
private array $sagaStarted = []; | ||
|
||
#[EventHandler] | ||
public function when(SagaStarted $event): void | ||
{ | ||
Assert::isTrue($this->isInitialized, 'Saga Projection is not initialized'); | ||
$this->sagaStarted[$event->getId()] = true; | ||
} | ||
|
||
#[ProjectionInitialization] | ||
public function init(): void | ||
{ | ||
$this->isInitialized = true; | ||
} | ||
|
||
#[QueryHandler('isSagaStarted')] | ||
public function isSagaStarted(string $sagaId): bool | ||
{ | ||
return $this->sagaStarted[$sagaId] ?? false; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/PdoEventSourcing/tests/Fixture/BasketWithSynchronousEventDrivenSaga/SagaStarted.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Test\Ecotone\EventSourcing\Fixture\BasketWithSynchronousEventDrivenSaga; | ||
|
||
class SagaStarted | ||
{ | ||
public function __construct(private string $id) | ||
{ | ||
} | ||
|
||
public function getId(): string | ||
{ | ||
return $this->id; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...ventSourcing/tests/Fixture/BasketWithSynchronousEventDrivenSaga/SynchronousBasketList.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Test\Ecotone\EventSourcing\Fixture\BasketWithSynchronousEventDrivenSaga; | ||
|
||
use Ecotone\EventSourcing\Attribute\Projection; | ||
use Ecotone\Modelling\Attribute\EventHandler; | ||
use Ecotone\Modelling\Attribute\QueryHandler; | ||
use Test\Ecotone\EventSourcing\Fixture\Basket\Basket; | ||
use Test\Ecotone\EventSourcing\Fixture\Basket\Event\BasketWasCreated; | ||
use Test\Ecotone\EventSourcing\Fixture\Basket\Event\ProductWasAddedToBasket; | ||
|
||
#[Projection(self::PROJECTION_NAME, Basket::BASKET_STREAM)] | ||
class SynchronousBasketList | ||
{ | ||
public const PROJECTION_NAME = 'basketList'; | ||
private array $basketsList = []; | ||
|
||
#[EventHandler(BasketWasCreated::EVENT_NAME)] | ||
public function addBasket(array $event): void | ||
{ | ||
$this->basketsList[$event['id']] = []; | ||
} | ||
|
||
#[EventHandler(ProductWasAddedToBasket::EVENT_NAME)] | ||
public function addProduct(ProductWasAddedToBasket $event): void | ||
{ | ||
$this->basketsList[$event->getId()][] = $event->getProductName(); | ||
} | ||
|
||
#[QueryHandler('getALlBaskets')] | ||
public function getAllBaskets(): array | ||
{ | ||
return $this->basketsList; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
packages/PdoEventSourcing/tests/Integration/SynchronousEventDrivenSagaTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Test\Ecotone\EventSourcing\Integration; | ||
|
||
use Ecotone\Lite\EcotoneLite; | ||
use Ecotone\Messaging\Config\ModulePackageList; | ||
use Ecotone\Messaging\Config\ServiceConfiguration; | ||
use PHPUnit\Framework\TestCase; | ||
use Test\Ecotone\EventSourcing\Fixture\Basket\Basket; | ||
use Test\Ecotone\EventSourcing\Fixture\Basket\BasketEventConverter; | ||
use Test\Ecotone\EventSourcing\Fixture\Basket\Command\AddProduct; | ||
use Test\Ecotone\EventSourcing\Fixture\Basket\Command\CreateBasket; | ||
use Test\Ecotone\EventSourcing\Fixture\Basket\Event\BasketWasCreated; | ||
use Test\Ecotone\EventSourcing\Fixture\Basket\Event\ProductWasAddedToBasket; | ||
use Test\Ecotone\EventSourcing\Fixture\BasketWithSynchronousEventDrivenSaga\Saga; | ||
use Test\Ecotone\EventSourcing\Fixture\BasketWithSynchronousEventDrivenSaga\SagaEventConverter; | ||
use Test\Ecotone\EventSourcing\Fixture\BasketWithSynchronousEventDrivenSaga\SagaProjection; | ||
use Test\Ecotone\EventSourcing\Fixture\BasketWithSynchronousEventDrivenSaga\SagaStarted; | ||
use Test\Ecotone\EventSourcing\Fixture\BasketWithSynchronousEventDrivenSaga\SynchronousBasketList; | ||
|
||
final class SynchronousEventDrivenSagaTest extends TestCase | ||
{ | ||
public function test_product_is_added_by_synchronous_event_driven_saga(): void | ||
{ | ||
$testSupport = EcotoneLite::bootstrapFlowTestingWithEventStore( | ||
containerOrAvailableServices: [new SagaProjection(), new SynchronousBasketList(), new SagaEventConverter(), new BasketEventConverter()], | ||
configuration: ServiceConfiguration::createWithDefaults() | ||
->withSkippedModulePackageNames(ModulePackageList::allPackagesExcept([ModulePackageList::EVENT_SOURCING_PACKAGE])) | ||
->withNamespaces([ | ||
'Test\Ecotone\EventSourcing\Fixture\Basket', | ||
'Test\Ecotone\EventSourcing\Fixture\BasketWithSynchronousEventDrivenSaga', | ||
]), | ||
pathToRootCatalog: __DIR__ . "/../../" | ||
); | ||
|
||
$testSupport->sendCommand(new CreateBasket('1000')); | ||
|
||
self::assertEquals([ | ||
new CreateBasket('1000'), | ||
new AddProduct('1000', 'chocolate'), | ||
], $testSupport->getRecordedCommands()); | ||
|
||
self::assertEquals([ | ||
new BasketWasCreated('1000'), | ||
new SagaStarted('1000'), | ||
new ProductWasAddedToBasket('1000', 'chocolate'), | ||
], $testSupport->getRecordedEvents()); | ||
|
||
self::assertEquals(true, $testSupport->sendQueryWithRouting('isSagaStarted', '1000')); | ||
|
||
self::assertEquals([ | ||
'1000' => ['chocolate'], | ||
], $testSupport->sendQueryWithRouting('getALlBaskets')); | ||
} | ||
} |