Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply early :) #17

Merged
merged 4 commits into from
Nov 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/AggregateRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ protected function recordThat(AggregateChanged $event)
$this->version += 1;

$this->recordedEvents[] = $event->withVersion($this->version);

$this->apply($event);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/EventStoreIntegration/AggregateRootDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function fromHistory($arClass, \Iterator $aggregateChangedEvents)
* @param AggregateRoot $aggregateRoot
* @param Iterator $events
*/
public function applyPendingStreamEvents(AggregateRoot $aggregateRoot, Iterator $events)
public function applyStreamEvents(AggregateRoot $aggregateRoot, Iterator $events)
{
foreach ($events as $event) {
$aggregateRoot->apply($event);
Expand Down
4 changes: 2 additions & 2 deletions src/EventStoreIntegration/AggregateTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ public function extractPendingStreamEvents($anEventSourcedAggregateRoot)
* @param object $anEventSourcedAggregateRoot
* @param Iterator $events
*/
public function applyPendingStreamEvents($anEventSourcedAggregateRoot, Iterator $events)
public function applyStreamEvents($anEventSourcedAggregateRoot, Iterator $events)
{
$this->getAggregateRootDecorator()->applyPendingStreamEvents($anEventSourcedAggregateRoot, $events);
$this->getAggregateRootDecorator()->applyStreamEvents($anEventSourcedAggregateRoot, $events);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/AggregateRootTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ public function it_applies_event_by_calling_appropriate_event_handler()
//In between would be the process of persisting recorded events to an event stream
//Only if this was successful the events can be applied to the aggregate root
//We skip the persistence process here and apply the events directly
$decorator->applyPendingStreamEvents($user, new \ArrayIterator($recordedEvents));
$decorator->applyStreamEvents($user, new \ArrayIterator($recordedEvents));

$this->assertEquals('John', $user->name());

$user->changeName('Max');

$additionalRecordedEvents = $decorator->extractRecordedEvents($user);

$decorator->applyPendingStreamEvents($user, new \ArrayIterator($additionalRecordedEvents));
$decorator->applyStreamEvents($user, new \ArrayIterator($additionalRecordedEvents));

$this->assertEquals('Max', $user->name());

Expand Down Expand Up @@ -74,7 +74,7 @@ public function it_throws_exception_when_no_handler_on_aggregate()
{
$brokenUser = BrokenUser::nameNew('John');

AggregateRootDecorator::newInstance()->applyPendingStreamEvents(
AggregateRootDecorator::newInstance()->applyStreamEvents(
$brokenUser,
new \ArrayIterator($brokenUser->accessRecordedEvents())
);
Expand All @@ -89,13 +89,13 @@ public function it_reconstructs_itself_from_history()

$recordedEvents = $user->accessRecordedEvents();

AggregateRootDecorator::newInstance()->applyPendingStreamEvents($user, new \ArrayIterator($recordedEvents));
AggregateRootDecorator::newInstance()->applyStreamEvents($user, new \ArrayIterator($recordedEvents));

$user->changeName('Max');

$additionalRecordedEvents = $user->accessRecordedEvents();

AggregateRootDecorator::newInstance()->applyPendingStreamEvents($user, new \ArrayIterator($additionalRecordedEvents));
AggregateRootDecorator::newInstance()->applyStreamEvents($user, new \ArrayIterator($additionalRecordedEvents));

$historyEvents = new \ArrayIterator(array_merge($recordedEvents, $additionalRecordedEvents));

Expand Down
22 changes: 19 additions & 3 deletions tests/EventStoreIntegration/AggregateTranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
use Prooph\EventSourcing\EventStoreIntegration\AggregateRootDecorator;
use Prooph\EventSourcing\EventStoreIntegration\AggregateTranslator;
use ProophTest\EventSourcing\Mock\User;
use ProophTest\EventSourcing\Mock\UserNameChanged;
use ProophTest\EventSourcing\TestCase;
use Prooph\EventStore\Adapter\InMemoryAdapter;
use Prooph\EventStore\Aggregate\AggregateRepository;
use Prooph\EventStore\Aggregate\AggregateType;
use Prooph\EventStore\EventStore;
use Prooph\EventStore\Stream\SingleStreamStrategy;
use Prooph\EventStore\Stream\Stream;
use Prooph\EventStore\Stream\StreamName;

Expand Down Expand Up @@ -97,6 +97,23 @@ public function it_extracts_version(User $loadedUser)
$this->assertEquals(2, $translator->extractAggregateVersion($loadedUser));
}

/**
* @test
* @depends it_translates_aggregate_back_and_forth
* @param User $loadedUser
*/
public function it_applies_stream_events(User $loadedUser)
{
$newName = 'Jane Doe';

$translator = new AggregateTranslator();
$translator->applyStreamEvents($loadedUser, new \ArrayIterator([UserNameChanged::occur($loadedUser->id(), [
'username' => $newName
])]));

$this->assertEquals($newName, $loadedUser->name());
}

/**
* @test
*/
Expand All @@ -115,8 +132,7 @@ protected function resetRepository()
$this->repository = new AggregateRepository(
$this->eventStore,
AggregateType::fromAggregateRootClass('ProophTest\EventSourcing\Mock\User'),
new AggregateTranslator(),
new SingleStreamStrategy($this->eventStore)
new AggregateTranslator()
);
}
}