Links

Testing Messaging

Testing Messaging architecture in PHP
Testing Message Driven Architecture can easily become nightmare from the perspective of speed and maintenance of such tests. It's really important to write easy to understand, quick and reliable test scenarios. especially in long term projects. That's why Ecotone comes with test supporting tools, which helps writing tests that are close to the way code runs in production, yet kept simple and isolated.

Ecotone Lite

Ecotone Lite is a way to run Ecotone Application with full possibility to customize it. You may point exactly what classes you would like to run, turn on and off modules that you want to include or exclude.
This makes Ecotone Lite great solution for running your tests in a way that they are really close to the way your code works on production with isolation to the set of classes you would like to test.

Configuring Ecotone Lite for your tests

$ecotoneLite = EcotoneLite::bootstrapForTesting(
// 1. Classes to resolve
[User::class],
// 2. Available services, you may inject container instead
[new EmailConverter(), new PhoneNumberConverter(), new UuidConverter()],
// 3. Service Configuration
ServiceConfiguration::createWithDefaults()
// 4. resolve all classes from given namespace
->withNamespaces(["App\Testing\Infrastructure\Converter"])
// 5 add extension objects
->withExtensionObjects([
// 6. register in memory repository for User
InMemoryStateStoredRepositoryBuilder::createForAllAggregates()
])
// 7. Turn off given Ecotone's modules
->withSkippedModulePackageNames([
// 8. Turning off asynchronous package
ModulePackageList::ASYNCHRONOUS_PACKAGE
])
);
  1. 1.
    Classes to resolve - The first parameter, sets list of classes that you would like to include in this test. Those are classes that containing Ecotone's attributes.
  2. 2.
    Available services - Provides references to the service classes used in this test. You may inject PSR compatible container instead.
  3. 3.
    Service Configuration - In here you may customize your configuration for this test case. All configs can be find here.
  4. 4.
    Service Configuration -withNamespaces - One of the configuration worth mention is withNamespaces this allows to use define given or set of namespaces, from which all classes will be included in this test.
  5. 5.
    Service Configuration -withExtensionObjects - This allows for defining extension object for customizing Ecotone's modules
  6. 6.
    Register in memory repository - This example extension object registers In Memory Repository for all your state stored aggregates
  7. 7.
    Turn off Modules - This allows you to turn off given Ecotone's module for this test.
  8. 8.
    Turning off asynchronous package - Turning off asynchronous package for example, will make your code execute synchronously, so you won't need to bother with running consumers in your test.
If you want to read more about Ecotone Lite configuration, check Module Page.

Calling Buses

After setting up Ecotone Lite you may know call Command/Query/Event Bus or any registered Gateway.
$ecotoneLite->getCommandBus()->send(new RegisterUser(
Uuid::uuid4(),
"johny",
Email::create("[email protected]"),
PhoneNumber::create("148518518518"))
));
The same way you can use getQueryBus or getEventBus.

Test Support Gateway

Ecotone comes with Test Support Gateway, that provides several methods that helps with testing message driven architecture.
$testSupport = $ecotoneLite->getMessagingTestSupport();

Verifying Published Events

After sending Command, you may verify, if given set of events were published as a result.
For this Ecotone introduce Message Collector which intercept your message flow.
$this->assertEquals(
[new OrderWasPlaced($orderId)],
$testSupport->getRecordedEvents()
);
Ecotone intercept all interactions with Event/Command/Query Buses. This allows you to spy on all recorded messages, to verify their state.

Verifying Message Headers

If you want to validate, if Event was sent with given set of headers:
$this->assertEquals(
$executorId,
$testSupport->getRecordedEventMessages()[0]->getHeaders()->get('executorId')
);

Verifying Commands/Queries

You may also verify, if Command or Query was sent:
/**
* @return array<int, mixed>
*/
public function getRecordedCommands(): array;
/**
* Allows to assert metadata of the message
*
* @return Message[]
*/
public function getRecordedCommandMessages(): array;
/**
* @return array<int, mixed>
*/
public function getRecordedQueries(): array;
/**
* Allows to assert metadata of the message
*
* @return Message[]
*/
public function getRecordedQueryMessages(): array;

Discarding recorded messages

In case you're not interested in current messages, you may clean up Message Collector
$testSupport->discardRecordedMessages();