From b298e74acf7c45e1f0c7c8954afac9915dc6c580 Mon Sep 17 00:00:00 2001 From: Sascha-Oliver Prolic Date: Wed, 4 Nov 2015 15:52:16 +0000 Subject: [PATCH 1/4] apply early :) see: https://github.com/prooph/event-store/pull/142 --- src/AggregateRoot.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/AggregateRoot.php b/src/AggregateRoot.php index 511e764..38244f7 100644 --- a/src/AggregateRoot.php +++ b/src/AggregateRoot.php @@ -83,6 +83,8 @@ protected function recordThat(AggregateChanged $event) $this->version += 1; $this->recordedEvents[] = $event->withVersion($this->version); + + $this->apply($event); } /** From dec315d93cbafb5a7801bea4fac163ae993dce2c Mon Sep 17 00:00:00 2001 From: prolic Date: Wed, 4 Nov 2015 15:57:25 +0000 Subject: [PATCH 2/4] apply php cs fixes --- src/AggregateRoot.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AggregateRoot.php b/src/AggregateRoot.php index 38244f7..f6d6f19 100644 --- a/src/AggregateRoot.php +++ b/src/AggregateRoot.php @@ -83,7 +83,7 @@ protected function recordThat(AggregateChanged $event) $this->version += 1; $this->recordedEvents[] = $event->withVersion($this->version); - + $this->apply($event); } From 77b999788bdbfcfb0eda4c3849a969aaf73271f3 Mon Sep 17 00:00:00 2001 From: prolic Date: Wed, 4 Nov 2015 16:17:56 +0000 Subject: [PATCH 3/4] remove single stream strategy from tests, further updates --- src/EventStoreIntegration/AggregateRootDecorator.php | 2 +- src/EventStoreIntegration/AggregateTranslator.php | 4 ++-- tests/AggregateRootTest.php | 10 +++++----- .../EventStoreIntegration/AggregateTranslatorTest.php | 4 +--- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/EventStoreIntegration/AggregateRootDecorator.php b/src/EventStoreIntegration/AggregateRootDecorator.php index b1c5d6d..7e87a14 100644 --- a/src/EventStoreIntegration/AggregateRootDecorator.php +++ b/src/EventStoreIntegration/AggregateRootDecorator.php @@ -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); diff --git a/src/EventStoreIntegration/AggregateTranslator.php b/src/EventStoreIntegration/AggregateTranslator.php index c92ffb5..388a47e 100644 --- a/src/EventStoreIntegration/AggregateTranslator.php +++ b/src/EventStoreIntegration/AggregateTranslator.php @@ -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); } /** diff --git a/tests/AggregateRootTest.php b/tests/AggregateRootTest.php index 28a85bb..10673c2 100644 --- a/tests/AggregateRootTest.php +++ b/tests/AggregateRootTest.php @@ -38,7 +38,7 @@ 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()); @@ -46,7 +46,7 @@ public function it_applies_event_by_calling_appropriate_event_handler() $additionalRecordedEvents = $decorator->extractRecordedEvents($user); - $decorator->applyPendingStreamEvents($user, new \ArrayIterator($additionalRecordedEvents)); + $decorator->applyStreamEvents($user, new \ArrayIterator($additionalRecordedEvents)); $this->assertEquals('Max', $user->name()); @@ -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()) ); @@ -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)); diff --git a/tests/EventStoreIntegration/AggregateTranslatorTest.php b/tests/EventStoreIntegration/AggregateTranslatorTest.php index f21607f..8b0eb35 100644 --- a/tests/EventStoreIntegration/AggregateTranslatorTest.php +++ b/tests/EventStoreIntegration/AggregateTranslatorTest.php @@ -20,7 +20,6 @@ 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; @@ -115,8 +114,7 @@ protected function resetRepository() $this->repository = new AggregateRepository( $this->eventStore, AggregateType::fromAggregateRootClass('ProophTest\EventSourcing\Mock\User'), - new AggregateTranslator(), - new SingleStreamStrategy($this->eventStore) + new AggregateTranslator() ); } } From 0b73ec2b9c35f81a457b36b1832018e9cb9ab2b9 Mon Sep 17 00:00:00 2001 From: prolic Date: Wed, 4 Nov 2015 16:47:56 +0000 Subject: [PATCH 4/4] add missing test --- .../AggregateTranslatorTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/EventStoreIntegration/AggregateTranslatorTest.php b/tests/EventStoreIntegration/AggregateTranslatorTest.php index 8b0eb35..1485b5d 100644 --- a/tests/EventStoreIntegration/AggregateTranslatorTest.php +++ b/tests/EventStoreIntegration/AggregateTranslatorTest.php @@ -15,6 +15,7 @@ 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; @@ -96,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 */