-
-
Save bwaidelich/7367de6e41f414f572e3759e4e5f196e to your computer and use it in GitHub Desktop.
Example Event Sourced Process Manager & corresponding Unit Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace Wwwision\UserRegistration\Domain; | |
use Neos\EventSourcing\ProcessManager\AbstractEventSourcedProcessManager; | |
use Wwwision\UserRegistration\Domain\Events\UserHasConfirmedRegistration; | |
use Wwwision\UserRegistration\Domain\Events\UserHasStartedRegistration; | |
use Wwwision\UserRegistration\Domain\Events\UserRegistrationWasFinalized; | |
use Wwwision\UserRegistration\Domain\Events\UserWasPromoted; | |
final class RegistrationProcessManager extends AbstractEventSourcedProcessManager | |
{ | |
/** | |
* promotion period (in seconds). A user confirmation within this period leads to the user being promoted | |
*/ | |
const PROMOTION_PERIOD = 10; | |
/** | |
* @var string | |
*/ | |
private $userId; | |
public function whenUserHasStartedRegistration(UserHasStartedRegistration $event): void | |
{ | |
$this->registrationTimestamp = $event->getTimestamp(); | |
} | |
public function whenUserHasConfirmedRegistration(UserHasConfirmedRegistration $event): void | |
{ | |
if ($event->getTimestamp()->diff($this->registrationTimestamp)->s < self::PROMOTION_PERIOD) { | |
$this->recordThat(new UserWasPromoted($event->getUserId())); | |
} | |
$this->recordThat(new UserRegistrationWasFinalized($event->getUserId())); | |
} | |
public function whenUserRegistrationWasFinalized(UserRegistrationWasFinalized $_): void | |
{ | |
$this->registrationTimestamp = null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace Wwwision\UserRegistration\Domain\Tests\Unit; | |
use Neos\Flow\Tests\UnitTestCase; | |
use Wwwision\UserRegistration\Domain\Events\UserHasConfirmedRegistration; | |
use Wwwision\UserRegistration\Domain\Events\UserHasStartedRegistration; | |
use Wwwision\UserRegistration\Domain\Events\UserRegistrationWasFinalized; | |
use Wwwision\UserRegistration\Domain\Events\UserWasPromoted; | |
use Wwwision\UserRegistration\Domain\RegistrationProcessManager; | |
class RegistrationProcessManagerTest extends UnitTestCase | |
{ | |
/** | |
* @var RegistrationProcessManager | |
*/ | |
private $processManager; | |
public function setUp() | |
{ | |
$this->processManager = new RegistrationProcessManager(); | |
} | |
/** | |
* @test | |
*/ | |
public function registrationIsFinalizedAfterUserConfirmation() | |
{ | |
// arrange | |
$this->processManager->whenUserHasStartedRegistration(new UserHasStartedRegistration('userId', 'foo@bar.com', new \DateTimeImmutable('2018-01-01 15:30:30'))); | |
$this->processManager->pullUncommittedEvents(); | |
// act | |
$this->processManager->whenUserHasConfirmedRegistration(new UserHasConfirmedRegistration('userId', new \DateTimeImmutable('2018-01-01 15:30:40'))); | |
// assert | |
$events = $this->processManager->pullUncommittedEvents(); | |
$this->assertThat($events, $this->countOf(1)); | |
$this->assertThat($events[0], $this->isInstanceOf(UserRegistrationWasFinalized::class)); | |
} | |
/** | |
* @test | |
*/ | |
public function userIsPromotedIfUserConfirmsWithinPromotionPeriod() | |
{ | |
// arrange | |
$this->processManager->whenUserHasStartedRegistration(new UserHasStartedRegistration('userId', 'foo@bar.com', new \DateTimeImmutable('2018-01-01 15:30:30'))); | |
$this->processManager->pullUncommittedEvents(); | |
// act | |
$this->processManager->whenUserHasConfirmedRegistration(new UserHasConfirmedRegistration('userId', new \DateTimeImmutable('2018-01-01 15:30:39'))); | |
// assert | |
$events = $this->processManager->pullUncommittedEvents(); | |
$this->assertThat($events, $this->countOf(2)); | |
$this->assertThat($events[0], $this->isInstanceOf(UserWasPromoted::class)); | |
$this->assertThat($events[1], $this->isInstanceOf(UserRegistrationWasFinalized::class)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment