@@ -445,6 +445,7 @@ public function testUpsertDocuments(): void
445445 foreach ($ results as $ index => $ document ) {
446446 $ createdAt [$ index ] = $ document ->getCreatedAt ();
447447 $ this ->assertNotEmpty (true , $ document ->getId ());
448+ $ this ->assertNotEmpty (true , $ document ->getSequence ());
448449 $ this ->assertIsString ($ document ->getAttribute ('string ' ));
449450 $ this ->assertEquals ('text📝 ' , $ document ->getAttribute ('string ' )); // Also makes sure an emoji is working
450451 $ this ->assertIsInt ($ document ->getAttribute ('integer ' ));
@@ -481,6 +482,7 @@ public function testUpsertDocuments(): void
481482
482483 foreach ($ results as $ document ) {
483484 $ this ->assertNotEmpty (true , $ document ->getId ());
485+ $ this ->assertNotEmpty (true , $ document ->getSequence ());
484486 $ this ->assertIsString ($ document ->getAttribute ('string ' ));
485487 $ this ->assertEquals ('new text📝 ' , $ document ->getAttribute ('string ' )); // Also makes sure an emoji is working
486488 $ this ->assertIsInt ($ document ->getAttribute ('integer ' ));
@@ -3953,27 +3955,6 @@ public function testDeleteBulkDocuments(): void
39533955 */
39543956 $ selects = ['$sequence ' , '$id ' , '$collection ' , '$permissions ' , '$updatedAt ' ];
39553957
3956- try {
3957- // a non existent document to test the error thrown
3958- $ database ->deleteDocuments (
3959- collection: 'bulk_delete ' ,
3960- queries: [
3961- Query::select ([...$ selects , '$createdAt ' ]),
3962- Query::lessThan ('$createdAt ' , '1800-01-01 ' ),
3963- Query::orderAsc ('$createdAt ' ),
3964- Query::orderAsc (),
3965- Query::limit (1 ),
3966- ],
3967- batchSize: 1 ,
3968- onNext: function () {
3969- throw new Exception ("Error thrown to test that deletion doesn't stop and error is caught " );
3970- }
3971- );
3972- } catch (Exception $ e ) {
3973- $ this ->assertInstanceOf (Exception::class, $ e );
3974- $ this ->assertEquals ("Error thrown to test that deletion doesn't stop and error is caught " , $ e ->getMessage ());
3975- }
3976-
39773958 $ count = $ database ->deleteDocuments (
39783959 collection: 'bulk_delete ' ,
39793960 queries: [
@@ -3984,25 +3965,13 @@ public function testDeleteBulkDocuments(): void
39843965 Query::orderAsc (),
39853966 Query::limit (2 ),
39863967 ],
3987- batchSize: 1 ,
3988- onNext: function () {
3989- throw new Exception ("Error thrown to test that deletion doesn't stop and error is caught " );
3990- },
3991- onError:function ($ e ) {
3992- $ this ->assertInstanceOf (Exception::class, $ e );
3993- $ this ->assertEquals ("Error thrown to test that deletion doesn't stop and error is caught " , $ e ->getMessage ());
3994- }
3968+ batchSize: 1
39953969 );
39963970
39973971 $ this ->assertEquals (2 , $ count );
39983972
39993973 // TEST: Bulk Delete All Documents
4000- $ this ->assertEquals (8 , $ database ->deleteDocuments ('bulk_delete ' , onNext: function () {
4001- throw new Exception ("Error thrown to test that deletion doesn't stop and error is caught " );
4002- }, onError:function ($ e ) {
4003- $ this ->assertInstanceOf (Exception::class, $ e );
4004- $ this ->assertEquals ("Error thrown to test that deletion doesn't stop and error is caught " , $ e ->getMessage ());
4005- }));
3974+ $ this ->assertEquals (8 , $ database ->deleteDocuments ('bulk_delete ' ));
40063975
40073976 $ docs = $ database ->find ('bulk_delete ' );
40083977 $ this ->assertCount (0 , $ docs );
@@ -4015,10 +3984,6 @@ public function testDeleteBulkDocuments(): void
40153984 Query::greaterThanEqual ('integer ' , 5 )
40163985 ], onNext: function ($ doc ) use (&$ results ) {
40173986 $ results [] = $ doc ;
4018- throw new Exception ("Error thrown to test that deletion doesn't stop and error is caught " );
4019- }, onError:function ($ e ) {
4020- $ this ->assertInstanceOf (Exception::class, $ e );
4021- $ this ->assertEquals ("Error thrown to test that deletion doesn't stop and error is caught " , $ e ->getMessage ());
40223987 });
40233988
40243989 $ this ->assertEquals (5 , $ count );
@@ -4035,16 +4000,7 @@ public function testDeleteBulkDocuments(): void
40354000
40364001 try {
40374002 $ this ->getDatabase ()->withRequestTimestamp ($ oneHourAgo , function () {
4038- return $ this ->getDatabase ()->deleteDocuments ('bulk_delete ' , onNext: function () {
4039- throw new Exception ("Error thrown to test that deletion doesn't stop and error is caught " );
4040- }, onError:function ($ e ) {
4041- if ($ e instanceof Exception) {
4042- $ this ->assertInstanceOf (Exception::class, $ e );
4043- $ this ->assertEquals ("Error thrown to test that deletion doesn't stop and error is caught " , $ e ->getMessage ());
4044- } else {
4045- $ this ->fail ("Caught value is not an Exception. " );
4046- }
4047- });
4003+ return $ this ->getDatabase ()->deleteDocuments ('bulk_delete ' );
40484004 });
40494005 $ this ->fail ('Failed to throw exception ' );
40504006 } catch (ConflictException $ e ) {
@@ -4088,12 +4044,7 @@ public function testDeleteBulkDocuments(): void
40884044 Permission::delete (Role::any ())
40894045 ], false );
40904046
4091- $ database ->deleteDocuments ('bulk_delete ' , onNext: function () {
4092- throw new Exception ("Error thrown to test that deletion doesn't stop and error is caught " );
4093- }, onError:function ($ e ) {
4094- $ this ->assertInstanceOf (Exception::class, $ e );
4095- $ this ->assertEquals ("Error thrown to test that deletion doesn't stop and error is caught " , $ e ->getMessage ());
4096- });
4047+ $ database ->deleteDocuments ('bulk_delete ' );
40974048
40984049 $ this ->assertEquals (0 , \count ($ this ->getDatabase ()->find ('bulk_delete ' )));
40994050
@@ -4138,20 +4089,10 @@ public function testDeleteBulkDocumentsQueries(): void
41384089 // Test limit
41394090 $ this ->propagateBulkDocuments ('bulk_delete_queries ' );
41404091
4141- $ this ->assertEquals (5 , $ database ->deleteDocuments ('bulk_delete_queries ' , [Query::limit (5 )], onNext: function () {
4142- throw new Exception ("Error thrown to test that deletion doesn't stop and error is caught " );
4143- }, onError:function ($ e ) {
4144- $ this ->assertInstanceOf (Exception::class, $ e );
4145- $ this ->assertEquals ("Error thrown to test that deletion doesn't stop and error is caught " , $ e ->getMessage ());
4146- }));
4092+ $ this ->assertEquals (5 , $ database ->deleteDocuments ('bulk_delete_queries ' , [Query::limit (5 )]));
41474093 $ this ->assertEquals (5 , \count ($ database ->find ('bulk_delete_queries ' )));
41484094
4149- $ this ->assertEquals (5 , $ database ->deleteDocuments ('bulk_delete_queries ' , [Query::limit (5 )], onNext: function () {
4150- throw new Exception ("Error thrown to test that deletion doesn't stop and error is caught " );
4151- }, onError:function ($ e ) {
4152- $ this ->assertInstanceOf (Exception::class, $ e );
4153- $ this ->assertEquals ("Error thrown to test that deletion doesn't stop and error is caught " , $ e ->getMessage ());
4154- }));
4095+ $ this ->assertEquals (5 , $ database ->deleteDocuments ('bulk_delete_queries ' , [Query::limit (5 )]));
41554096 $ this ->assertEquals (0 , \count ($ database ->find ('bulk_delete_queries ' )));
41564097
41574098 // Test Limit more than batchSize
@@ -4176,6 +4117,130 @@ public function testDeleteBulkDocumentsQueries(): void
41764117 $ database ->deleteCollection ('bulk_delete_queries ' );
41774118 }
41784119
4120+ public function testDeleteBulkDocumentsWithCallbackSupport (): void
4121+ {
4122+ /** @var Database $database */
4123+ $ database = static ::getDatabase ();
4124+
4125+ if (!$ database ->getAdapter ()->getSupportForBatchOperations ()) {
4126+ $ this ->expectNotToPerformAssertions ();
4127+ return ;
4128+ }
4129+
4130+ $ database ->createCollection (
4131+ 'bulk_delete_with_callback ' ,
4132+ attributes: [
4133+ new Document ([
4134+ '$id ' => 'text ' ,
4135+ 'type ' => Database::VAR_STRING ,
4136+ 'size ' => 100 ,
4137+ 'required ' => true ,
4138+ ]),
4139+ new Document ([
4140+ '$id ' => 'integer ' ,
4141+ 'type ' => Database::VAR_INTEGER ,
4142+ 'size ' => 10 ,
4143+ 'required ' => true ,
4144+ ])
4145+ ],
4146+ permissions: [
4147+ Permission::create (Role::any ()),
4148+ Permission::read (Role::any ()),
4149+ Permission::delete (Role::any ())
4150+ ],
4151+ documentSecurity: false
4152+ );
4153+
4154+ $ this ->propagateBulkDocuments ('bulk_delete_with_callback ' );
4155+
4156+ $ docs = $ database ->find ('bulk_delete_with_callback ' );
4157+ $ this ->assertCount (10 , $ docs );
4158+
4159+ /**
4160+ * Test Short select query, test pagination as well, Add order to select
4161+ */
4162+ $ selects = ['$sequence ' , '$id ' , '$collection ' , '$permissions ' , '$updatedAt ' ];
4163+
4164+ try {
4165+ // a non existent document to test the error thrown
4166+ $ database ->deleteDocuments (
4167+ collection: 'bulk_delete_with_callback ' ,
4168+ queries: [
4169+ Query::select ([...$ selects , '$createdAt ' ]),
4170+ Query::lessThan ('$createdAt ' , '1800-01-01 ' ),
4171+ Query::orderAsc ('$createdAt ' ),
4172+ Query::orderAsc (),
4173+ Query::limit (1 ),
4174+ ],
4175+ batchSize: 1 ,
4176+ onNext: function () {
4177+ throw new Exception ("Error thrown to test that deletion doesn't stop and error is caught " );
4178+ }
4179+ );
4180+ } catch (Exception $ e ) {
4181+ $ this ->assertInstanceOf (Exception::class, $ e );
4182+ $ this ->assertEquals ("Error thrown to test that deletion doesn't stop and error is caught " , $ e ->getMessage ());
4183+ }
4184+
4185+ $ docs = $ database ->find ('bulk_delete_with_callback ' );
4186+ $ this ->assertCount (10 , $ docs );
4187+
4188+ $ count = $ database ->deleteDocuments (
4189+ collection: 'bulk_delete_with_callback ' ,
4190+ queries: [
4191+ Query::select ([...$ selects , '$createdAt ' ]),
4192+ Query::cursorAfter ($ docs [6 ]),
4193+ Query::greaterThan ('$createdAt ' , '2000-01-01 ' ),
4194+ Query::orderAsc ('$createdAt ' ),
4195+ Query::orderAsc (),
4196+ Query::limit (2 ),
4197+ ],
4198+ batchSize: 1 ,
4199+ onNext: function () {
4200+ // simulating error throwing but should not stop deletion
4201+ throw new Exception ("Error thrown to test that deletion doesn't stop and error is caught " );
4202+ },
4203+ onError:function ($ e ) {
4204+ $ this ->assertInstanceOf (Exception::class, $ e );
4205+ $ this ->assertEquals ("Error thrown to test that deletion doesn't stop and error is caught " , $ e ->getMessage ());
4206+ }
4207+ );
4208+
4209+ $ this ->assertEquals (2 , $ count );
4210+
4211+ // TEST: Bulk Delete All Documents without passing callbacks
4212+ $ this ->assertEquals (8 , $ database ->deleteDocuments ('bulk_delete_with_callback ' ));
4213+
4214+ $ docs = $ database ->find ('bulk_delete_with_callback ' );
4215+ $ this ->assertCount (0 , $ docs );
4216+
4217+ // TEST: Bulk delete documents with queries with callbacks
4218+ $ this ->propagateBulkDocuments ('bulk_delete_with_callback ' );
4219+
4220+ $ results = [];
4221+ $ count = $ database ->deleteDocuments ('bulk_delete_with_callback ' , [
4222+ Query::greaterThanEqual ('integer ' , 5 )
4223+ ], onNext: function ($ doc ) use (&$ results ) {
4224+ $ results [] = $ doc ;
4225+ throw new Exception ("Error thrown to test that deletion doesn't stop and error is caught " );
4226+ }, onError:function ($ e ) {
4227+ $ this ->assertInstanceOf (Exception::class, $ e );
4228+ $ this ->assertEquals ("Error thrown to test that deletion doesn't stop and error is caught " , $ e ->getMessage ());
4229+ });
4230+
4231+ $ this ->assertEquals (5 , $ count );
4232+
4233+ foreach ($ results as $ document ) {
4234+ $ this ->assertGreaterThanOrEqual (5 , $ document ->getAttribute ('integer ' ));
4235+ }
4236+
4237+ $ docs = $ database ->find ('bulk_delete_with_callback ' );
4238+ $ this ->assertEquals (5 , \count ($ docs ));
4239+
4240+ // Teardown
4241+ $ database ->deleteCollection ('bulk_delete_with_callback ' );
4242+ }
4243+
41794244 public function testUpdateDocumentsQueries (): void
41804245 {
41814246 /** @var Database $database */
0 commit comments