Skip to content

Commit fd0e2c8

Browse files
added callback tests for updates
1 parent 80bebaa commit fd0e2c8

1 file changed

Lines changed: 95 additions & 0 deletions

File tree

tests/e2e/Adapter/Scopes/DocumentTests.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3427,6 +3427,101 @@ public function testUpdateDocument(Document $document): Document
34273427
return $document;
34283428
}
34293429

3430+
public function testBulkDocumentsWithCallbackSupport(): void
3431+
{
3432+
/** @var Database $database */
3433+
$database = static::getDatabase();
3434+
3435+
if (!$database->getAdapter()->getSupportForBatchOperations()) {
3436+
$this->expectNotToPerformAssertions();
3437+
return;
3438+
}
3439+
3440+
$collection = 'update_callback';
3441+
Authorization::cleanRoles();
3442+
Authorization::setRole(Role::any()->toString());
3443+
3444+
$database->createCollection($collection, attributes: [
3445+
new Document([
3446+
'$id' => ID::custom('string'),
3447+
'type' => Database::VAR_STRING,
3448+
'format' => '',
3449+
'size' => 100,
3450+
'signed' => true,
3451+
'required' => false,
3452+
'default' => null,
3453+
'array' => false,
3454+
'filters' => [],
3455+
]),
3456+
new Document([
3457+
'$id' => ID::custom('integer'),
3458+
'type' => Database::VAR_INTEGER,
3459+
'format' => '',
3460+
'size' => 10000,
3461+
'signed' => true,
3462+
'required' => false,
3463+
'default' => null,
3464+
'array' => false,
3465+
'filters' => [],
3466+
]),
3467+
], permissions: [
3468+
Permission::read(Role::any()),
3469+
Permission::create(Role::any()),
3470+
Permission::update(Role::any()),
3471+
Permission::delete(Role::any())
3472+
], documentSecurity: false);
3473+
3474+
for ($i = 0; $i < 10; $i++) {
3475+
$database->createDocument($collection, new Document([
3476+
'$id' => 'doc' . $i,
3477+
'string' => 'text📝 ' . $i,
3478+
'integer' => $i
3479+
]));
3480+
}
3481+
// Test onNext is throwing the error without the onError
3482+
// a non existent document to test the error thrown
3483+
try {
3484+
$results = [];
3485+
$count = $database->updateDocuments($collection, new Document([
3486+
'string' => 'text📝 updated',
3487+
]), [
3488+
Query::greaterThanEqual('integer', 100),
3489+
], onNext: function ($doc) use (&$results) {
3490+
$results[] = $doc;
3491+
throw new Exception("Error thrown to test that update doesn't stop and error is caught");
3492+
});
3493+
} catch (Exception $e) {
3494+
$this->assertInstanceOf(Exception::class, $e);
3495+
$this->assertEquals("Error thrown to test that update doesn't stop and error is caught", $e->getMessage());
3496+
}
3497+
3498+
// Test Update half of the documents
3499+
$results = [];
3500+
$count = $database->updateDocuments($collection, new Document([
3501+
'string' => 'text📝 updated',
3502+
]), [
3503+
Query::greaterThanEqual('integer', 5),
3504+
], onNext: function ($doc) use (&$results) {
3505+
$results[] = $doc;
3506+
throw new Exception("Error thrown to test that update doesn't stop and error is caught");
3507+
}, onError:function ($e) {
3508+
$this->assertInstanceOf(Exception::class, $e);
3509+
$this->assertEquals("Error thrown to test that update doesn't stop and error is caught", $e->getMessage());
3510+
});
3511+
3512+
$this->assertEquals(5, $count);
3513+
3514+
foreach ($results as $document) {
3515+
$this->assertEquals('text📝 updated', $document->getAttribute('string'));
3516+
}
3517+
3518+
$updatedDocuments = $database->find($collection, [
3519+
Query::greaterThanEqual('integer', 5),
3520+
]);
3521+
3522+
$this->assertCount(5, $updatedDocuments);
3523+
}
3524+
34303525
/**
34313526
* @depends testUpdateDocument
34323527
*/

0 commit comments

Comments
 (0)