Skip to content

Commit 644ed82

Browse files
committed
Merge pull request #648 from utopia-php/fix-dec-min-0
Fix min check
1 parent 83278d6 commit 644ed82

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/Database/Database.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5178,7 +5178,7 @@ public function increaseDocumentAttribute(
51785178
}
51795179
}
51805180

5181-
if ($max && ($document->getAttribute($attribute) + $value > $max)) {
5181+
if (!\is_null($max) && ($document->getAttribute($attribute) + $value > $max)) {
51825182
throw new LimitException('Attribute value exceeds maximum limit: ' . $max);
51835183
}
51845184

@@ -5277,7 +5277,7 @@ public function decreaseDocumentAttribute(
52775277
}
52785278
}
52795279

5280-
if ($min && ($document->getAttribute($attribute) - $value < $min)) {
5280+
if (!\is_null($min) && ($document->getAttribute($attribute) - $value < $min)) {
52815281
throw new LimitException('Attribute value exceeds minimum limit: ' . $min);
52825282
}
52835283

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;
@@ -1041,8 +1042,31 @@ public function testDecreaseLimitMin(Document $document): void
10411042
/** @var Database $database */
10421043
$database = static::getDatabase();
10431044

1044-
$this->expectException(Exception::class);
1045-
$this->assertEquals(false, $database->decreaseDocumentAttribute('increase_decrease', $document->getId(), 'decrease', 10, 99));
1045+
try {
1046+
$database->decreaseDocumentAttribute(
1047+
'increase_decrease',
1048+
$document->getId(),
1049+
'decrease',
1050+
10,
1051+
99
1052+
);
1053+
$this->fail('Failed to throw exception');
1054+
} catch (Exception $e) {
1055+
$this->assertInstanceOf(LimitException::class, $e);
1056+
}
1057+
1058+
try {
1059+
$database->decreaseDocumentAttribute(
1060+
'increase_decrease',
1061+
$document->getId(),
1062+
'decrease',
1063+
1000,
1064+
0
1065+
);
1066+
$this->fail('Failed to throw exception');
1067+
} catch (Exception $e) {
1068+
$this->assertInstanceOf(LimitException::class, $e);
1069+
}
10461070
}
10471071

10481072
/**

0 commit comments

Comments
 (0)