@@ -6363,4 +6363,190 @@ public function testValidationGuardsWithNullRequired(): void
63636363 // Cleanup
63646364 $ database ->deleteCollection ($ collection );
63656365 }
6366+
6367+ public function testUpsertWithJSONFilters (): void
6368+ {
6369+ $ database = static ::getDatabase ();
6370+
6371+ if (!$ database ->getAdapter ()->getSupportForAttributes ()) {
6372+ $ this ->expectNotToPerformAssertions ();
6373+ return ;
6374+ }
6375+
6376+ // Create collection with JSON filter attribute
6377+ $ collection = ID ::unique ();
6378+ $ database ->createCollection ($ collection , permissions: [
6379+ Permission::read (Role::any ()),
6380+ Permission::create (Role::any ()),
6381+ Permission::update (Role::any ()),
6382+ Permission::delete (Role::any ()),
6383+ ]);
6384+
6385+ $ database ->createAttribute ($ collection , 'name ' , Database::VAR_STRING , 128 , true );
6386+ $ database ->createAttribute ($ collection , 'metadata ' , Database::VAR_STRING , 4000 , true , filters: ['json ' ]);
6387+
6388+ $ permissions = [
6389+ Permission::read (Role::any ()),
6390+ Permission::create (Role::any ()),
6391+ Permission::update (Role::any ()),
6392+ Permission::delete (Role::any ()),
6393+ ];
6394+
6395+ // Test 1: Insertion (createDocument) with JSON filter
6396+ $ docId1 = 'json-doc-1 ' ;
6397+ $ initialMetadata = [
6398+ 'version ' => '1.0.0 ' ,
6399+ 'tags ' => ['php ' , 'database ' ],
6400+ 'config ' => [
6401+ 'debug ' => false ,
6402+ 'timeout ' => 30
6403+ ]
6404+ ];
6405+
6406+ $ document1 = $ database ->createDocument ($ collection , new Document ([
6407+ '$id ' => $ docId1 ,
6408+ 'name ' => 'Initial Document ' ,
6409+ 'metadata ' => $ initialMetadata ,
6410+ '$permissions ' => $ permissions ,
6411+ ]));
6412+
6413+ $ this ->assertEquals ($ docId1 , $ document1 ->getId ());
6414+ $ this ->assertEquals ('Initial Document ' , $ document1 ->getAttribute ('name ' ));
6415+ $ this ->assertIsArray ($ document1 ->getAttribute ('metadata ' ));
6416+ $ this ->assertEquals ('1.0.0 ' , $ document1 ->getAttribute ('metadata ' )['version ' ]);
6417+ $ this ->assertEquals (['php ' , 'database ' ], $ document1 ->getAttribute ('metadata ' )['tags ' ]);
6418+
6419+ // Test 2: Update (updateDocument) with modified JSON filter
6420+ $ updatedMetadata = [
6421+ 'version ' => '2.0.0 ' ,
6422+ 'tags ' => ['php ' , 'database ' , 'json ' ],
6423+ 'config ' => [
6424+ 'debug ' => true ,
6425+ 'timeout ' => 60 ,
6426+ 'cache ' => true
6427+ ],
6428+ 'updated ' => true
6429+ ];
6430+
6431+ $ document1 ->setAttribute ('name ' , 'Updated Document ' );
6432+ $ document1 ->setAttribute ('metadata ' , $ updatedMetadata );
6433+
6434+ $ updatedDoc = $ database ->updateDocument ($ collection , $ docId1 , $ document1 );
6435+
6436+ $ this ->assertEquals ($ docId1 , $ updatedDoc ->getId ());
6437+ $ this ->assertEquals ('Updated Document ' , $ updatedDoc ->getAttribute ('name ' ));
6438+ $ this ->assertIsArray ($ updatedDoc ->getAttribute ('metadata ' ));
6439+ $ this ->assertEquals ('2.0.0 ' , $ updatedDoc ->getAttribute ('metadata ' )['version ' ]);
6440+ $ this ->assertEquals (['php ' , 'database ' , 'json ' ], $ updatedDoc ->getAttribute ('metadata ' )['tags ' ]);
6441+ $ this ->assertTrue ($ updatedDoc ->getAttribute ('metadata ' )['config ' ]['debug ' ]);
6442+ $ this ->assertTrue ($ updatedDoc ->getAttribute ('metadata ' )['updated ' ]);
6443+
6444+ // Test 3: Upsert - Create new document (upsertDocument)
6445+ $ docId2 = 'json-doc-2 ' ;
6446+ $ newMetadata = [
6447+ 'version ' => '1.5.0 ' ,
6448+ 'tags ' => ['javascript ' , 'node ' ],
6449+ 'config ' => [
6450+ 'debug ' => false ,
6451+ 'timeout ' => 45
6452+ ]
6453+ ];
6454+
6455+ $ document2 = new Document ([
6456+ '$id ' => $ docId2 ,
6457+ 'name ' => 'New Upsert Document ' ,
6458+ 'metadata ' => $ newMetadata ,
6459+ '$permissions ' => $ permissions ,
6460+ ]);
6461+
6462+ $ upsertedDoc = $ database ->upsertDocument ($ collection , $ document2 );
6463+
6464+ $ this ->assertEquals ($ docId2 , $ upsertedDoc ->getId ());
6465+ $ this ->assertEquals ('New Upsert Document ' , $ upsertedDoc ->getAttribute ('name ' ));
6466+ $ this ->assertIsArray ($ upsertedDoc ->getAttribute ('metadata ' ));
6467+ $ this ->assertEquals ('1.5.0 ' , $ upsertedDoc ->getAttribute ('metadata ' )['version ' ]);
6468+
6469+ // Test 4: Upsert - Update existing document (upsertDocument)
6470+ $ document2 ->setAttribute ('name ' , 'Updated Upsert Document ' );
6471+ $ document2 ->setAttribute ('metadata ' , [
6472+ 'version ' => '2.5.0 ' ,
6473+ 'tags ' => ['javascript ' , 'node ' , 'typescript ' ],
6474+ 'config ' => [
6475+ 'debug ' => true ,
6476+ 'timeout ' => 90
6477+ ],
6478+ 'migrated ' => true
6479+ ]);
6480+
6481+ $ upsertedDoc2 = $ database ->upsertDocument ($ collection , $ document2 );
6482+
6483+ $ this ->assertEquals ($ docId2 , $ upsertedDoc2 ->getId ());
6484+ $ this ->assertEquals ('Updated Upsert Document ' , $ upsertedDoc2 ->getAttribute ('name ' ));
6485+ $ this ->assertIsArray ($ upsertedDoc2 ->getAttribute ('metadata ' ));
6486+ $ this ->assertEquals ('2.5.0 ' , $ upsertedDoc2 ->getAttribute ('metadata ' )['version ' ]);
6487+ $ this ->assertEquals (['javascript ' , 'node ' , 'typescript ' ], $ upsertedDoc2 ->getAttribute ('metadata ' )['tags ' ]);
6488+ $ this ->assertTrue ($ upsertedDoc2 ->getAttribute ('metadata ' )['migrated ' ]);
6489+
6490+ // Test 5: Upsert - Bulk upsertDocuments (create and update)
6491+ $ docId3 = 'json-doc-3 ' ;
6492+ $ docId4 = 'json-doc-4 ' ;
6493+
6494+ $ bulkDocuments = [
6495+ new Document ([
6496+ '$id ' => $ docId3 ,
6497+ 'name ' => 'Bulk Upsert 1 ' ,
6498+ 'metadata ' => [
6499+ 'version ' => '3.0.0 ' ,
6500+ 'tags ' => ['python ' , 'flask ' ],
6501+ 'config ' => ['debug ' => false ]
6502+ ],
6503+ '$permissions ' => $ permissions ,
6504+ ]),
6505+ new Document ([
6506+ '$id ' => $ docId4 ,
6507+ 'name ' => 'Bulk Upsert 2 ' ,
6508+ 'metadata ' => [
6509+ 'version ' => '3.1.0 ' ,
6510+ 'tags ' => ['go ' , 'golang ' ],
6511+ 'config ' => ['debug ' => true ]
6512+ ],
6513+ '$permissions ' => $ permissions ,
6514+ ]),
6515+ // Update existing document
6516+ new Document ([
6517+ '$id ' => $ docId1 ,
6518+ 'name ' => 'Bulk Updated Document ' ,
6519+ 'metadata ' => [
6520+ 'version ' => '3.0.0 ' ,
6521+ 'tags ' => ['php ' , 'database ' , 'bulk ' ],
6522+ 'config ' => [
6523+ 'debug ' => false ,
6524+ 'timeout ' => 120
6525+ ],
6526+ 'bulkUpdated ' => true
6527+ ],
6528+ '$permissions ' => $ permissions ,
6529+ ]),
6530+ ];
6531+
6532+ $ count = $ database ->upsertDocuments ($ collection , $ bulkDocuments );
6533+ $ this ->assertEquals (3 , $ count );
6534+
6535+ // Verify bulk upsert results
6536+ $ bulkDoc1 = $ database ->getDocument ($ collection , $ docId3 );
6537+ $ this ->assertEquals ('Bulk Upsert 1 ' , $ bulkDoc1 ->getAttribute ('name ' ));
6538+ $ this ->assertEquals ('3.0.0 ' , $ bulkDoc1 ->getAttribute ('metadata ' )['version ' ]);
6539+
6540+ $ bulkDoc2 = $ database ->getDocument ($ collection , $ docId4 );
6541+ $ this ->assertEquals ('Bulk Upsert 2 ' , $ bulkDoc2 ->getAttribute ('name ' ));
6542+ $ this ->assertEquals ('3.1.0 ' , $ bulkDoc2 ->getAttribute ('metadata ' )['version ' ]);
6543+
6544+ $ bulkDoc3 = $ database ->getDocument ($ collection , $ docId1 );
6545+ $ this ->assertEquals ('Bulk Updated Document ' , $ bulkDoc3 ->getAttribute ('name ' ));
6546+ $ this ->assertEquals ('3.0.0 ' , $ bulkDoc3 ->getAttribute ('metadata ' )['version ' ]);
6547+ $ this ->assertTrue ($ bulkDoc3 ->getAttribute ('metadata ' )['bulkUpdated ' ]);
6548+
6549+ // Cleanup
6550+ $ database ->deleteCollection ($ collection );
6551+ }
63666552}
0 commit comments