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

v4.0 #22

Merged
merged 30 commits into from
Nov 22, 2015
Merged

v4.0 #22

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
29941ea
Adjustments for prooph/event-store v6
Sep 29, 2015
f3bd25e
Apply CS fixes
Sep 29, 2015
3ac58f4
Merge pull request #10 from codeliner/feature/pes_6
prolic Sep 30, 2015
4b60078
update according to https://github.com/prooph/event-store/pull/112
prolic Oct 9, 2015
58d4934
Fix test error caused by identity map dirty checking
Oct 9, 2015
5299cb4
Merge pull request #1 from codeliner/apply_iterator
prolic Oct 9, 2015
faf399c
Merge pull request #12 from prolic/apply_iterator
codeliner Oct 9, 2015
6bb1db2
fix namespace organisation in tests
prolic Oct 17, 2015
5c6ada9
Merge pull request #13 from prolic/test-namespaces
codeliner Oct 17, 2015
d9dfbba
event store 6.0-beta dep
codeliner Oct 21, 2015
04b4972
Merge pull request #14 from prooph/codeliner-patch-1
codeliner Oct 21, 2015
09cfdc7
extract aggregate version
prolic Nov 2, 2015
b647bfe
Merge pull request #16 from prolic/extract_version
codeliner Nov 2, 2015
b298e74
apply early :)
prolic Nov 4, 2015
dec315d
apply php cs fixes
prolic Nov 4, 2015
77b9997
remove single stream strategy from tests, further updates
prolic Nov 4, 2015
0b73ec2
add missing test
prolic Nov 4, 2015
ccccdbf
Merge pull request #17 from prooph/apply-early
codeliner Nov 4, 2015
7528ef1
Align translator and decorator to fix replay bug
Nov 4, 2015
115b5a0
Merge pull request #18 from codeliner/fix/replay_not_appy
prolic Nov 4, 2015
2371cc0
Update composer.json
prolic Nov 4, 2015
6fbf896
php7 support
prolic Nov 5, 2015
f4b4d65
php7 support
prolic Nov 7, 2015
cd988de
allow empty payload in occur-method
prolic Nov 16, 2015
a7a7334
small fix
prolic Nov 16, 2015
c6e3c62
Merge pull request #20 from prooph/prolic-patch-1
codeliner Nov 16, 2015
4a0a617
Remove outdated proophessor link
codeliner Nov 21, 2015
5a68c98
Update quickstart
Nov 21, 2015
07f1e12
Make CS happy
Nov 21, 2015
45241c1
Merge pull request #21 from codeliner/update_quickstart
codeliner Nov 21, 2015
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
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ php:
- 5.6
- 7

matrix:
allow_failures:
- php: 7

before_script:
- composer self-update
- composer --dev install
Expand Down
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@ Simple and lightweight event sourcing library with out of the box support for [P

#Installation

You can install ProophEventSourcing via composer by adding `"prooph/event-sourcing": "~2.0"` as requirement to your composer.json.
You can install ProophEventSourcing via composer by adding `"prooph/event-sourcing": "~4.0"` as requirement to your composer.json.

#Usage

Our [quickstart](https://github.com/prooph/event-sourcing/blob/master/examples/quickstart.php) should give you a starting point.
It's a very small domain but shows you the useage of ProophEventSourcing and the integration with ProophEventStore.


Detailed information can also be found in the documentation of [proophessor](http://prooph.github.io/proophessor) which integrates prooph/event-sourcing with Zend Framework 2.


#ProophEventStore Integration

ProophEventSourcing ships with a [ProophEventStore](https://github.com/prooph/event-store) AggregateTranslator to connect the store
Expand Down
14 changes: 8 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "PHP EventSourcing library",
"type": "library",
"license": "BSD-3-Clause",
"homepage": "https://github.com/prooph/event-sourcing/",
"homepage": "http://getprooph.org/",
"authors": [
{
"name": "Alexander Miertsch",
Expand All @@ -21,26 +21,28 @@
"prooph"
],
"require": {
"php": ">=5.5",
"php": "~5.5|~7.0",
"ramsey/uuid" : "~2.8",
"beberlei/assert": "~2.3",
"prooph/common": "~3.5"
},
"require-dev": {
"phpunit/phpunit": "~4.7",
"prooph/event-store" : "dev-master",
"phpunit/phpunit": "~4.8",
"prooph/event-store" : "dev-develop",
"fabpot/php-cs-fixer": "1.7.*",
"satooshi/php-coveralls": "dev-master"
},
"autoload": {
"psr-4": {
"Prooph\\EventSourcing\\": "src/"

}
},
"suggest" : {
"prooph/event-store" : "^6.0 - Use prooph/event-store to persist recorded events in event streams"
},
"autoload-dev": {
"psr-4": {
"Prooph\\EventSourcingTest\\": "tests/"
"ProophTest\\EventSourcing\\": "tests/"
}
}
}
5 changes: 3 additions & 2 deletions examples/quickstart.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ public function get(Uuid $uuid);
use Prooph\EventStore\Aggregate\AggregateRepository;
use Prooph\EventStore\Aggregate\AggregateType;
use Prooph\EventStore\EventStore;
use Prooph\EventStore\Stream\AggregateStreamStrategy;
use Rhumsaa\Uuid\Uuid;

/**
Expand All @@ -215,7 +214,9 @@ public function __construct(EventStore $eventStore)
$eventStore,
AggregateType::fromAggregateRootClass('My\Model\User'),
new AggregateTranslator(),
new AggregateStreamStrategy($eventStore)
null, //We don't use a snapshot store in the example
null, //Also a custom stream name is not required
true //But we enable the "one-stream-per-aggregate" mode
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/AggregateChanged.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class AggregateChanged extends DomainEvent
* @param array $payload
* @return static
*/
public static function occur($aggregateId, array $payload)
public static function occur($aggregateId, array $payload = [])
{
$instance = new static($aggregateId, $payload);

Expand Down
11 changes: 6 additions & 5 deletions src/AggregateRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ abstract class AggregateRoot
/**
* Current version
*
* @var float
* @var int
*/
protected $version = 0;

Expand All @@ -35,10 +35,10 @@ abstract class AggregateRoot
protected $recordedEvents = [];

/**
* @param AggregateChanged[] $historyEvents
* @param \Iterator $historyEvents
* @return static
*/
protected static function reconstituteFromHistory(array $historyEvents)
protected static function reconstituteFromHistory(\Iterator $historyEvents)
{
$instance = new static();
$instance->replay($historyEvents);
Expand Down Expand Up @@ -90,14 +90,15 @@ protected function recordThat(AggregateChanged $event)
/**
* Replay past events
*
* @param AggregateChanged[] $historyEvents
* @param \Iterator $historyEvents
*
* @throws \RuntimeException
* @return void
*/
protected function replay(array $historyEvents)
protected function replay(\Iterator $historyEvents)
{
foreach ($historyEvents as $pastEvent) {
/** @var AggregateChanged $pastEvent */
$this->version = $pastEvent->version();

$this->apply($pastEvent);
Expand Down
23 changes: 21 additions & 2 deletions src/EventStoreIntegration/AggregateRootDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Prooph\EventSourcing\EventStoreIntegration;

use Iterator;
use Prooph\EventSourcing\AggregateRoot;

/**
Expand All @@ -29,6 +30,15 @@ public static function newInstance()
return new static();
}

/**
* @param AggregateRoot $anAggregateRoot
* @return int
*/
public function extractAggregateVersion(AggregateRoot $anAggregateRoot)
{
return $anAggregateRoot->version;
}

/**
* @param AggregateRoot $anAggregateRoot
* @return \Prooph\EventSourcing\AggregateChanged[]
Expand All @@ -49,11 +59,11 @@ public function extractAggregateId(AggregateRoot $anAggregateRoot)

/**
* @param string $arClass
* @param array $aggregateChangedEvents
* @param \Iterator $aggregateChangedEvents
* @return AggregateRoot
* @throws \RuntimeException
*/
public function fromHistory($arClass, array $aggregateChangedEvents)
public function fromHistory($arClass, \Iterator $aggregateChangedEvents)
{
if (! class_exists($arClass)) {
throw new \RuntimeException(
Expand All @@ -64,6 +74,15 @@ public function fromHistory($arClass, array $aggregateChangedEvents)
return $arClass::reconstituteFromHistory($aggregateChangedEvents);
}

/**
* @param AggregateRoot $aggregateRoot
* @param Iterator $events
*/
public function replayStreamEvents(AggregateRoot $aggregateRoot, Iterator $events)
{
$aggregateRoot->replay($events);
}

/**
* @throws \BadMethodCallException
* @return string representation of the unique identifier of the aggregate root
Expand Down
33 changes: 21 additions & 12 deletions src/EventStoreIntegration/AggregateTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Prooph\EventSourcing\EventStoreIntegration;

use Iterator;
use Prooph\Common\Messaging\Message;
use Prooph\EventStore\Aggregate\AggregateType;
use Prooph\EventStore\Aggregate\AggregateTranslator as EventStoreAggregateTranslator;
Expand All @@ -28,6 +29,15 @@ class AggregateTranslator implements EventStoreAggregateTranslator
*/
protected $aggregateRootDecorator;

/**
* @param object $eventSourcedAggregateRoot
* @return int
*/
public function extractAggregateVersion($eventSourcedAggregateRoot)
{
return (int) $this->getAggregateRootDecorator()->extractAggregateVersion($eventSourcedAggregateRoot);
}

/**
* @param object $anEventSourcedAggregateRoot
* @return string
Expand All @@ -39,21 +49,11 @@ public function extractAggregateId($anEventSourcedAggregateRoot)

/**
* @param AggregateType $aggregateType
* @param Message[] $historyEvents
* @throws \RuntimeException
* @param \Iterator $historyEvents
* @return object reconstructed AggregateRoot
*/
public function reconstituteAggregateFromHistory(AggregateType $aggregateType, $historyEvents)
public function reconstituteAggregateFromHistory(AggregateType $aggregateType, \Iterator $historyEvents)
{
if (count($historyEvents) === 0) {
throw new \RuntimeException(
sprintf(
"Can not reconstitute Aggregate %s from history. No stream events given",
$aggregateType->toString()
)
);
}

return $this->getAggregateRootDecorator()
->fromHistory($aggregateType->toString(), $historyEvents);
}
Expand All @@ -67,6 +67,15 @@ public function extractPendingStreamEvents($anEventSourcedAggregateRoot)
return $this->getAggregateRootDecorator()->extractRecordedEvents($anEventSourcedAggregateRoot);
}

/**
* @param object $anEventSourcedAggregateRoot
* @param Iterator $events
*/
public function replayStreamEvents($anEventSourcedAggregateRoot, Iterator $events)
{
$this->getAggregateRootDecorator()->replayStreamEvents($anEventSourcedAggregateRoot, $events);
}

/**
* @return AggregateRootDecorator
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/AggregateChangedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
* Date: 04/17/14 - 21:45
*/

namespace Prooph\EventSourcingTest;
namespace ProophTest\EventSourcing;

use Prooph\EventSourcing\AggregateChanged;

/**
* Class AggregateChangedTest
*
* @package Prooph\EventSourcingTest\EventSourcing
* @package ProophTest\EventSourcing\EventSourcing
* @author Alexander Miertsch <contact@prooph.de>
*/
class AggregateChangedTest extends TestCase
Expand Down
50 changes: 38 additions & 12 deletions tests/AggregateRootTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
* Date: 04/18/14 - 00:03
*/

namespace Prooph\EventSourcingTest;
namespace ProophTest\EventSourcing;

use Prooph\EventSourcingTest\Mock\BrokenUser;
use Prooph\EventSourcingTest\Mock\User;
use Prooph\EventSourcing\EventStoreIntegration\AggregateRootDecorator;
use ProophTest\EventSourcing\Mock\BrokenUser;
use ProophTest\EventSourcing\Mock\User;

/**
* Class AggregateRootTest
*
* @package Prooph\EventSourcingTest\EventSourcing
* @package ProophTest\EventSourcing\EventSourcing
* @author Alexander Miertsch <contact@prooph.de>
*/
class AggregateRootTest extends TestCase
Expand All @@ -27,24 +28,38 @@ class AggregateRootTest extends TestCase
*/
public function it_applies_event_by_calling_appropriate_event_handler()
{
$decorator = AggregateRootDecorator::newInstance();

$user = User::nameNew('John');

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

//Recording and applying events are two different steps
//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->replayStreamEvents($user, new \ArrayIterator($recordedEvents));

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

$user->changeName('Max');

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

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

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

$pendingEvents = $user->accessRecordedEvents();
$recordedEvents = array_merge($recordedEvents, $additionalRecordedEvents);

$this->assertEquals(2, count($pendingEvents));
$this->assertEquals(2, count($recordedEvents));

$userCreatedEvent = $pendingEvents[0];
$userCreatedEvent = $recordedEvents[0];

$this->assertEquals('John', $userCreatedEvent->name());
$this->assertEquals(1, $userCreatedEvent->version());

$userNameChangedEvent = $pendingEvents[1];
$userNameChangedEvent = $recordedEvents[1];

$this->assertEquals('Max', $userNameChangedEvent->newUsername());
$this->assertEquals(2, $userNameChangedEvent->version());
Expand All @@ -53,11 +68,16 @@ public function it_applies_event_by_calling_appropriate_event_handler()
/**
* @test
* @expectedException RuntimeException
* @expectedExceptionMessage Missing event handler method whenUserCreated for aggregate root Prooph\EventSourcingTest\Mock\BrokenUser
* @expectedExceptionMessage Missing event handler method whenUserCreated for aggregate root ProophTest\EventSourcing\Mock\BrokenUser
*/
public function it_throws_exception_when_no_handler_on_aggregate()
{
BrokenUser::nameNew('John');
$brokenUser = BrokenUser::nameNew('John');

AggregateRootDecorator::newInstance()->applyStreamEvents(
$brokenUser,
new \ArrayIterator($brokenUser->accessRecordedEvents())
);
}

/**
Expand All @@ -67,11 +87,17 @@ public function it_reconstructs_itself_from_history()
{
$user = User::nameNew('John');

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

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

$user->changeName('Max');

$historyEvents = $user->accessRecordedEvents();
$additionalRecordedEvents = $user->accessRecordedEvents();

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

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

$sameUser = User::fromHistory($historyEvents);

Expand Down
Loading