Skip to content

Commit e596cde

Browse files
committed
Fix PDOException on increment array attribute
1 parent f0c28b7 commit e596cde

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

src/Database/Database.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5132,8 +5132,8 @@ public function increaseDocumentAttribute(
51325132

51335133
/** @var Document $attr */
51345134
$attr = \end($attr);
5135-
if (!in_array($attr->getAttribute('type'), $whiteList)) {
5136-
throw new TypeException('Attribute type must be one of: ' . implode(',', $whiteList));
5135+
if (!\in_array($attr->getAttribute('type'), $whiteList) || $attr->getAttribute('array')) {
5136+
throw new TypeException('Attribute must be an integer or float and can not be an array.');
51375137
}
51385138

51395139
$document = $this->withTransaction(function () use ($collection, $id, $attribute, $value, $max) {

tests/e2e/Adapter/Scopes/AttributeTests.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,7 @@ function (mixed $value) {
11741174
try {
11751175
$queries = [Query::equal('title', ['test'])];
11761176
$database->find($col->getId(), $queries);
1177-
} catch (Throwable $e) {
1177+
} catch (Throwable) {
11781178
$this->fail('Should not have thrown error');
11791179
}
11801180
}

tests/e2e/Adapter/Scopes/DocumentTests.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Utopia\Database\Exception\Conflict as ConflictException;
1313
use Utopia\Database\Exception\Duplicate as DuplicateException;
1414
use Utopia\Database\Exception\Structure as StructureException;
15+
use Utopia\Database\Exception\Type as TypeException;
1516
use Utopia\Database\Helpers\ID;
1617
use Utopia\Database\Helpers\Permission;
1718
use Utopia\Database\Helpers\Role;
@@ -550,7 +551,7 @@ public function testUpsertDocumentsInc(): void
550551

551552
$database->createOrUpdateDocumentsWithIncrease(
552553
collection: __FUNCTION__,
553-
attribute:'integer',
554+
attribute: 'integer',
554555
documents: $documents
555556
);
556557

@@ -565,7 +566,7 @@ public function testUpsertDocumentsInc(): void
565566

566567
$database->createOrUpdateDocumentsWithIncrease(
567568
collection: __FUNCTION__,
568-
attribute:'integer',
569+
attribute: 'integer',
569570
documents: $documents
570571
);
571572

@@ -964,6 +965,7 @@ public function testCreateDocumentDefaults(): void
964965
// cleanup collection
965966
$database->deleteCollection('defaults');
966967
}
968+
967969
public function testIncreaseDecrease(): Document
968970
{
969971
/** @var Database $database */
@@ -976,12 +978,14 @@ public function testIncreaseDecrease(): Document
976978
$this->assertEquals(true, $database->createAttribute($collection, 'decrease', Database::VAR_INTEGER, 0, true));
977979
$this->assertEquals(true, $database->createAttribute($collection, 'increase_text', Database::VAR_STRING, 255, true));
978980
$this->assertEquals(true, $database->createAttribute($collection, 'increase_float', Database::VAR_FLOAT, 0, true));
981+
$this->assertEquals(true, $database->createAttribute($collection, 'sizes', Database::VAR_INTEGER, 8, required: false, array: true));
979982

980983
$document = $database->createDocument($collection, new Document([
981984
'increase' => 100,
982985
'decrease' => 100,
983986
'increase_float' => 100,
984987
'increase_text' => 'some text',
988+
'sizes' => [10, 20, 30],
985989
'$permissions' => [
986990
Permission::read(Role::any()),
987991
Permission::create(Role::any()),
@@ -1016,6 +1020,7 @@ public function testIncreaseDecrease(): Document
10161020

10171021
return $document;
10181022
}
1023+
10191024
/**
10201025
* @depends testIncreaseDecrease
10211026
*/
@@ -1040,7 +1045,6 @@ public function testDecreaseLimitMin(Document $document): void
10401045
$this->assertEquals(false, $database->decreaseDocumentAttribute('increase_decrease', $document->getId(), 'decrease', 10, 99));
10411046
}
10421047

1043-
10441048
/**
10451049
* @depends testIncreaseDecrease
10461050
*/
@@ -1049,8 +1053,28 @@ public function testIncreaseTextAttribute(Document $document): void
10491053
/** @var Database $database */
10501054
$database = static::getDatabase();
10511055

1052-
$this->expectException(Exception::class);
1053-
$this->assertEquals(false, $database->increaseDocumentAttribute('increase_decrease', $document->getId(), 'increase_text'));
1056+
try {
1057+
$this->assertEquals(false, $database->increaseDocumentAttribute('increase_decrease', $document->getId(), 'increase_text'));
1058+
$this->fail('Expected TypeException not thrown');
1059+
} catch (Exception $e) {
1060+
$this->assertInstanceOf(TypeException::class, $e, $e->getMessage());
1061+
}
1062+
}
1063+
1064+
/**
1065+
* @depends testIncreaseDecrease
1066+
*/
1067+
public function testIncreaseArrayAttribute(Document $document): void
1068+
{
1069+
/** @var Database $database */
1070+
$database = static::getDatabase();
1071+
1072+
try {
1073+
$this->assertEquals(false, $database->increaseDocumentAttribute('increase_decrease', $document->getId(), 'sizes'));
1074+
$this->fail('Expected TypeException not thrown');
1075+
} catch (Exception $e) {
1076+
$this->assertInstanceOf(TypeException::class, $e);
1077+
}
10541078
}
10551079

10561080
/**
@@ -1081,8 +1105,6 @@ public function testGetDocument(Document $document): Document
10811105
return $document;
10821106
}
10831107

1084-
1085-
10861108
/**
10871109
* @depends testCreateDocument
10881110
*/

0 commit comments

Comments
 (0)