@@ -3427,6 +3427,7 @@ public function testUpdateDocument(Document $document): Document
34273427 return $ document ;
34283428 }
34293429
3430+
34303431 /**
34313432 * @depends testUpdateDocument
34323433 */
@@ -3691,6 +3692,102 @@ public function testUpdateDocuments(): void
36913692 Authorization::cleanRoles ();
36923693 Authorization::setRole (Role::any ()->toString ());
36933694 }
3695+
3696+ public function testUpdateDocumentsWithCallbackSupport (): void
3697+ {
3698+ /** @var Database $database */
3699+ $ database = static ::getDatabase ();
3700+
3701+ if (!$ database ->getAdapter ()->getSupportForBatchOperations ()) {
3702+ $ this ->expectNotToPerformAssertions ();
3703+ return ;
3704+ }
3705+
3706+ $ collection = 'update_callback ' ;
3707+ Authorization::cleanRoles ();
3708+ Authorization::setRole (Role::any ()->toString ());
3709+
3710+ $ database ->createCollection ($ collection , attributes: [
3711+ new Document ([
3712+ '$id ' => ID ::custom ('string ' ),
3713+ 'type ' => Database::VAR_STRING ,
3714+ 'format ' => '' ,
3715+ 'size ' => 100 ,
3716+ 'signed ' => true ,
3717+ 'required ' => false ,
3718+ 'default ' => null ,
3719+ 'array ' => false ,
3720+ 'filters ' => [],
3721+ ]),
3722+ new Document ([
3723+ '$id ' => ID ::custom ('integer ' ),
3724+ 'type ' => Database::VAR_INTEGER ,
3725+ 'format ' => '' ,
3726+ 'size ' => 10000 ,
3727+ 'signed ' => true ,
3728+ 'required ' => false ,
3729+ 'default ' => null ,
3730+ 'array ' => false ,
3731+ 'filters ' => [],
3732+ ]),
3733+ ], permissions: [
3734+ Permission::read (Role::any ()),
3735+ Permission::create (Role::any ()),
3736+ Permission::update (Role::any ()),
3737+ Permission::delete (Role::any ())
3738+ ], documentSecurity: false );
3739+
3740+ for ($ i = 0 ; $ i < 10 ; $ i ++) {
3741+ $ database ->createDocument ($ collection , new Document ([
3742+ '$id ' => 'doc ' . $ i ,
3743+ 'string ' => 'text📝 ' . $ i ,
3744+ 'integer ' => $ i
3745+ ]));
3746+ }
3747+ // Test onNext is throwing the error without the onError
3748+ // a non existent document to test the error thrown
3749+ try {
3750+ $ results = [];
3751+ $ count = $ database ->updateDocuments ($ collection , new Document ([
3752+ 'string ' => 'text📝 updated ' ,
3753+ ]), [
3754+ Query::greaterThanEqual ('integer ' , 100 ),
3755+ ], onNext: function ($ doc ) use (&$ results ) {
3756+ $ results [] = $ doc ;
3757+ throw new Exception ("Error thrown to test that update doesn't stop and error is caught " );
3758+ });
3759+ } catch (Exception $ e ) {
3760+ $ this ->assertInstanceOf (Exception::class, $ e );
3761+ $ this ->assertEquals ("Error thrown to test that update doesn't stop and error is caught " , $ e ->getMessage ());
3762+ }
3763+
3764+ // Test Update half of the documents
3765+ $ results = [];
3766+ $ count = $ database ->updateDocuments ($ collection , new Document ([
3767+ 'string ' => 'text📝 updated ' ,
3768+ ]), [
3769+ Query::greaterThanEqual ('integer ' , 5 ),
3770+ ], onNext: function ($ doc ) use (&$ results ) {
3771+ $ results [] = $ doc ;
3772+ throw new Exception ("Error thrown to test that update doesn't stop and error is caught " );
3773+ }, onError:function ($ e ) {
3774+ $ this ->assertInstanceOf (Exception::class, $ e );
3775+ $ this ->assertEquals ("Error thrown to test that update doesn't stop and error is caught " , $ e ->getMessage ());
3776+ });
3777+
3778+ $ this ->assertEquals (5 , $ count );
3779+
3780+ foreach ($ results as $ document ) {
3781+ $ this ->assertEquals ('text📝 updated ' , $ document ->getAttribute ('string ' ));
3782+ }
3783+
3784+ $ updatedDocuments = $ database ->find ($ collection , [
3785+ Query::greaterThanEqual ('integer ' , 5 ),
3786+ ]);
3787+
3788+ $ this ->assertCount (5 , $ updatedDocuments );
3789+ }
3790+
36943791 /**
36953792 * @depends testCreateDocument
36963793 */
@@ -4117,6 +4214,130 @@ public function testDeleteBulkDocumentsQueries(): void
41174214 $ database ->deleteCollection ('bulk_delete_queries ' );
41184215 }
41194216
4217+ public function testDeleteBulkDocumentsWithCallbackSupport (): void
4218+ {
4219+ /** @var Database $database */
4220+ $ database = static ::getDatabase ();
4221+
4222+ if (!$ database ->getAdapter ()->getSupportForBatchOperations ()) {
4223+ $ this ->expectNotToPerformAssertions ();
4224+ return ;
4225+ }
4226+
4227+ $ database ->createCollection (
4228+ 'bulk_delete_with_callback ' ,
4229+ attributes: [
4230+ new Document ([
4231+ '$id ' => 'text ' ,
4232+ 'type ' => Database::VAR_STRING ,
4233+ 'size ' => 100 ,
4234+ 'required ' => true ,
4235+ ]),
4236+ new Document ([
4237+ '$id ' => 'integer ' ,
4238+ 'type ' => Database::VAR_INTEGER ,
4239+ 'size ' => 10 ,
4240+ 'required ' => true ,
4241+ ])
4242+ ],
4243+ permissions: [
4244+ Permission::create (Role::any ()),
4245+ Permission::read (Role::any ()),
4246+ Permission::delete (Role::any ())
4247+ ],
4248+ documentSecurity: false
4249+ );
4250+
4251+ $ this ->propagateBulkDocuments ('bulk_delete_with_callback ' );
4252+
4253+ $ docs = $ database ->find ('bulk_delete_with_callback ' );
4254+ $ this ->assertCount (10 , $ docs );
4255+
4256+ /**
4257+ * Test Short select query, test pagination as well, Add order to select
4258+ */
4259+ $ selects = ['$sequence ' , '$id ' , '$collection ' , '$permissions ' , '$updatedAt ' ];
4260+
4261+ try {
4262+ // a non existent document to test the error thrown
4263+ $ database ->deleteDocuments (
4264+ collection: 'bulk_delete_with_callback ' ,
4265+ queries: [
4266+ Query::select ([...$ selects , '$createdAt ' ]),
4267+ Query::lessThan ('$createdAt ' , '1800-01-01 ' ),
4268+ Query::orderAsc ('$createdAt ' ),
4269+ Query::orderAsc (),
4270+ Query::limit (1 ),
4271+ ],
4272+ batchSize: 1 ,
4273+ onNext: function () {
4274+ throw new Exception ("Error thrown to test that deletion doesn't stop and error is caught " );
4275+ }
4276+ );
4277+ } catch (Exception $ e ) {
4278+ $ this ->assertInstanceOf (Exception::class, $ e );
4279+ $ this ->assertEquals ("Error thrown to test that deletion doesn't stop and error is caught " , $ e ->getMessage ());
4280+ }
4281+
4282+ $ docs = $ database ->find ('bulk_delete_with_callback ' );
4283+ $ this ->assertCount (10 , $ docs );
4284+
4285+ $ count = $ database ->deleteDocuments (
4286+ collection: 'bulk_delete_with_callback ' ,
4287+ queries: [
4288+ Query::select ([...$ selects , '$createdAt ' ]),
4289+ Query::cursorAfter ($ docs [6 ]),
4290+ Query::greaterThan ('$createdAt ' , '2000-01-01 ' ),
4291+ Query::orderAsc ('$createdAt ' ),
4292+ Query::orderAsc (),
4293+ Query::limit (2 ),
4294+ ],
4295+ batchSize: 1 ,
4296+ onNext: function () {
4297+ // simulating error throwing but should not stop deletion
4298+ throw new Exception ("Error thrown to test that deletion doesn't stop and error is caught " );
4299+ },
4300+ onError:function ($ e ) {
4301+ $ this ->assertInstanceOf (Exception::class, $ e );
4302+ $ this ->assertEquals ("Error thrown to test that deletion doesn't stop and error is caught " , $ e ->getMessage ());
4303+ }
4304+ );
4305+
4306+ $ this ->assertEquals (2 , $ count );
4307+
4308+ // TEST: Bulk Delete All Documents without passing callbacks
4309+ $ this ->assertEquals (8 , $ database ->deleteDocuments ('bulk_delete_with_callback ' ));
4310+
4311+ $ docs = $ database ->find ('bulk_delete_with_callback ' );
4312+ $ this ->assertCount (0 , $ docs );
4313+
4314+ // TEST: Bulk delete documents with queries with callbacks
4315+ $ this ->propagateBulkDocuments ('bulk_delete_with_callback ' );
4316+
4317+ $ results = [];
4318+ $ count = $ database ->deleteDocuments ('bulk_delete_with_callback ' , [
4319+ Query::greaterThanEqual ('integer ' , 5 )
4320+ ], onNext: function ($ doc ) use (&$ results ) {
4321+ $ results [] = $ doc ;
4322+ throw new Exception ("Error thrown to test that deletion doesn't stop and error is caught " );
4323+ }, onError:function ($ e ) {
4324+ $ this ->assertInstanceOf (Exception::class, $ e );
4325+ $ this ->assertEquals ("Error thrown to test that deletion doesn't stop and error is caught " , $ e ->getMessage ());
4326+ });
4327+
4328+ $ this ->assertEquals (5 , $ count );
4329+
4330+ foreach ($ results as $ document ) {
4331+ $ this ->assertGreaterThanOrEqual (5 , $ document ->getAttribute ('integer ' ));
4332+ }
4333+
4334+ $ docs = $ database ->find ('bulk_delete_with_callback ' );
4335+ $ this ->assertEquals (5 , \count ($ docs ));
4336+
4337+ // Teardown
4338+ $ database ->deleteCollection ('bulk_delete_with_callback ' );
4339+ }
4340+
41204341 public function testUpdateDocumentsQueries (): void
41214342 {
41224343 /** @var Database $database */
0 commit comments