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

Convert transaction manager to event store plugin #11

Merged
merged 2 commits into from
Nov 5, 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: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
],
"require": {
"php": ">=5.5",
"prooph/event-store" : "6.0-beta.1",
"prooph/event-store" : "dev-develop",
"prooph/service-bus" : "^4.0"
},
"require-dev": {
Expand Down
10 changes: 6 additions & 4 deletions docs/transaction_manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

## Set Up
To enable transaction handling based on command dispatch you need to set up the [TransactionManager](src/TransactionManager.php).
The only dependency of the transaction manager is an instance of `Prooph\EventStore\EventStore`.
The transaction manager acts as command bus AND event store plugin so you need to attach it to both:

Then simply add the transaction manger as a plugin to the `command bus`:
```php
/** @var $eventStore Prooph\EventStore\EventStore */
$transactionManager->setUp($eventStore);

/** @var $commandBus Prooph\ServiceBus\CommandBus */
$commandBus->utilize($transactionManager);
```
Expand All @@ -14,9 +16,9 @@ That's it!

### Container-Driven Set Up
If you are using the `container-aware factories` shipped with prooph/service-bus you may also
want to auto register the `TransactionManager`. As long as the event store is available as service `Prooph\EventStore\EventStore` in the container you can use
want to auto register the `TransactionManager`. As long as the command bus is available as service `Prooph\ServiceBus\CommandBus` in the container you can use
the [TransactionManagerFactory](src/Container/TransactionManagerFactory.php) for that. Just map the factory to a service name like `prooph.transaction_manager` and
add the service name to the plugin list of the command bus configuration. Please refer to [prooph/service-bus docs](https://github.com/prooph/service-bus/blob/master/docs/factories.md)
add the service name to the plugin list of the event store configuration. Please refer to [prooph/event-store docs](https://github.com/prooph/event-store/blob/master/docs/interop_factories.md#event-store-factory)
for more details.

## Features
Expand Down
11 changes: 8 additions & 3 deletions src/Container/TransactionManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
namespace Prooph\EventStoreBusBridge\Container;

use Interop\Container\ContainerInterface;
use Prooph\EventStore\EventStore;
use Prooph\EventStoreBusBridge\TransactionManager;
use Prooph\ServiceBus\CommandBus;

/**
* Class TransactionManagerFactory
Expand All @@ -23,7 +23,12 @@ final class TransactionManagerFactory
{
public function __invoke(ContainerInterface $container)
{
$eventStore = $container->get(EventStore::class);
return new TransactionManager($eventStore);
$commandBus = $container->get(CommandBus::class);

$transactionManager = new TransactionManager();

$commandBus->utilize($transactionManager);

return $transactionManager;
}
}
6 changes: 4 additions & 2 deletions src/TransactionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Prooph\Common\Messaging\Command;
use Prooph\Common\Messaging\Message;
use Prooph\EventStore\EventStore;
use Prooph\EventStore\Plugin\Plugin;
use Prooph\EventStore\Stream\Stream;
use Prooph\ServiceBus\CommandBus;

Expand All @@ -32,7 +33,7 @@
*
* @package Prooph\EventStoreBusBridge
*/
final class TransactionManager implements ActionEventListenerAggregate
final class TransactionManager implements Plugin, ActionEventListenerAggregate
{
use DetachAggregateHandlers;

Expand All @@ -48,8 +49,9 @@ final class TransactionManager implements ActionEventListenerAggregate

/**
* @param EventStore $eventStore
* @return void
*/
public function __construct(EventStore $eventStore)
public function setUp(EventStore $eventStore)
{
$this->eventStore = $eventStore;
$this->eventStore->getActionEventEmitter()->attachListener('create.pre', [$this, 'onEventStoreCreateStream'], -1000);
Expand Down
11 changes: 5 additions & 6 deletions tests/Container/TransactionManagerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
namespace ProophTest\EventStoreBusBridge\Container;

use Interop\Container\ContainerInterface;
use Prooph\Common\Event\ActionEventEmitter;
use Prooph\EventStore\EventStore;
use Prooph\EventStoreBusBridge\Container\TransactionManagerFactory;
use Prooph\EventStoreBusBridge\TransactionManager;
use Prooph\ServiceBus\CommandBus;
use Prophecy\Argument;

/**
* Class TransactionManagerFactoryTest
Expand All @@ -28,14 +28,13 @@ final class TransactionManagerFactoryTest extends \PHPUnit_Framework_TestCase
*/
public function it_creates_a_transaction_manager()
{
$actionEventEmitter = $this->prophesize(ActionEventEmitter::class);
$eventStore = $this->prophesize(EventStore::class);
$commandBus = $this->prophesize(CommandBus::class);

$eventStore->getActionEventEmitter()->willReturn($actionEventEmitter->reveal());
$commandBus->utilize(Argument::type(TransactionManager::class))->shouldBeCalled();

$container = $this->prophesize(ContainerInterface::class);

$container->get(EventStore::class)->willReturn($eventStore->reveal());
$container->get(CommandBus::class)->willReturn($commandBus->reveal());

$factory = new TransactionManagerFactory();

Expand Down
38 changes: 28 additions & 10 deletions tests/TransactionManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ function ($args) use (&$appendToStreamListener) {

$eventStoreMock->getActionEventEmitter()->willReturn($emitter->reveal());

$transactionManager = new TransactionManager($eventStoreMock->reveal());
$transactionManager = new TransactionManager();

$transactionManager->setUp($eventStoreMock->reveal());

$this->assertEquals([$transactionManager, 'onEventStoreCreateStream'], $createStreamListener);
$this->assertEquals([$transactionManager, 'onEventStoreAppendToStream'], $appendToStreamListener);
Expand All @@ -66,7 +68,7 @@ function ($args) use (&$appendToStreamListener) {
*/
public function it_attaches_itself_to_command_bus_initialize_and_finalize_events()
{
$transactionManager = new TransactionManager($this->getEventStoreObjectProphecy()->reveal());
$transactionManager = new TransactionManager();

$commandBusEmitter = $this->prophesize(ActionEventEmitter::class);

Expand All @@ -87,7 +89,9 @@ public function it_begins_a_transaction_on_command_dispatch_initialize()

$eventStoreMock->beginTransaction()->shouldBeCalled();

$transactionManager = new TransactionManager($eventStoreMock->reveal());
$transactionManager = new TransactionManager();

$transactionManager->setUp($eventStoreMock->reveal());

$actionEvent = $this->prophesize(ActionEvent::class);

Expand All @@ -107,7 +111,9 @@ public function it_commits_a_transaction_on_command_dispatch_finalize_if_no_exce

$eventStoreMock->commit()->shouldBeCalled();

$transactionManager = new TransactionManager($eventStoreMock->reveal());
$transactionManager = new TransactionManager();

$transactionManager->setUp($eventStoreMock->reveal());

$actionEvent = $this->prophesize(ActionEvent::class);

Expand All @@ -127,7 +133,9 @@ public function it_rollback_a_transaction_on_command_dispatch_finalize_if_except

$eventStoreMock->rollback()->shouldBeCalled();

$transactionManager = new TransactionManager($eventStoreMock->reveal());
$transactionManager = new TransactionManager();

$transactionManager->setUp($eventStoreMock->reveal());

$actionEvent = $this->prophesize(ActionEvent::class);

Expand All @@ -149,7 +157,9 @@ public function it_does_not_perform_rollback_after_transaction_commit()

$eventStoreMock->rollback()->shouldNotBeCalled();

$transactionManager = new TransactionManager($eventStoreMock->reveal());
$transactionManager = new TransactionManager();

$transactionManager->setUp($eventStoreMock->reveal());

$actionEvent = $this->prophesize(ActionEvent::class);

Expand Down Expand Up @@ -181,7 +191,9 @@ public function it_adds_causation_id_and_causation_name_on_event_store_create_st

$eventStoreMock->beginTransaction()->shouldBeCalled();

$transactionManager = new TransactionManager($eventStoreMock->reveal());
$transactionManager = new TransactionManager();

$transactionManager->setUp($eventStoreMock->reveal());

//Now the command is set as currentCommand internally and later used when new stream is going to be created
$transactionManager->onInitialize($initializeActionEvent->reveal());
Expand Down Expand Up @@ -237,7 +249,9 @@ public function it_adds_causation_id_and_causation_name_on_event_store_append_to

$eventStoreMock->beginTransaction()->shouldBeCalled();

$transactionManager = new TransactionManager($eventStoreMock->reveal());
$transactionManager = new TransactionManager();

$transactionManager->setUp($eventStoreMock->reveal());

//Now the command is set as currentCommand internally and later used when new stream is going to be created
$transactionManager->onInitialize($initializeActionEvent->reveal());
Expand Down Expand Up @@ -281,7 +295,9 @@ public function it_returns_early_on_event_store_create_stream_if_event_has_no_st

$eventStoreMock = $this->getEventStoreObjectProphecy();

$transactionManager = new TransactionManager($eventStoreMock->reveal());
$transactionManager = new TransactionManager();

$transactionManager->setUp($eventStoreMock->reveal());

$this->assertNull($transactionManager->onEventStoreCreateStream($createStreamActionEvent->reveal()));
}
Expand All @@ -302,7 +318,9 @@ public function it_returns_early_if_command_was_null_when_handling_events()

$eventStoreMock->beginTransaction()->shouldBeCalled();

$transactionManager = new TransactionManager($eventStoreMock->reveal());
$transactionManager = new TransactionManager();

$transactionManager->setUp($eventStoreMock->reveal());

$transactionManager->onInitialize($initializeActionEvent->reveal());

Expand Down