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

Add convenience methods to event store #228

Merged
merged 3 commits into from
Dec 10, 2016
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
15 changes: 15 additions & 0 deletions src/EventStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@

use Iterator;
use Prooph\EventStore\Metadata\MetadataMatcher;
use Prooph\EventStore\Projection\Projection;
use Prooph\EventStore\Projection\ProjectionOptions;
use Prooph\EventStore\Projection\Query;
use Prooph\EventStore\Projection\ReadModel;
use Prooph\EventStore\Projection\ReadModelProjection;

interface EventStore
{
Expand All @@ -40,4 +45,14 @@ public function loadReverse(
): Stream;

public function delete(StreamName $streamName): void;

public function createQuery(): Query;

public function createProjection(string $name, ProjectionOptions $options = null): Projection;

public function createReadModelProjection(
string $name,
ReadModel $readModel,
ProjectionOptions $options = null
): ReadModelProjection;
}
42 changes: 42 additions & 0 deletions src/InMemoryEventStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
use Prooph\Common\Event\ActionEventEmitter;
use Prooph\EventStore\Metadata\MetadataMatcher;
use Prooph\EventStore\Metadata\Operator;
use Prooph\EventStore\Projection\InMemoryEventStoreProjection;
use Prooph\EventStore\Projection\InMemoryEventStoreQuery;
use Prooph\EventStore\Projection\InMemoryEventStoreReadModelProjection;
use Prooph\EventStore\Projection\Projection;
use Prooph\EventStore\Projection\ProjectionOptions;
use Prooph\EventStore\Projection\Query;
use Prooph\EventStore\Projection\ReadModel;
use Prooph\EventStore\Projection\ReadModelProjection;

final class InMemoryEventStore extends AbstractTransactionalActionEventEmitterEventStore
{
Expand Down Expand Up @@ -231,6 +239,40 @@ public function __construct(ActionEventEmitter $actionEventEmitter)
});
}

public function createQuery(): Query
{
return new InMemoryEventStoreQuery($this);
}

public function createProjection(string $name, ProjectionOptions $options = null): Projection
{
if (null === $options) {
$options = new ProjectionOptions();
}

return new InMemoryEventStoreProjection(
$this,
$name,
$options->cacheSize(),
$options->persistBlockSize()
);
}

public function createReadModelProjection(string $name, ReadModel $readModel, ProjectionOptions $options = null): ReadModelProjection
{
if (null === $options) {
$options = new ProjectionOptions();
}

return new InMemoryEventStoreReadModelProjection(
$this,
$name,
$readModel,
$options->cacheSize(),
$options->persistBlockSize()
);
}

private function matchesMetadata(MetadataMatcher $metadataMatcher, array $metadata): bool
{
foreach ($metadataMatcher->data() as $match) {
Expand Down
65 changes: 65 additions & 0 deletions src/Projection/ProjectionOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Prooph\EventStore\Projection;

use Prooph\EventStore\Exception\InvalidArgumentException;

class ProjectionOptions
{
/**
* @var int
*/
protected $cacheSize;

/**
* @var int
*/
protected $persistBlockSize;

public function __construct(int $cacheSize = 1000, int $persistBlockSize = 1000)
{
$this->cacheSize = $cacheSize;
$this->persistBlockSize = $persistBlockSize;
}

public static function fromArray(array $data): ProjectionOptions
{
self::validateData($data);

return new self($data['cache_size'], $data['persist_block_size']);
}

public function cacheSize(): int
{
return $this->cacheSize;
}

public function persistBlockSize(): int
{
return $this->persistBlockSize;
}

/**
* @throws InvalidArgumentException
*/
protected static function validateData(array $data): void
{
if (! isset($data['cache_size'])) {
throw new InvalidArgumentException('cache_size option is missing');
}

if (! isset($data['persist_block_size'])) {
throw new InvalidArgumentException('persist_block_size option is missing');
}
}
}
2 changes: 1 addition & 1 deletion tests/Mock/ReadModelMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ public function reset(): void

public function delete(): void
{
$this->storage = [];
$this->storage = null;
}
}
9 changes: 4 additions & 5 deletions tests/Projection/InMemoryEventStoreProjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Prooph\Common\Messaging\Message;
use Prooph\EventStore\Exception\RuntimeException;
use Prooph\EventStore\Exception\StreamNotFound;
use Prooph\EventStore\Projection\InMemoryEventStoreProjection;
use Prooph\EventStore\Stream;
use Prooph\EventStore\StreamName;
use ProophTest\EventStore\Mock\UserCreated;
Expand All @@ -34,7 +33,7 @@ public function it_links_to(): void

$testCase = $this;

$projection = new InMemoryEventStoreProjection($this->eventStore, 'test_projection', 100, 1);
$projection = $this->eventStore->createProjection('test_projection');
$projection
->fromStream('user-123')
->whenAny(
Expand Down Expand Up @@ -66,7 +65,7 @@ public function it_emits_events_and_resets(): void

$testCase = $this;

$projection = new InMemoryEventStoreProjection($this->eventStore, 'test_projection', 1, 1);
$projection = $this->eventStore->createProjection('test_projection');
$projection
->fromStream('user-123')
->when([
Expand Down Expand Up @@ -98,7 +97,7 @@ public function it_emits_events_and_deletes(): void
{
$this->prepareEventStream('user-123');

$projection = new InMemoryEventStoreProjection($this->eventStore, 'test_projection', 100, 1);
$projection = $this->eventStore->createProjection('test_projection');
$projection
->fromStream('user-123')
->when([
Expand Down Expand Up @@ -130,7 +129,7 @@ public function it_throws_exception_on_run_when_nothing_configured(): void
{
$this->expectException(RuntimeException::class);

$projection = new InMemoryEventStoreProjection($this->eventStore, 'test_projection', 100, 1);
$projection = $this->eventStore->createProjection('test_projection');
$projection->run();
}

Expand Down
39 changes: 19 additions & 20 deletions tests/Projection/InMemoryEventStoreQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Prooph\Common\Messaging\Message;
use Prooph\EventStore\Exception\InvalidArgumentException;
use Prooph\EventStore\Exception\RuntimeException;
use Prooph\EventStore\Projection\InMemoryEventStoreQuery;
use Prooph\EventStore\Stream;
use Prooph\EventStore\StreamName;
use ProophTest\EventStore\Mock\UserCreated;
Expand All @@ -32,7 +31,7 @@ public function it_can_query_from_stream_and_reset()
{
$this->prepareEventStream('user-123');

$query = new InMemoryEventStoreQuery($this->eventStore);
$query = $this->eventStore->createQuery();

$query
->init(function (): array {
Expand Down Expand Up @@ -64,7 +63,7 @@ public function it_can_be_stopped_while_processing()
{
$this->prepareEventStream('user-123');

$query = new InMemoryEventStoreQuery($this->eventStore);
$query = $this->eventStore->createQuery();

$query
->init(function (): array {
Expand Down Expand Up @@ -93,7 +92,7 @@ public function it_can_query_from_streams(): void
$this->prepareEventStream('user-123');
$this->prepareEventStream('user-234');

$query = new InMemoryEventStoreQuery($this->eventStore);
$query = $this->eventStore->createQuery();

$query
->init(function (): array {
Expand Down Expand Up @@ -123,7 +122,7 @@ public function it_can_query_from_all_ignoring_internal_streams(): void

$testCase = $this;

$query = new InMemoryEventStoreQuery($this->eventStore);
$query = $this->eventStore->createQuery();

$query
->init(function (): array {
Expand Down Expand Up @@ -155,7 +154,7 @@ public function it_can_query_from_category_with_when_all()
$this->prepareEventStream('user-123');
$this->prepareEventStream('user-234');

$query = new InMemoryEventStoreQuery($this->eventStore);
$query = $this->eventStore->createQuery();

$query
->init(function (): array {
Expand Down Expand Up @@ -184,7 +183,7 @@ public function it_can_query_from_categories_with_when()
$this->prepareEventStream('guest-345');
$this->prepareEventStream('guest-456');

$query = new InMemoryEventStoreQuery($this->eventStore);
$query = $this->eventStore->createQuery();

$query
->init(function (): array {
Expand All @@ -207,7 +206,7 @@ public function it_resumes_query_from_position(): void
{
$this->prepareEventStream('user-123');

$query = new InMemoryEventStoreQuery($this->eventStore);
$query = $this->eventStore->createQuery();

$query
->init(function (): array {
Expand Down Expand Up @@ -244,7 +243,7 @@ public function it_resumes_query_from_position(): void
*/
public function it_resets_to_empty_array(): void
{
$query = new InMemoryEventStoreQuery($this->eventStore);
$query = $this->eventStore->createQuery();

$state = $query->getState();

Expand All @@ -264,7 +263,7 @@ public function it_throws_exception_when_init_callback_provided_twice(): void
{
$this->expectException(RuntimeException::class);

$query = new InMemoryEventStoreQuery($this->eventStore);
$query = $this->eventStore->createQuery();

$query->init(function (): array {
return [];
Expand All @@ -281,7 +280,7 @@ public function it_throws_exception_when_from_called_twice(): void
{
$this->expectException(RuntimeException::class);

$query = new InMemoryEventStoreQuery($this->eventStore);
$query = $this->eventStore->createQuery();

$query->fromStream('foo');
$query->fromStream('bar');
Expand All @@ -294,7 +293,7 @@ public function it_throws_exception_when_from_called_twice_2(): void
{
$this->expectException(RuntimeException::class);

$query = new InMemoryEventStoreQuery($this->eventStore);
$query = $this->eventStore->createQuery();

$query->fromStreams('foo');
$query->fromCategory('bar');
Expand All @@ -307,7 +306,7 @@ public function it_throws_exception_when_from_called_twice_3(): void
{
$this->expectException(RuntimeException::class);

$query = new InMemoryEventStoreQuery($this->eventStore);
$query = $this->eventStore->createQuery();

$query->fromCategory('foo');
$query->fromStreams('bar');
Expand All @@ -320,7 +319,7 @@ public function it_throws_exception_when_from_called_twice_4(): void
{
$this->expectException(RuntimeException::class);

$query = new InMemoryEventStoreQuery($this->eventStore);
$query = $this->eventStore->createQuery();

$query->fromCategories('foo');
$query->fromCategories('bar');
Expand All @@ -333,7 +332,7 @@ public function it_throws_exception_when_from_called_twice_5(): void
{
$this->expectException(RuntimeException::class);

$query = new InMemoryEventStoreQuery($this->eventStore);
$query = $this->eventStore->createQuery();

$query->fromCategories('foo');
$query->fromAll('bar');
Expand All @@ -346,7 +345,7 @@ public function it_throws_exception_when_when_called_twice_(): void
{
$this->expectException(RuntimeException::class);

$query = new InMemoryEventStoreQuery($this->eventStore);
$query = $this->eventStore->createQuery();

$query->when(['foo' => function (): void {
}]);
Expand All @@ -361,7 +360,7 @@ public function it_throws_exception_when_invalid_handlers_configured(): void
{
$this->expectException(InvalidArgumentException::class);

$query = new InMemoryEventStoreQuery($this->eventStore);
$query = $this->eventStore->createQuery();

$query->when(['1' => function (): void {
}]);
Expand All @@ -374,7 +373,7 @@ public function it_throws_exception_when_invalid_handlers_configured_2(): void
{
$this->expectException(InvalidArgumentException::class);

$query = new InMemoryEventStoreQuery($this->eventStore);
$query = $this->eventStore->createQuery();

$query->when(['foo' => 'invalid']);
}
Expand All @@ -386,7 +385,7 @@ public function it_throws_exception_when_whenAny_called_twice(): void
{
$this->expectException(RuntimeException::class);

$query = new InMemoryEventStoreQuery($this->eventStore);
$query = $this->eventStore->createQuery();

$query->whenAny(function (): void {
});
Expand All @@ -401,7 +400,7 @@ public function it_throws_exception_on_run_when_nothing_configured(): void
{
$this->expectException(RuntimeException::class);

$query = new InMemoryEventStoreQuery($this->eventStore);
$query = $this->eventStore->createQuery();
$query->run();
}

Expand Down
Loading