Skip to content

Commit

Permalink
initializing aggregate in tests won't publish its events (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
unixslayer authored Aug 26, 2024
1 parent 5b1a299 commit 82a2846
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 23 deletions.
6 changes: 3 additions & 3 deletions packages/Ecotone/src/Lite/Test/FlowTestSupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ public function withEventsFor(string|object|array $identifiers, string $aggregat
AggregateMessage::RESULT_AGGREGATE_OBJECT => $aggregateClass,
AggregateMessage::RESULT_AGGREGATE_EVENTS => $events,
],
ModellingHandlerModule::getRegisterAggregateSaveRepositoryInputChannel($aggregateClass)
ModellingHandlerModule::getRegisterAggregateSaveRepositoryInputChannel($aggregateClass). '.test_setup_state'
);

return $this->discardRecordedMessages();
return $this;
}

public function withStateFor(object $aggregate): self
Expand All @@ -192,7 +192,7 @@ public function withStateFor(object $aggregate): self
[
AggregateMessage::RESULT_AGGREGATE_OBJECT => $aggregate,
],
ModellingHandlerModule::getRegisterAggregateSaveRepositoryInputChannel($aggregate::class)
ModellingHandlerModule::getRegisterAggregateSaveRepositoryInputChannel($aggregate::class). '.test_setup_state'
);

return $this;
Expand Down
2 changes: 2 additions & 0 deletions packages/Ecotone/src/Messaging/Config/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,6 @@ public function registerServiceDefinition(string|Reference $id, Container\Defini
public function registerServiceAlias(string|Reference $id, Reference $aliasTo): Configuration;

public function isRunningForEnterpriseLicence(): bool;

public function isRunningForTest(): bool;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Ecotone\Messaging\Config;

use Ecotone\Lite\Test\TestConfiguration;
use function array_map;

use Ecotone\AnnotationFinder\AnnotationFinder;
Expand Down Expand Up @@ -156,6 +157,8 @@ final class MessagingSystemConfiguration implements Configuration

private bool $isRunningForEnterpriseLicence;

private bool $isRunningForTest = false;

/**
* @param object[] $extensionObjects
*/
Expand Down Expand Up @@ -197,6 +200,13 @@ function ($extensionObject) {
return ! ($extensionObject instanceof ServiceConfiguration);
}
);

foreach ($extensionObjects as $extensionObject) {
if ($extensionObject instanceof TestConfiguration) {
$this->isRunningForTest = true;
}
}

$extensionObjects[] = $serviceConfiguration;
$this->isRunningForEnterpriseLicence = $serviceConfiguration->hasEnterpriseLicence();
$this->initialize($moduleConfigurationRetrievingService, $extensionObjects, $serviceConfiguration);
Expand Down Expand Up @@ -818,6 +828,11 @@ public function isRunningForEnterpriseLicence(): bool
return $this->isRunningForEnterpriseLicence;
}

public function isRunningForTest(): bool
{
return $this->isRunningForTest;
}

/**
* @inheritDoc
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ class ChainedMessageProcessorBuilder
{
private ?InterceptedMessageProcessorBuilder $interceptedProcessor = null;

private function __construct(private array $processors = []) {
}
private function __construct(private array $processors = []) {}

public static function create(): self
{
Expand Down Expand Up @@ -78,4 +77,4 @@ public function compileProcessor(MessagingContainerBuilder $builder, MethodInter
default => new Definition(ChainedMessageProcessor::class, [$compiledProcessors])
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ private function __construct(
$this->chainedMessageProcessorBuilder = ChainedMessageProcessorBuilder::create();
}

public function __clone(): void
{
$this->chainedMessageProcessorBuilder = clone $this->chainedMessageProcessorBuilder;
}

public static function create(): self
{
return new self();
Expand Down
35 changes: 22 additions & 13 deletions packages/Ecotone/src/Modelling/Config/ModellingHandlerModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -727,19 +727,28 @@ private function registerSaveAggregate(ClassDefinition $aggregateClassDefinition
/** @TODO do not require method name in save service */
$methodName = $aggregateClassDefinition->getPublicMethodNames() ? $aggregateClassDefinition->getPublicMethodNames()[0] : '__construct';

$configuration->registerMessageHandler(
$chainMessageHandlerBuilder
->chain(ResolveAggregateEventsServiceBuilder::create($aggregateClassDefinition, $methodName, $interfaceToCallRegistry))
->chain(
SaveAggregateServiceBuilder::create(
$aggregateClassDefinition,
$methodName,
$interfaceToCallRegistry,
$baseEventSourcingConfiguration
)
->withAggregateRepositoryFactories($this->aggregateRepositoryReferenceNames)
$saveAggregateBuilder = $chainMessageHandlerBuilder
->chain(ResolveAggregateEventsServiceBuilder::create($aggregateClassDefinition, $methodName, $interfaceToCallRegistry))
->chain(
SaveAggregateServiceBuilder::create(
$aggregateClassDefinition,
$methodName,
$interfaceToCallRegistry,
$baseEventSourcingConfiguration
)
->chain(PublishAggregateEventsServiceBuilder::create($aggregateClassDefinition, $methodName))
);
->withAggregateRepositoryFactories($this->aggregateRepositoryReferenceNames)
)
;

if ($configuration->isRunningForTest()) {
$saveAggregateBuilderWithTestState = clone $saveAggregateBuilder;
$configuration->registerMessageHandler(
$saveAggregateBuilderWithTestState
->withInputChannelName($saveAggregateBuilderWithTestState->getInputMessageChannelName() . '.test_setup_state')
);
}

$saveAggregateBuilder = $saveAggregateBuilder->chain(PublishAggregateEventsServiceBuilder::create($aggregateClassDefinition, $methodName));
$configuration->registerMessageHandler($saveAggregateBuilder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ classesToResolve: [Order::class, Basket::class, BasketProjection::class, OrderPr
new BasketCreated('basket-2'),
]
)
;

$ecotone
->publishEventWithRoutingKey(OrderCreated::NAME, new OrderCreated('order-1'))
->publishEventWithRoutingKey(OrderCreated::NAME, new OrderCreated('order-2'))
->publishEventWithRoutingKey(BasketCreated::NAME, new BasketCreated('basket-1'))
->publishEventWithRoutingKey(BasketCreated::NAME, new BasketCreated('basket-2'))
->triggerProjection(BasketProjection::NAME)
->triggerProjection(OrderProjection::NAME)
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public function test_list_unapproved_products()
$productName = 'Wooden table';
$productPrice = 1000;

$productWasAdded = new ProductWasAdded(Uuid::fromString($productId), $productName, Money::EUR($productPrice));

$this->assertEquals(
[
[
Expand All @@ -41,9 +43,8 @@ public function test_list_unapproved_products()
]
],
EcotoneLite::bootstrapFlowTestingWithEventStore([UnapprovedProductList::class, Product::class, UuidConverter::class], [new UnapprovedProductList(), new UuidConverter()])
->withEventsFor($productId, Product::class, [
new ProductWasAdded(Uuid::fromString($productId), $productName, Money::EUR($productPrice))
])
->withEventsFor($productId, Product::class, [$productWasAdded])
->publishEvent($productWasAdded)
->sendQueryWithRouting('getUnapprovedProducts')
);
}
Expand All @@ -64,4 +65,4 @@ public function test_list_when_product_was_approved()
->sendQueryWithRouting('getUnapprovedProducts')
);
}
}
}

0 comments on commit 82a2846

Please sign in to comment.