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

reset/stop/delete projections #258

Merged
merged 5 commits into from
Feb 12, 2017
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 @@ -35,7 +35,7 @@
"phpspec/prophecy": "dev-patch-1 as 1.6.2",
"prooph/php-cs-fixer-config": "^0.1.1",
"satooshi/php-coveralls": "^1.0",
"zendframework/zend-servicemanager": "~2.6",
"zendframework/zend-servicemanager": "^3.1",
"tobiju/bookdown-bootswatch-templates": "^1.0",
"malukenho/docheader": "^0.1.4"
},
Expand Down
4 changes: 2 additions & 2 deletions examples/event/QuickStartSucceeded.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
/**
* This file is part of the prooph/event-store.
* (c) 2014-2016 prooph software GmbH <[email protected]>
* (c) 2015-2016 Sascha-Oliver Prolic <[email protected]>
* (c) 2014-2017 prooph software GmbH <[email protected]>
* (c) 2015-2017 Sascha-Oliver Prolic <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand Down
7 changes: 5 additions & 2 deletions examples/quickstart.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
/**
* This file is part of the prooph/event-store.
* (c) 2014-2016 prooph software GmbH <[email protected]>
* (c) 2015-2016 Sascha-Oliver Prolic <[email protected]>
* (c) 2014-2017 prooph software GmbH <[email protected]>
* (c) 2015-2017 Sascha-Oliver Prolic <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand Down Expand Up @@ -44,6 +44,9 @@
TransactionalActionEventEmitterEventStore::EVENT_HAS_STREAM,
TransactionalActionEventEmitterEventStore::EVENT_FETCH_STREAM_METADATA,
TransactionalActionEventEmitterEventStore::EVENT_UPDATE_STREAM_METADATA,
TransactionalActionEventEmitterEventStore::EVENT_DELETE_PROJECTION,
TransactionalActionEventEmitterEventStore::EVENT_RESET_PROJECTION,
TransactionalActionEventEmitterEventStore::EVENT_STOP_PROJECTION,
TransactionalActionEventEmitterEventStore::EVENT_BEGIN_TRANSACTION,
TransactionalActionEventEmitterEventStore::EVENT_COMMIT,
TransactionalActionEventEmitterEventStore::EVENT_ROLLBACK,
Expand Down
58 changes: 58 additions & 0 deletions src/ActionEventEmitterEventStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class ActionEventEmitterEventStore implements EventStoreDecorator
public const EVENT_HAS_STREAM = 'hasStream';
public const EVENT_FETCH_STREAM_METADATA = 'fetchStreamMetadata';
public const EVENT_UPDATE_STREAM_METADATA = 'updateStreamMetadata';
public const EVENT_DELETE_PROJECTION = 'deleteProjection';
public const EVENT_RESET_PROJECTION = 'resetProjection';
public const EVENT_STOP_PROJECTION = 'stopProjection';

/**
* @var ActionEventEmitter
Expand Down Expand Up @@ -144,6 +147,25 @@ public function __construct(EventStore $eventStore, ActionEventEmitter $actionEv
$event->setParam('streamNotFound', true);
}
});

$actionEventEmitter->attachListener(self::EVENT_DELETE_PROJECTION, function (ActionEvent $event): void {
$name = $event->getParam('name');
$deleteEmittedEvents = $event->getParam('deleteEmittedEvents');

$this->eventStore->deleteProjection($name, $deleteEmittedEvents);
});

$actionEventEmitter->attachListener(self::EVENT_RESET_PROJECTION, function (ActionEvent $event): void {
$name = $event->getParam('name');

$this->eventStore->resetProjection($name);
});

$actionEventEmitter->attachListener(self::EVENT_STOP_PROJECTION, function (ActionEvent $event): void {
$name = $event->getParam('name');

$this->eventStore->stopProjection($name);
});
}

public function create(Stream $stream): void
Expand Down Expand Up @@ -356,6 +378,42 @@ public function getDefaultReadModelProjectionFactory(): ReadModelProjectionFacto
return $this->eventStore->getDefaultReadModelProjectionFactory();
}

public function deleteProjection(string $name, bool $deleteEmittedEvents): void
{
$event = $this->actionEventEmitter->getNewActionEvent(
self::EVENT_DELETE_PROJECTION,
$this,
[
'name' => $name,
'deleteEmittedEvents' => $deleteEmittedEvents,
]
);

$this->actionEventEmitter->dispatch($event);
}

public function resetProjection(string $name): void
{
$event = $this->actionEventEmitter->getNewActionEvent(
self::EVENT_RESET_PROJECTION,
$this,
['name' => $name]
);

$this->actionEventEmitter->dispatch($event);
}

public function stopProjection(string $name): void
{
$event = $this->actionEventEmitter->getNewActionEvent(
self::EVENT_STOP_PROJECTION,
$this,
['name' => $name]
);

$this->actionEventEmitter->dispatch($event);
}

public function attach(string $eventName, callable $listener, int $priority = 0): ListenerHandler
{
return $this->actionEventEmitter->attachListener($eventName, $listener, $priority);
Expand Down
3 changes: 3 additions & 0 deletions src/Container/InMemoryEventStoreFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ public function __invoke(ContainerInterface $container): TransactionalEventStore
TransactionalActionEventEmitterEventStore::EVENT_HAS_STREAM,
TransactionalActionEventEmitterEventStore::EVENT_FETCH_STREAM_METADATA,
TransactionalActionEventEmitterEventStore::EVENT_UPDATE_STREAM_METADATA,
TransactionalActionEventEmitterEventStore::EVENT_DELETE_PROJECTION,
TransactionalActionEventEmitterEventStore::EVENT_RESET_PROJECTION,
TransactionalActionEventEmitterEventStore::EVENT_STOP_PROJECTION,
TransactionalActionEventEmitterEventStore::EVENT_BEGIN_TRANSACTION,
TransactionalActionEventEmitterEventStore::EVENT_COMMIT,
TransactionalActionEventEmitterEventStore::EVENT_ROLLBACK,
Expand Down
6 changes: 6 additions & 0 deletions src/EventStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,10 @@ public function getDefaultQueryFactory(): QueryFactory;
public function getDefaultProjectionFactory(): ProjectionFactory;

public function getDefaultReadModelProjectionFactory(): ReadModelProjectionFactory;

public function deleteProjection(string $name, bool $deleteEmittedEvents): void;

public function resetProjection(string $name): void;

public function stopProjection(string $name): void;
}
15 changes: 15 additions & 0 deletions src/InMemoryEventStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,21 @@ public function getDefaultReadModelProjectionFactory(): ReadModelProjectionFacto
return $this->defaultReadModelProjectionFactory;
}

public function deleteProjection(string $name, bool $deleteEmittedEvents): void
{
throw new Exception\RuntimeException('Deleting a projection is not supported in ' . get_class($this));
}

public function resetProjection(string $name): void
{
throw new Exception\RuntimeException('Resetting a projection is not supported in ' . get_class($this));
}

public function stopProjection(string $name): void
{
throw new Exception\RuntimeException('Stopping a projection is not supported in ' . get_class($this));
}

private function matchesMetadata(MetadataMatcher $metadataMatcher, array $metadata): bool
{
foreach ($metadataMatcher->data() as $match) {
Expand Down
41 changes: 41 additions & 0 deletions tests/ActionEventEmitterEventStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

use ArrayIterator;
use Prooph\Common\Event\ActionEvent;
use Prooph\Common\Event\ProophActionEventEmitter;
use Prooph\EventStore\ActionEventEmitterEventStore;
use Prooph\EventStore\EventStore;
use Prooph\EventStore\Exception\ConcurrencyException;
use Prooph\EventStore\Exception\StreamExistsAlready;
use Prooph\EventStore\Exception\StreamNotFound;
Expand Down Expand Up @@ -522,4 +524,43 @@ public function it_creates_read_model_projection(): void

$this->assertInstanceOf(ReadModelProjection::class, $this->eventStore->createReadModelProjection('foo', $readModel));
}

/**
* @test
*/
public function it_deletes_projections(): void
{
$eventStore = $this->prophesize(EventStore::class);
$eventStore->deleteProjection('foo', true)->shouldBeCalled();

$wrapper = new ActionEventEmitterEventStore($eventStore->reveal(), new ProophActionEventEmitter());

$wrapper->deleteProjection('foo', true);
}

/**
* @test
*/
public function it_resets_projections(): void
{
$eventStore = $this->prophesize(EventStore::class);
$eventStore->resetProjection('foo')->shouldBeCalled();

$wrapper = new ActionEventEmitterEventStore($eventStore->reveal(), new ProophActionEventEmitter());

$wrapper->resetProjection('foo');
}

/**
* @test
*/
public function it_stops_projections(): void
{
$eventStore = $this->prophesize(EventStore::class);
$eventStore->stopProjection('foo')->shouldBeCalled();

$wrapper = new ActionEventEmitterEventStore($eventStore->reveal(), new ProophActionEventEmitter());

$wrapper->stopProjection('foo');
}
}
20 changes: 11 additions & 9 deletions tests/ActionEventEmitterEventStoreTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Prooph\Common\Event\ProophActionEventEmitter;
use Prooph\EventStore\ActionEventEmitterEventStore;
use Prooph\EventStore\InMemoryEventStore;
use Prooph\EventStore\TransactionalActionEventEmitterEventStore;

abstract class ActionEventEmitterEventStoreTestCase extends EventStoreTestCase
{
Expand All @@ -27,14 +26,17 @@ abstract class ActionEventEmitterEventStoreTestCase extends EventStoreTestCase
protected function setUp(): void
{
$eventEmitter = new ProophActionEventEmitter([
TransactionalActionEventEmitterEventStore::EVENT_APPEND_TO,
TransactionalActionEventEmitterEventStore::EVENT_CREATE,
TransactionalActionEventEmitterEventStore::EVENT_LOAD,
TransactionalActionEventEmitterEventStore::EVENT_LOAD_REVERSE,
TransactionalActionEventEmitterEventStore::EVENT_DELETE,
TransactionalActionEventEmitterEventStore::EVENT_HAS_STREAM,
TransactionalActionEventEmitterEventStore::EVENT_FETCH_STREAM_METADATA,
TransactionalActionEventEmitterEventStore::EVENT_UPDATE_STREAM_METADATA,
ActionEventEmitterEventStore::EVENT_APPEND_TO,
ActionEventEmitterEventStore::EVENT_CREATE,
ActionEventEmitterEventStore::EVENT_LOAD,
ActionEventEmitterEventStore::EVENT_LOAD_REVERSE,
ActionEventEmitterEventStore::EVENT_DELETE,
ActionEventEmitterEventStore::EVENT_HAS_STREAM,
ActionEventEmitterEventStore::EVENT_FETCH_STREAM_METADATA,
ActionEventEmitterEventStore::EVENT_UPDATE_STREAM_METADATA,
ActionEventEmitterEventStore::EVENT_DELETE_PROJECTION,
ActionEventEmitterEventStore::EVENT_RESET_PROJECTION,
ActionEventEmitterEventStore::EVENT_STOP_PROJECTION,
]);

$this->eventStore = new ActionEventEmitterEventStore(new InMemoryEventStore(), $eventEmitter);
Expand Down
31 changes: 31 additions & 0 deletions tests/InMemoryEventStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use ArrayIterator;
use Prooph\EventStore\EventStore;
use Prooph\EventStore\Exception\InvalidArgumentException;
use Prooph\EventStore\Exception\RuntimeException;
use Prooph\EventStore\Exception\StreamExistsAlready;
use Prooph\EventStore\Exception\StreamNotFound;
use Prooph\EventStore\Exception\TransactionAlreadyStarted;
Expand Down Expand Up @@ -754,4 +755,34 @@ public function it_wraps_up_code_in_transaction_properly(): void

$this->assertCount(2, $streamEvents);
}

/**
* @test
*/
public function it_cannot_delete_projections(): void
{
$this->expectException(RuntimeException::class);

$this->eventStore->deleteProjection('foo', true);
}

/**
* @test
*/
public function it_cannot_reset_projections(): void
{
$this->expectException(RuntimeException::class);

$this->eventStore->resetProjection('foo');
}

/**
* @test
*/
public function it_cannot_stop_projections(): void
{
$this->expectException(RuntimeException::class);

$this->eventStore->stopProjection('foo');
}
}
5 changes: 2 additions & 3 deletions tests/Plugin/PluginManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use ProophTest\EventStore\ActionEventEmitterEventStoreTestCase;
use ProophTest\EventStore\Mock\EventLoggerPlugin;
use ProophTest\EventStore\Mock\UserCreated;
use Zend\ServiceManager\Config;
use Zend\ServiceManager\ServiceManager;

class PluginManagerTest extends ActionEventEmitterEventStoreTestCase
Expand All @@ -27,11 +26,11 @@ class PluginManagerTest extends ActionEventEmitterEventStoreTestCase
*/
public function an_invokable_plugin_is_loaded_by_plugin_manager_and_attached_to_event_store_by_configuration(): void
{
$pluginManager = new ServiceManager(new Config([
$pluginManager = new ServiceManager([
'invokables' => [
'eventlogger' => EventLoggerPlugin::class,
],
]));
]);

$logger = $pluginManager->get('eventlogger');
$logger->attachToEventStore($this->eventStore);
Expand Down
3 changes: 3 additions & 0 deletions tests/TransactionalActionEventEmitterEventStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ protected function setUp(): void
TransactionalActionEventEmitterEventStore::EVENT_HAS_STREAM,
TransactionalActionEventEmitterEventStore::EVENT_FETCH_STREAM_METADATA,
TransactionalActionEventEmitterEventStore::EVENT_UPDATE_STREAM_METADATA,
TransactionalActionEventEmitterEventStore::EVENT_DELETE_PROJECTION,
TransactionalActionEventEmitterEventStore::EVENT_RESET_PROJECTION,
TransactionalActionEventEmitterEventStore::EVENT_STOP_PROJECTION,
TransactionalActionEventEmitterEventStore::EVENT_BEGIN_TRANSACTION,
TransactionalActionEventEmitterEventStore::EVENT_COMMIT,
TransactionalActionEventEmitterEventStore::EVENT_ROLLBACK,
Expand Down