diff --git a/docs/pages/getting_started.md b/docs/pages/getting_started.md index 07ed91b37..86597dc24 100644 --- a/docs/pages/getting_started.md +++ b/docs/pages/getting_started.md @@ -156,15 +156,15 @@ Each projector is then responsible for a specific projection and version. ```php use Doctrine\DBAL\Connection; -use Patchlevel\EventSourcing\Attribute\Create; -use Patchlevel\EventSourcing\Attribute\Drop; +use Patchlevel\EventSourcing\Attribute\Setup; +use Patchlevel\EventSourcing\Attribute\Teardown; use Patchlevel\EventSourcing\Attribute\Subscribe; -use Patchlevel\EventSourcing\Attribute\Projection; +use Patchlevel\EventSourcing\Attribute\Projector; use Patchlevel\EventSourcing\EventBus\Message; use Patchlevel\EventSourcing\Projection\Projection\ProjectionId; use Patchlevel\EventSourcing\Projection\Projector\ProjectorUtil; -#[Projection('hotel')] +#[Projector('hotel')] final class HotelProjector { use ProjectorUtil; @@ -215,13 +215,13 @@ final class HotelProjector ); } - #[Create] + #[Setup] public function create(): void { $this->db->executeStatement("CREATE TABLE IF NOT EXISTS ${this->table()} (id VARCHAR PRIMARY KEY, name VARCHAR, guests INTEGER);"); } - #[Drop] + #[Teardown] public function drop(): void { $this->db->executeStatement("DROP TABLE IF EXISTS ${this->table()};"); diff --git a/docs/pages/projection.md b/docs/pages/projection.md index c71c6c987..290390174 100644 --- a/docs/pages/projection.md +++ b/docs/pages/projection.md @@ -15,14 +15,14 @@ In this example we always create a new data set in a relational database when a ```php use Doctrine\DBAL\Connection; -use Patchlevel\EventSourcing\Attribute\Create; -use Patchlevel\EventSourcing\Attribute\Drop; +use Patchlevel\EventSourcing\Attribute\Setup; +use Patchlevel\EventSourcing\Attribute\Teardown; use Patchlevel\EventSourcing\Attribute\Subscribe; -use Patchlevel\EventSourcing\Attribute\Projection; +use Patchlevel\EventSourcing\Attribute\Projector; use Patchlevel\EventSourcing\EventBus\Message; use Patchlevel\EventSourcing\Projection\Projector\ProjectorUtil; -#[Projection('profile')] +#[Projector('profile')] final class ProfileProjector { use ProjectorUtil; @@ -40,7 +40,7 @@ final class ProfileProjector return $this->connection->fetchAllAssociative("SELECT id, name FROM ${this->table()};"); } - #[Create] + #[Setup] public function create(): void { $this->connection->executeStatement( @@ -48,7 +48,7 @@ final class ProfileProjector ); } - #[Drop] + #[Teardown] public function drop(): void { $this->connection->executeStatement("DROP TABLE IF EXISTS ${this->table()};"); @@ -81,14 +81,14 @@ final class ProfileProjector Each projector is responsible for a specific projection and version. This combination of information results in the so-called `project ID`. -In order for us to be able to define this, we have to use the `Projection` attribute. +In order for us to be able to define this, we have to use the `Projector` attribute. In our example, the projection is called "profile" and has the version "0" because we did not specify it. So that there is no problems with existing projection, both the name of the projection and the version should be part of the table/collection name. In our example, we build a `table` helper method, what creates the following string: "projection_profile_0". -Projectors can have one `create` and `drop` method that is executed when the projection is created or deleted. -For this there are the attributes `Create` and `Drop`. The method name itself doesn't matter. +Projectors can have one `setup` and `teardown` method that is executed when the projection is created or deleted. +For this there are the attributes `Setup` and `Teardown`. The method name itself doesn't matter. In some cases it may be that no schema has to be created for the projection, as the target does it automatically, so you can skip this. @@ -114,17 +114,17 @@ Several projectors can also listen to the same event. As soon as the structure of a projection changes, the version must be change or increment. Otherwise the projectionist will not recognize that the projection has changed and will not rebuild it. -To do this, you have to change the version in the `Projection` attribute. +To do this, you have to change the version in the `Projector` attribute. ```php use Doctrine\DBAL\Connection; -use Patchlevel\EventSourcing\Attribute\Create; -use Patchlevel\EventSourcing\Attribute\Drop; +use Patchlevel\EventSourcing\Attribute\Setup; +use Patchlevel\EventSourcing\Attribute\Teardown; use Patchlevel\EventSourcing\Attribute\Handle; -use Patchlevel\EventSourcing\Attribute\Projection; +use Patchlevel\EventSourcing\Attribute\Projector; use Patchlevel\EventSourcing\EventBus\Message; -#[Projection('profile', version: 1)] +#[Projector('profile', version: 1)] final class ProfileProjector { // ... @@ -169,12 +169,12 @@ If something breaks, the projectionist marks the individual projections as fault ## Projection Id A projection id consists of a unique name and a version. -It can be defined using the `Projection` attribute. +It can be defined using the `Projector` attribute. ```php -use Patchlevel\EventSourcing\Attribute\Projection; +use Patchlevel\EventSourcing\Attribute\Projector; -#[Projection('profile', version: 1)] +#[Projector('profile', version: 1)] final class ProfileProjector { // ... diff --git a/src/Attribute/Projection.php b/src/Attribute/Projector.php similarity index 94% rename from src/Attribute/Projection.php rename to src/Attribute/Projector.php index 647894e19..04c3ca079 100644 --- a/src/Attribute/Projection.php +++ b/src/Attribute/Projector.php @@ -7,7 +7,7 @@ use Attribute; #[Attribute(Attribute::TARGET_CLASS)] -final class Projection +final class Projector { public function __construct( private string $name, diff --git a/src/Attribute/Drop.php b/src/Attribute/Setup.php similarity index 88% rename from src/Attribute/Drop.php rename to src/Attribute/Setup.php index c40d3df07..5bc95a818 100644 --- a/src/Attribute/Drop.php +++ b/src/Attribute/Setup.php @@ -7,6 +7,6 @@ use Attribute; #[Attribute(Attribute::TARGET_METHOD)] -final class Drop +final class Setup { } diff --git a/src/Attribute/Create.php b/src/Attribute/Teardown.php similarity index 86% rename from src/Attribute/Create.php rename to src/Attribute/Teardown.php index 990e3cc0c..b982c4f14 100644 --- a/src/Attribute/Create.php +++ b/src/Attribute/Teardown.php @@ -7,6 +7,6 @@ use Attribute; #[Attribute(Attribute::TARGET_METHOD)] -final class Create +final class Teardown { } diff --git a/src/Metadata/Projector/AttributeProjectorMetadataFactory.php b/src/Metadata/Projector/AttributeProjectorMetadataFactory.php index 358028ce2..01ce448ad 100644 --- a/src/Metadata/Projector/AttributeProjectorMetadataFactory.php +++ b/src/Metadata/Projector/AttributeProjectorMetadataFactory.php @@ -4,10 +4,10 @@ namespace Patchlevel\EventSourcing\Metadata\Projector; -use Patchlevel\EventSourcing\Attribute\Create; -use Patchlevel\EventSourcing\Attribute\Drop; -use Patchlevel\EventSourcing\Attribute\Projection; +use Patchlevel\EventSourcing\Attribute\Projector; +use Patchlevel\EventSourcing\Attribute\Setup; use Patchlevel\EventSourcing\Attribute\Subscribe; +use Patchlevel\EventSourcing\Attribute\Teardown; use ReflectionClass; use function array_key_exists; @@ -26,7 +26,7 @@ public function metadata(string $projector): ProjectorMetadata $reflector = new ReflectionClass($projector); - $attributes = $reflector->getAttributes(Projection::class); + $attributes = $reflector->getAttributes(Projector::class); if ($attributes === []) { throw new ClassIsNotAProjector($projector); @@ -59,9 +59,9 @@ public function metadata(string $projector): ProjectorMetadata $subscribeMethods[$eventClass] = $method->getName(); } - if ($method->getAttributes(Create::class)) { + if ($method->getAttributes(Setup::class)) { if ($createMethod) { - throw new DuplicateCreateMethod( + throw new DuplicateSetupMethod( $projector, $createMethod, $method->getName(), @@ -71,12 +71,12 @@ public function metadata(string $projector): ProjectorMetadata $createMethod = $method->getName(); } - if (!$method->getAttributes(Drop::class)) { + if (!$method->getAttributes(Teardown::class)) { continue; } if ($dropMethod) { - throw new DuplicateDropMethod( + throw new DuplicateTeardownMethod( $projector, $dropMethod, $method->getName(), diff --git a/src/Metadata/Projector/DuplicateDropMethod.php b/src/Metadata/Projector/DuplicateSetupMethod.php similarity index 79% rename from src/Metadata/Projector/DuplicateDropMethod.php rename to src/Metadata/Projector/DuplicateSetupMethod.php index 35bb77717..c279145cb 100644 --- a/src/Metadata/Projector/DuplicateDropMethod.php +++ b/src/Metadata/Projector/DuplicateSetupMethod.php @@ -8,14 +8,14 @@ use function sprintf; -final class DuplicateDropMethod extends MetadataException +final class DuplicateSetupMethod extends MetadataException { /** @param class-string $projector */ public function __construct(string $projector, string $fistMethod, string $secondMethod) { parent::__construct( sprintf( - 'Two methods "%s" and "%s" on the projector "%s" have been marked as "create" methods. Only one method can be defined like this.', + 'Two methods "%s" and "%s" on the projector "%s" have been marked as "setup" methods. Only one method can be defined like this.', $fistMethod, $secondMethod, $projector, diff --git a/src/Metadata/Projector/DuplicateCreateMethod.php b/src/Metadata/Projector/DuplicateTeardownMethod.php similarity index 78% rename from src/Metadata/Projector/DuplicateCreateMethod.php rename to src/Metadata/Projector/DuplicateTeardownMethod.php index 6b9e6faff..808a10ac2 100644 --- a/src/Metadata/Projector/DuplicateCreateMethod.php +++ b/src/Metadata/Projector/DuplicateTeardownMethod.php @@ -8,14 +8,14 @@ use function sprintf; -final class DuplicateCreateMethod extends MetadataException +final class DuplicateTeardownMethod extends MetadataException { /** @param class-string $projector */ public function __construct(string $projector, string $fistMethod, string $secondMethod) { parent::__construct( sprintf( - 'Two methods "%s" and "%s" on the projector "%s" have been marked as "create" methods. Only one method can be defined like this.', + 'Two methods "%s" and "%s" on the projector "%s" have been marked as "teardown" methods. Only one method can be defined like this.', $fistMethod, $secondMethod, $projector, diff --git a/src/Metadata/Projector/ProjectorMetadata.php b/src/Metadata/Projector/ProjectorMetadata.php index 11904cfa3..e337033d9 100644 --- a/src/Metadata/Projector/ProjectorMetadata.php +++ b/src/Metadata/Projector/ProjectorMetadata.php @@ -11,8 +11,8 @@ public function __construct( public readonly int $version, /** @var array */ public readonly array $subscribeMethods = [], - public readonly string|null $createMethod = null, - public readonly string|null $dropMethod = null, + public readonly string|null $setupMethod = null, + public readonly string|null $teardownMethod = null, ) { } } diff --git a/src/Projection/Projectionist/DefaultProjectionist.php b/src/Projection/Projectionist/DefaultProjectionist.php index cadabc914..f4a6eeda6 100644 --- a/src/Projection/Projectionist/DefaultProjectionist.php +++ b/src/Projection/Projectionist/DefaultProjectionist.php @@ -63,11 +63,11 @@ public function boot( $projection->id()->toString(), )); - $createMethod = $this->projectorResolver->resolveCreateMethod($projector); + $setupMethod = $this->projectorResolver->resolveSetupMethod($projector); - if (!$createMethod) { + if (!$setupMethod) { $this->logger?->info(sprintf( - 'projector "%s" for "%s" has no create method', + 'projector "%s" for "%s" has no "setup" method', $projector::class, $projection->id()->toString(), )); @@ -76,7 +76,7 @@ public function boot( } try { - $createMethod(); + $setupMethod(); $this->logger?->info(sprintf( 'projector "%s" for "%s" prepared', $projector::class, @@ -242,11 +242,11 @@ public function teardown(ProjectionCriteria $criteria = new ProjectionCriteria() continue; } - $dropMethod = $this->projectorResolver->resolveDropMethod($projector); + $teardownMethod = $this->projectorResolver->resolveTeardownMethod($projector); - if ($dropMethod) { + if ($teardownMethod) { try { - $dropMethod(); + $teardownMethod(); } catch (Throwable $e) { $this->logger?->error( sprintf('projection for "%s" could not be removed, skipped', $projection->id()->toString()), @@ -281,9 +281,9 @@ public function remove(ProjectionCriteria $criteria = new ProjectionCriteria()): continue; } - $dropMethod = $this->projectorResolver->resolveDropMethod($projector); + $teardownMethod = $this->projectorResolver->resolveTeardownMethod($projector); - if (!$dropMethod) { + if (!$teardownMethod) { $this->projectionStore->remove($projection->id()); $this->logger?->info( @@ -294,11 +294,11 @@ public function remove(ProjectionCriteria $criteria = new ProjectionCriteria()): } try { - $dropMethod(); + $teardownMethod(); } catch (Throwable $e) { $this->logger?->error( sprintf( - 'projector "%s" drop method could not be executed:', + 'projector "%s" teardown method could not be executed:', $projector::class, ), ); diff --git a/src/Projection/Projector/MetadataProjectorResolver.php b/src/Projection/Projector/MetadataProjectorResolver.php index 6a3678fe6..e74ec8ee3 100644 --- a/src/Projection/Projector/MetadataProjectorResolver.php +++ b/src/Projection/Projector/MetadataProjectorResolver.php @@ -19,10 +19,10 @@ public function __construct( ) { } - public function resolveCreateMethod(object $projector): Closure|null + public function resolveSetupMethod(object $projector): Closure|null { $metadata = $this->metadataFactory->metadata($projector::class); - $method = $metadata->createMethod; + $method = $metadata->setupMethod; if (!$method) { return null; @@ -31,10 +31,10 @@ public function resolveCreateMethod(object $projector): Closure|null return $projector->$method(...); } - public function resolveDropMethod(object $projector): Closure|null + public function resolveTeardownMethod(object $projector): Closure|null { $metadata = $this->metadataFactory->metadata($projector::class); - $method = $metadata->dropMethod; + $method = $metadata->teardownMethod; if (!$method) { return null; diff --git a/src/Projection/Projector/ProjectorResolver.php b/src/Projection/Projector/ProjectorResolver.php index 83fea7bd2..5b51beaf9 100644 --- a/src/Projection/Projector/ProjectorResolver.php +++ b/src/Projection/Projector/ProjectorResolver.php @@ -10,9 +10,9 @@ interface ProjectorResolver { - public function resolveCreateMethod(object $projector): Closure|null; + public function resolveSetupMethod(object $projector): Closure|null; - public function resolveDropMethod(object $projector): Closure|null; + public function resolveTeardownMethod(object $projector): Closure|null; public function resolveSubscribeMethod(object $projector, Message $message): Closure|null; diff --git a/tests/Benchmark/BasicImplementation/Projection/ProfileProjector.php b/tests/Benchmark/BasicImplementation/Projection/ProfileProjector.php index 0376c17d7..db1511745 100644 --- a/tests/Benchmark/BasicImplementation/Projection/ProfileProjector.php +++ b/tests/Benchmark/BasicImplementation/Projection/ProfileProjector.php @@ -5,16 +5,16 @@ namespace Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Projection; use Doctrine\DBAL\Connection; -use Patchlevel\EventSourcing\Attribute\Create; -use Patchlevel\EventSourcing\Attribute\Drop; -use Patchlevel\EventSourcing\Attribute\Projection; +use Patchlevel\EventSourcing\Attribute\Projector; +use Patchlevel\EventSourcing\Attribute\Setup; use Patchlevel\EventSourcing\Attribute\Subscribe; +use Patchlevel\EventSourcing\Attribute\Teardown; use Patchlevel\EventSourcing\EventBus\Message; use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Events\ProfileCreated; use function assert; -#[Projection('dummy', 1)] +#[Projector('dummy', 1)] final class ProfileProjector { public function __construct( @@ -22,13 +22,13 @@ public function __construct( ) { } - #[Create] + #[Setup] public function create(): void { $this->connection->executeStatement('CREATE TABLE IF NOT EXISTS projection_profile (id VARCHAR PRIMARY KEY, name VARCHAR);'); } - #[Drop] + #[Teardown] public function drop(): void { $this->connection->executeStatement('DROP TABLE IF EXISTS projection_profile;'); diff --git a/tests/Integration/BankAccountSplitStream/IntegrationTest.php b/tests/Integration/BankAccountSplitStream/IntegrationTest.php index d22ec17b7..9d97ddc42 100644 --- a/tests/Integration/BankAccountSplitStream/IntegrationTest.php +++ b/tests/Integration/BankAccountSplitStream/IntegrationTest.php @@ -23,7 +23,7 @@ use Patchlevel\EventSourcing\Tests\Integration\BankAccountSplitStream\Events\BalanceAdded; use Patchlevel\EventSourcing\Tests\Integration\BankAccountSplitStream\Events\BankAccountCreated; use Patchlevel\EventSourcing\Tests\Integration\BankAccountSplitStream\Events\MonthPassed; -use Patchlevel\EventSourcing\Tests\Integration\BankAccountSplitStream\Projection\BankAccountProjection; +use Patchlevel\EventSourcing\Tests\Integration\BankAccountSplitStream\Projection\BankAccountProjector; use Patchlevel\EventSourcing\Tests\Integration\DbalManager; use PHPUnit\Framework\TestCase; use Symfony\Component\Lock\LockFactory; @@ -55,8 +55,8 @@ public function testSuccessful(): void 'eventstore', ); - $bankAccountProjection = new BankAccountProjection($this->connection); - $projectionRepository = new InMemoryProjectorRepository([$bankAccountProjection]); + $bankAccountProjector = new BankAccountProjector($this->connection); + $projectionRepository = new InMemoryProjectorRepository([$bankAccountProjector]); $projectionist = new DefaultProjectionist( $store, diff --git a/tests/Integration/BankAccountSplitStream/Projection/BankAccountProjection.php b/tests/Integration/BankAccountSplitStream/Projection/BankAccountProjector.php similarity index 84% rename from tests/Integration/BankAccountSplitStream/Projection/BankAccountProjection.php rename to tests/Integration/BankAccountSplitStream/Projection/BankAccountProjector.php index 50de2991c..76d435f2c 100644 --- a/tests/Integration/BankAccountSplitStream/Projection/BankAccountProjection.php +++ b/tests/Integration/BankAccountSplitStream/Projection/BankAccountProjector.php @@ -6,23 +6,25 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\Schema\Table; -use Patchlevel\EventSourcing\Attribute\Create; -use Patchlevel\EventSourcing\Attribute\Drop; -use Patchlevel\EventSourcing\Attribute\Projection; +use Patchlevel\EventSourcing\Attribute\Projector; +use Patchlevel\EventSourcing\Attribute\Setup; use Patchlevel\EventSourcing\Attribute\Subscribe; +use Patchlevel\EventSourcing\Attribute\Teardown; use Patchlevel\EventSourcing\EventBus\Message; use Patchlevel\EventSourcing\Tests\Integration\BankAccountSplitStream\Events\BalanceAdded; use Patchlevel\EventSourcing\Tests\Integration\BankAccountSplitStream\Events\BankAccountCreated; -#[Projection('dummy', 1)] -final class BankAccountProjection +use function assert; + +#[Projector('dummy', 1)] +final class BankAccountProjector { public function __construct( private Connection $connection, ) { } - #[Create] + #[Setup] public function create(): void { $table = new Table('projection_bank_account'); @@ -34,7 +36,7 @@ public function create(): void $this->connection->createSchemaManager()->createTable($table); } - #[Drop] + #[Teardown] public function drop(): void { $this->connection->createSchemaManager()->dropTable('projection_bank_account'); @@ -45,6 +47,8 @@ public function handleBankAccountCreated(Message $message): void { $event = $message->event(); + assert($event instanceof BankAccountCreated); + $this->connection->executeStatement( 'INSERT INTO projection_bank_account (id, name, balance_in_cents) VALUES(:id, :name, 0);', [ @@ -59,6 +63,8 @@ public function handleBalanceAdded(Message $message): void { $event = $message->event(); + assert($event instanceof BalanceAdded); + $this->connection->executeStatement( 'UPDATE projection_bank_account SET balance_in_cents = balance_in_cents + :balance WHERE id = :id;', [ diff --git a/tests/Integration/BasicImplementation/BasicIntegrationTest.php b/tests/Integration/BasicImplementation/BasicIntegrationTest.php index d55cd17ce..0073e53f1 100644 --- a/tests/Integration/BasicImplementation/BasicIntegrationTest.php +++ b/tests/Integration/BasicImplementation/BasicIntegrationTest.php @@ -22,7 +22,7 @@ use Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\Aggregate\Profile; use Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\MessageDecorator\FooMessageDecorator; use Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\Processor\SendEmailProcessor; -use Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\Projection\ProfileProjection; +use Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\Projection\ProfileProjector; use Patchlevel\EventSourcing\Tests\Integration\DbalManager; use PHPUnit\Framework\TestCase; use Symfony\Component\Lock\LockFactory; @@ -53,9 +53,9 @@ public function testSuccessful(): void 'eventstore', ); - $profileProjection = new ProfileProjection($this->connection); + $profileProjector = new ProfileProjector($this->connection); $projectorRepository = new InMemoryProjectorRepository( - [$profileProjection], + [$profileProjector], ); $projectionist = new DefaultProjectionist( @@ -127,7 +127,7 @@ public function testWithSymfonySuccessful(): void 'eventstore', ); - $profileProjection = new ProfileProjection($this->connection); + $profileProjection = new ProfileProjector($this->connection); $projectorRepository = new InMemoryProjectorRepository( [$profileProjection], ); @@ -204,7 +204,7 @@ public function testSnapshot(): void 'eventstore', ); - $profileProjection = new ProfileProjection($this->connection); + $profileProjection = new ProfileProjector($this->connection); $projectorRepository = new InMemoryProjectorRepository( [$profileProjection], ); diff --git a/tests/Integration/BasicImplementation/Projection/ProfileProjection.php b/tests/Integration/BasicImplementation/Projection/ProfileProjector.php similarity index 85% rename from tests/Integration/BasicImplementation/Projection/ProfileProjection.php rename to tests/Integration/BasicImplementation/Projection/ProfileProjector.php index 927c8271a..ce93fc774 100644 --- a/tests/Integration/BasicImplementation/Projection/ProfileProjection.php +++ b/tests/Integration/BasicImplementation/Projection/ProfileProjector.php @@ -6,24 +6,24 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\Schema\Table; -use Patchlevel\EventSourcing\Attribute\Create; -use Patchlevel\EventSourcing\Attribute\Drop; -use Patchlevel\EventSourcing\Attribute\Projection; +use Patchlevel\EventSourcing\Attribute\Projector; +use Patchlevel\EventSourcing\Attribute\Setup; use Patchlevel\EventSourcing\Attribute\Subscribe; +use Patchlevel\EventSourcing\Attribute\Teardown; use Patchlevel\EventSourcing\EventBus\Message; use Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\Events\ProfileCreated; use function assert; -#[Projection('profile', 1)] -final class ProfileProjection +#[Projector('profile', 1)] +final class ProfileProjector { public function __construct( private Connection $connection, ) { } - #[Create] + #[Setup] public function create(): void { $table = new Table('projection_profile'); @@ -34,7 +34,7 @@ public function create(): void $this->connection->createSchemaManager()->createTable($table); } - #[Drop] + #[Teardown] public function drop(): void { $this->connection->createSchemaManager()->dropTable('projection_profile'); diff --git a/tests/Integration/Outbox/OutboxTest.php b/tests/Integration/Outbox/OutboxTest.php index 44ac1d23e..466ba7f02 100644 --- a/tests/Integration/Outbox/OutboxTest.php +++ b/tests/Integration/Outbox/OutboxTest.php @@ -23,7 +23,7 @@ use Patchlevel\EventSourcing\Tests\Integration\Outbox\Aggregate\Profile; use Patchlevel\EventSourcing\Tests\Integration\Outbox\Events\ProfileCreated; use Patchlevel\EventSourcing\Tests\Integration\Outbox\Processor\SendEmailProcessor; -use Patchlevel\EventSourcing\Tests\Integration\Outbox\Projection\ProfileProjection; +use Patchlevel\EventSourcing\Tests\Integration\Outbox\Projection\ProfileProjector; use PHPUnit\Framework\TestCase; use Symfony\Component\Lock\LockFactory; use Symfony\Component\Lock\Store\InMemoryStore as LockInMemoryStore; @@ -68,9 +68,9 @@ public function testSuccessful(): void $outboxEventBus = new OutboxEventBus($outboxStore); - $profileProjection = new ProfileProjection($this->connection); + $profileProjector = new ProfileProjector($this->connection); $projectorRepository = new InMemoryProjectorRepository( - [$profileProjection], + [$profileProjector], ); $projectionist = new DefaultProjectionist( diff --git a/tests/Integration/Outbox/Projection/ProfileProjection.php b/tests/Integration/Outbox/Projection/ProfileProjector.php similarity index 80% rename from tests/Integration/Outbox/Projection/ProfileProjection.php rename to tests/Integration/Outbox/Projection/ProfileProjector.php index e4535208b..6d8d2f55d 100644 --- a/tests/Integration/Outbox/Projection/ProfileProjection.php +++ b/tests/Integration/Outbox/Projection/ProfileProjector.php @@ -6,22 +6,24 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\Schema\Table; -use Patchlevel\EventSourcing\Attribute\Create; -use Patchlevel\EventSourcing\Attribute\Drop; -use Patchlevel\EventSourcing\Attribute\Projection; +use Patchlevel\EventSourcing\Attribute\Projector; +use Patchlevel\EventSourcing\Attribute\Setup; use Patchlevel\EventSourcing\Attribute\Subscribe; +use Patchlevel\EventSourcing\Attribute\Teardown; use Patchlevel\EventSourcing\EventBus\Message; use Patchlevel\EventSourcing\Tests\Integration\Outbox\Events\ProfileCreated; -#[Projection('dummy', 1)] -final class ProfileProjection +use function assert; + +#[Projector('dummy', 1)] +final class ProfileProjector { public function __construct( private Connection $connection, ) { } - #[Create] + #[Setup] public function create(): void { $table = new Table('projection_profile'); @@ -32,7 +34,7 @@ public function create(): void $this->connection->createSchemaManager()->createTable($table); } - #[Drop] + #[Teardown] public function drop(): void { $this->connection->createSchemaManager()->dropTable('projection_profile'); @@ -43,6 +45,8 @@ public function handleProfileCreated(Message $message): void { $profileCreated = $message->event(); + assert($profileCreated instanceof ProfileCreated); + $this->connection->executeStatement( 'INSERT INTO projection_profile (id, name) VALUES(:id, :name);', [ diff --git a/tests/Integration/Projectionist/Projection/ProfileProjection.php b/tests/Integration/Projectionist/Projection/ProfileProjector.php similarity index 88% rename from tests/Integration/Projectionist/Projection/ProfileProjection.php rename to tests/Integration/Projectionist/Projection/ProfileProjector.php index 98a75b1d8..1fdb4d357 100644 --- a/tests/Integration/Projectionist/Projection/ProfileProjection.php +++ b/tests/Integration/Projectionist/Projection/ProfileProjector.php @@ -6,10 +6,10 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\Schema\Table; -use Patchlevel\EventSourcing\Attribute\Create; -use Patchlevel\EventSourcing\Attribute\Drop; -use Patchlevel\EventSourcing\Attribute\Projection; +use Patchlevel\EventSourcing\Attribute\Projector; +use Patchlevel\EventSourcing\Attribute\Setup; use Patchlevel\EventSourcing\Attribute\Subscribe; +use Patchlevel\EventSourcing\Attribute\Teardown; use Patchlevel\EventSourcing\EventBus\Message; use Patchlevel\EventSourcing\Projection\Projector\ProjectorUtil; use Patchlevel\EventSourcing\Tests\Integration\Projectionist\Events\ProfileCreated; @@ -17,8 +17,8 @@ use function assert; use function sprintf; -#[Projection('profile', 1)] -final class ProfileProjection +#[Projector('profile', 1)] +final class ProfileProjector { use ProjectorUtil; @@ -27,7 +27,7 @@ public function __construct( ) { } - #[Create] + #[Setup] public function create(): void { $table = new Table($this->tableName()); @@ -38,7 +38,7 @@ public function create(): void $this->connection->createSchemaManager()->createTable($table); } - #[Drop] + #[Teardown] public function drop(): void { $this->connection->createSchemaManager()->dropTable($this->tableName()); diff --git a/tests/Integration/Projectionist/ProjectionistTest.php b/tests/Integration/Projectionist/ProjectionistTest.php index 578efd4f3..a1f84dec3 100644 --- a/tests/Integration/Projectionist/ProjectionistTest.php +++ b/tests/Integration/Projectionist/ProjectionistTest.php @@ -20,7 +20,7 @@ use Patchlevel\EventSourcing\Store\DoctrineDbalStore; use Patchlevel\EventSourcing\Tests\Integration\DbalManager; use Patchlevel\EventSourcing\Tests\Integration\Projectionist\Aggregate\Profile; -use Patchlevel\EventSourcing\Tests\Integration\Projectionist\Projection\ProfileProjection; +use Patchlevel\EventSourcing\Tests\Integration\Projectionist\Projection\ProfileProjector; use PHPUnit\Framework\TestCase; use Symfony\Component\Lock\LockFactory; use Symfony\Component\Lock\Store\DoctrineDbalStore as LockStore; @@ -76,7 +76,7 @@ public function testAsync(): void $store, $projectionStore, new InMemoryProjectorRepository( - [new ProfileProjection($this->connection)], + [new ProfileProjector($this->connection)], ), ); @@ -111,7 +111,7 @@ public function testSync(): void $store, $projectionStore, new InMemoryProjectorRepository( - [new ProfileProjection($this->connection)], + [new ProfileProjector($this->connection)], ), ); diff --git a/tests/Unit/Fixture/DummyProjection.php b/tests/Unit/Fixture/Dummy2Projector.php similarity index 62% rename from tests/Unit/Fixture/DummyProjection.php rename to tests/Unit/Fixture/Dummy2Projector.php index 2d28f590b..be3355d71 100644 --- a/tests/Unit/Fixture/DummyProjection.php +++ b/tests/Unit/Fixture/Dummy2Projector.php @@ -4,38 +4,32 @@ namespace Patchlevel\EventSourcing\Tests\Unit\Fixture; -use Patchlevel\EventSourcing\Attribute\Create; -use Patchlevel\EventSourcing\Attribute\Drop; -use Patchlevel\EventSourcing\Attribute\Projection; +use Patchlevel\EventSourcing\Attribute\Projector; +use Patchlevel\EventSourcing\Attribute\Setup; use Patchlevel\EventSourcing\Attribute\Subscribe; +use Patchlevel\EventSourcing\Attribute\Teardown; use Patchlevel\EventSourcing\EventBus\Message as EventMessage; -use Patchlevel\EventSourcing\Projection\Projection\ProjectionId; -#[Projection('dummy', 1)] -final class DummyProjection +#[Projector('dummy2', 1)] +final class Dummy2Projector { public EventMessage|null $handledMessage = null; public bool $createCalled = false; public bool $dropCalled = false; - public function targetProjection(): ProjectionId - { - return new ProjectionId('dummy', 1); - } - #[Subscribe(ProfileCreated::class)] public function handleProfileCreated(EventMessage $message): void { $this->handledMessage = $message; } - #[Create] + #[Setup] public function create(): void { $this->createCalled = true; } - #[Drop] + #[Teardown] public function drop(): void { $this->dropCalled = true; diff --git a/tests/Unit/Fixture/Dummy2Projection.php b/tests/Unit/Fixture/DummyProjector.php similarity index 62% rename from tests/Unit/Fixture/Dummy2Projection.php rename to tests/Unit/Fixture/DummyProjector.php index aef8dbb33..0a4ba0aa9 100644 --- a/tests/Unit/Fixture/Dummy2Projection.php +++ b/tests/Unit/Fixture/DummyProjector.php @@ -4,38 +4,32 @@ namespace Patchlevel\EventSourcing\Tests\Unit\Fixture; -use Patchlevel\EventSourcing\Attribute\Create; -use Patchlevel\EventSourcing\Attribute\Drop; -use Patchlevel\EventSourcing\Attribute\Projection; +use Patchlevel\EventSourcing\Attribute\Projector; +use Patchlevel\EventSourcing\Attribute\Setup; use Patchlevel\EventSourcing\Attribute\Subscribe; +use Patchlevel\EventSourcing\Attribute\Teardown; use Patchlevel\EventSourcing\EventBus\Message as EventMessage; -use Patchlevel\EventSourcing\Projection\Projection\ProjectionId; -#[Projection('dummy2', 1)] -final class Dummy2Projection +#[Projector('dummy', 1)] +final class DummyProjector { public EventMessage|null $handledMessage = null; public bool $createCalled = false; public bool $dropCalled = false; - public function targetProjection(): ProjectionId - { - return new ProjectionId('dummy2', 1); - } - #[Subscribe(ProfileCreated::class)] public function handleProfileCreated(EventMessage $message): void { $this->handledMessage = $message; } - #[Create] + #[Setup] public function create(): void { $this->createCalled = true; } - #[Drop] + #[Teardown] public function drop(): void { $this->dropCalled = true; diff --git a/tests/Unit/Metadata/Projector/AttributeProjectorMetadataFactoryTest.php b/tests/Unit/Metadata/Projector/AttributeProjectorMetadataFactoryTest.php index ff02a17d3..2a5e4895d 100644 --- a/tests/Unit/Metadata/Projector/AttributeProjectorMetadataFactoryTest.php +++ b/tests/Unit/Metadata/Projector/AttributeProjectorMetadataFactoryTest.php @@ -4,14 +4,14 @@ namespace Patchlevel\EventSourcing\Tests\Unit\Metadata\Projector; -use Patchlevel\EventSourcing\Attribute\Create; -use Patchlevel\EventSourcing\Attribute\Drop; -use Patchlevel\EventSourcing\Attribute\Projection; +use Patchlevel\EventSourcing\Attribute\Projector; +use Patchlevel\EventSourcing\Attribute\Setup; use Patchlevel\EventSourcing\Attribute\Subscribe; +use Patchlevel\EventSourcing\Attribute\Teardown; use Patchlevel\EventSourcing\Metadata\Projector\AttributeProjectorMetadataFactory; use Patchlevel\EventSourcing\Metadata\Projector\ClassIsNotAProjector; -use Patchlevel\EventSourcing\Metadata\Projector\DuplicateCreateMethod; -use Patchlevel\EventSourcing\Metadata\Projector\DuplicateDropMethod; +use Patchlevel\EventSourcing\Metadata\Projector\DuplicateSetupMethod; +use Patchlevel\EventSourcing\Metadata\Projector\DuplicateTeardownMethod; use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated; use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileVisited; use PHPUnit\Framework\TestCase; @@ -31,7 +31,7 @@ public function testNotAProjection(): void public function testEmptyProjection(): void { - $projection = new #[Projection('foo', 1)] + $projection = new #[Projector('foo', 1)] class { }; @@ -39,27 +39,27 @@ class { $metadata = $metadataFactory->metadata($projection::class); self::assertSame([], $metadata->subscribeMethods); - self::assertNull($metadata->createMethod); - self::assertNull($metadata->dropMethod); + self::assertNull($metadata->setupMethod); + self::assertNull($metadata->teardownMethod); self::assertSame('foo', $metadata->name); self::assertSame(1, $metadata->version); } public function testStandardProjection(): void { - $projection = new #[Projection('foo', 1)] + $projection = new #[Projector('foo', 1)] class { #[Subscribe(ProfileVisited::class)] public function handle(): void { } - #[Create] + #[Setup] public function create(): void { } - #[Drop] + #[Teardown] public function drop(): void { } @@ -73,13 +73,13 @@ public function drop(): void $metadata->subscribeMethods, ); - self::assertSame('create', $metadata->createMethod); - self::assertSame('drop', $metadata->dropMethod); + self::assertSame('create', $metadata->setupMethod); + self::assertSame('drop', $metadata->teardownMethod); } public function testMultipleHandlerOnOneMethod(): void { - $projection = new #[Projection('foo', 1)] + $projection = new #[Projector('foo', 1)] class { #[Subscribe(ProfileVisited::class)] #[Subscribe(ProfileCreated::class)] @@ -100,18 +100,18 @@ public function handle(): void ); } - public function testDuplicateCreateAttributeException(): void + public function testDuplicateSetupAttributeException(): void { - $this->expectException(DuplicateCreateMethod::class); + $this->expectException(DuplicateSetupMethod::class); - $projection = new #[Projection('foo', 1)] + $projection = new #[Projector('foo', 1)] class { - #[Create] + #[Setup] public function create1(): void { } - #[Create] + #[Setup] public function create2(): void { } @@ -121,18 +121,18 @@ public function create2(): void $metadataFactory->metadata($projection::class); } - public function testDuplicateDropAttributeException(): void + public function testDuplicateTeardownAttributeException(): void { - $this->expectException(DuplicateDropMethod::class); + $this->expectException(DuplicateTeardownMethod::class); - $projection = new #[Projection('foo', 1)] + $projection = new #[Projector('foo', 1)] class { - #[Drop] + #[Teardown] public function drop1(): void { } - #[Drop] + #[Teardown] public function drop2(): void { } diff --git a/tests/Unit/Projection/Projectionist/DefaultProjectionistTest.php b/tests/Unit/Projection/Projectionist/DefaultProjectionistTest.php index c4497ea93..cd8b5998f 100644 --- a/tests/Unit/Projection/Projectionist/DefaultProjectionistTest.php +++ b/tests/Unit/Projection/Projectionist/DefaultProjectionistTest.php @@ -4,7 +4,7 @@ namespace Patchlevel\EventSourcing\Tests\Unit\Projection\Projectionist; -use Patchlevel\EventSourcing\Attribute\Projection as ProjectionAttribute; +use Patchlevel\EventSourcing\Attribute\Projector as ProjectionAttribute; use Patchlevel\EventSourcing\EventBus\Message; use Patchlevel\EventSourcing\Projection\Projection\Projection; use Patchlevel\EventSourcing\Projection\Projection\ProjectionCollection; @@ -78,7 +78,7 @@ class { $projectorResolver = $this->prophesize(ProjectorResolver::class); $projectorResolver->projectionId($projector)->willReturn($projectionId); - $projectorResolver->resolveCreateMethod($projector)->willReturn(null); + $projectorResolver->resolveSetupMethod($projector)->willReturn(null); $projectorResolver->resolveSubscribeMethod($projector, $message)->willReturn(null); $projectionist = new DefaultProjectionist( @@ -127,7 +127,7 @@ public function handle(Message $message): void $projectorRepository->projectors()->willReturn([$projector])->shouldBeCalledOnce(); $projectorResolver = $this->prophesize(ProjectorResolver::class); - $projectorResolver->resolveCreateMethod($projector)->willReturn($projector->create(...)); + $projectorResolver->resolveSetupMethod($projector)->willReturn($projector->create(...)); $projectorResolver->resolveSubscribeMethod($projector, $message)->willReturn($projector->handle(...)); $projectorResolver->projectionId($projector)->willReturn($projectionId); @@ -180,7 +180,7 @@ public function handle(Message $message): void $projectorRepository->projectors()->willReturn([$projector])->shouldBeCalledOnce(); $projectorResolver = $this->prophesize(ProjectorResolver::class); - $projectorResolver->resolveCreateMethod($projector)->willReturn($projector->create(...)); + $projectorResolver->resolveSetupMethod($projector)->willReturn($projector->create(...)); $projectorResolver->resolveSubscribeMethod($projector, $message)->willReturn($projector->handle(...)); $projectorResolver->projectionId($projector)->willReturn($projectionId); @@ -229,7 +229,7 @@ public function create(): void $projectorRepository->projectors()->willReturn([$projector])->shouldBeCalledOnce(); $projectorResolver = $this->prophesize(ProjectorResolver::class); - $projectorResolver->resolveCreateMethod($projector)->willReturn($projector->create(...)); + $projectorResolver->resolveSetupMethod($projector)->willReturn($projector->create(...)); $projectorResolver->projectionId($projector)->willReturn($projectionId); $projectionist = new DefaultProjectionist( @@ -537,7 +537,7 @@ public function drop(): void $projectorRepository->projectors()->willReturn([$projector])->shouldBeCalledOnce(); $projectorResolver = $this->prophesize(ProjectorResolver::class); - $projectorResolver->resolveDropMethod($projector)->willReturn($projector->drop(...)); + $projectorResolver->resolveTeardownMethod($projector)->willReturn($projector->drop(...)); $projectorResolver->projectionId($projector)->willReturn($projectionId); $projectionist = new DefaultProjectionist( @@ -576,7 +576,7 @@ public function drop(): void $projectorRepository->projectors()->willReturn([$projector])->shouldBeCalledOnce(); $projectorResolver = $this->prophesize(ProjectorResolver::class); - $projectorResolver->resolveDropMethod($projector)->willReturn($projector->drop(...)); + $projectorResolver->resolveTeardownMethod($projector)->willReturn($projector->drop(...)); $projectorResolver->projectionId($projector)->willReturn($projectionId); $projectionist = new DefaultProjectionist( @@ -639,7 +639,7 @@ public function drop(): void $projectorRepository->projectors()->willReturn([$projector])->shouldBeCalledOnce(); $projectorResolver = $this->prophesize(ProjectorResolver::class); - $projectorResolver->resolveDropMethod($projector)->willReturn($projector->drop(...)); + $projectorResolver->resolveTeardownMethod($projector)->willReturn($projector->drop(...)); $projectorResolver->projectionId($projector)->willReturn($projectionId); $projectionist = new DefaultProjectionist( @@ -671,7 +671,7 @@ class { $projectorRepository->projectors()->willReturn([$projector])->shouldBeCalledOnce(); $projectorResolver = $this->prophesize(ProjectorResolver::class); - $projectorResolver->resolveDropMethod($projector)->willReturn(null); + $projectorResolver->resolveTeardownMethod($projector)->willReturn(null); $projectorResolver->projectionId($projector)->willReturn($projectionId); $projectionist = new DefaultProjectionist( @@ -708,7 +708,7 @@ public function drop(): void $projectorRepository->projectors()->willReturn([$projector])->shouldBeCalledOnce(); $projectorResolver = $this->prophesize(ProjectorResolver::class); - $projectorResolver->resolveDropMethod($projector)->willReturn($projector->drop(...)); + $projectorResolver->resolveTeardownMethod($projector)->willReturn($projector->drop(...)); $projectorResolver->projectionId($projector)->willReturn($projectionId); $projectionist = new DefaultProjectionist( diff --git a/tests/Unit/Projection/Projector/MetadataProjectorResolverTest.php b/tests/Unit/Projection/Projector/MetadataProjectorResolverTest.php index b07880c54..6b0561f83 100644 --- a/tests/Unit/Projection/Projector/MetadataProjectorResolverTest.php +++ b/tests/Unit/Projection/Projector/MetadataProjectorResolverTest.php @@ -4,10 +4,10 @@ namespace Patchlevel\EventSourcing\Tests\Unit\Projection\Projector; -use Patchlevel\EventSourcing\Attribute\Create; -use Patchlevel\EventSourcing\Attribute\Drop; -use Patchlevel\EventSourcing\Attribute\Projection; +use Patchlevel\EventSourcing\Attribute\Projector; +use Patchlevel\EventSourcing\Attribute\Setup; use Patchlevel\EventSourcing\Attribute\Subscribe; +use Patchlevel\EventSourcing\Attribute\Teardown; use Patchlevel\EventSourcing\EventBus\Message; use Patchlevel\EventSourcing\Projection\Projector\MetadataProjectorResolver; use Patchlevel\EventSourcing\Tests\Unit\Fixture\Email; @@ -21,7 +21,7 @@ final class MetadataProjectorResolverTest extends TestCase { public function testResolveHandleMethod(): void { - $projection = new #[Projection('dummy')] + $projection = new #[Projector('dummy')] class { public static Message|null $handledMessage = null; @@ -51,7 +51,7 @@ public function handleProfileCreated(Message $message): void public function testNotResolveHandleMethod(): void { - $projection = new #[Projection('dummy')] + $projection = new #[Projector('dummy')] class { }; @@ -69,11 +69,11 @@ class { public function testResolveCreateMethod(): void { - $projection = new #[Projection('dummy')] + $projection = new #[Projector('dummy')] class { public static bool $called = false; - #[Create] + #[Setup] public function method(): void { self::$called = true; @@ -81,7 +81,7 @@ public function method(): void }; $resolver = new MetadataProjectorResolver(); - $result = $resolver->resolveCreateMethod($projection); + $result = $resolver->resolveSetupMethod($projection); self::assertIsCallable($result); @@ -92,23 +92,23 @@ public function method(): void public function testNotResolveCreateMethod(): void { - $projection = new #[Projection('dummy')] + $projection = new #[Projector('dummy')] class { }; $resolver = new MetadataProjectorResolver(); - $result = $resolver->resolveCreateMethod($projection); + $result = $resolver->resolveSetupMethod($projection); self::assertNull($result); } public function testResolveDropMethod(): void { - $projection = new #[Projection('dummy')] + $projection = new #[Projector('dummy')] class { public static bool $called = false; - #[Drop] + #[Teardown] public function method(): void { self::$called = true; @@ -116,7 +116,7 @@ public function method(): void }; $resolver = new MetadataProjectorResolver(); - $result = $resolver->resolveDropMethod($projection); + $result = $resolver->resolveTeardownMethod($projection); self::assertIsCallable($result); @@ -127,19 +127,19 @@ public function method(): void public function testNotResolveDropMethod(): void { - $projection = new #[Projection('dummy')] + $projection = new #[Projector('dummy')] class { }; $resolver = new MetadataProjectorResolver(); - $result = $resolver->resolveDropMethod($projection); + $result = $resolver->resolveTeardownMethod($projection); self::assertNull($result); } public function testProjectionId(): void { - $projection = new #[Projection('dummy', 1)] + $projection = new #[Projector('dummy', 1)] class { }; diff --git a/tests/Unit/Projection/Projector/ProjectorHelperTest.php b/tests/Unit/Projection/Projector/ProjectorHelperTest.php index 51c29ac95..bd833035a 100644 --- a/tests/Unit/Projection/Projector/ProjectorHelperTest.php +++ b/tests/Unit/Projection/Projector/ProjectorHelperTest.php @@ -4,7 +4,7 @@ namespace Patchlevel\EventSourcing\Tests\Unit\Projection\Projector; -use Patchlevel\EventSourcing\Attribute\Projection; +use Patchlevel\EventSourcing\Attribute\Projector; use Patchlevel\EventSourcing\Projection\Projection\ProjectionId; use Patchlevel\EventSourcing\Projection\Projector\ProjectorHelper; use PHPUnit\Framework\TestCase; @@ -14,7 +14,7 @@ final class ProjectorHelperTest extends TestCase { public function testProjectionName(): void { - $projector = new #[Projection('dummy')] + $projector = new #[Projector('dummy')] class { }; @@ -25,7 +25,7 @@ class { public function testProjectionVersion(): void { - $projector = new #[Projection('dummy', 1)] + $projector = new #[Projector('dummy', 1)] class { }; @@ -36,7 +36,7 @@ class { public function testProjectionId(): void { - $projector = new #[Projection('dummy', 1)] + $projector = new #[Projector('dummy', 1)] class { };