Skip to content

Instantly share code, notes, and snippets.

@beberlei
Created August 19, 2012 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beberlei/3392926 to your computer and use it in GitHub Desktop.
Save beberlei/3392926 to your computer and use it in GitHub Desktop.
CQRS "complexities" compared to "regular" apps
<?php
class User
{
private $email;
private $emailChangedAt;
public function changeEmail($email)
{
// assignment in wrong method, easy to miss in larger methods
$this->emailChangedAt = new \DateTime("now");
$this->apply(new EmailChanged(array("email" => $email)));
}
protected function applyEmailChanged($event)
{
$this->email = $event->email;
}
}
<?php
class UserTest extends PHPUnit_Framework_TestCase
{
/**
* For every event a test of this kind is necessary,
* where you compare all properties of the aggregate root
* and all its children to a "real" object and one object
* that was completly reconstructed from events.
*/
public function testApplyChangeEmail()
{
$user = new User();
$user->changeEmail("kontakt@beberlei.de");
$userEs = new User();
$userEs->loadFromHistory(array(
new EmailChanged(array("email" => "kontakt@beberlei.de"))
));
$this->assertEquals($user, $userEs);
}
}
<?php
class Subscription
{
private $position;
private $user;
private $valid;
public function __construct(User $user, OrderPosition $position)
{
$this->apply(new SubscriptionCreated(array(
"user" => $user,
"position" => $position
)));
}
protected function applySubscriptionCreated($event)
{
$this->user = $event->user;
$this->position = $event->position;
// now replaying this method requires "position"
// to be the instance of the "position" at the point ´
// where the event was triggered, otherwise it could be
// "true" in the past, and "false" during reconstruction
// of OrderPosition entity.
$this->valid = $event->position->isValid();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment