1212namespace Symfony \AI \Chat \Tests \Bridge \Doctrine ;
1313
1414use Doctrine \DBAL \Connection ;
15+ use Doctrine \DBAL \Query \QueryBuilder ;
16+ use Doctrine \DBAL \Result ;
1517use Doctrine \DBAL \Schema \Schema ;
1618use Doctrine \DBAL \Schema \SQLiteSchemaManager ;
1719use Doctrine \DBAL \Schema \Table ;
18- use Doctrine \Persistence \ConnectionRegistry ;
1920use PHPUnit \Framework \TestCase ;
20- use Symfony \AI \Chat \Bridge \Doctrine \OrmMessageStore ;
21+ use Symfony \AI \Chat \Bridge \Doctrine \DoctrineDbalMessageStore ;
2122use Symfony \AI \Chat \Exception \InvalidArgumentException ;
22-
23- final class OrmMessageStoreTest extends TestCase
23+ use Symfony \AI \Chat \MessageNormalizer ;
24+ use Symfony \AI \Platform \Message \Message ;
25+ use Symfony \AI \Platform \Message \MessageBag ;
26+ use Symfony \Component \Serializer \Encoder \JsonEncoder ;
27+ use Symfony \Component \Serializer \Normalizer \ArrayDenormalizer ;
28+ use Symfony \Component \Serializer \Serializer ;
29+
30+ final class DoctrineDbalMessageStoreTest extends TestCase
2431{
2532 public function testMessageStoreTableCannotBeSetupWithExtraOptions ()
2633 {
2734 $ connection = $ this ->createMock (Connection::class);
2835
29- $ registry = $ this ->createMock (ConnectionRegistry::class);
30- $ registry ->expects ($ this ->once ())->method ('getConnection ' )->willReturn ($ connection );
31-
32- $ messageStore = new OrmMessageStore ('foo ' , 'bar ' , $ registry );
36+ $ messageStore = new DoctrineDbalMessageStore ('foo ' , $ connection );
3337
3438 $ this ->expectException (InvalidArgumentException::class);
3539 $ this ->expectExceptionMessage ('No supported options. ' );
@@ -50,77 +54,119 @@ public function testMessageStoreTableCannotBeSetupIfItAlreadyExist()
5054 $ connection = $ this ->createMock (Connection::class);
5155 $ connection ->expects ($ this ->once ())->method ('createSchemaManager ' )->willReturn ($ sqliteSchemaManager );
5256
53- $ registry = $ this ->createMock (ConnectionRegistry::class);
54- $ registry ->expects ($ this ->once ())->method ('getConnection ' )->willReturn ($ connection );
55-
56- $ messageStore = new OrmMessageStore ('foo ' , 'bar ' , $ registry );
57+ $ messageStore = new DoctrineDbalMessageStore ('foo ' , $ connection );
5758 $ messageStore ->setup ();
5859 }
5960
6061 public function testMessageStoreTableCanBeSetup ()
6162 {
6263 $ table = $ this ->createMock (Table::class);
64+ $ table ->expects ($ this ->once ())->method ('addOption ' )
65+ ->with ('_symfony_ai_chat_table_name ' , 'foo ' )
66+ ->willReturnSelf ();
6367
6468 $ schema = $ this ->createMock (Schema::class);
6569 $ schema ->expects ($ this ->once ())->method ('hasTable ' )->willReturn (false );
66- $ schema ->expects ($ this ->once ())->method ('createTable ' )->with ('bar ' )->willReturn ($ table );
70+ $ schema ->expects ($ this ->once ())->method ('createTable ' )->with ('foo ' )->willReturn ($ table );
6771
6872 $ sqliteSchemaManager = $ this ->createMock (SQLiteSchemaManager::class);
6973 $ sqliteSchemaManager ->expects ($ this ->once ())->method ('introspectSchema ' )->willReturn ($ schema );
7074
7175 $ connection = $ this ->createMock (Connection::class);
7276 $ connection ->expects ($ this ->once ())->method ('createSchemaManager ' )->willReturn ($ sqliteSchemaManager );
7377
74- $ registry = $ this ->createMock (ConnectionRegistry::class);
75- $ registry ->expects ($ this ->once ())->method ('getConnection ' )->willReturn ($ connection );
76-
77- $ messageStore = new OrmMessageStore ('foo ' , 'bar ' , $ registry );
78+ $ messageStore = new DoctrineDbalMessageStore ('foo ' , $ connection );
7879 $ messageStore ->setup ();
7980 }
8081
8182 public function testMessageStoreTableCannotBeDroppedIfTableDoesNotExist ()
8283 {
84+ $ queryBuilder = $ this ->createMock (QueryBuilder::class);
85+ $ queryBuilder ->expects ($ this ->never ())->method ('delete ' );
86+
8387 $ schema = $ this ->createMock (Schema::class);
8488 $ schema ->expects ($ this ->once ())->method ('hasTable ' )->willReturn (false );
85- $ schema ->expects ($ this ->never ())->method ('dropTable ' );
8689
8790 $ sqliteSchemaManager = $ this ->createMock (SQLiteSchemaManager::class);
8891 $ sqliteSchemaManager ->expects ($ this ->once ())->method ('introspectSchema ' )->willReturn ($ schema );
8992
9093 $ connection = $ this ->createMock (Connection::class);
9194 $ connection ->expects ($ this ->once ())->method ('createSchemaManager ' )->willReturn ($ sqliteSchemaManager );
95+ $ connection ->expects ($ this ->never ())->method ('createQueryBuilder ' );
96+ $ connection ->expects ($ this ->never ())->method ('transactional ' );
9297
93- $ registry = $ this ->createMock (ConnectionRegistry::class);
94- $ registry ->expects ($ this ->once ())->method ('getConnection ' )->willReturn ($ connection );
95-
96- $ messageStore = new OrmMessageStore ('foo ' , 'bar ' , $ registry );
98+ $ messageStore = new DoctrineDbalMessageStore ('foo ' , $ connection );
9799 $ messageStore ->drop ();
98100 }
99101
100102 public function testMessageStoreTableCanBeDropped ()
101103 {
104+ $ queryBuilder = $ this ->createMock (QueryBuilder::class);
105+ $ queryBuilder ->expects ($ this ->once ())->method ('delete ' )->with ('foo ' );
106+
102107 $ schema = $ this ->createMock (Schema::class);
103108 $ schema ->expects ($ this ->once ())->method ('hasTable ' )->willReturn (true );
104- $ schema ->expects ($ this ->once ())->method ('dropTable ' )->with ('bar ' );
105109
106110 $ sqliteSchemaManager = $ this ->createMock (SQLiteSchemaManager::class);
107111 $ sqliteSchemaManager ->expects ($ this ->once ())->method ('introspectSchema ' )->willReturn ($ schema );
108112
109113 $ connection = $ this ->createMock (Connection::class);
110114 $ connection ->expects ($ this ->once ())->method ('createSchemaManager ' )->willReturn ($ sqliteSchemaManager );
115+ $ connection ->expects ($ this ->once ())->method ('createQueryBuilder ' )->willReturn ($ queryBuilder );
116+ $ connection ->expects ($ this ->once ())->method ('transactional ' );
111117
112- $ registry = $ this ->createMock (ConnectionRegistry::class);
113- $ registry ->expects ($ this ->once ())->method ('getConnection ' )->willReturn ($ connection );
114-
115- $ messageStore = new OrmMessageStore ('foo ' , 'bar ' , $ registry );
118+ $ messageStore = new DoctrineDbalMessageStore ('foo ' , $ connection );
116119 $ messageStore ->drop ();
117120 }
118121
119122 public function testMessageBagCanBeSaved ()
120123 {
124+ $ queryBuilder = $ this ->createMock (QueryBuilder::class);
125+ $ queryBuilder ->expects ($ this ->once ())->method ('insert ' )->with ('foo ' )->willReturnSelf ();
126+ $ queryBuilder ->expects ($ this ->once ())->method ('values ' )->with ([
127+ 'messages ' => '? ' ,
128+ ])->willReturnSelf ();
129+
130+ $ connection = $ this ->createMock (Connection::class);
131+ $ connection ->expects ($ this ->once ())->method ('createQueryBuilder ' )->willReturn ($ queryBuilder );
132+ $ connection ->expects ($ this ->once ())->method ('transactional ' );
133+
134+ $ messageStore = new DoctrineDbalMessageStore ('foo ' , $ connection );
135+ $ messageStore ->save (new MessageBag (
136+ Message::ofUser ('Hello world ' ),
137+ ));
121138 }
122139
123140 public function testMessageBagCanBeLoaded ()
124141 {
142+ $ serializer = new Serializer ([
143+ new ArrayDenormalizer (),
144+ new MessageNormalizer (),
145+ ], [new JsonEncoder ()]);
146+
147+ $ messageBag = new MessageBag (
148+ Message::ofUser ('Hello world ' ),
149+ );
150+
151+ $ result = $ this ->createMock (Result::class);
152+ $ result ->expects ($ this ->once ())->method ('fetchAllAssociative ' )->willReturn ([
153+ [
154+ 'messages ' => $ serializer ->serialize ($ messageBag ->getMessages (), 'json ' ),
155+ ],
156+ ]);
157+
158+ $ queryBuilder = $ this ->createMock (QueryBuilder::class);
159+ $ queryBuilder ->expects ($ this ->once ())->method ('select ' )->with ('messages ' )->willReturnSelf ();
160+ $ queryBuilder ->expects ($ this ->once ())->method ('from ' )->with ('foo ' )->willReturnSelf ();
161+
162+ $ connection = $ this ->createMock (Connection::class);
163+ $ connection ->expects ($ this ->once ())->method ('createQueryBuilder ' )->willReturn ($ queryBuilder );
164+ $ connection ->expects ($ this ->once ())->method ('transactional ' )->willReturn ($ result );
165+
166+ $ messageStore = new DoctrineDbalMessageStore ('foo ' , $ connection , $ serializer );
167+
168+ $ messages = $ messageStore ->load ();
169+
170+ $ this ->assertCount (1 , $ messages );
125171 }
126172}
0 commit comments