From 0494d1693107abdf1f9e974c09188228e23d959d Mon Sep 17 00:00:00 2001 From: David Badura Date: Wed, 3 Jan 2024 13:07:25 +0100 Subject: [PATCH] rename create into setup and drop into teardown --- docs/pages/getting_started.md | 8 ++--- docs/pages/projection.md | 16 ++++----- src/Attribute/{Drop.php => Setup.php} | 2 +- src/Attribute/{Create.php => Teardown.php} | 2 +- .../AttributeProjectorMetadataFactory.php | 12 +++---- ...ropMethod.php => DuplicateSetupMethod.php} | 4 +-- ...Method.php => DuplicateTeardownMethod.php} | 4 +-- src/Metadata/Projector/ProjectorMetadata.php | 4 +-- .../Projectionist/DefaultProjectionist.php | 22 ++++++------ .../Projector/MetadataProjectorResolver.php | 8 ++--- .../Projector/ProjectorResolver.php | 4 +-- .../Projection/ProfileProjector.php | 8 ++--- .../IntegrationTest.php | 6 ++-- ...rojection.php => BankAccountProjector.php} | 16 ++++++--- .../BasicIntegrationTest.php | 10 +++--- ...ileProjection.php => ProfileProjector.php} | 10 +++--- tests/Integration/Outbox/OutboxTest.php | 6 ++-- ...ileProjection.php => ProfileProjector.php} | 14 +++++--- ...ileProjection.php => ProfileProjector.php} | 10 +++--- .../Projectionist/ProjectionistTest.php | 6 ++-- ...mmy2Projection.php => Dummy2Projector.php} | 16 +++------ ...DummyProjection.php => DummyProjector.php} | 16 +++------ .../AttributeProjectorMetadataFactoryTest.php | 36 +++++++++---------- .../DefaultProjectionistTest.php | 18 +++++----- .../MetadataProjectorResolverTest.php | 16 ++++----- 25 files changed, 136 insertions(+), 138 deletions(-) rename src/Attribute/{Drop.php => Setup.php} (88%) rename src/Attribute/{Create.php => Teardown.php} (86%) rename src/Metadata/Projector/{DuplicateDropMethod.php => DuplicateSetupMethod.php} (79%) rename src/Metadata/Projector/{DuplicateCreateMethod.php => DuplicateTeardownMethod.php} (78%) rename tests/Integration/BankAccountSplitStream/Projection/{BankAccountProjection.php => BankAccountProjector.php} (87%) rename tests/Integration/BasicImplementation/Projection/{ProfileProjection.php => ProfileProjector.php} (90%) rename tests/Integration/Outbox/Projection/{ProfileProjection.php => ProfileProjector.php} (85%) rename tests/Integration/Projectionist/Projection/{ProfileProjection.php => ProfileProjector.php} (92%) rename tests/Unit/Fixture/{Dummy2Projection.php => Dummy2Projector.php} (69%) rename tests/Unit/Fixture/{DummyProjection.php => DummyProjector.php} (69%) diff --git a/docs/pages/getting_started.md b/docs/pages/getting_started.md index 344c683b6..86597dc24 100644 --- a/docs/pages/getting_started.md +++ b/docs/pages/getting_started.md @@ -156,8 +156,8 @@ 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\Projector; use Patchlevel\EventSourcing\EventBus\Message; @@ -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 64db04044..290390174 100644 --- a/docs/pages/projection.md +++ b/docs/pages/projection.md @@ -15,8 +15,8 @@ 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\Projector; use Patchlevel\EventSourcing\EventBus\Message; @@ -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()};"); @@ -87,8 +87,8 @@ 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. @@ -118,8 +118,8 @@ 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\Projector; use Patchlevel\EventSourcing\EventBus\Message; 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 461f68eca..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\Projector; +use Patchlevel\EventSourcing\Attribute\Setup; use Patchlevel\EventSourcing\Attribute\Subscribe; +use Patchlevel\EventSourcing\Attribute\Teardown; use ReflectionClass; use function array_key_exists; @@ -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 42c4a1373..db1511745 100644 --- a/tests/Benchmark/BasicImplementation/Projection/ProfileProjector.php +++ b/tests/Benchmark/BasicImplementation/Projection/ProfileProjector.php @@ -5,10 +5,10 @@ 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\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; @@ -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 87% rename from tests/Integration/BankAccountSplitStream/Projection/BankAccountProjection.php rename to tests/Integration/BankAccountSplitStream/Projection/BankAccountProjector.php index 83aa17e00..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\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; +use function assert; + #[Projector('dummy', 1)] -final class BankAccountProjection +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 90% rename from tests/Integration/BasicImplementation/Projection/ProfileProjection.php rename to tests/Integration/BasicImplementation/Projection/ProfileProjector.php index 65c582d40..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\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; #[Projector('profile', 1)] -final class ProfileProjection +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 85% rename from tests/Integration/Outbox/Projection/ProfileProjection.php rename to tests/Integration/Outbox/Projection/ProfileProjector.php index c33f1a5a9..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\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; +use function assert; + #[Projector('dummy', 1)] -final class ProfileProjection +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 92% rename from tests/Integration/Projectionist/Projection/ProfileProjection.php rename to tests/Integration/Projectionist/Projection/ProfileProjector.php index 0ae9e6d10..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\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; @@ -18,7 +18,7 @@ use function sprintf; #[Projector('profile', 1)] -final class ProfileProjection +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/Dummy2Projection.php b/tests/Unit/Fixture/Dummy2Projector.php similarity index 69% rename from tests/Unit/Fixture/Dummy2Projection.php rename to tests/Unit/Fixture/Dummy2Projector.php index fc0eba19b..be3355d71 100644 --- a/tests/Unit/Fixture/Dummy2Projection.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\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; #[Projector('dummy2', 1)] -final class Dummy2Projection +final class Dummy2Projector { 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/Fixture/DummyProjection.php b/tests/Unit/Fixture/DummyProjector.php similarity index 69% rename from tests/Unit/Fixture/DummyProjection.php rename to tests/Unit/Fixture/DummyProjector.php index be1ba5fb5..0a4ba0aa9 100644 --- a/tests/Unit/Fixture/DummyProjection.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\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; #[Projector('dummy', 1)] -final class DummyProjection +final class DummyProjector { 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/Metadata/Projector/AttributeProjectorMetadataFactoryTest.php b/tests/Unit/Metadata/Projector/AttributeProjectorMetadataFactoryTest.php index f90d1a607..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\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; @@ -39,8 +39,8 @@ 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); } @@ -54,12 +54,12 @@ public function handle(): void { } - #[Create] + #[Setup] public function create(): void { } - #[Drop] + #[Teardown] public function drop(): void { } @@ -73,8 +73,8 @@ 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 @@ -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 #[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 #[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 94986870b..cd8b5998f 100644 --- a/tests/Unit/Projection/Projectionist/DefaultProjectionistTest.php +++ b/tests/Unit/Projection/Projectionist/DefaultProjectionistTest.php @@ -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 278988ae7..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\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; @@ -73,7 +73,7 @@ public function testResolveCreateMethod(): void 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); @@ -97,7 +97,7 @@ class { }; $resolver = new MetadataProjectorResolver(); - $result = $resolver->resolveCreateMethod($projection); + $result = $resolver->resolveSetupMethod($projection); self::assertNull($result); } @@ -108,7 +108,7 @@ public function testResolveDropMethod(): void 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); @@ -132,7 +132,7 @@ class { }; $resolver = new MetadataProjectorResolver(); - $result = $resolver->resolveDropMethod($projection); + $result = $resolver->resolveTeardownMethod($projection); self::assertNull($result); }