Installation
composer require aulasoftwarelibre/ddd-test
Ensure your Laravel project meets the PHP 7.2+ requirement and includes the listed dependencies (e.g., prooph/event-store, doctrine/collections).
First Use Case: Domain Event Testing
Extend the provided DomainEventTestCase in your test:
use AulaSoftwareLibre\DDDTest\DomainEventTestCase;
class MyDomainEventTest extends DomainEventTestCase
{
public function testEventSerialization()
{
$event = new MyDomainEvent(['key' => 'value']);
$this->assertEventSerialization($event);
}
}
src/DomainEventTestCase.php for built-in assertions like assertEventSerialization(), assertEventDeserialization(), and assertEventPublished().Domain Event Testing
DomainEventTestCase for Prooph Event Sourcing events.$event = new OrderCreatedEvent($orderId, $customerId);
$this->assertEventPublished($event, 'order_created');
Aggregate Root Testing
AggregateRootTestCase for Prooph aggregates.class OrderAggregateTest extends AggregateRootTestCase
{
public function testOrderCreation()
{
$order = new OrderAggregate($orderId);
$order->create($customerId);
$this->assertEventsEmitted($order, [
new OrderCreatedEvent($orderId, $customerId)
]);
}
}
Command/Query Testing
CommandTestCase and QueryTestCase for Symfony Messenger integration.$command = new CreateOrderCommand($orderId, $customerId);
$this->assertCommandHandled($command, CreateOrderHandler::class);
Mockery or PHPUnit for mocking services.EventStore in config/prooph.php to match your test environment.php-matcher for custom assertions (e.g., nested object validation).Dependency Conflicts
composer.json:
"prooph/event-sourcing": "5.6.*",
"symfony/http-foundation": "4.4.*"
Event Store Configuration
EventStore. For CI/CD, mock the store:
$this->eventStore = $this->createMock(EventStore::class);
Serialization Issues
JsonSerializable or Arrayable. Use assertEventSerialization() to debug.DomainEventTestCase::dumpEvent() to log raw event data.createMock() for Symfony Messenger handlers:
$handler = $this->createMock(CreateOrderHandler::class);
$this->messenger->setHandler($handler);
Custom Assertions
Add methods to DomainEventTestCase for project-specific rules:
protected function assertCustomRule(Event $event)
{
$this->assertTrue($event->getPayload()['valid'] ?? false);
}
Test Traits
Extract reusable logic into traits (e.g., AssertsEventMetadata):
trait AssertsEventMetadata
{
protected function assertMetadata(Event $event, array $expected)
{
$this->assertEquals($expected, $event->getMetadata());
}
}
Laravel Artisan Commands
Test commands with ArtisanTestCase (if extended):
$this->artisan('order:create', ['id' => 123])
->assertExitCode(0);
How can I help you explore Laravel packages today?