Skip to content

Commit

Permalink
allow multiple wildcarts
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Jan 15, 2025
1 parent fedc793 commit 5483417
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 49 deletions.
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,10 +4,6 @@

namespace Patchlevel\EventSourcing\Store\Criteria;

use Patchlevel\EventSourcing\Store\InvalidStreamName;

use function preg_match;

final class StreamCriterion
{
/** @var list<string> */
Expand All @@ -16,12 +12,6 @@ final class StreamCriterion
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 @@ private function applyCriteria(QueryBuilder $builder, Criteria $criteria): void
$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
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

0 comments on commit 5483417

Please sign in to comment.