44
55namespace 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 ;
109use Gember \EventSourcing \EventStore \Rdbms \RdbmsEvent ;
1110use Gember \RdbmsEventStoreDoctrineDbal \DoctrineDbalRdbmsEventFactory ;
1211use Gember \RdbmsEventStoreDoctrineDbal \DoctrineDbalRdbmsEventStoreRepository ;
1312use Gember \RdbmsEventStoreDoctrineDbal \TableSchema \TableSchemaFactory ;
14- use Mockery ;
15- use Mockery \Adapter \Phpunit \MockeryPHPUnitIntegration ;
16- use Mockery \MockInterface ;
1713use PHPUnit \Framework \Attributes \Test ;
1814use PHPUnit \Framework \TestCase ;
1915use Override ;
2420 */
2521final 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}
0 commit comments