Skip to content

Commit 5dd58c1

Browse files
authored
Merge pull request #72 from utopia-php/feat-check-attribute-method
Add checkAttribute method
2 parents fc6304c + 2d1f483 commit 5dd58c1

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

src/Database/Database.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,40 @@ public function createAttribute(string $collection, string $id, string $type, in
472472
return $this->adapter->createAttribute($collection->getId(), $id, $type, $size, $signed, $array);
473473
}
474474

475+
/**
476+
* Checks if attribute can be added to collection.
477+
* Used to check attribute limits without asking the database
478+
* Returns true if attribute can be added to collection, throws exception otherwise
479+
*
480+
* @param Document $collection
481+
* @param Document $attribute
482+
*
483+
* @throws LimitException
484+
* @return bool
485+
*/
486+
public function checkAttribute(Document $collection, Document $attribute): bool
487+
{
488+
$collection = clone $collection;
489+
490+
$collection->setAttribute('attributes', $attribute, Document::SET_TYPE_APPEND);
491+
492+
if ($this->adapter->getAttributeLimit() > 0 &&
493+
$this->adapter->getAttributeCount($collection) > $this->adapter->getAttributeLimit())
494+
{
495+
throw new LimitException('Column limit reached. Cannot create new attribute.');
496+
return false;
497+
}
498+
499+
if ($this->adapter->getRowLimit() > 0 &&
500+
$this->adapter->getAttributeWidth($collection) >= $this->adapter->getRowLimit())
501+
{
502+
throw new LimitException('Row width limit reached. Cannot create new attribute.');
503+
return false;
504+
}
505+
506+
return true;
507+
}
508+
475509
/**
476510
* Delete Attribute
477511
*

tests/Database/Base.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,35 @@ public function testExceptionAttributeLimit()
12951295
$this->assertEquals(1,1);
12961296
}
12971297

1298+
/**
1299+
* @depends testExceptionAttributeLimit
1300+
*/
1301+
public function testCheckAttributeCountLimit()
1302+
{
1303+
if (static::getAdapterName() === 'mariadb' || static::getAdapterName() === 'mysql') {
1304+
$collection = static::getDatabase()->getCollection('attributeLimit');
1305+
1306+
// create same attribute in testExceptionAttributeLimit
1307+
$attribute = new Document([
1308+
'$id' => 'breaking',
1309+
'type' => Database::VAR_INTEGER,
1310+
'size' => 0,
1311+
'required' => true,
1312+
'default' => null,
1313+
'signed' => true,
1314+
'array' => false,
1315+
'filters' => [],
1316+
]);
1317+
1318+
$this->expectException(LimitException::class);
1319+
$this->assertEquals(false, static::getDatabase()->checkAttribute($collection, $attribute));
1320+
}
1321+
1322+
// Default assertion for other adapters
1323+
$this->assertEquals(1,1);
1324+
1325+
}
1326+
12981327
/**
12991328
* Using phpunit dataProviders to check that all these combinations of types/sizes throw exceptions
13001329
* https://phpunit.de/manual/3.7/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.data-providers
@@ -1393,6 +1422,35 @@ public function testExceptionWidthLimit($key, $stringSize, $stringCount, $intCou
13931422
$this->assertEquals(1,1);
13941423
}
13951424

1425+
/**
1426+
* @dataProvider rowWidthExceedsMaximum
1427+
* @depends testExceptionWidthLimit
1428+
*/
1429+
public function testCheckAttributeWidthLimit($key, $stringSize, $stringCount, $intCount, $floatCount, $boolCount)
1430+
{
1431+
if (static::getAdapterRowLimit() > 0) {
1432+
$collection = static::getDatabase()->getCollection("widthLimit{$key}");
1433+
1434+
// create same attribute in testExceptionWidthLimit
1435+
$attribute = new Document([
1436+
'$id' => 'breaking',
1437+
'type' => Database::VAR_STRING,
1438+
'size' => 100,
1439+
'required' => true,
1440+
'default' => null,
1441+
'signed' => true,
1442+
'array' => false,
1443+
'filters' => [],
1444+
]);
1445+
1446+
$this->expectException(LimitException::class);
1447+
$this->assertEquals(false, static::getDatabase()->checkAttribute($collection, $attribute));
1448+
}
1449+
1450+
// Default assertion for other adapters
1451+
$this->assertEquals(1,1);
1452+
}
1453+
13961454
public function testExceptionIndexLimit()
13971455
{
13981456
static::getDatabase()->createCollection('indexLimit');

0 commit comments

Comments
 (0)