From 4a7829e60843fc6fe35f43ae0e1bc72452f56578 Mon Sep 17 00:00:00 2001 From: David Badura Date: Sat, 17 Feb 2024 19:47:55 +0100 Subject: [PATCH] add tests & docs --- docs/pages/projection.md | 23 ++++++------ .../DefaultProjectionistTest.php | 36 +++++++++++++++++++ 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/docs/pages/projection.md b/docs/pages/projection.md index 9c89d8bd7..06a6f514b 100644 --- a/docs/pages/projection.md +++ b/docs/pages/projection.md @@ -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 @@ -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 @@ -305,7 +304,7 @@ use Patchlevel\EventSourcing\Projection\Projectionist\DefaultProjectionist; $projectionist = new DefaultProjectionist( $eventStore, $projectionStore, - $projectorRepository + [$projector1, $projector2, $projector3] ); ``` diff --git a/tests/Unit/Projection/Projectionist/DefaultProjectionistTest.php b/tests/Unit/Projection/Projectionist/DefaultProjectionistTest.php index 62753a933..1ef01a780 100644 --- a/tests/Unit/Projection/Projectionist/DefaultProjectionistTest.php +++ b/tests/Unit/Projection/Projectionist/DefaultProjectionistTest.php @@ -317,6 +317,42 @@ 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 { + public Message|null $message = null; + + #[Subscribe(ProfileVisited::class)] + public function handle(Message $message): void + { + $this->message = $message; + } + }; + + $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';