Skip to content

Commit 55b8630

Browse files
committed
Fix: Add missing event name filter in SQL statements
Even though events which are unknown (do not have an apply* method in the domain context) will just be ignored, it should not load those, the event names are input to be used to filter on :)
1 parent bedcddf commit 55b8630

File tree

4 files changed

+72
-77
lines changed

4 files changed

+72
-77
lines changed

composer.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@
2828
"require-dev": {
2929
"captainhook/captainhook": "^5.23",
3030
"friendsofphp/php-cs-fixer": "^3.58",
31-
"mockery/mockery": "^1.6",
32-
"phpstan/extension-installer": "^1.4",
3331
"phpstan/phpstan": "^2.1",
34-
"phpstan/phpstan-mockery": "^2.0",
3532
"phpunit/phpunit": "^12.1",
3633
"rector/rector": "^2.0",
3734
"rregeer/phpunit-coverage-check": "^0.3.1",
@@ -50,10 +47,7 @@
5047
}
5148
},
5249
"config": {
53-
"sort-packages": true,
54-
"allow-plugins": {
55-
"phpstan/extension-installer": true
56-
}
50+
"sort-packages": true
5751
},
5852
"scripts": {
5953
"coverage": "vendor/bin/coverage-check var/coverage/clover.xml 65",

src/DoctrineDbalRdbmsEventStoreRepository.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ public function getEvents(array $domainIds, array $eventNames): array
5555
$eventStoreSchema->eventIdFieldName,
5656
$eventStoreRelationSchema->eventIdFieldName,
5757
))
58+
->where(sprintf('es.%s IN(:eventNames)', $eventStoreSchema->eventNameFieldName))
5859
->andWhere(sprintf('esr.%s IN(:domainIds)', $eventStoreRelationSchema->domainIdFieldName))
60+
->setParameter('eventNames', $eventNames, ArrayParameterType::STRING)
5961
->setParameter('domainIds', $domainIds, ArrayParameterType::STRING)
6062
->orderBy(sprintf('es.%s', $eventStoreSchema->appliedAtFieldName), 'asc')
6163
->executeQuery()
@@ -91,7 +93,9 @@ public function getLastEventIdPersisted(array $domainIds, array $eventNames): ?s
9193
$eventStoreSchema->eventIdFieldName,
9294
$eventStoreRelationSchema->eventIdFieldName,
9395
))
96+
->where(sprintf('es.%s IN(:eventNames)', $eventStoreSchema->eventNameFieldName))
9497
->andWhere(sprintf('esr.%s IN(:domainIds)', $eventStoreRelationSchema->domainIdFieldName))
98+
->setParameter('eventNames', $eventNames, ArrayParameterType::STRING)
9599
->setParameter('domainIds', $domainIds, ArrayParameterType::STRING)
96100
->orderBy(sprintf('es.%s', $eventStoreSchema->appliedAtFieldName), 'desc')
97101
->setMaxResults(1)

tests/DoctrineDbalRdbmsEventStoreRepositoryTest.php

Lines changed: 55 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@
44

55
namespace Gember\RdbmsEventStoreDoctrineDbal\Test;
66

7-
use Doctrine\DBAL\Connection;
8-
use Doctrine\DBAL\Query\QueryBuilder;
9-
use Doctrine\DBAL\Result;
7+
use Doctrine\DBAL\DriverManager;
8+
use Doctrine\DBAL\Tools\DsnParser;
109
use Gember\EventSourcing\EventStore\Rdbms\RdbmsEvent;
1110
use Gember\RdbmsEventStoreDoctrineDbal\DoctrineDbalRdbmsEventFactory;
1211
use Gember\RdbmsEventStoreDoctrineDbal\DoctrineDbalRdbmsEventStoreRepository;
1312
use Gember\RdbmsEventStoreDoctrineDbal\TableSchema\TableSchemaFactory;
14-
use Mockery;
15-
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
16-
use Mockery\MockInterface;
1713
use PHPUnit\Framework\Attributes\Test;
1814
use PHPUnit\Framework\TestCase;
1915
use Override;
@@ -24,73 +20,66 @@
2420
*/
2521
final class DoctrineDbalRdbmsEventStoreRepositoryTest extends TestCase
2622
{
27-
use MockeryPHPUnitIntegration;
28-
29-
private MockInterface&Result $result;
3023
private DoctrineDbalRdbmsEventStoreRepository $repository;
3124

3225
#[Override]
3326
protected function setUp(): void
3427
{
3528
parent::setUp();
3629

37-
$connection = Mockery::mock(Connection::class);
38-
$queryBuilder = Mockery::mock(QueryBuilder::class);
39-
40-
$this->result = Mockery::mock(Result::class);
41-
42-
$connection->allows('createQueryBuilder')->andReturn($queryBuilder);
43-
$queryBuilder->allows('select')->andReturn($queryBuilder);
44-
$queryBuilder->allows('from')->andReturn($queryBuilder);
45-
$queryBuilder->allows('join')->andReturn($queryBuilder);
46-
$queryBuilder->allows('andWhere')->andReturn($queryBuilder);
47-
$queryBuilder->allows('setParameter')->andReturn($queryBuilder);
48-
$queryBuilder->allows('orderBy')->andReturn($queryBuilder);
49-
$queryBuilder->allows('setMaxResults')->andReturn($queryBuilder);
50-
$queryBuilder->allows('executeQuery')->andReturn($this->result);
30+
$connection = DriverManager::getConnection((new DsnParser())->parse('pdo-sqlite:///:memory:'));
31+
$connection->executeStatement((string) file_get_contents(__DIR__ . '/schema.sql'));
5132

5233
$this->repository = new DoctrineDbalRdbmsEventStoreRepository(
5334
$connection,
5435
TableSchemaFactory::createDefaultEventStore(),
5536
TableSchemaFactory::createDefaultEventStoreRelation(),
5637
new DoctrineDbalRdbmsEventFactory(),
5738
);
39+
40+
$this->repository->saveEvents([
41+
new RdbmsEvent(
42+
'63129dc3-4a27-4242-a8bc-6f79636a6fa9',
43+
['6ae07469-0f43-4f33-979b-c783b6824ce0', '0c1ff409-a4be-42f1-90dd-5d7b0130a426'],
44+
'event_name',
45+
'{"data":"some"}',
46+
['metadata' => 'some'],
47+
new DateTimeImmutable('2024-12-06 12:05:04.456344'),
48+
),
49+
new RdbmsEvent(
50+
'707678d3-c91d-4864-9729-555b22496853',
51+
['0e76f2bd-2aae-44a4-b149-740c080e4d05'],
52+
'event_name',
53+
'{"data":"another_event"}',
54+
['metadata' => 'another_event'],
55+
new DateTimeImmutable('2024-12-01 13:16:24.467784'),
56+
),
57+
new RdbmsEvent(
58+
'7ac51abe-9176-4794-8246-24b75c2ba914',
59+
['0c1ff409-a4be-42f1-90dd-5d7b0130a426'],
60+
'event_name_2',
61+
'{"data":"another"}',
62+
['metadata' => 'another'],
63+
new DateTimeImmutable('2024-12-04 13:15:26.755844'),
64+
),
65+
new RdbmsEvent(
66+
'd404e3c1-c782-4115-b8ec-d8cb341d87cb',
67+
['6ae07469-0f43-4f33-979b-c783b6824ce0'],
68+
'event_name_3',
69+
'{"data":"another"}',
70+
['metadata' => 'another3'],
71+
new DateTimeImmutable('2024-12-02 13:16:24.467784'),
72+
),
73+
]);
5874
}
5975

6076
#[Test]
6177
public function itShouldGetEvents(): void
6278
{
63-
$this->result->expects('fetchAllAssociative')->andReturn([
64-
[
65-
'eventId' => '63129dc3-4a27-4242-a8bc-6f79636a6fa9',
66-
'eventName'=> 'event_name',
67-
'payload' => '{"data":"some"}',
68-
'metadata' => '{"metadata":"some"}',
69-
'appliedAt' => '2024-12-03 12:05:04.456344',
70-
'domainId' => '6ae07469-0f43-4f33-979b-c783b6824ce0',
71-
],
72-
[
73-
'eventId' => '63129dc3-4a27-4242-a8bc-6f79636a6fa9',
74-
'eventName'=> 'event_name',
75-
'payload' => '{"data":"some"}',
76-
'metadata' => '{"metadata":"some"}',
77-
'appliedAt' => '2024-12-03 12:05:04.456344',
78-
'domainId' => '0c1ff409-a4be-42f1-90dd-5d7b0130a426',
79-
],
80-
[
81-
'eventId' => '7ac51abe-9176-4794-8246-24b75c2ba914',
82-
'eventName'=> 'event_name_2',
83-
'payload' => '{"data":"another"}',
84-
'metadata' => '{"metadata":"another"}',
85-
'appliedAt' => '2024-12-04 13:15:26.755844',
86-
'domainId' => '6ae07469-0f43-4f33-979b-c783b6824ce0',
87-
],
88-
]);
89-
9079
$events = $this->repository->getEvents(
9180
[
92-
'072fd355-bd4e-423b-a7ba-fb1a77e32d7c',
93-
'60a8635c-c769-4d19-8cae-a9571401848f',
81+
'0c1ff409-a4be-42f1-90dd-5d7b0130a426',
82+
'6ae07469-0f43-4f33-979b-c783b6824ce0',
9483
],
9584
[
9685
'event_name',
@@ -100,34 +89,32 @@ public function itShouldGetEvents(): void
10089

10190
self::assertEquals([
10291
new RdbmsEvent(
103-
'63129dc3-4a27-4242-a8bc-6f79636a6fa9',
92+
'7ac51abe-9176-4794-8246-24b75c2ba914',
10493
[
105-
'6ae07469-0f43-4f33-979b-c783b6824ce0',
10694
'0c1ff409-a4be-42f1-90dd-5d7b0130a426',
10795
],
108-
'event_name',
109-
'{"data":"some"}',
110-
['metadata' => 'some'],
111-
new DateTimeImmutable('2024-12-03 12:05:04.456344'),
96+
'event_name_2',
97+
'{"data":"another"}',
98+
['metadata' => 'another'],
99+
new DateTimeImmutable('2024-12-04 13:15:26.755844'),
112100
),
113101
new RdbmsEvent(
114-
'7ac51abe-9176-4794-8246-24b75c2ba914',
102+
'63129dc3-4a27-4242-a8bc-6f79636a6fa9',
115103
[
104+
'0c1ff409-a4be-42f1-90dd-5d7b0130a426',
116105
'6ae07469-0f43-4f33-979b-c783b6824ce0',
117106
],
118-
'event_name_2',
119-
'{"data":"another"}',
120-
['metadata' => 'another'],
121-
new DateTimeImmutable('2024-12-04 13:15:26.755844'),
107+
'event_name',
108+
'{"data":"some"}',
109+
['metadata' => 'some'],
110+
new DateTimeImmutable('2024-12-06 12:05:04.456344'),
122111
),
123112
], $events);
124113
}
125114

126115
#[Test]
127116
public function itShouldReturnNullWhenGetLastEventIdPersistedIsNotFound(): void
128117
{
129-
$this->result->expects('fetchFirstColumn')->andReturn([]);
130-
131118
$lastEventIdPersisted = $this->repository->getLastEventIdPersisted(
132119
[
133120
'072fd355-bd4e-423b-a7ba-fb1a77e32d7c',
@@ -145,19 +132,17 @@ public function itShouldReturnNullWhenGetLastEventIdPersistedIsNotFound(): void
145132
#[Test]
146133
public function itShouldGetLastEventIdPersisted(): void
147134
{
148-
$this->result->expects('fetchFirstColumn')->andReturn(['id' => 'dc99db45-1d1f-4d9d-b52a-83b1cabad89d']);
149-
150135
$lastEventIdPersisted = $this->repository->getLastEventIdPersisted(
151136
[
152-
'072fd355-bd4e-423b-a7ba-fb1a77e32d7c',
153-
'60a8635c-c769-4d19-8cae-a9571401848f',
137+
'0c1ff409-a4be-42f1-90dd-5d7b0130a426',
138+
'6ae07469-0f43-4f33-979b-c783b6824ce0',
154139
],
155140
[
156141
'event_name',
157142
'event_name_2',
158143
],
159144
);
160145

161-
self::assertSame('dc99db45-1d1f-4d9d-b52a-83b1cabad89d', $lastEventIdPersisted);
146+
self::assertSame('63129dc3-4a27-4242-a8bc-6f79636a6fa9', $lastEventIdPersisted);
162147
}
163148
}

tests/schema.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CREATE TABLE `event_store` (
2+
`id` varchar(50) NOT NULL,
3+
`event_name` varchar(100) NOT NULL,
4+
`payload` json NOT NULL,
5+
`metadata` json NOT NULL,
6+
`applied_at` timestamp(6) NOT NULL
7+
);
8+
9+
CREATE TABLE `event_store_relation` (
10+
`event_id` varchar(50) NOT NULL,
11+
`domain_id` varchar(50) NOT NULL
12+
);

0 commit comments

Comments
 (0)