Skip to content

Commit

Permalink
add tests & docs
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Feb 17, 2024
1 parent 4f59f20 commit 3475b9f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
23 changes: 11 additions & 12 deletions docs/pages/projection.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,7 @@ Otherwise the projectionist will not recognize that the projection has changed a
To do this, you can add a version to the `projectorId`:

```php
use Doctrine\DBAL\Connection;
use Patchlevel\EventSourcing\Attribute\Setup;
use Patchlevel\EventSourcing\Attribute\Teardown;
use Patchlevel\EventSourcing\Attribute\Handle;
use Patchlevel\EventSourcing\Attribute\Projector;
use Patchlevel\EventSourcing\EventBus\Message;

#[Projector('profile_1')]
final class ProfileProjector
Expand All @@ -140,16 +135,20 @@ final class ProfileProjector

You can also use the `ProjectorUtil` to build the table/collection name.

## Projector Repository
## From Now

The projector repository is responsible for managing the projectors.
Certain projectors operate exclusively on post-release events, disregarding historical data.
Consider, for instance, the scenario of launching a fresh email service.
Its primary function is to dispatch welcome emails to newly registered users triggered by a `ProfileCreated` event.

```php
use Patchlevel\EventSourcing\Projection\Projector\InMemoryProjectorRepository;
use Patchlevel\EventSourcing\Attribute\Projector;

$projectorRepository = new InMemoryProjectorRepository([
new ProfileProjection($connection)
]);
#[Projector('profile_1', fromNow: true)]
final class WelcomeEmailProjector
{
// ...
}
```

## Projectionist
Expand Down Expand Up @@ -305,7 +304,7 @@ use Patchlevel\EventSourcing\Projection\Projectionist\DefaultProjectionist;
$projectionist = new DefaultProjectionist(
$eventStore,
$projectionStore,
$projectorRepository
[$projector1, $projector2, $projector3]
);
```

Expand Down
37 changes: 37 additions & 0 deletions tests/Unit/Projection/Projectionist/DefaultProjectionistTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,43 @@ public function handle(Message $message): void
self::assertSame([$message1, $message2], $projector->messages);
}

public function testBootingWithFromNow(): void
{
$projectionId = 'test';
$projector = new #[ProjectionAttribute('test', fromNow: true)]
class {
/** @var list<Message> */
public Message|null $message = null;

Check failure on line 326 in tests/Unit/Projection/Projectionist/DefaultProjectionistTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis by Psalm (locked, 8.3, ubuntu-latest)

MismatchingDocblockPropertyType

tests/Unit/Projection/Projectionist/DefaultProjectionistTest.php:326:20: MismatchingDocblockPropertyType: Parameter _home_runner_work_event_sourcing_event_sourcing_tests_Unit_Projection_Projectionist_DefaultProjectionistTest_php_323_10895::$message has wrong type 'list<Patchlevel\EventSourcing\EventBus\Message>|null', should be 'Patchlevel\EventSourcing\EventBus\Message|null' (see https://psalm.dev/264)

#[Subscribe(ProfileVisited::class)]
public function handle(Message $message): void
{
$this->message = $message;

Check failure on line 331 in tests/Unit/Projection/Projectionist/DefaultProjectionistTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis by Psalm (locked, 8.3, ubuntu-latest)

InvalidPropertyAssignmentValue

tests/Unit/Projection/Projectionist/DefaultProjectionistTest.php:331:34: InvalidPropertyAssignmentValue: $this->message with declared type 'list<Patchlevel\EventSourcing\EventBus\Message>|null' cannot be assigned type 'Patchlevel\EventSourcing\EventBus\Message<object>' (see https://psalm.dev/145)
}
};

$projectionStore = new DummyStore([new Projection($projectionId, ProjectionStatus::Booting)]);

$message1 = new Message(new ProfileVisited(ProfileId::fromString('test')));

$streamableStore = $this->prophesize(Store::class);
$streamableStore->load(null, 1, null, true)->willReturn(new ArrayStream([$message1]))->shouldBeCalledOnce();

$projectionist = new DefaultProjectionist(
$streamableStore->reveal(),
$projectionStore,
[$projector],
);

$projectionist->boot();

self::assertEquals([
new Projection($projectionId, ProjectionStatus::Active, 1),
], $projectionStore->savedProjections);

self::assertNull($projector->message);
}

public function testRunning(): void
{
$projectionId = 'test';
Expand Down

0 comments on commit 3475b9f

Please sign in to comment.