Skip to content

Commit 477cb44

Browse files
committed
Tests
1 parent 4756f22 commit 477cb44

File tree

1 file changed

+90
-6
lines changed

1 file changed

+90
-6
lines changed

tests/e2e/Adapter/Scopes/DocumentTests.php

Lines changed: 90 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ public function testCreateDocumentsWithDifferentAttributes(): void
322322
static::getDatabase()->deleteCollection($collection);
323323
}
324324

325-
public function testCreateOrUpdateDocuments(): void
325+
public function testUpsertDocuments(): void
326326
{
327327
if (!static::getDatabase()->getAdapter()->getSupportForUpserts()) {
328328
$this->expectNotToPerformAssertions();
@@ -362,9 +362,13 @@ public function testCreateOrUpdateDocuments(): void
362362
];
363363

364364
$results = [];
365-
$count = static::getDatabase()->createOrUpdateDocuments(__FUNCTION__, $documents, onNext: function ($doc) use (&$results) {
366-
$results[] = $doc;
367-
});
365+
$count = static::getDatabase()->createOrUpdateDocuments(
366+
__FUNCTION__,
367+
$documents,
368+
onNext: function ($doc) use (&$results) {
369+
$results[] = $doc;
370+
}
371+
);
368372

369373
$this->assertEquals(2, $count);
370374

@@ -432,7 +436,7 @@ public function testCreateOrUpdateDocuments(): void
432436
}
433437
}
434438

435-
public function testCreateOrUpdateDocumentsInc(): void
439+
public function testUpsertDocumentsInc(): void
436440
{
437441
if (!static::getDatabase()->getAdapter()->getSupportForUpserts()) {
438442
$this->expectNotToPerformAssertions();
@@ -501,7 +505,7 @@ public function testCreateOrUpdateDocumentsInc(): void
501505
}
502506
}
503507

504-
public function testCreateOrUpdateDocumentsPermissions(): void
508+
public function testUpsertDocumentsPermissions(): void
505509
{
506510
if (!static::getDatabase()->getAdapter()->getSupportForUpserts()) {
507511
$this->expectNotToPerformAssertions();
@@ -587,6 +591,86 @@ public function testCreateOrUpdateDocumentsPermissions(): void
587591
$this->assertEquals($newPermissions, $document->getPermissions());
588592
}
589593

594+
public function testUpsertDocumentsAttributeMismatch(): void
595+
{
596+
/** @var Database $database */
597+
$database = static::getDatabase();
598+
599+
if (!$database->getAdapter()->getSupportForUpserts()) {
600+
$this->expectNotToPerformAssertions();
601+
return;
602+
}
603+
604+
$database->createCollection(__FUNCTION__, permissions: [
605+
Permission::create(Role::any()),
606+
Permission::read(Role::any()),
607+
Permission::update(Role::any()),
608+
Permission::delete(Role::any()),
609+
], documentSecurity: false);
610+
$database->createAttribute(__FUNCTION__, 'first', Database::VAR_STRING, 128, true);
611+
$database->createAttribute(__FUNCTION__, 'last', Database::VAR_STRING, 128, false);
612+
613+
$existingDocument = $database->createDocument(__FUNCTION__, new Document([
614+
'$id' => 'first',
615+
'first' => 'first',
616+
'last' => 'last',
617+
]));
618+
619+
$newDocument = new Document([
620+
'$id' => 'second',
621+
'first' => 'second',
622+
]);
623+
624+
$docs = $database->createOrUpdateDocuments(__FUNCTION__, [
625+
$existingDocument->setAttribute('first', 'updated'),
626+
$newDocument,
627+
]);
628+
629+
$this->assertEquals(2, $docs);
630+
$this->assertEquals('updated', $existingDocument->getAttribute('first'));
631+
$this->assertEquals('second', $newDocument->getAttribute('first'));
632+
$this->assertEquals('', $newDocument->getAttribute('last'));
633+
634+
try {
635+
$database->createOrUpdateDocuments(__FUNCTION__, [
636+
$existingDocument->removeAttribute('first'),
637+
$newDocument
638+
]);
639+
$this->fail('Failed to throw exception');
640+
} catch (Throwable $e) {
641+
$this->assertTrue($e instanceof StructureException, $e->getMessage());
642+
}
643+
}
644+
645+
public function testUpsertDocumentsNoop(): void
646+
{
647+
if (!static::getDatabase()->getAdapter()->getSupportForUpserts()) {
648+
$this->expectNotToPerformAssertions();
649+
return;
650+
}
651+
652+
static::getDatabase()->createCollection(__FUNCTION__);
653+
static::getDatabase()->createAttribute(__FUNCTION__, 'string', Database::VAR_STRING, 128, true);
654+
655+
$document = new Document([
656+
'$id' => 'first',
657+
'string' => 'text📝',
658+
'$permissions' => [
659+
Permission::read(Role::any()),
660+
Permission::create(Role::any()),
661+
Permission::update(Role::any()),
662+
Permission::delete(Role::any()),
663+
],
664+
]);
665+
666+
$count = static::getDatabase()->createOrUpdateDocuments(__FUNCTION__, [$document]);
667+
$this->assertEquals(1, $count);
668+
669+
// No changes, should return 0
670+
$count = static::getDatabase()->createOrUpdateDocuments(__FUNCTION__, [$document]);
671+
$this->assertEquals(0, $count);
672+
}
673+
590674
public function testRespectNulls(): Document
591675
{
592676
static::getDatabase()->createCollection('documents_nulls');

0 commit comments

Comments
 (0)