Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow multiple wildcards in stream store #675

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,14 @@
<code><![CDATA[$row['payload']]]></code>
</MixedArgument>
</file>
<file src="tests/Integration/Store/StreamDoctrineDbalStoreTest.php">
<RedundantCondition>
<code><![CDATA[$stream?->close()]]></code>
</RedundantCondition>
<TypeDoesNotContainNull>
<code><![CDATA[$stream]]></code>
</TypeDoesNotContainNull>
</file>
<file src="tests/Unit/CommandBus/AggregateHandlerProviderTest.php">
<InvalidArrayAccess>
<code><![CDATA[$result[0]]]></code>
Expand Down
2 changes: 1 addition & 1 deletion src/Message/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use function is_a;

/**
* @template-covariant T of object
* @template-covariant T of object = object
* @psalm-immutable
*/
final class Message
Expand Down
10 changes: 0 additions & 10 deletions src/Store/Criteria/StreamCriterion.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,14 @@

namespace Patchlevel\EventSourcing\Store\Criteria;

use Patchlevel\EventSourcing\Store\InvalidStreamName;

use function preg_match;

final class StreamCriterion
{
/** @var list<string> */
public readonly array $streamName;

Check failure on line 10 in src/Store/Criteria/StreamCriterion.php

View workflow job for this annotation

GitHub Actions / Backward Compatibility Check (locked, 8.2, ubuntu-latest)

Type of property Patchlevel\EventSourcing\Store\Criteria\StreamCriterion#$streamName changed from string to array

public function __construct(
string ...$streamName,
) {
foreach ($streamName as $name) {
if (!preg_match('/^[^*]*\*?$/', $name)) {
throw new InvalidStreamName($name);
}
}

$this->streamName = $streamName;
}

Expand Down
8 changes: 4 additions & 4 deletions src/Store/StreamDoctrineDbalStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
use function in_array;
use function is_int;
use function is_string;
use function mb_substr;
use function sprintf;
use function str_ends_with;
use function str_contains;
use function str_replace;

final class StreamDoctrineDbalStore implements StreamStore, SubscriptionStore, DoctrineSchemaConfigurator
{
Expand Down Expand Up @@ -159,9 +159,9 @@
$streamFilters = [];

foreach ($criterion->streamName as $index => $streamName) {
if (str_ends_with($streamName, '*')) {
if (str_contains($streamName, '*')) {
$streamFilters[] = 'stream LIKE :stream_' . $index;
$builder->setParameter('stream_' . $index, mb_substr($streamName, 0, -1) . '%');
$builder->setParameter('stream_' . $index, str_replace('*', '%', $streamName));
} else {
$streamFilters[] = 'stream = :stream_' . $index;
$builder->setParameter('stream_' . $index, $streamName);
Expand Down Expand Up @@ -339,7 +339,7 @@
return $streams;
}

public function remove(Criteria|null $criteria = null): void

Check failure on line 342 in src/Store/StreamDoctrineDbalStore.php

View workflow job for this annotation

GitHub Actions / Backward Compatibility Check (locked, 8.2, ubuntu-latest)

The parameter $streamName of Patchlevel\EventSourcing\Store\StreamDoctrineDbalStore#remove() changed from string to a non-contravariant Patchlevel\EventSourcing\Store\Criteria\Criteria|null
{
$builder = $this->connection->createQueryBuilder();

Expand Down
10 changes: 10 additions & 0 deletions tests/Integration/Store/StreamDoctrineDbalStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,16 @@ public function testLoadWithWildcard(): void
} finally {
$stream?->close();
}

try {
$stream = $this->store->load(new Criteria(new StreamCriterion('*-*')));

$messages = iterator_to_array($stream);

self::assertCount(2, $messages);
} finally {
$stream?->close();
}
}

public function testStreams(): void
Expand Down
34 changes: 0 additions & 34 deletions tests/Unit/Store/StreamDoctrineDbalStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
use Patchlevel\EventSourcing\Store\Header\PlayheadHeader;
use Patchlevel\EventSourcing\Store\Header\RecordedOnHeader;
use Patchlevel\EventSourcing\Store\Header\StreamNameHeader;
use Patchlevel\EventSourcing\Store\InvalidStreamName;
use Patchlevel\EventSourcing\Store\MissingDataForStorage;
use Patchlevel\EventSourcing\Store\StreamDoctrineDbalStore;
use Patchlevel\EventSourcing\Store\UniqueConstraintViolation;
Expand Down Expand Up @@ -356,39 +355,6 @@ public function testLoadWithLikeAll(): void
self::assertSame(null, $stream->position());
}

public function testLoadWithLikeInvalid(): void
{
$connection = $this->prophesize(Connection::class);

$abstractPlatform = $this->prophesize(AbstractPlatform::class);

$connection->getDatabasePlatform()->willReturn($abstractPlatform->reveal());
$queryBuilder = new QueryBuilder($connection->reveal());
$connection->createQueryBuilder()->willReturn($queryBuilder);

$eventSerializer = $this->prophesize(EventSerializer::class);
$headersSerializer = $this->prophesize(HeadersSerializer::class);

$doctrineDbalStore = new StreamDoctrineDbalStore(
$connection->reveal(),
$eventSerializer->reveal(),
$headersSerializer->reveal(),
);

$this->expectException(InvalidStreamName::class);

$stream = $doctrineDbalStore->load(
(new CriteriaBuilder())
->streamName('*-*')
->fromPlayhead(0)
->archived(false)
->build(),
);

self::assertSame(null, $stream->index());
self::assertSame(null, $stream->position());
}

public function testLoadMultipleStream(): void
{
$connection = $this->prophesize(Connection::class);
Expand Down
Loading