Skip to content

Commit 669f38d

Browse files
authored
Merge pull request #648 from utopia-php/fix-dec-min-0
Fix min check
2 parents 400eb89 + f948a35 commit 669f38d

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

src/Database/Database.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5233,7 +5233,7 @@ public function increaseDocumentAttribute(
52335233
}
52345234
}
52355235

5236-
if ($max && ($document->getAttribute($attribute) + $value > $max)) {
5236+
if (!\is_null($max) && ($document->getAttribute($attribute) + $value > $max)) {
52375237
throw new LimitException('Attribute value exceeds maximum limit: ' . $max);
52385238
}
52395239

@@ -5332,7 +5332,7 @@ public function decreaseDocumentAttribute(
53325332
}
53335333
}
53345334

5335-
if ($min && ($document->getAttribute($attribute) - $value < $min)) {
5335+
if (!\is_null($min) && ($document->getAttribute($attribute) - $value < $min)) {
53365336
throw new LimitException('Attribute value exceeds minimum limit: ' . $min);
53375337
}
53385338

tests/e2e/Adapter/Scopes/DocumentTests.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Utopia\Database\Exception\Authorization as AuthorizationException;
1212
use Utopia\Database\Exception\Conflict as ConflictException;
1313
use Utopia\Database\Exception\Duplicate as DuplicateException;
14+
use Utopia\Database\Exception\Limit as LimitException;
1415
use Utopia\Database\Exception\Structure as StructureException;
1516
use Utopia\Database\Exception\Type as TypeException;
1617
use Utopia\Database\Helpers\ID;
@@ -1205,8 +1206,31 @@ public function testDecreaseLimitMin(Document $document): void
12051206
/** @var Database $database */
12061207
$database = static::getDatabase();
12071208

1208-
$this->expectException(Exception::class);
1209-
$this->assertEquals(false, $database->decreaseDocumentAttribute('increase_decrease', $document->getId(), 'decrease', 10, 99));
1209+
try {
1210+
$database->decreaseDocumentAttribute(
1211+
'increase_decrease',
1212+
$document->getId(),
1213+
'decrease',
1214+
10,
1215+
99
1216+
);
1217+
$this->fail('Failed to throw exception');
1218+
} catch (Exception $e) {
1219+
$this->assertInstanceOf(LimitException::class, $e);
1220+
}
1221+
1222+
try {
1223+
$database->decreaseDocumentAttribute(
1224+
'increase_decrease',
1225+
$document->getId(),
1226+
'decrease',
1227+
1000,
1228+
0
1229+
);
1230+
$this->fail('Failed to throw exception');
1231+
} catch (Exception $e) {
1232+
$this->assertInstanceOf(LimitException::class, $e);
1233+
}
12101234
}
12111235

12121236
/**

0 commit comments

Comments
 (0)