@@ -4214,6 +4214,130 @@ public function testDeleteBulkDocumentsQueries(): void
42144214 $ database ->deleteCollection ('bulk_delete_queries ' );
42154215 }
42164216
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+
42174341 public function testUpdateDocumentsQueries (): void
42184342 {
42194343 /** @var Database $database */
0 commit comments