diff --git a/docs/pages/getting_started.md b/docs/pages/getting_started.md index 4f3c22ea2..d5ce83335 100644 --- a/docs/pages/getting_started.md +++ b/docs/pages/getting_started.md @@ -231,11 +231,7 @@ final class HotelProjector private function table(): string { - return sprintf( - 'projection_%s_%s', - $this->projectionName(), - $this->projectionVersion() - ); + return 'projection_' . $this->projectorId(); } } ``` diff --git a/docs/pages/projection.md b/docs/pages/projection.md index 290390174..9c89d8bd7 100644 --- a/docs/pages/projection.md +++ b/docs/pages/projection.md @@ -22,7 +22,7 @@ use Patchlevel\EventSourcing\Attribute\Projector; use Patchlevel\EventSourcing\EventBus\Message; use Patchlevel\EventSourcing\Projection\Projector\ProjectorUtil; -#[Projector('profile')] +#[Projector('profile_1')] final class ProfileProjector { use ProjectorUtil; @@ -70,22 +70,21 @@ final class ProfileProjector private function table(): string { - return sprintf( - 'projection_%s_%s', - $this->projectionName(), - $this->projectionVersion() - ); + return 'projection_' . $this->projectionId(); } } ``` -Each projector is responsible for a specific projection and version. -This combination of information results in the so-called `project ID`. +!!! tip + + Add a version as suffix to the `projectorId`, so you can increment it when the projection changes. + +Each projector has an unique ID and is responsible for a specific projection. 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. +In our example, the projection is called "profile". 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". +the name of the projection should be part of the table/collection name. +In our example, we build a `table` helper method, what creates the following string: "projection_profile". 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. @@ -112,9 +111,11 @@ Several projectors can also listen to the same event. ## Versioning -As soon as the structure of a projection changes, the version must be change or increment. +As soon as the structure of a projection changes or the or you need other events from the past, +the `projectorId` 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 `Projector` attribute. +To do this, you can add a version to the `projectorId`: ```php use Doctrine\DBAL\Connection; @@ -124,7 +125,7 @@ use Patchlevel\EventSourcing\Attribute\Handle; use Patchlevel\EventSourcing\Attribute\Projector; use Patchlevel\EventSourcing\EventBus\Message; -#[Projector('profile', version: 1)] +#[Projector('profile_1')] final class ProfileProjector { // ... @@ -133,7 +134,7 @@ final class ProfileProjector !!! warning - If you change the version, you must also change the table/collection name. + If you change the ID, you must also change the table/collection name. !!! tip @@ -168,20 +169,19 @@ 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 `Projector` attribute. +A projection id is unique and can be defined using the `Projector` attribute. ```php use Patchlevel\EventSourcing\Attribute\Projector; -#[Projector('profile', version: 1)] +#[Projector('profile_1')] final class ProfileProjector { // ... } ``` -As soon as the projection changes, such as the structure or the data, the version of the projection must be incremented. +As soon as the projection changes, such as the structure or the data, the id of the projection must be changed. This tells the projectionist to build an another projection with this projector. !!! note @@ -217,7 +217,7 @@ stateDiagram-v2 ### New A projection gets the status new if there is a projector with an unknown `projection id`. -This can happen when either a new projector has been added, the version has changed +This can happen when either a new projector has been added, the `projector id` has changed or the projection has been manually deleted from the projection store. ### Booting diff --git a/src/Attribute/Projector.php b/src/Attribute/Projector.php index c5c02cdd4..c4ed7011d 100644 --- a/src/Attribute/Projector.php +++ b/src/Attribute/Projector.php @@ -10,8 +10,7 @@ final class Projector { public function __construct( - public readonly string $name, - public readonly int $version = 0, + public readonly string $id, ) { } } diff --git a/src/Console/Command/ProjectionCommand.php b/src/Console/Command/ProjectionCommand.php index 0d9bd0de0..16d0f6fc2 100644 --- a/src/Console/Command/ProjectionCommand.php +++ b/src/Console/Command/ProjectionCommand.php @@ -6,7 +6,6 @@ use Patchlevel\EventSourcing\Console\InvalidArgumentGiven; use Patchlevel\EventSourcing\Projection\Projection\ProjectionCriteria; -use Patchlevel\EventSourcing\Projection\Projection\ProjectionId; use Patchlevel\EventSourcing\Projection\Projectionist\Projectionist; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; @@ -44,7 +43,7 @@ protected function projectionCriteria(InputInterface $input): ProjectionCriteria ); } - /** @return list|null */ + /** @return list|null */ private function projectionIds(InputInterface $input): array|null { $ids = $input->getOption('id'); @@ -59,12 +58,12 @@ private function projectionIds(InputInterface $input): array|null return array_values( array_map( - static function (mixed $id) use ($ids): ProjectionId { + static function (mixed $id) use ($ids): string { if (!is_string($id)) { throw new InvalidArgumentGiven($ids, 'list'); } - return ProjectionId::fromString($id); + return $id; }, $ids, ), diff --git a/src/Console/Command/ProjectionStatusCommand.php b/src/Console/Command/ProjectionStatusCommand.php index 7db9b076e..eebfd9987 100644 --- a/src/Console/Command/ProjectionStatusCommand.php +++ b/src/Console/Command/ProjectionStatusCommand.php @@ -7,7 +7,6 @@ use Patchlevel\EventSourcing\Console\InputHelper; use Patchlevel\EventSourcing\Console\OutputStyle; use Patchlevel\EventSourcing\Projection\Projection\Projection; -use Patchlevel\EventSourcing\Projection\Projection\ProjectionId; use Patchlevel\EventSourcing\Projection\Projection\Store\ErrorContext; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputArgument; @@ -47,17 +46,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int $io->table( [ 'id', - 'name', - 'version', 'position', 'status', 'error message', ], array_map( static fn (Projection $projection) => [ - $projection->id()->toString(), - $projection->id()->name(), - $projection->id()->version(), + $projection->id(), $projection->position(), $projection->status()->value, $projection->projectionError()?->errorMessage, @@ -69,22 +64,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 0; } - $projection = $projections->get(ProjectionId::fromString($id)); + $projection = $projections->get($id); $io->horizontalTable( [ 'id', - 'name', - 'version', 'position', 'status', 'error message', ], [ [ - $projection->id()->toString(), - $projection->id()->name(), - $projection->id()->version(), + $projection->id(), $projection->position(), $projection->status()->value, $projection->projectionError()?->errorMessage, diff --git a/src/Metadata/Projector/AttributeProjectorMetadataFactory.php b/src/Metadata/Projector/AttributeProjectorMetadataFactory.php index 5bee31352..06d2072f0 100644 --- a/src/Metadata/Projector/AttributeProjectorMetadataFactory.php +++ b/src/Metadata/Projector/AttributeProjectorMetadataFactory.php @@ -78,8 +78,7 @@ public function metadata(string $projector): ProjectorMetadata } $metadata = new ProjectorMetadata( - $projectorInfo->name, - $projectorInfo->version, + $projectorInfo->id, $subscribeMethods, $createMethod, $dropMethod, diff --git a/src/Metadata/Projector/ProjectorMetadata.php b/src/Metadata/Projector/ProjectorMetadata.php index 2e2363e61..251ea0155 100644 --- a/src/Metadata/Projector/ProjectorMetadata.php +++ b/src/Metadata/Projector/ProjectorMetadata.php @@ -7,8 +7,7 @@ final class ProjectorMetadata { public function __construct( - public readonly string $name, - public readonly int $version, + public readonly string $id, /** @var array> */ public readonly array $subscribeMethods = [], public readonly string|null $setupMethod = null, diff --git a/src/Projection/Projection/DuplicateProjectionId.php b/src/Projection/Projection/DuplicateProjectionId.php index bd56d52d5..7025da814 100644 --- a/src/Projection/Projection/DuplicateProjectionId.php +++ b/src/Projection/Projection/DuplicateProjectionId.php @@ -10,8 +10,8 @@ final class DuplicateProjectionId extends RuntimeException { - public function __construct(ProjectionId $projectionId) + public function __construct(string $projectionId) { - parent::__construct(sprintf('projection with the id "%s" exist already', $projectionId->toString())); + parent::__construct(sprintf('projection with the id "%s" exist already', $projectionId)); } } diff --git a/src/Projection/Projection/Projection.php b/src/Projection/Projection/Projection.php index a78623c61..49e308235 100644 --- a/src/Projection/Projection/Projection.php +++ b/src/Projection/Projection/Projection.php @@ -7,7 +7,7 @@ final class Projection { public function __construct( - private readonly ProjectionId $id, + private readonly string $id, private ProjectionStatus $status = ProjectionStatus::New, private int $position = 0, private ProjectionError|null $error = null, @@ -15,7 +15,7 @@ public function __construct( ) { } - public function id(): ProjectionId + public function id(): string { return $this->id; } diff --git a/src/Projection/Projection/ProjectionCollection.php b/src/Projection/Projection/ProjectionCollection.php index 52933722e..73151882e 100644 --- a/src/Projection/Projection/ProjectionCollection.php +++ b/src/Projection/Projection/ProjectionCollection.php @@ -25,28 +25,28 @@ public function __construct(array $projections = []) $result = []; foreach ($projections as $projection) { - if (array_key_exists($projection->id()->toString(), $result)) { + if (array_key_exists($projection->id(), $result)) { throw new DuplicateProjectionId($projection->id()); } - $result[$projection->id()->toString()] = $projection; + $result[$projection->id()] = $projection; } $this->projections = $result; } - public function get(ProjectionId $projectorId): Projection + public function get(string $projectorId): Projection { if (!$this->has($projectorId)) { throw new ProjectionNotFound($projectorId); } - return $this->projections[$projectorId->toString()]; + return $this->projections[$projectorId]; } - public function has(ProjectionId $projectorId): bool + public function has(string $projectorId): bool { - return array_key_exists($projectorId->toString(), $this->projections); + return array_key_exists($projectorId, $this->projections); } public function add(Projection $projection): self @@ -104,7 +104,7 @@ public function filterByCriteria(ProjectionCriteria $criteria): self static function (Projection $projection) use ($criteria): bool { if ($criteria->ids !== null) { foreach ($criteria->ids as $id) { - if ($projection->id()->equals($id)) { + if ($projection->id() === $id) { return true; } } diff --git a/src/Projection/Projection/ProjectionCriteria.php b/src/Projection/Projection/ProjectionCriteria.php index 7b58cc98c..e1f4b02a6 100644 --- a/src/Projection/Projection/ProjectionCriteria.php +++ b/src/Projection/Projection/ProjectionCriteria.php @@ -7,7 +7,7 @@ /** @psalm-immutable */ final class ProjectionCriteria { - /** @param list|null $ids */ + /** @param list|null $ids */ public function __construct( public readonly array|null $ids = null, ) { diff --git a/src/Projection/Projection/ProjectionId.php b/src/Projection/Projection/ProjectionId.php deleted file mode 100644 index 523ac1fac..000000000 --- a/src/Projection/Projection/ProjectionId.php +++ /dev/null @@ -1,63 +0,0 @@ -name, $this->version); - } - - public function name(): string - { - return $this->name; - } - - public function version(): int - { - return $this->version; - } - - public function equals(self $other): bool - { - return $this->name === $other->name && $this->version === $other->version; - } - - public static function fromString(string $value): self - { - $parts = explode('-', $value); - - if (count($parts) < 2) { - throw new InvalidArgumentException(); - } - - $version = array_pop($parts); - - if (!ctype_digit($version)) { - throw new InvalidArgumentException(); - } - - $name = implode('-', $parts); - - return new self($name, (int)$version); - } -} diff --git a/src/Projection/Projection/ProjectionNotFound.php b/src/Projection/Projection/ProjectionNotFound.php index 679eb4784..4ada78b52 100644 --- a/src/Projection/Projection/ProjectionNotFound.php +++ b/src/Projection/Projection/ProjectionNotFound.php @@ -10,8 +10,8 @@ final class ProjectionNotFound extends RuntimeException { - public function __construct(ProjectionId $projectionId) + public function __construct(string $projectionId) { - parent::__construct(sprintf('projection with the id "%s" not found', $projectionId->toString())); + parent::__construct(sprintf('projection with the id "%s" not found', $projectionId)); } } diff --git a/src/Projection/Projection/Store/DoctrineStore.php b/src/Projection/Projection/Store/DoctrineStore.php index 5035b43f8..e42ff7679 100644 --- a/src/Projection/Projection/Store/DoctrineStore.php +++ b/src/Projection/Projection/Store/DoctrineStore.php @@ -10,7 +10,6 @@ use Patchlevel\EventSourcing\Projection\Projection\Projection; use Patchlevel\EventSourcing\Projection\Projection\ProjectionCollection; use Patchlevel\EventSourcing\Projection\Projection\ProjectionError; -use Patchlevel\EventSourcing\Projection\Projection\ProjectionId; use Patchlevel\EventSourcing\Projection\Projection\ProjectionNotFound; use Patchlevel\EventSourcing\Projection\Projection\ProjectionStatus; use Patchlevel\EventSourcing\Schema\SchemaConfigurator; @@ -22,8 +21,7 @@ use const JSON_THROW_ON_ERROR; /** @psalm-type Data = array{ - * name: string, - * version: int, + * id: string, * position: int, * status: string, * error_message: string|null, @@ -39,19 +37,16 @@ public function __construct( ) { } - public function get(ProjectionId $projectionId): Projection + public function get(string $projectionId): Projection { $sql = $this->connection->createQueryBuilder() ->select('*') ->from($this->projectionTable) - ->where('name = :name AND version = :version') + ->where('id = :id') ->getSQL(); /** @var Data|false $result */ - $result = $this->connection->fetchAssociative($sql, [ - 'name' => $projectionId->name(), - 'version' => $projectionId->version(), - ]); + $result = $this->connection->fetchAssociative($sql, ['id' => $projectionId]); if ($result === false) { throw new ProjectionNotFound($projectionId); @@ -85,7 +80,7 @@ private function createProjection(array $row): Projection json_decode($row['error_context'], true, 512, JSON_THROW_ON_ERROR) : null; return new Projection( - new ProjectionId($row['name'], $row['version']), + $row['id'], ProjectionStatus::from($row['status']), $row['position'], $row['error_message'] !== null ? new ProjectionError( @@ -114,8 +109,7 @@ function (Connection $connection) use ($projections): void { 'retry' => $projection->retry(), ], [ - 'name' => $projection->id()->name(), - 'version' => $projection->id()->version(), + 'id' => $projection->id(), ], ); @@ -126,8 +120,7 @@ function (Connection $connection) use ($projections): void { $connection->insert( $this->projectionTable, [ - 'name' => $projection->id()->name(), - 'version' => $projection->id()->version(), + 'id' => $projection->id(), 'position' => $projection->position(), 'status' => $projection->status()->value, 'error_message' => $projectionError?->errorMessage, @@ -141,15 +134,12 @@ function (Connection $connection) use ($projections): void { ); } - public function remove(ProjectionId ...$projectionIds): void + public function remove(string ...$projectionIds): void { $this->connection->transactional( function (Connection $connection) use ($projectionIds): void { foreach ($projectionIds as $projectionId) { - $connection->delete($this->projectionTable, [ - 'name' => $projectionId->name(), - 'version' => $projectionId->version(), - ]); + $connection->delete($this->projectionTable, ['id' => $projectionId]); } }, ); @@ -159,11 +149,9 @@ public function configureSchema(Schema $schema, Connection $connection): void { $table = $schema->createTable($this->projectionTable); - $table->addColumn('name', Types::STRING) + $table->addColumn('id', Types::STRING) ->setLength(255) ->setNotnull(true); - $table->addColumn('version', Types::INTEGER) - ->setNotnull(true); $table->addColumn('position', Types::INTEGER) ->setNotnull(true); $table->addColumn('status', Types::STRING) @@ -178,6 +166,6 @@ public function configureSchema(Schema $schema, Connection $connection): void ->setNotnull(true) ->setDefault(0); - $table->setPrimaryKey(['name', 'version']); + $table->setPrimaryKey(['id']); } } diff --git a/src/Projection/Projection/Store/InMemoryStore.php b/src/Projection/Projection/Store/InMemoryStore.php index 999cab3fd..ad7059146 100644 --- a/src/Projection/Projection/Store/InMemoryStore.php +++ b/src/Projection/Projection/Store/InMemoryStore.php @@ -6,7 +6,6 @@ use Patchlevel\EventSourcing\Projection\Projection\Projection; use Patchlevel\EventSourcing\Projection\Projection\ProjectionCollection; -use Patchlevel\EventSourcing\Projection\Projection\ProjectionId; use Patchlevel\EventSourcing\Projection\Projection\ProjectionNotFound; use function array_key_exists; @@ -17,10 +16,10 @@ final class InMemoryStore implements ProjectionStore /** @var array */ private array $projections = []; - public function get(ProjectionId $projectionId): Projection + public function get(string $projectionId): Projection { - if (array_key_exists($projectionId->toString(), $this->projections)) { - return $this->projections[$projectionId->toString()]; + if (array_key_exists($projectionId, $this->projections)) { + return $this->projections[$projectionId]; } throw new ProjectionNotFound($projectionId); @@ -34,14 +33,14 @@ public function all(): ProjectionCollection public function save(Projection ...$projections): void { foreach ($projections as $projection) { - $this->projections[$projection->id()->toString()] = $projection; + $this->projections[$projection->id()] = $projection; } } - public function remove(ProjectionId ...$projectionIds): void + public function remove(string ...$projectionIds): void { foreach ($projectionIds as $projectionId) { - unset($this->projections[$projectionId->toString()]); + unset($this->projections[$projectionId]); } } } diff --git a/src/Projection/Projection/Store/ProjectionStore.php b/src/Projection/Projection/Store/ProjectionStore.php index f79a91b3d..252b75fdf 100644 --- a/src/Projection/Projection/Store/ProjectionStore.php +++ b/src/Projection/Projection/Store/ProjectionStore.php @@ -6,15 +6,14 @@ use Patchlevel\EventSourcing\Projection\Projection\Projection; use Patchlevel\EventSourcing\Projection\Projection\ProjectionCollection; -use Patchlevel\EventSourcing\Projection\Projection\ProjectionId; interface ProjectionStore { - public function get(ProjectionId $projectionId): Projection; + public function get(string $projectionId): Projection; public function all(): ProjectionCollection; public function save(Projection ...$projections): void; - public function remove(ProjectionId ...$projectionIds): void; + public function remove(string ...$projectionIds): void; } diff --git a/src/Projection/Projectionist/DefaultProjectionist.php b/src/Projection/Projectionist/DefaultProjectionist.php index 3a7b94c1a..915b297de 100644 --- a/src/Projection/Projectionist/DefaultProjectionist.php +++ b/src/Projection/Projectionist/DefaultProjectionist.php @@ -9,7 +9,6 @@ use Patchlevel\EventSourcing\Projection\Projection\ProjectionCollection; use Patchlevel\EventSourcing\Projection\Projection\ProjectionCriteria; use Patchlevel\EventSourcing\Projection\Projection\ProjectionError; -use Patchlevel\EventSourcing\Projection\Projection\ProjectionId; use Patchlevel\EventSourcing\Projection\Projection\ProjectionStatus; use Patchlevel\EventSourcing\Projection\Projection\Store\ProjectionStore; use Patchlevel\EventSourcing\Projection\Projector\MetadataProjectorResolver; @@ -64,7 +63,7 @@ public function boot( $this->logger?->info(sprintf( 'Projectionist: New Projector "%s" for "%s" was found and is now booting.', $projector::class, - $projection->id()->toString(), + $projection->id(), )); $setupMethod = $this->projectorResolver->resolveSetupMethod($projector); @@ -73,7 +72,7 @@ public function boot( $this->logger?->debug(sprintf( 'Projectionist: Projector "%s" for "%s" has no setup method, continue.', $projector::class, - $projection->id()->toString(), + $projection->id(), )); continue; @@ -84,13 +83,13 @@ public function boot( $this->logger?->debug(sprintf( 'Projectionist: For Projector "%s" for "%s" the setup method has been executed and is now prepared for data.', $projector::class, - $projection->id()->toString(), + $projection->id(), )); } catch (Throwable $e) { $this->logger?->error(sprintf( 'Projectionist: Projector "%s" for "%s" has an error in the setup method: %s', $projector::class, - $projection->id()->toString(), + $projection->id(), $e->getMessage(), )); @@ -145,7 +144,7 @@ public function boot( $this->logger?->debug( sprintf( 'Projectionist: Projection "%s" is farther than the current position (%d > %d), continue booting.', - $projection->id()->toString(), + $projection->id(), $projection->position(), $index, ), @@ -189,7 +188,7 @@ public function boot( $this->logger?->info(sprintf( 'Projectionist: Projection "%s" has been set to active after booting.', - $projection->id()->toString(), + $projection->id(), )); } @@ -245,7 +244,7 @@ public function run( $this->logger?->debug( sprintf( 'Projectionist: Projection "%s" is farther than the current position (%d > %d), continue processing.', - $projection->id()->toString(), + $projection->id(), $projection->position(), $index, ), @@ -300,7 +299,7 @@ public function teardown(ProjectionCriteria $criteria = new ProjectionCriteria() $this->logger?->warning( sprintf( 'Projectionist: Projector for "%s" to teardown not found, skipped.', - $projection->id()->toString(), + $projection->id(), ), ); @@ -316,7 +315,7 @@ public function teardown(ProjectionCriteria $criteria = new ProjectionCriteria() sprintf( 'Projectionist: Projector "%s" for "%s" has no teardown method and was immediately removed.', $projector::class, - $projection->id()->toString(), + $projection->id(), ), ); @@ -329,14 +328,14 @@ public function teardown(ProjectionCriteria $criteria = new ProjectionCriteria() $this->logger?->debug(sprintf( 'Projectionist: For Projector "%s" for "%s" the teardown method has been executed and is now prepared to be removed.', $projector::class, - $projection->id()->toString(), + $projection->id(), )); } catch (Throwable $e) { $this->logger?->error( sprintf( 'Projectionist: Projection "%s" for "%s" has an error in the teardown method, skipped: %s', $projector::class, - $projection->id()->toString(), + $projection->id(), $e->getMessage(), ), ); @@ -348,7 +347,7 @@ public function teardown(ProjectionCriteria $criteria = new ProjectionCriteria() $this->logger?->info( sprintf( 'Projectionist: Projection "%s" removed.', - $projection->id()->toString(), + $projection->id(), ), ); } @@ -367,7 +366,7 @@ public function remove(ProjectionCriteria $criteria = new ProjectionCriteria()): $this->projectionStore->remove($projection->id()); $this->logger?->info( - sprintf('Projectionist: Projection "%s" removed without a suitable projector.', $projection->id()->toString()), + sprintf('Projectionist: Projection "%s" removed without a suitable projector.', $projection->id()), ); continue; @@ -379,7 +378,7 @@ public function remove(ProjectionCriteria $criteria = new ProjectionCriteria()): $this->projectionStore->remove($projection->id()); $this->logger?->info( - sprintf('Projectionist: Projection "%s" removed.', $projection->id()->toString()), + sprintf('Projectionist: Projection "%s" removed.', $projection->id()), ); continue; @@ -400,7 +399,7 @@ public function remove(ProjectionCriteria $criteria = new ProjectionCriteria()): $this->projectionStore->remove($projection->id()); $this->logger?->info( - sprintf('Projectionist: Projection "%s" removed.', $projection->id()->toString()), + sprintf('Projectionist: Projection "%s" removed.', $projection->id()), ); } } @@ -417,7 +416,7 @@ public function reactivate(ProjectionCriteria $criteria = new ProjectionCriteria if (!$projector) { $this->logger?->debug( - sprintf('Projectionist: Projector for "%s" not found, skipped.', $projection->id()->toString()), + sprintf('Projectionist: Projector for "%s" not found, skipped.', $projection->id()), ); continue; @@ -429,7 +428,7 @@ public function reactivate(ProjectionCriteria $criteria = new ProjectionCriteria $this->logger?->info(sprintf( 'Projectionist: Projector "%s" for "%s" is reactivated.', $projector::class, - $projection->id()->toString(), + $projection->id(), )); } } @@ -441,13 +440,12 @@ public function projections(): ProjectionCollection foreach ($projectors as $projector) { $projectorId = $this->projectorResolver->projectorId($projector); - $projectionId = $projectorId->toProjectionId(); - if ($projections->has($projectionId)) { + if ($projections->has($projectorId)) { continue; } - $projections = $projections->add(new Projection($projectionId)); + $projections = $projections->add(new Projection($projectorId)); } return $projections; @@ -471,7 +469,7 @@ private function handleMessage(int $index, Message $message, Projection $project sprintf( 'Projectionist: Projector "%s" for "%s" has no subscribe methods for "%s", continue.', $projector::class, - $projection->id()->toString(), + $projection->id(), $message->event()::class, ), ); @@ -488,7 +486,7 @@ private function handleMessage(int $index, Message $message, Projection $project sprintf( 'Projectionist: Projector "%s" for "%s" could not process the event "%s": %s', $projector::class, - $projection->id()->toString(), + $projection->id(), $message->event()::class, $e->getMessage(), ), @@ -517,17 +515,17 @@ private function handleMessage(int $index, Message $message, Projection $project sprintf( 'Projectionist: Projector "%s" for "%s" processed the event "%s".', $projector::class, - $projection->id()->toString(), + $projection->id(), $message->event()::class, ), ); } - private function projector(ProjectionId $projectorId): object|null + private function projector(string $projectorId): object|null { $projectors = $this->projectors(); - return $projectors[$projectorId->toString()] ?? null; + return $projectors[$projectorId] ?? null; } /** @return array */ @@ -539,7 +537,7 @@ private function projectors(): array foreach ($this->projectorRepository->projectors() as $projector) { $projectorId = $this->projectorResolver->projectorId($projector); - $this->projectors[$projectorId->toString()] = $projector; + $this->projectors[$projectorId] = $projector; } } @@ -569,7 +567,7 @@ private function handleOutdatedProjections(ProjectionCollection $projections): v $this->logger?->info( sprintf( 'Projectionist: Projector for "%s" not found and has been marked as outdated.', - $projection->id()->toString(), + $projection->id(), ), ); } @@ -588,7 +586,7 @@ private function handleRetryProjections(ProjectionCollection $projections): void $this->logger?->info( sprintf( 'Projectionist: Retry projection "%s" (%d/%d) and set to active.', - $projection->id()->toString(), + $projection->id(), $projection->retry(), self::RETRY_LIMIT, ), diff --git a/src/Projection/Projectionist/ProjectionistError.php b/src/Projection/Projectionist/ProjectionistError.php index be947edc6..0ab37e96a 100644 --- a/src/Projection/Projectionist/ProjectionistError.php +++ b/src/Projection/Projectionist/ProjectionistError.php @@ -4,7 +4,6 @@ namespace Patchlevel\EventSourcing\Projection\Projectionist; -use Patchlevel\EventSourcing\Projection\Projection\ProjectionId; use RuntimeException; use Throwable; @@ -14,14 +13,14 @@ final class ProjectionistError extends RuntimeException { public function __construct( public readonly string $projector, - public readonly ProjectionId $projectionId, + public readonly string $projectionId, Throwable $error, ) { parent::__construct( sprintf( 'error in projector "%s" for "%s": %s', $projector, - $projectionId->toString(), + $projectionId, $error->getMessage(), ), 0, diff --git a/src/Projection/Projectionist/ProjectorNotFound.php b/src/Projection/Projectionist/ProjectorNotFound.php index 993ab55d6..f2af3622d 100644 --- a/src/Projection/Projectionist/ProjectorNotFound.php +++ b/src/Projection/Projectionist/ProjectorNotFound.php @@ -4,19 +4,18 @@ namespace Patchlevel\EventSourcing\Projection\Projectionist; -use Patchlevel\EventSourcing\Projection\Projection\ProjectionId; use RuntimeException; use function sprintf; final class ProjectorNotFound extends RuntimeException { - public static function forProjectionId(ProjectionId $projectionId): self + public static function forProjectionId(string $projectionId): self { return new self( sprintf( 'projector with the projection id "%s" not found', - $projectionId->toString(), + $projectionId, ), ); } diff --git a/src/Projection/Projector/MetadataProjectorResolver.php b/src/Projection/Projector/MetadataProjectorResolver.php index a6bfbe3ee..f19b8c2ff 100644 --- a/src/Projection/Projector/MetadataProjectorResolver.php +++ b/src/Projection/Projector/MetadataProjectorResolver.php @@ -61,13 +61,8 @@ public function resolveSubscribeMethods(object $projector, Message $message): it ); } - public function projectorId(object $projector): ProjectorId + public function projectorId(object $projector): string { - $metadata = $this->metadataFactory->metadata($projector::class); - - return new ProjectorId( - $metadata->name, - $metadata->version, - ); + return $this->metadataFactory->metadata($projector::class)->id; } } diff --git a/src/Projection/Projector/ProjectorHelper.php b/src/Projection/Projector/ProjectorHelper.php index 00bc7e334..5ca501d51 100644 --- a/src/Projection/Projector/ProjectorHelper.php +++ b/src/Projection/Projector/ProjectorHelper.php @@ -15,21 +15,9 @@ public function __construct( ) { } - public function name(object $projector): string + public function projectorId(object $projector): string { - return $this->getProjectorMetadata($projector)->name; - } - - public function version(object $projector): int - { - return $this->getProjectorMetadata($projector)->version; - } - - public function projectorId(object $projector): ProjectorId - { - $metadata = $this->getProjectorMetadata($projector); - - return new ProjectorId($metadata->name, $metadata->version); + return $this->getProjectorMetadata($projector)->id; } private function getProjectorMetadata(object $projector): ProjectorMetadata diff --git a/src/Projection/Projector/ProjectorId.php b/src/Projection/Projector/ProjectorId.php deleted file mode 100644 index 26c1895fe..000000000 --- a/src/Projection/Projector/ProjectorId.php +++ /dev/null @@ -1,69 +0,0 @@ -name, $this->version); - } - - public function name(): string - { - return $this->name; - } - - public function version(): int - { - return $this->version; - } - - public function equals(self $other): bool - { - return $this->name === $other->name && $this->version === $other->version; - } - - public static function fromString(string $value): self - { - $parts = explode('-', $value); - - if (count($parts) < 2) { - throw new InvalidArgumentException(); - } - - $version = array_pop($parts); - - if (!ctype_digit($version)) { - throw new InvalidArgumentException(); - } - - $name = implode('-', $parts); - - return new self($name, (int)$version); - } - - public function toProjectionId(): ProjectionId - { - return new ProjectionId($this->name, $this->version); - } -} diff --git a/src/Projection/Projector/ProjectorResolver.php b/src/Projection/Projector/ProjectorResolver.php index a3a666876..a21ea6d3a 100644 --- a/src/Projection/Projector/ProjectorResolver.php +++ b/src/Projection/Projector/ProjectorResolver.php @@ -16,5 +16,5 @@ public function resolveTeardownMethod(object $projector): Closure|null; /** @return iterable */ public function resolveSubscribeMethods(object $projector, Message $message): iterable; - public function projectorId(object $projector): ProjectorId; + public function projectorId(object $projector): string; } diff --git a/src/Projection/Projector/ProjectorUtil.php b/src/Projection/Projector/ProjectorUtil.php index 73039557e..ea27f821f 100644 --- a/src/Projection/Projector/ProjectorUtil.php +++ b/src/Projection/Projector/ProjectorUtil.php @@ -30,17 +30,7 @@ private function getProjectorHelper(): ProjectorHelper return new ProjectorHelper(self::metadataFactory()); } - private function projectorName(): string - { - return $this->getProjectorHelper()->name($this); - } - - private function projectorVersion(): int - { - return $this->getProjectorHelper()->version($this); - } - - private function projectorId(): ProjectorId + private function projectorId(): string { return $this->getProjectorHelper()->projectorId($this); } diff --git a/tests/Benchmark/BasicImplementation/Projection/ProfileProjector.php b/tests/Benchmark/BasicImplementation/Projection/ProfileProjector.php index db1511745..229492df3 100644 --- a/tests/Benchmark/BasicImplementation/Projection/ProfileProjector.php +++ b/tests/Benchmark/BasicImplementation/Projection/ProfileProjector.php @@ -14,7 +14,7 @@ use function assert; -#[Projector('dummy', 1)] +#[Projector('dummy_1')] final class ProfileProjector { public function __construct( diff --git a/tests/Integration/BankAccountSplitStream/Projection/BankAccountProjector.php b/tests/Integration/BankAccountSplitStream/Projection/BankAccountProjector.php index ba7adb1c1..310610db2 100644 --- a/tests/Integration/BankAccountSplitStream/Projection/BankAccountProjector.php +++ b/tests/Integration/BankAccountSplitStream/Projection/BankAccountProjector.php @@ -16,7 +16,7 @@ use function assert; -#[Projector('dummy', 1)] +#[Projector('dummy-1')] final class BankAccountProjector { public function __construct( diff --git a/tests/Integration/BasicImplementation/Projection/ProfileProjector.php b/tests/Integration/BasicImplementation/Projection/ProfileProjector.php index 6ec636f85..7913679cf 100644 --- a/tests/Integration/BasicImplementation/Projection/ProfileProjector.php +++ b/tests/Integration/BasicImplementation/Projection/ProfileProjector.php @@ -15,7 +15,7 @@ use function assert; -#[Projector('profile', 1)] +#[Projector('profile-1')] final class ProfileProjector { public function __construct( diff --git a/tests/Integration/Outbox/Projection/ProfileProjector.php b/tests/Integration/Outbox/Projection/ProfileProjector.php index 3e28a648c..35b97e809 100644 --- a/tests/Integration/Outbox/Projection/ProfileProjector.php +++ b/tests/Integration/Outbox/Projection/ProfileProjector.php @@ -15,7 +15,7 @@ use function assert; -#[Projector('dummy', 1)] +#[Projector('dummy-1')] final class ProfileProjector { public function __construct( diff --git a/tests/Integration/Projectionist/Projection/ProfileProjector.php b/tests/Integration/Projectionist/Projection/ProfileProjector.php index 7d328ca7d..efd4969ed 100644 --- a/tests/Integration/Projectionist/Projection/ProfileProjector.php +++ b/tests/Integration/Projectionist/Projection/ProfileProjector.php @@ -15,9 +15,8 @@ use Patchlevel\EventSourcing\Tests\Integration\Projectionist\Events\ProfileCreated; use function assert; -use function sprintf; -#[Projector('profile', 1)] +#[Projector('profile_1')] final class ProfileProjector { use ProjectorUtil; @@ -62,10 +61,6 @@ public function handleProfileCreated(Message $message): void private function tableName(): string { - return sprintf( - 'projection_%s_%s', - $this->projectorName(), - $this->projectorVersion(), - ); + return 'projection_' . $this->projectorId(); } } diff --git a/tests/Unit/Attribute/ProjectorTest.php b/tests/Unit/Attribute/ProjectorTest.php index b71b1217c..8ad51c26e 100644 --- a/tests/Unit/Attribute/ProjectorTest.php +++ b/tests/Unit/Attribute/ProjectorTest.php @@ -14,7 +14,6 @@ public function testInstantiate(): void { $attribute = new Projector('foo'); - self::assertSame('foo', $attribute->name); - self::assertSame(0, $attribute->version); + self::assertSame('foo', $attribute->id); } } diff --git a/tests/Unit/Fixture/Dummy2Projector.php b/tests/Unit/Fixture/Dummy2Projector.php index be3355d71..b64bd5337 100644 --- a/tests/Unit/Fixture/Dummy2Projector.php +++ b/tests/Unit/Fixture/Dummy2Projector.php @@ -10,7 +10,7 @@ use Patchlevel\EventSourcing\Attribute\Teardown; use Patchlevel\EventSourcing\EventBus\Message as EventMessage; -#[Projector('dummy2', 1)] +#[Projector('dummy2')] final class Dummy2Projector { public EventMessage|null $handledMessage = null; diff --git a/tests/Unit/Fixture/DummyProjector.php b/tests/Unit/Fixture/DummyProjector.php index 0a4ba0aa9..59c1197b0 100644 --- a/tests/Unit/Fixture/DummyProjector.php +++ b/tests/Unit/Fixture/DummyProjector.php @@ -10,7 +10,7 @@ use Patchlevel\EventSourcing\Attribute\Teardown; use Patchlevel\EventSourcing\EventBus\Message as EventMessage; -#[Projector('dummy', 1)] +#[Projector('dummy')] final class DummyProjector { public EventMessage|null $handledMessage = null; diff --git a/tests/Unit/Metadata/Projector/AttributeProjectorMetadataFactoryTest.php b/tests/Unit/Metadata/Projector/AttributeProjectorMetadataFactoryTest.php index 5d57e270a..ab4064c13 100644 --- a/tests/Unit/Metadata/Projector/AttributeProjectorMetadataFactoryTest.php +++ b/tests/Unit/Metadata/Projector/AttributeProjectorMetadataFactoryTest.php @@ -32,7 +32,7 @@ public function testNotAProjection(): void public function testEmptyProjection(): void { - $projection = new #[Projector('foo', 1)] + $projection = new #[Projector('foo')] class { }; @@ -42,13 +42,12 @@ class { self::assertSame([], $metadata->subscribeMethods); self::assertNull($metadata->setupMethod); self::assertNull($metadata->teardownMethod); - self::assertSame('foo', $metadata->name); - self::assertSame(1, $metadata->version); + self::assertSame('foo', $metadata->id); } public function testStandardProjection(): void { - $projection = new #[Projector('foo', 1)] + $projection = new #[Projector('foo')] class { #[Subscribe(ProfileVisited::class)] public function handle(): void @@ -80,7 +79,7 @@ public function drop(): void public function testMultipleHandlerOnOneMethod(): void { - $projection = new #[Projector('foo', 1)] + $projection = new #[Projector('foo')] class { #[Subscribe(ProfileVisited::class)] #[Subscribe(ProfileCreated::class)] @@ -103,7 +102,7 @@ public function handle(): void public function testSubscribeAll(): void { - $projection = new #[Projector('foo', 1)] + $projection = new #[Projector('foo')] class { #[Subscribe(Subscribe::ALL)] public function handle(): void @@ -126,7 +125,7 @@ public function testDuplicateSetupAttributeException(): void { $this->expectException(DuplicateSetupMethod::class); - $projection = new #[Projector('foo', 1)] + $projection = new #[Projector('foo')] class { #[Setup] public function create1(): void @@ -147,7 +146,7 @@ public function testDuplicateTeardownAttributeException(): void { $this->expectException(DuplicateTeardownMethod::class); - $projection = new #[Projector('foo', 1)] + $projection = new #[Projector('foo')] class { #[Teardown] public function drop1(): void diff --git a/tests/Unit/Projection/DummyStore.php b/tests/Unit/Projection/DummyStore.php index 423b16dc8..9008894a0 100644 --- a/tests/Unit/Projection/DummyStore.php +++ b/tests/Unit/Projection/DummyStore.php @@ -6,7 +6,6 @@ use Patchlevel\EventSourcing\Projection\Projection\Projection; use Patchlevel\EventSourcing\Projection\Projection\ProjectionCollection; -use Patchlevel\EventSourcing\Projection\Projection\ProjectionId; use Patchlevel\EventSourcing\Projection\Projection\ProjectionNotFound; use Patchlevel\EventSourcing\Projection\Projection\Store\ProjectionStore; @@ -19,21 +18,21 @@ final class DummyStore implements ProjectionStore private array $projections = []; /** @var list */ public array $savedProjections = []; - /** @var list */ + /** @var list */ public array $removedProjectionIds = []; /** @param list $projections */ public function __construct(array $projections = []) { foreach ($projections as $projection) { - $this->projections[$projection->id()->toString()] = $projection; + $this->projections[$projection->id()] = $projection; } } - public function get(ProjectionId $projectionId): Projection + public function get(string $projectionId): Projection { - if (array_key_exists($projectionId->toString(), $this->projections)) { - return $this->projections[$projectionId->toString()]; + if (array_key_exists($projectionId, $this->projections)) { + return $this->projections[$projectionId]; } throw new ProjectionNotFound($projectionId); @@ -47,16 +46,16 @@ public function all(): ProjectionCollection public function save(Projection ...$projections): void { foreach ($projections as $projection) { - $this->projections[$projection->id()->toString()] = $projection; + $this->projections[$projection->id()] = $projection; $this->savedProjections[] = clone $projection; } } - public function remove(ProjectionId ...$projectionIds): void + public function remove(string ...$projectionIds): void { foreach ($projectionIds as $projectionId) { $this->removedProjectionIds[] = $projectionId; - unset($this->projections[$projectionId->toString()]); + unset($this->projections[$projectionId]); } } } diff --git a/tests/Unit/Projection/Projection/DuplicateProjectionIdTest.php b/tests/Unit/Projection/Projection/DuplicateProjectionIdTest.php index 170435af3..9cf3b67de 100644 --- a/tests/Unit/Projection/Projection/DuplicateProjectionIdTest.php +++ b/tests/Unit/Projection/Projection/DuplicateProjectionIdTest.php @@ -5,7 +5,6 @@ namespace Patchlevel\EventSourcing\Tests\Unit\Projection\Projection; use Patchlevel\EventSourcing\Projection\Projection\DuplicateProjectionId; -use Patchlevel\EventSourcing\Projection\Projection\ProjectionId; use PHPUnit\Framework\TestCase; /** @covers \Patchlevel\EventSourcing\Projection\Projection\DuplicateProjectionId */ @@ -13,7 +12,7 @@ final class DuplicateProjectionIdTest extends TestCase { public function testCreate(): void { - $exception = new DuplicateProjectionId(new ProjectionId('foo', 1)); + $exception = new DuplicateProjectionId('foo-1'); self::assertSame( 'projection with the id "foo-1" exist already', diff --git a/tests/Unit/Projection/Projection/ProjectionCollectionTest.php b/tests/Unit/Projection/Projection/ProjectionCollectionTest.php index bc428e4dd..3881853c8 100644 --- a/tests/Unit/Projection/Projection/ProjectionCollectionTest.php +++ b/tests/Unit/Projection/Projection/ProjectionCollectionTest.php @@ -8,7 +8,6 @@ use Patchlevel\EventSourcing\Projection\Projection\Projection; use Patchlevel\EventSourcing\Projection\Projection\ProjectionCollection; use Patchlevel\EventSourcing\Projection\Projection\ProjectionCriteria; -use Patchlevel\EventSourcing\Projection\Projection\ProjectionId; use Patchlevel\EventSourcing\Projection\Projection\ProjectionNotFound; use Patchlevel\EventSourcing\Projection\Projection\ProjectionStatus; use PHPUnit\Framework\TestCase; @@ -18,7 +17,7 @@ final class ProjectionCollectionTest extends TestCase { public function testCreate(): void { - $id = new ProjectionId('test', 1); + $id = 'test'; $projection = new Projection($id); $collection = new ProjectionCollection([$projection]); @@ -31,7 +30,7 @@ public function testCreateWithDuplicatedId(): void { $this->expectException(DuplicateProjectionId::class); - $id = new ProjectionId('test', 1); + $id = 'test'; new ProjectionCollection([ new Projection($id), @@ -45,12 +44,12 @@ public function testNotFound(): void $collection = new ProjectionCollection(); /** @psalm-suppress UnusedMethodCall */ - $collection->get(new ProjectionId('test', 1)); + $collection->get('test'); } public function testAdd(): void { - $id = new ProjectionId('test', 1); + $id = 'test'; $projection = new Projection($id); $collection = new ProjectionCollection(); @@ -65,7 +64,7 @@ public function testAddWithDuplicatedId(): void { $this->expectException(DuplicateProjectionId::class); - $id = new ProjectionId('test', 1); + $id = 'test'; /** @psalm-suppress UnusedMethodCall */ (new ProjectionCollection()) @@ -77,17 +76,17 @@ public function testLowestProjectionPosition(): void { $collection = new ProjectionCollection([ new Projection( - new ProjectionId('foo', 1), + 'foo', ProjectionStatus::Active, 10, ), new Projection( - new ProjectionId('bar', 1), + 'bar', ProjectionStatus::Active, 5, ), new Projection( - new ProjectionId('baz', 1), + 'baz', ProjectionStatus::Active, 15, ), @@ -105,8 +104,8 @@ public function testLowestProjectionPositionWithEmptyCollection(): void public function testFilter(): void { - $fooId = new ProjectionId('foo', 1); - $barId = new ProjectionId('bar', 1); + $fooId = 'foo'; + $barId = 'bar'; $collection = new ProjectionCollection([ new Projection( @@ -129,8 +128,8 @@ public function testFilter(): void public function testFilterByProjectStatus(): void { - $fooId = new ProjectionId('foo', 1); - $barId = new ProjectionId('bar', 1); + $fooId = 'foo'; + $barId = 'bar'; $collection = new ProjectionCollection([ new Projection( @@ -153,8 +152,8 @@ public function testFilterByProjectStatus(): void public function testFilterByCriteriaEmpty(): void { - $fooId = new ProjectionId('foo', 1); - $barId = new ProjectionId('bar', 1); + $fooId = 'foo'; + $barId = 'bar'; $collection = new ProjectionCollection([ new Projection( @@ -179,8 +178,8 @@ public function testFilterByCriteriaEmpty(): void public function testFilterByCriteriaWithIds(): void { - $fooId = new ProjectionId('foo', 1); - $barId = new ProjectionId('bar', 1); + $fooId = 'foo'; + $barId = 'bar'; $collection = new ProjectionCollection([ new Projection( @@ -205,7 +204,7 @@ public function testFilterByCriteriaWithIds(): void public function testIterator(): void { - $id = new ProjectionId('test', 1); + $id = 'test'; $projection = new Projection($id); $collection = new ProjectionCollection([$projection]); diff --git a/tests/Unit/Projection/Projection/ProjectionCriteriaTest.php b/tests/Unit/Projection/Projection/ProjectionCriteriaTest.php index f6c4d3d83..01fdc238b 100644 --- a/tests/Unit/Projection/Projection/ProjectionCriteriaTest.php +++ b/tests/Unit/Projection/Projection/ProjectionCriteriaTest.php @@ -5,7 +5,6 @@ namespace Patchlevel\EventSourcing\Tests\Unit\Projection\Projection; use Patchlevel\EventSourcing\Projection\Projection\ProjectionCriteria; -use Patchlevel\EventSourcing\Projection\Projection\ProjectionId; use PHPUnit\Framework\TestCase; /** @covers \Patchlevel\EventSourcing\Projection\Projection\ProjectionCriteria */ @@ -13,7 +12,7 @@ final class ProjectionCriteriaTest extends TestCase { public function testProjectionId(): void { - $id = new ProjectionId('test', 1); + $id = 'test'; $criteria = new ProjectionCriteria([$id]); self::assertEquals([$id], $criteria->ids); diff --git a/tests/Unit/Projection/Projection/ProjectionIdTest.php b/tests/Unit/Projection/Projection/ProjectionIdTest.php deleted file mode 100644 index f782d600d..000000000 --- a/tests/Unit/Projection/Projection/ProjectionIdTest.php +++ /dev/null @@ -1,86 +0,0 @@ -name()); - self::assertSame(1, $projectionId->version()); - self::assertSame('test-1', $projectionId->toString()); - } - - public function testEquals(): void - { - $a = new ProjectionId( - 'test', - 1, - ); - - $b = new ProjectionId( - 'test', - 1, - ); - - $c = new ProjectionId( - 'foo', - 1, - ); - - $d = new ProjectionId( - 'test', - 2, - ); - - self::assertTrue($a->equals($b)); - self::assertFalse($a->equals($c)); - self::assertFalse($a->equals($d)); - } - - /** @dataProvider validFromStringProvider */ - public function testValidFromString(string $string, string $name, int $version): void - { - $projectionId = ProjectionId::fromString($string); - - self::assertSame($name, $projectionId->name()); - self::assertSame($version, $projectionId->version()); - self::assertSame($string, $projectionId->toString()); - } - - /** @return Generator */ - public static function validFromStringProvider(): Generator - { - yield ['hotel-1', 'hotel', 1]; - yield ['hotel-bar-1', 'hotel-bar', 1]; - yield ['hotel-bar--1', 'hotel-bar-', 1]; - } - - /** @dataProvider invalidFromStringProvider */ - public function testInvalidFromString(string $string): void - { - $this->expectException(InvalidArgumentException::class); - - ProjectionId::fromString($string); - } - - /** @return Generator */ - public static function invalidFromStringProvider(): Generator - { - yield ['hotel']; - yield ['hotel-bar']; - } -} diff --git a/tests/Unit/Projection/Projection/ProjectionNotFoundTest.php b/tests/Unit/Projection/Projection/ProjectionNotFoundTest.php index 554d5cd3c..c8e22e3e8 100644 --- a/tests/Unit/Projection/Projection/ProjectionNotFoundTest.php +++ b/tests/Unit/Projection/Projection/ProjectionNotFoundTest.php @@ -4,7 +4,6 @@ namespace Patchlevel\EventSourcing\Tests\Unit\Projection\Projection; -use Patchlevel\EventSourcing\Projection\Projection\ProjectionId; use Patchlevel\EventSourcing\Projection\Projection\ProjectionNotFound; use PHPUnit\Framework\TestCase; @@ -13,7 +12,7 @@ final class ProjectionNotFoundTest extends TestCase { public function testCreate(): void { - $exception = new ProjectionNotFound(new ProjectionId('foo', 1)); + $exception = new ProjectionNotFound('foo-1'); self::assertSame( 'projection with the id "foo-1" not found', diff --git a/tests/Unit/Projection/Projection/ProjectionTest.php b/tests/Unit/Projection/Projection/ProjectionTest.php index 137b6f61c..1017e6132 100644 --- a/tests/Unit/Projection/Projection/ProjectionTest.php +++ b/tests/Unit/Projection/Projection/ProjectionTest.php @@ -6,7 +6,6 @@ use Patchlevel\EventSourcing\Projection\Projection\Projection; use Patchlevel\EventSourcing\Projection\Projection\ProjectionError; -use Patchlevel\EventSourcing\Projection\Projection\ProjectionId; use Patchlevel\EventSourcing\Projection\Projection\ProjectionStatus; use Patchlevel\EventSourcing\Projection\Projection\Store\ErrorContext; use PHPUnit\Framework\TestCase; @@ -17,7 +16,7 @@ final class ProjectionTest extends TestCase { public function testCreate(): void { - $id = new ProjectionId('test', 1); + $id = 'test'; $projection = new Projection($id); self::assertSame($id, $projection->id()); @@ -33,7 +32,7 @@ public function testCreate(): void public function testBooting(): void { $projection = new Projection( - new ProjectionId('test', 1), + 'test', ); $projection->booting(); @@ -49,7 +48,7 @@ public function testBooting(): void public function testActive(): void { $projection = new Projection( - new ProjectionId('test', 1), + 'test', ); $projection->active(); @@ -65,7 +64,7 @@ public function testActive(): void public function testError(): void { $projection = new Projection( - new ProjectionId('test', 1), + 'test', ); $exception = new RuntimeException('test'); @@ -90,7 +89,7 @@ public function testError(): void public function testOutdated(): void { $projection = new Projection( - new ProjectionId('test', 1), + 'test', ); $projection->outdated(); @@ -106,7 +105,7 @@ public function testOutdated(): void public function testChangePosition(): void { $projection = new Projection( - new ProjectionId('test', 1), + 'test', ); $projection->changePosition(10); @@ -117,7 +116,7 @@ public function testChangePosition(): void public function testRetry(): void { $projection = new Projection( - new ProjectionId('test', 1), + 'test', ); self::assertEquals(0, $projection->retry()); diff --git a/tests/Unit/Projection/Projection/Store/InMemoryStoreTest.php b/tests/Unit/Projection/Projection/Store/InMemoryStoreTest.php index f4aeab614..6e4f88ac8 100644 --- a/tests/Unit/Projection/Projection/Store/InMemoryStoreTest.php +++ b/tests/Unit/Projection/Projection/Store/InMemoryStoreTest.php @@ -5,7 +5,6 @@ namespace Patchlevel\EventSourcing\Tests\Unit\Projection\Projection\Store; use Patchlevel\EventSourcing\Projection\Projection\Projection; -use Patchlevel\EventSourcing\Projection\Projection\ProjectionId; use Patchlevel\EventSourcing\Projection\Projection\ProjectionNotFound; use Patchlevel\EventSourcing\Projection\Projection\Store\InMemoryStore; use PHPUnit\Framework\TestCase; @@ -17,7 +16,7 @@ public function testSave(): void { $store = new InMemoryStore(); - $id = new ProjectionId('test', 1); + $id = 'test'; $projection = new Projection($id); $store->save($projection); @@ -34,14 +33,14 @@ public function testNotFound(): void $this->expectException(ProjectionNotFound::class); $store = new InMemoryStore(); - $store->get(new ProjectionId('test', 1)); + $store->get('test'); } public function testRemove(): void { $store = new InMemoryStore(); - $id = new ProjectionId('test', 1); + $id = 'test'; $projection = new Projection($id); $store->save($projection); diff --git a/tests/Unit/Projection/Projectionist/DefaultProjectionistTest.php b/tests/Unit/Projection/Projectionist/DefaultProjectionistTest.php index ef26d6ce5..f447002ab 100644 --- a/tests/Unit/Projection/Projectionist/DefaultProjectionistTest.php +++ b/tests/Unit/Projection/Projectionist/DefaultProjectionistTest.php @@ -10,12 +10,10 @@ use Patchlevel\EventSourcing\Projection\Projection\ProjectionCollection; use Patchlevel\EventSourcing\Projection\Projection\ProjectionCriteria; use Patchlevel\EventSourcing\Projection\Projection\ProjectionError; -use Patchlevel\EventSourcing\Projection\Projection\ProjectionId; use Patchlevel\EventSourcing\Projection\Projection\ProjectionStatus; use Patchlevel\EventSourcing\Projection\Projection\Store\ErrorContext; use Patchlevel\EventSourcing\Projection\Projection\Store\ProjectionStore; use Patchlevel\EventSourcing\Projection\Projectionist\DefaultProjectionist; -use Patchlevel\EventSourcing\Projection\Projector\ProjectorId; use Patchlevel\EventSourcing\Projection\Projector\ProjectorRepository; use Patchlevel\EventSourcing\Projection\Projector\ProjectorResolver; use Patchlevel\EventSourcing\Store\ArrayStream; @@ -60,9 +58,9 @@ public function testNothingToBoot(): void public function testBootWithoutCreateMethod(): void { - $projectionId = new ProjectionId('test', 1); - $projectorId = new ProjectorId('test', 1); - $projector = new #[ProjectionAttribute('test', 1)] + $projectionId = 'test'; + $projectorId = 'test'; + $projector = new #[ProjectionAttribute('test')] class { }; @@ -101,9 +99,9 @@ class { public function testBootWithMethods(): void { - $projectionId = new ProjectionId('test', 1); - $projectorId = new ProjectorId('test', 1); - $projector = new #[ProjectionAttribute('test', 1)] + $projectionId = 'test'; + $projectorId = 'test'; + $projector = new #[ProjectionAttribute('test')] class { public Message|null $message = null; public bool $created = false; @@ -155,9 +153,9 @@ public function handle(Message $message): void public function testBootWithLimit(): void { - $projectionId = new ProjectionId('test', 1); - $projectorId = new ProjectorId('test', 1); - $projector = new #[ProjectionAttribute('test', 1)] + $projectionId = 'test'; + $projectorId = 'test'; + $projector = new #[ProjectionAttribute('test')] class { public Message|null $message = null; public bool $created = false; @@ -208,9 +206,9 @@ public function handle(Message $message): void public function testBootingWithSkip(): void { - $projectionId1 = new ProjectionId('test1', 1); - $projectorId1 = new ProjectorId('test1', 1); - $projector1 = new #[ProjectionAttribute('test1', 1)] + $projectionId1 = 'test1'; + $projectorId1 = 'test1'; + $projector1 = new #[ProjectionAttribute('test1')] class { public Message|null $message = null; @@ -220,9 +218,9 @@ public function handle(Message $message): void } }; - $projectionId2 = new ProjectionId('test2', 1); - $projectorId2 = new ProjectorId('test2', 1); - $projector2 = new #[ProjectionAttribute('test1', 1)] + $projectionId2 = 'test2'; + $projectorId2 = 'test2'; + $projector2 = new #[ProjectionAttribute('test1')] class { public Message|null $message = null; @@ -271,9 +269,9 @@ public function handle(Message $message): void public function testBootWithCreateError(): void { - $projectionId = new ProjectionId('test', 1); - $projectorId = new ProjectorId('test', 1); - $projector = new #[ProjectionAttribute('test', 1)] + $projectionId = 'test'; + $projectorId = 'test'; + $projector = new #[ProjectionAttribute('test')] class { public function __construct( public readonly RuntimeException $exception = new RuntimeException('ERROR'), @@ -329,9 +327,9 @@ public function create(): void public function testBootingWithGabInIndex(): void { - $projectionId = new ProjectionId('test', 1); - $projectorId = new ProjectorId('test', 1); - $projector = new #[ProjectionAttribute('test', 1)] + $projectionId = 'test'; + $projectorId = 'test'; + $projector = new #[ProjectionAttribute('test')] class { /** @var list */ public array $messages = []; @@ -378,9 +376,9 @@ public function handle(Message $message): void public function testRunning(): void { - $projectionId = new ProjectionId('test', 1); - $projectorId = new ProjectorId('test', 1); - $projector = new #[ProjectionAttribute('test', 1)] + $projectionId = 'test'; + $projectorId = 'test'; + $projector = new #[ProjectionAttribute('test')] class { public Message|null $message = null; @@ -422,9 +420,9 @@ public function handle(Message $message): void public function testRunningWithLimit(): void { - $projectionId = new ProjectionId('test', 1); - $projectorId = new ProjectorId('test', 1); - $projector = new #[ProjectionAttribute('test', 1)] + $projectionId = 'test'; + $projectorId = 'test'; + $projector = new #[ProjectionAttribute('test')] class { public Message|null $message = null; @@ -470,9 +468,9 @@ public function handle(Message $message): void public function testRunningWithSkip(): void { - $projectionId1 = new ProjectionId('test1', 1); - $projectorId1 = new ProjectorId('test1', 1); - $projector1 = new #[ProjectionAttribute('test1', 1)] + $projectionId1 = 'test1'; + $projectorId1 = 'test1'; + $projector1 = new #[ProjectionAttribute('test1')] class { public Message|null $message = null; @@ -482,9 +480,9 @@ public function handle(Message $message): void } }; - $projectionId2 = new ProjectionId('test2', 1); - $projectorId2 = new ProjectorId('test2', 1); - $projector2 = new #[ProjectionAttribute('test1', 1)] + $projectionId2 = 'test2'; + $projectorId2 = 'test2'; + $projector2 = new #[ProjectionAttribute('test1')] class { public Message|null $message = null; @@ -531,9 +529,9 @@ public function handle(Message $message): void public function testRunningWithError(): void { - $projectionId = new ProjectionId('test', 1); - $projectorId = new ProjectorId('test', 1); - $projector = new #[ProjectionAttribute('test', 1)] + $projectionId = 'test'; + $projectorId = 'test'; + $projector = new #[ProjectionAttribute('test')] class { public function __construct( public readonly RuntimeException $exception = new RuntimeException('ERROR'), @@ -585,7 +583,7 @@ public function handle(Message $message): void public function testRunningMarkOutdated(): void { - $projectionId = new ProjectionId('test', 1); + $projectionId = 'test'; $projectionStore = new DummyStore([new Projection($projectionId, ProjectionStatus::Active)]); @@ -613,7 +611,7 @@ public function testRunningMarkOutdated(): void public function testRunningWithoutActiveProjectors(): void { - $projectionId = new ProjectionId('test', 1); + $projectionId = 'test'; $projectionStore = new DummyStore([new Projection($projectionId, ProjectionStatus::Booting)]); @@ -639,9 +637,9 @@ public function testRunningWithoutActiveProjectors(): void public function testRunningWithGabInIndex(): void { - $projectionId = new ProjectionId('test', 1); - $projectorId = new ProjectorId('test', 1); - $projector = new #[ProjectionAttribute('test', 1)] + $projectionId = 'test'; + $projectorId = 'test'; + $projector = new #[ProjectionAttribute('test')] class { /** @var list */ public array $messages = []; @@ -687,9 +685,9 @@ public function handle(Message $message): void public function testTeardownWithProjector(): void { - $projectionId = new ProjectionId('test', 1); - $projectorId = new ProjectorId('test', 1); - $projector = new #[ProjectionAttribute('test', 1)] + $projectionId = 'test'; + $projectorId = 'test'; + $projector = new #[ProjectionAttribute('test')] class { public Message|null $message = null; public bool $dropped = false; @@ -727,9 +725,9 @@ public function drop(): void public function testTeardownWithProjectorAndError(): void { - $projectionId = new ProjectionId('test', 1); - $projectorId = new ProjectorId('test', 1); - $projector = new #[ProjectionAttribute('test', 1)] + $projectionId = 'test'; + $projectorId = 'test'; + $projector = new #[ProjectionAttribute('test')] class { public Message|null $message = null; public bool $dropped = false; @@ -766,7 +764,7 @@ public function drop(): void public function testTeardownWithoutProjector(): void { - $projectorId = new ProjectionId('test', 1); + $projectorId = 'test'; $projectionStore = new DummyStore([new Projection($projectorId, ProjectionStatus::Outdated)]); @@ -792,9 +790,9 @@ public function testTeardownWithoutProjector(): void public function testRemoveWithProjector(): void { - $projectionId = new ProjectionId('test', 1); - $projectorId = new ProjectorId('test', 1); - $projector = new #[ProjectionAttribute('test', 1)] + $projectionId = 'test'; + $projectorId = 'test'; + $projector = new #[ProjectionAttribute('test')] class { public bool $dropped = false; @@ -831,9 +829,9 @@ public function drop(): void public function testRemoveWithoutDropMethod(): void { - $projectionId = new ProjectionId('test', 1); - $projectorId = new ProjectorId('test', 1); - $projector = new #[ProjectionAttribute('test', 1)] + $projectionId = 'test'; + $projectorId = 'test'; + $projector = new #[ProjectionAttribute('test')] class { }; @@ -863,9 +861,9 @@ class { public function testRemoveWithProjectorAndError(): void { - $projectionId = new ProjectionId('test', 1); - $projectorId = new ProjectorId('test', 1); - $projector = new #[ProjectionAttribute('test', 1)] + $projectionId = 'test'; + $projectorId = 'test'; + $projector = new #[ProjectionAttribute('test')] class { public bool $dropped = false; @@ -901,7 +899,7 @@ public function drop(): void public function testRemoveWithoutProjector(): void { - $projectorId = new ProjectionId('test', 1); + $projectorId = 'test'; $projectionStore = new DummyStore([new Projection($projectorId, ProjectionStatus::Outdated)]); @@ -927,9 +925,9 @@ public function testRemoveWithoutProjector(): void public function testReactivate(): void { - $projectionId = new ProjectionId('test', 1); - $projectorId = new ProjectorId('test', 1); - $projector = new #[ProjectionAttribute('test', 1)] + $projectionId = 'test'; + $projectorId = 'test'; + $projector = new #[ProjectionAttribute('test')] class { }; diff --git a/tests/Unit/Projection/Projector/MetadataProjectorResolverTest.php b/tests/Unit/Projection/Projector/MetadataProjectorResolverTest.php index 5251dfee8..9d3eed507 100644 --- a/tests/Unit/Projection/Projector/MetadataProjectorResolverTest.php +++ b/tests/Unit/Projection/Projector/MetadataProjectorResolverTest.php @@ -165,14 +165,12 @@ class { public function testProjectionId(): void { - $projector = new #[Projector('dummy', 1)] + $projector = new #[Projector('dummy')] class { }; $resolver = new MetadataProjectorResolver(); - $projectorId = $resolver->projectorId($projector); - self::assertEquals('dummy', $projectorId->name()); - self::assertEquals(1, $projectorId->version()); + self::assertEquals('dummy', $resolver->projectorId($projector)); } } diff --git a/tests/Unit/Projection/Projector/ProjectorHelperTest.php b/tests/Unit/Projection/Projector/ProjectorHelperTest.php index 20293bda3..738171c24 100644 --- a/tests/Unit/Projection/Projector/ProjectorHelperTest.php +++ b/tests/Unit/Projection/Projector/ProjectorHelperTest.php @@ -6,42 +6,19 @@ use Patchlevel\EventSourcing\Attribute\Projector; use Patchlevel\EventSourcing\Projection\Projector\ProjectorHelper; -use Patchlevel\EventSourcing\Projection\Projector\ProjectorId; use PHPUnit\Framework\TestCase; /** @covers \Patchlevel\EventSourcing\Projection\Projector\ProjectorHelper */ final class ProjectorHelperTest extends TestCase { - public function testProjectionName(): void - { - $projector = new #[Projector('dummy')] - class { - }; - - $helper = new ProjectorHelper(); - - self::assertSame('dummy', $helper->name($projector)); - } - - public function testProjectionVersion(): void - { - $projector = new #[Projector('dummy', 1)] - class { - }; - - $helper = new ProjectorHelper(); - - self::assertSame(1, $helper->version($projector)); - } - public function testProjectionId(): void { - $projector = new #[Projector('dummy', 1)] + $projector = new #[Projector('dummy')] class { }; $helper = new ProjectorHelper(); - self::assertEquals(new ProjectorId('dummy', 1), $helper->projectorId($projector)); + self::assertEquals('dummy', $helper->projectorId($projector)); } } diff --git a/tests/Unit/Projection/Projector/ProjectorIdTest.php b/tests/Unit/Projection/Projector/ProjectorIdTest.php deleted file mode 100644 index 3193302fc..000000000 --- a/tests/Unit/Projection/Projector/ProjectorIdTest.php +++ /dev/null @@ -1,96 +0,0 @@ -name()); - self::assertSame(1, $projectorId->version()); - self::assertSame('test-1', $projectorId->toString()); - } - - public function testEquals(): void - { - $a = new ProjectorId( - 'test', - 1, - ); - - $b = new ProjectorId( - 'test', - 1, - ); - - $c = new ProjectorId( - 'foo', - 1, - ); - - $d = new ProjectorId( - 'test', - 2, - ); - - self::assertTrue($a->equals($b)); - self::assertFalse($a->equals($c)); - self::assertFalse($a->equals($d)); - } - - /** @dataProvider validFromStringProvider */ - public function testValidFromString(string $string, string $name, int $version): void - { - $projectorId = ProjectorId::fromString($string); - - self::assertSame($name, $projectorId->name()); - self::assertSame($version, $projectorId->version()); - self::assertSame($string, $projectorId->toString()); - } - - /** @return Generator */ - public static function validFromStringProvider(): Generator - { - yield ['hotel-1', 'hotel', 1]; - yield ['hotel-bar-1', 'hotel-bar', 1]; - yield ['hotel-bar--1', 'hotel-bar-', 1]; - } - - /** @dataProvider invalidFromStringProvider */ - public function testInvalidFromString(string $string): void - { - $this->expectException(InvalidArgumentException::class); - - ProjectorId::fromString($string); - } - - /** @return Generator */ - public static function invalidFromStringProvider(): Generator - { - yield ['hotel']; - yield ['hotel-bar']; - } - - public function testToProjectionId(): void - { - $projectorId = new ProjectorId('test', 1); - $projectionId = $projectorId->toProjectionId(); - - self::assertSame($projectorId->name(), $projectionId->name()); - self::assertSame($projectorId->version(), $projectionId->version()); - self::assertSame($projectorId->toString(), $projectionId->toString()); - } -}