Download the PHP package patchlevel/event-sourcing-phpunit without Composer
On this page you can find all versions of the php package patchlevel/event-sourcing-phpunit. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download patchlevel/event-sourcing-phpunit
More information about patchlevel/event-sourcing-phpunit
Files in patchlevel/event-sourcing-phpunit
Package event-sourcing-phpunit
Short Description PHPUnit testing utilities for patchlevel/event-sourcing
License MIT
Homepage https://event-sourcing.patchlevel.io
Informations about the package event-sourcing-phpunit
Testing utilities
With this library you can ease the testing for your event-sourcing project when using PHPUnit. It comes with utilities for aggregates and subscribers.
Installation
Testing Aggregates
There is a special TestCase
for aggregate tests which you can extend from. Extending from AggregateRootTestCase
enables you to use the given / when / then notation. This makes it very clear what the test is doing. When extending the
class you will need to implement a method which provides the FQCN of the aggregate you want to test.
When this is done, you already can start testing your behaviour. For example testing that a event is recorded.
You can also provide multiple given events and expect multiple events:
You can also test the creation of the aggregate:
And expect an exception and the message of it:
Using Commandbus like syntax
When using the command bus and the #[Handle]
attributes in your aggregate you can also provide the command directly
for the when
method.
If more parameters than the command is needed, these can also be provided as additional parameters for when
. In this
example the we need a string which will be directly passed to the event.
Testing Subscriber
For testing a subscriber there is a utility class which you can use. Using SubscriberUtilities
will provide you a
bunch of dx features which makes the testing easier. First, you will need to provide the utility class the subscriptions
you will want to test, this is done when initialiszing the class. After that, you can call these 3 methods:
executeSetup
, executeRun
and executeTeardown
. These methods will be calling the right methods which are defined
via the attributes. For our example we are taking as simplified subscriber:
With this, we can now write our test for it:
This Util class can be used for integration or unit tests.
All versions of event-sourcing-phpunit with dependencies
patchlevel/event-sourcing Version ^3.10.0
phpunit/phpunit Version ^10.5.45 || ^11.0.0 || ^12.0.0