June 2017
Intermediate to advanced
394 pages
8h 52m
English
You already know how to publish Domain Events, but how can you unit test this and ensure that UserRegistered is really fired? The easiest way we suggest is to use a specific EventListener that will work as a Spy to record whether or not the Domain Event was published. Let's see an example of the User Entity unit test:
use Ddd\Domain\DomainEventPublisher;use Ddd\Domain\DomainEventSubscriber;class UserTest extends \PHPUnit_Framework_TestCase{ // ... /** * @test */ public function itShouldPublishUserRegisteredEvent() { $subscriber = new SpySubscriber(); $id = DomainEventPublisher::instance()->subscribe($subscriber); $userId = new UserId(); new User($userId, 'valid@email.com', 'password'); DomainEventPublisher::instance()->unsubscribe($id); ...