Skip to content

Commit 0f35426

Browse files
committed
Scheduling the same command multiple times for multiple handlers
1 parent 82ee991 commit 0f35426

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Test\Ecotone\EventSourcing\Fixture\MultipleAsyncHandlersForOneMessage;
6+
7+
final class ActionCalled
8+
{
9+
public function __construct(public string $id)
10+
{
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Test\Ecotone\EventSourcing\Fixture\MultipleAsyncHandlersForOneMessage;
6+
7+
final class ActionCommand
8+
{
9+
public function __construct(public string $id)
10+
{
11+
}
12+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Test\Ecotone\EventSourcing\Fixture\MultipleAsyncHandlersForOneMessage;
6+
7+
use Ecotone\Messaging\Attribute\Converter;
8+
9+
final class EventConverter
10+
{
11+
#[Converter]
12+
public function convertFromEvent(ActionCalled $event): array
13+
{
14+
return ['id' => $event->id];
15+
}
16+
17+
#[Converter]
18+
public function convertToEvent(array $payload): ActionCalled
19+
{
20+
return new ActionCalled($payload['id']);
21+
}
22+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Test\Ecotone\EventSourcing\Fixture\MultipleAsyncHandlersForOneMessage;
6+
7+
use Ecotone\Messaging\Attribute\Asynchronous;
8+
use Ecotone\Modelling\Attribute\CommandHandler;
9+
use Ecotone\Modelling\Attribute\EventSourcingAggregate;
10+
use Ecotone\Modelling\Attribute\EventSourcingHandler;
11+
use Ecotone\Modelling\Attribute\Identifier;
12+
use Ecotone\Modelling\WithAggregateVersioning;
13+
14+
#[Asynchronous('testAggregate')]
15+
#[EventSourcingAggregate]
16+
final class TestAggregate
17+
{
18+
use WithAggregateVersioning;
19+
20+
#[Identifier]
21+
private string $id;
22+
23+
private int $counter = 0;
24+
25+
#[CommandHandler(endpointId: 'testAggregate.staticAction')]
26+
public static function staticAction(ActionCommand $command, array $metadata): array
27+
{
28+
return [new ActionCalled($command->id)];
29+
}
30+
31+
#[CommandHandler(endpointId: 'testAggregate.action')]
32+
public function action(ActionCommand $command, array $metadata): array
33+
{
34+
return [new ActionCalled($command->id)];
35+
}
36+
37+
#[EventSourcingHandler]
38+
public function applyActionCalled(ActionCalled $event): void
39+
{
40+
$this->id = $event->id;
41+
++$this->counter;
42+
}
43+
44+
public function counter(): int
45+
{
46+
return $this->counter;
47+
}
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Test\Ecotone\EventSourcing\Integration;
6+
7+
use Ecotone\Dbal\Configuration\DbalConfiguration;
8+
use Ecotone\EventSourcing\EventSourcingConfiguration;
9+
use Ecotone\Lite\EcotoneLite;
10+
use Ecotone\Messaging\Config\ModulePackageList;
11+
use Ecotone\Messaging\Config\ServiceConfiguration;
12+
use Ecotone\Messaging\Endpoint\ExecutionPollingMetadata;
13+
use Enqueue\Dbal\DbalConnectionFactory;
14+
use Test\Ecotone\EventSourcing\EventSourcingMessagingTestCase;
15+
use Test\Ecotone\EventSourcing\Fixture\MultipleAsyncHandlersForOneMessage\ActionCommand;
16+
use Test\Ecotone\EventSourcing\Fixture\MultipleAsyncHandlersForOneMessage\EventConverter;
17+
use Test\Ecotone\EventSourcing\Fixture\MultipleAsyncHandlersForOneMessage\TestAggregate;
18+
19+
final class MultipleAsyncHandlersForOneMessageTest extends EventSourcingMessagingTestCase
20+
{
21+
public function test_handling_multiple_same_messages(): void
22+
{
23+
$ecotone = EcotoneLite::bootstrapFlowTestingWithEventStore(
24+
classesToResolve: [TestAggregate::class, EventConverter::class],
25+
containerOrAvailableServices: [
26+
new EventConverter(),
27+
DbalConnectionFactory::class => self::getConnectionFactory(),
28+
],
29+
configuration: ServiceConfiguration::createWithDefaults()
30+
->withSkippedModulePackageNames(ModulePackageList::allPackagesExcept([ModulePackageList::EVENT_SOURCING_PACKAGE, ModulePackageList::ASYNCHRONOUS_PACKAGE]))
31+
->withNamespaces(['Test\Ecotone\Modelling\Fixture\MultipleAsyncHandlersForOneMessage'])
32+
->withExtensionObjects([
33+
DbalConfiguration::createWithDefaults(),
34+
EventSourcingConfiguration::createWithDefaults(),
35+
]),
36+
runForProductionEventStore: true
37+
);
38+
39+
$ecotone->sendCommand(command: new ActionCommand('123'), metadata: ['call' => 1]);
40+
$ecotone->sendCommand(command: new ActionCommand('123'), metadata: ['call' => 2]);
41+
42+
$ecotone->run('testAggregate', ExecutionPollingMetadata::createWithTestingSetup());
43+
self::assertEquals(1, $ecotone->getAggregate(TestAggregate::class, '123')->counter());
44+
45+
$ecotone->run('testAggregate', ExecutionPollingMetadata::createWithTestingSetup());
46+
self::assertEquals(2, $ecotone->getAggregate(TestAggregate::class, '123')->counter());
47+
}
48+
}

0 commit comments

Comments
 (0)