From 769f1accc644876f45d8c77d86aa1b7297d70580 Mon Sep 17 00:00:00 2001 From: fogelito Date: Sun, 13 Jul 2025 09:57:01 +0300 Subject: [PATCH 01/24] Introduce ID type --- phpunit.xml | 2 +- src/Database/Adapter.php | 7 ++++ src/Database/Adapter/MariaDB.php | 3 ++ src/Database/Adapter/SQL.php | 12 ++++++ src/Database/Database.php | 8 ++++ src/Database/Validator/Sequence.php | 42 +++++++++++++++++++++ src/Database/Validator/Structure.php | 11 ++++-- tests/e2e/Adapter/Scopes/AttributeTests.php | 11 ++++-- tests/e2e/Adapter/Scopes/DocumentTests.php | 4 ++ 9 files changed, 92 insertions(+), 8 deletions(-) create mode 100644 src/Database/Validator/Sequence.php diff --git a/phpunit.xml b/phpunit.xml index 2a0531cfd..34365d48d 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,7 +7,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="false" + stopOnFailure="true" > diff --git a/src/Database/Adapter.php b/src/Database/Adapter.php index 88fd7d64f..27bff9647 100644 --- a/src/Database/Adapter.php +++ b/src/Database/Adapter.php @@ -854,6 +854,13 @@ abstract public function getMaxIndexLength(): int; */ abstract public function getMinDateTime(): \DateTime; + /** + * Get Id's attribute primitive type int or string + * + * @return string + */ + abstract public function getIdAttributeType(): string; + /** * Get the maximum supported DateTime value * diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 708926548..2a2d5955c 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -1904,6 +1904,9 @@ protected function getSQLType(string $type, int $size, bool $signed = true, bool } switch ($type) { + case Database::VAR_ID: + return 'bigint UNSIGNED'; + case Database::VAR_STRING: // $size = $size * 4; // Convert utf8mb4 size to bytes if ($size > 16777215) { diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index 31bc7e6a3..7f9ed8c83 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -1003,6 +1003,10 @@ public function getAttributeWidth(Document $collection): int } switch ($attribute['type']) { + case Database::VAR_ID: + $total += 8; // BIGINT 8 bytes + break; + case Database::VAR_STRING: /** * Text / Mediumtext / Longtext @@ -1577,6 +1581,14 @@ public function getMaxVarcharLength(): int return 16381; // Floor value for Postgres:16383 | MySQL:16381 | MariaDB:16382 } + /** + * @return string + */ + public function getIdAttributeType(): string + { + return 'string'; + } + /** * @return int */ diff --git a/src/Database/Database.php b/src/Database/Database.php index 0658065cc..39aa61108 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -40,6 +40,7 @@ class Database public const VAR_FLOAT = 'double'; public const VAR_BOOLEAN = 'boolean'; public const VAR_DATETIME = 'datetime'; + public const VAR_ID = 'id'; public const INT_MAX = 2147483647; public const BIG_INT_MAX = PHP_INT_MAX; @@ -1787,6 +1788,8 @@ private function validateAttribute( $this->checkAttribute($collection, $attribute); switch ($type) { + case self::VAR_ID: + break; case self::VAR_STRING: if ($size > $this->adapter->getLimitForString()) { throw new DatabaseException('Max size allowed for string is: ' . number_format($this->adapter->getLimitForString())); @@ -3646,6 +3649,7 @@ public function createDocument(string $collection, Document $document): Document $structure = new Structure( $collection, + $this->adapter->getIdAttributeType(), $this->adapter->getMinDateTime(), $this->adapter->getMaxDateTime(), ); @@ -3734,6 +3738,7 @@ public function createDocuments( $validator = new Structure( $collection, + $this->adapter->getIdAttributeType(), $this->adapter->getMinDateTime(), $this->adapter->getMaxDateTime(), ); @@ -4264,6 +4269,7 @@ public function updateDocument(string $collection, string $id, Document $documen $structureValidator = new Structure( $collection, + $this->adapter->getIdAttributeType(), $this->adapter->getMinDateTime(), $this->adapter->getMaxDateTime(), ); @@ -4381,6 +4387,7 @@ public function updateDocuments( // Check new document structure $validator = new PartialStructure( $collection, + $this->adapter->getIdAttributeType(), $this->adapter->getMinDateTime(), $this->adapter->getMaxDateTime(), ); @@ -5018,6 +5025,7 @@ public function createOrUpdateDocumentsWithIncrease( $validator = new Structure( $collection, + $this->adapter->getIdAttributeType(), $this->adapter->getMinDateTime(), $this->adapter->getMaxDateTime(), ); diff --git a/src/Database/Validator/Sequence.php b/src/Database/Validator/Sequence.php new file mode 100644 index 000000000..1e801e201 --- /dev/null +++ b/src/Database/Validator/Sequence.php @@ -0,0 +1,42 @@ +idAttributeType = $idAttributeType; + } + + public function isArray(): bool + { + return false; + } + + public function getType(): string + { + if($this->idAttributeType === 'string'){ + return self::TYPE_STRING; + } + + return self::TYPE_INTEGER; + } + + public function isValid($value): bool + { + return true; + } +} diff --git a/src/Database/Validator/Structure.php b/src/Database/Validator/Structure.php index 478557697..a870e6740 100644 --- a/src/Database/Validator/Structure.php +++ b/src/Database/Validator/Structure.php @@ -32,8 +32,8 @@ class Structure extends Validator ], [ '$id' => '$sequence', - 'type' => Database::VAR_STRING, - 'size' => 255, + 'type' => Database::VAR_ID, + 'size' => 0, 'required' => false, 'signed' => true, 'array' => false, @@ -50,7 +50,7 @@ class Structure extends Validator ], [ '$id' => '$tenant', - 'type' => Database::VAR_INTEGER, + 'type' => Database::VAR_INTEGER, // ????? 'size' => 8, 'required' => false, 'default' => null, @@ -103,6 +103,7 @@ class Structure extends Validator */ public function __construct( protected readonly Document $collection, + private readonly string $idAttributeType, private readonly \DateTime $minAllowedDate = new \DateTime('0000-01-01'), private readonly \DateTime $maxAllowedDate = new \DateTime('9999-12-31'), ) { @@ -315,6 +316,10 @@ protected function checkForInvalidAttributeValues(array $structure, array $keys) $validators = []; switch ($type) { + case Database::VAR_ID: + $validators[] = new Sequence($this->idAttributeType); + break; + case Database::VAR_STRING: $validators[] = new Text($size, min: 0); break; diff --git a/tests/e2e/Adapter/Scopes/AttributeTests.php b/tests/e2e/Adapter/Scopes/AttributeTests.php index fa401db2a..20fd695a2 100644 --- a/tests/e2e/Adapter/Scopes/AttributeTests.php +++ b/tests/e2e/Adapter/Scopes/AttributeTests.php @@ -71,14 +71,16 @@ public function testCreateDeleteAttribute(): void $this->assertEquals(true, $database->createAttribute('attributes', 'bigint', Database::VAR_INTEGER, 8, true)); $this->assertEquals(true, $database->createAttribute('attributes', 'float', Database::VAR_FLOAT, 0, true)); $this->assertEquals(true, $database->createAttribute('attributes', 'boolean', Database::VAR_BOOLEAN, 0, true)); + $this->assertEquals(true, $database->createAttribute('attributes', 'id', Database::VAR_ID, 0, true)); + $this->assertEquals(true, $database->createIndex('attributes', 'id_index', Database::INDEX_KEY, ['id'])); $this->assertEquals(true, $database->createIndex('attributes', 'string1_index', Database::INDEX_KEY, ['string1'])); $this->assertEquals(true, $database->createIndex('attributes', 'string2_index', Database::INDEX_KEY, ['string2'], [255])); $this->assertEquals(true, $database->createIndex('attributes', 'multi_index', Database::INDEX_KEY, ['string1', 'string2', 'string3'], [128, 128, 128])); $collection = $database->getCollection('attributes'); - $this->assertCount(8, $collection->getAttribute('attributes')); - $this->assertCount(3, $collection->getAttribute('indexes')); + $this->assertCount(9, $collection->getAttribute('attributes')); + $this->assertCount(4, $collection->getAttribute('indexes')); // Array $this->assertEquals(true, $database->createAttribute('attributes', 'string_list', Database::VAR_STRING, 128, true, null, true, true)); @@ -87,7 +89,7 @@ public function testCreateDeleteAttribute(): void $this->assertEquals(true, $database->createAttribute('attributes', 'boolean_list', Database::VAR_BOOLEAN, 0, true, null, true, true)); $collection = $database->getCollection('attributes'); - $this->assertCount(12, $collection->getAttribute('attributes')); + $this->assertCount(13, $collection->getAttribute('attributes')); // Default values $this->assertEquals(true, $database->createAttribute('attributes', 'string_default', Database::VAR_STRING, 256, false, 'test')); @@ -97,7 +99,7 @@ public function testCreateDeleteAttribute(): void $this->assertEquals(true, $database->createAttribute('attributes', 'datetime_default', Database::VAR_DATETIME, 0, false, '2000-06-12T14:12:55.000+00:00', true, false, null, [], ['datetime'])); $collection = $database->getCollection('attributes'); - $this->assertCount(17, $collection->getAttribute('attributes')); + $this->assertCount(18, $collection->getAttribute('attributes')); // Delete $this->assertEquals(true, $database->deleteAttribute('attributes', 'string1')); @@ -108,6 +110,7 @@ public function testCreateDeleteAttribute(): void $this->assertEquals(true, $database->deleteAttribute('attributes', 'bigint')); $this->assertEquals(true, $database->deleteAttribute('attributes', 'float')); $this->assertEquals(true, $database->deleteAttribute('attributes', 'boolean')); + $this->assertEquals(true, $database->deleteAttribute('attributes', 'id')); $collection = $database->getCollection('attributes'); $this->assertCount(9, $collection->getAttribute('attributes')); diff --git a/tests/e2e/Adapter/Scopes/DocumentTests.php b/tests/e2e/Adapter/Scopes/DocumentTests.php index 50bbcba57..c807155c4 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -39,6 +39,7 @@ public function testCreateDocument(): Document $this->assertEquals(true, $database->createAttribute('documents', 'colors', Database::VAR_STRING, 32, true, null, true, true)); $this->assertEquals(true, $database->createAttribute('documents', 'empty', Database::VAR_STRING, 32, false, null, true, true)); $this->assertEquals(true, $database->createAttribute('documents', 'with-dash', Database::VAR_STRING, 128, false, null)); + $this->assertEquals(true, $database->createAttribute('documents', 'id', Database::VAR_ID, 0, false, null)); $document = $database->createDocument('documents', new Document([ '$permissions' => [ @@ -66,8 +67,11 @@ public function testCreateDocument(): Document 'colors' => ['pink', 'green', 'blue'], 'empty' => [], 'with-dash' => 'Works', + 'id' => '1000000', ])); + $this->assertIsString($document->getAttribute('id')); + $this->assertEquals('1000000', $document->getAttribute('id')); $this->assertNotEmpty(true, $document->getId()); $this->assertIsString($document->getAttribute('string')); $this->assertEquals('text📝', $document->getAttribute('string')); // Also makes sure an emoji is working From 06f06fead3294b7c246078a3ab928a32d4d3d150 Mon Sep 17 00:00:00 2001 From: fogelito Date: Sun, 13 Jul 2025 10:31:21 +0300 Subject: [PATCH 02/24] Sequence validator --- src/Database/Adapter/SQL.php | 2 +- src/Database/Validator/Sequence.php | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index 7f9ed8c83..ab056e908 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -1586,7 +1586,7 @@ public function getMaxVarcharLength(): int */ public function getIdAttributeType(): string { - return 'string'; + return 'int'; } /** diff --git a/src/Database/Validator/Sequence.php b/src/Database/Validator/Sequence.php index 1e801e201..ac9bc7757 100644 --- a/src/Database/Validator/Sequence.php +++ b/src/Database/Validator/Sequence.php @@ -2,7 +2,10 @@ namespace Utopia\Database\Validator; +use Utopia\Database\Database; use Utopia\Validator; +use Utopia\Validator\Integer; +use Utopia\Validator\Range; class Sequence extends Validator { @@ -37,6 +40,25 @@ public function getType(): string public function isValid($value): bool { - return true; + if ($this->idAttributeType === 'string') { + return preg_match('/^[a-f0-9]{24}$/i', $value) === 1; + } + else if ($this->idAttributeType === 'int') { + $value = (int)$value; + + $validator = new Integer(); + if (!$validator->isValid($value)){ + return false; + } + + $validator = new Range(1, Database::BIG_INT_MAX, Database::VAR_INTEGER); + if (!$validator->isValid($value)){ + return false; + } + + return true; + } + + return false; } } From 5b4bb75a187556b56b759e05a156e00e3b7d3a1c Mon Sep 17 00:00:00 2001 From: fogelito Date: Sun, 13 Jul 2025 15:17:49 +0300 Subject: [PATCH 03/24] Hnadle zero and null to ID attribute --- src/Database/Adapter/Postgres.php | 3 + src/Database/Adapter/SQL.php | 1 + src/Database/Database.php | 6 +- src/Database/Validator/Queries/Documents.php | 2 +- src/Database/Validator/Query/Filter.php | 9 ++ src/Database/Validator/Sequence.php | 21 ++++- src/Database/Validator/Structure.php | 8 +- tests/e2e/Adapter/Scopes/DocumentTests.php | 95 +++++++++++++++++++- 8 files changed, 136 insertions(+), 9 deletions(-) diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index aad33c42e..c4895017f 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -1817,6 +1817,9 @@ protected function getSQLType(string $type, int $size, bool $signed = true, bool } switch ($type) { + case Database::VAR_ID: + return 'bigint'; + case Database::VAR_STRING: // $size = $size * 4; // Convert utf8mb4 size to bytes if ($size > $this->getMaxVarcharLength()) { diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index ab056e908..7324e789f 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -1003,6 +1003,7 @@ public function getAttributeWidth(Document $collection): int } switch ($attribute['type']) { + case Database::VAR_SEQUENCE: case Database::VAR_ID: $total += 8; // BIGINT 8 bytes break; diff --git a/src/Database/Database.php b/src/Database/Database.php index 39aa61108..1c040d1f0 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -41,6 +41,7 @@ class Database public const VAR_BOOLEAN = 'boolean'; public const VAR_DATETIME = 'datetime'; public const VAR_ID = 'id'; + public const VAR_SEQUENCE = 'sequence'; public const INT_MAX = 2147483647; public const BIG_INT_MAX = PHP_INT_MAX; @@ -167,8 +168,8 @@ class Database ], [ '$id' => '$sequence', - 'type' => self::VAR_STRING, - 'size' => Database::LENGTH_KEY, + 'type' => self::VAR_SEQUENCE, + 'size' => 0, 'required' => true, 'signed' => true, 'array' => false, @@ -1789,6 +1790,7 @@ private function validateAttribute( switch ($type) { case self::VAR_ID: + break; case self::VAR_STRING: if ($size > $this->adapter->getLimitForString()) { diff --git a/src/Database/Validator/Queries/Documents.php b/src/Database/Validator/Queries/Documents.php index 4e5c13f5f..2e1be4b37 100644 --- a/src/Database/Validator/Queries/Documents.php +++ b/src/Database/Validator/Queries/Documents.php @@ -38,7 +38,7 @@ public function __construct( $attributes[] = new Document([ '$id' => '$sequence', 'key' => '$sequence', - 'type' => Database::VAR_STRING, + 'type' => Database::VAR_SEQUENCE, 'array' => false, ]); $attributes[] = new Document([ diff --git a/src/Database/Validator/Query/Filter.php b/src/Database/Validator/Query/Filter.php index 9fb3fe32e..95dd8ef4c 100644 --- a/src/Database/Validator/Query/Filter.php +++ b/src/Database/Validator/Query/Filter.php @@ -6,6 +6,7 @@ use Utopia\Database\Document; use Utopia\Database\Query; use Utopia\Database\Validator\Datetime as DatetimeValidator; +use Utopia\Database\Validator\Sequence; use Utopia\Validator\Boolean; use Utopia\Validator\FloatValidator; use Utopia\Validator\Integer; @@ -106,6 +107,14 @@ protected function isValidAttributeAndValues(string $attribute, array $values, s $validator = null; switch ($attributeType) { + case Database::VAR_SEQUENCE: + $validator = new Sequence('int', true); + break; + + case Database::VAR_ID: + $validator = new Sequence('int', false); + break; + case Database::VAR_STRING: $validator = new Text(0, 0); break; diff --git a/src/Database/Validator/Sequence.php b/src/Database/Validator/Sequence.php index ac9bc7757..ea3676ef6 100644 --- a/src/Database/Validator/Sequence.php +++ b/src/Database/Validator/Sequence.php @@ -10,6 +10,7 @@ class Sequence extends Validator { private string $idAttributeType; + private bool $primary; public function getDescription(): string { @@ -19,8 +20,9 @@ public function getDescription(): string /** * Expression constructor */ - public function __construct(string $idAttributeType) + public function __construct(string $idAttributeType, bool $primary) { + $this->primary = $primary; $this->idAttributeType = $idAttributeType; } @@ -40,6 +42,19 @@ public function getType(): string public function isValid($value): bool { + if ($this->primary && empty($value)) { + return false; + } + + $type = gettype($value); + +var_dump('========'); +var_dump($type); +var_dump($value); + if($type !== 'string'){ + return false; + } + if ($this->idAttributeType === 'string') { return preg_match('/^[a-f0-9]{24}$/i', $value) === 1; } @@ -51,7 +66,9 @@ public function isValid($value): bool return false; } - $validator = new Range(1, Database::BIG_INT_MAX, Database::VAR_INTEGER); + $start = ($this->primary) ? 1:0; + + $validator = new Range($start, Database::BIG_INT_MAX, Database::VAR_INTEGER); if (!$validator->isValid($value)){ return false; } diff --git a/src/Database/Validator/Structure.php b/src/Database/Validator/Structure.php index a870e6740..dd4156b58 100644 --- a/src/Database/Validator/Structure.php +++ b/src/Database/Validator/Structure.php @@ -32,7 +32,7 @@ class Structure extends Validator ], [ '$id' => '$sequence', - 'type' => Database::VAR_ID, + 'type' => Database::VAR_SEQUENCE, 'size' => 0, 'required' => false, 'signed' => true, @@ -316,8 +316,12 @@ protected function checkForInvalidAttributeValues(array $structure, array $keys) $validators = []; switch ($type) { + case Database::VAR_SEQUENCE: + $validators[] = new Sequence($this->idAttributeType, true); + break; + case Database::VAR_ID: - $validators[] = new Sequence($this->idAttributeType); + $validators[] = new Sequence($this->idAttributeType, false); break; case Database::VAR_STRING: diff --git a/tests/e2e/Adapter/Scopes/DocumentTests.php b/tests/e2e/Adapter/Scopes/DocumentTests.php index c807155c4..cc4dcd49f 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -70,8 +70,6 @@ public function testCreateDocument(): Document 'id' => '1000000', ])); - $this->assertIsString($document->getAttribute('id')); - $this->assertEquals('1000000', $document->getAttribute('id')); $this->assertNotEmpty(true, $document->getId()); $this->assertIsString($document->getAttribute('string')); $this->assertEquals('text📝', $document->getAttribute('string')); // Also makes sure an emoji is working @@ -93,6 +91,8 @@ public function testCreateDocument(): Document $this->assertEquals(['pink', 'green', 'blue'], $document->getAttribute('colors')); $this->assertEquals([], $document->getAttribute('empty')); $this->assertEquals('Works', $document->getAttribute('with-dash')); + $this->assertIsString($document->getAttribute('id')); + $this->assertEquals('1000000', $document->getAttribute('id')); // Test create document with manual internal id $manualIdDocument = $database->createDocument('documents', new Document([ @@ -147,6 +147,7 @@ public function testCreateDocument(): Document $this->assertEquals(['pink', 'green', 'blue'], $manualIdDocument->getAttribute('colors')); $this->assertEquals([], $manualIdDocument->getAttribute('empty')); $this->assertEquals('Works', $manualIdDocument->getAttribute('with-dash')); + $this->assertEquals(null, $manualIdDocument->getAttribute('id')); $manualIdDocument = $database->getDocument('documents', '56000'); @@ -211,6 +212,96 @@ public function testCreateDocument(): Document $this->assertEquals('Invalid document structure: Attribute "bigint_unsigned" has invalid type. Value must be a valid range between 0 and 9,223,372,036,854,775,807', $e->getMessage()); } + try { + $database->createDocument('documents', new Document([ + '$sequence' => '0', + '$permissions' => [], + 'string' => '', + 'integer_signed' => 1, + 'integer_unsigned' => 1, + 'bigint_signed' => 1, + 'bigint_unsigned' => 1, + 'float_signed' => 1, + 'float_unsigned' => 1, + 'boolean' => true, + 'colors' => [], + 'empty' => [], + 'with-dash' => '', + ])); + $this->fail('Failed to throw exception'); + } catch (Throwable $e) { + $this->assertTrue($e instanceof StructureException); + $this->assertEquals('Invalid document structure: Attribute "$sequence" has invalid type. Invalid sequence value', $e->getMessage()); + } + + /** + * Insert ID attribute with NULL + */ + + $documentIdNull = $database->createDocument('documents', new Document([ + 'id' => null, + '$permissions' => [Permission::read(Role::any())], + 'string' => '', + 'integer_signed' => 1, + 'integer_unsigned' => 1, + 'bigint_signed' => 1, + 'bigint_unsigned' => 1, + 'float_signed' => 1, + 'float_unsigned' => 1, + 'boolean' => true, + 'colors' => [], + 'empty' => [], + 'with-dash' => '', + ])); + $this->assertNull($documentIdNull->getAttribute('id')); + + $documentIdNull = $database->getDocument('documents', $documentIdNull->getId()); + $this->assertNotEmpty(true, $documentIdNull->getId()); + $this->assertNull($documentIdNull->getAttribute('id')); + var_dump($documentIdNull); + + $documentIdNull = $database->findOne('documents', [ + query::isNull('id') + ]); + var_dump($documentIdNull); + $this->assertNotEmpty(true, $documentIdNull->getId()); + $this->assertNull($documentIdNull->getAttribute('id')); + + /** + * Insert ID attribute with '0' + */ + $documentId0 = $database->createDocument('documents', new Document([ + 'id' => '0', + '$permissions' => [Permission::read(Role::any())], + 'string' => '', + 'integer_signed' => 1, + 'integer_unsigned' => 1, + 'bigint_signed' => 1, + 'bigint_unsigned' => 1, + 'float_signed' => 1, + 'float_unsigned' => 1, + 'boolean' => true, + 'colors' => [], + 'empty' => [], + 'with-dash' => '', + ])); + $this->assertIsString($documentId0->getAttribute('id')); + $this->assertEquals('0', $documentId0->getAttribute('id')); + + $documentId0 = $database->getDocument('documents', $documentId0->getId()); + $this->assertNotEmpty(true, $documentId0->getId()); + $this->assertIsString($documentId0->getAttribute('id')); + $this->assertEquals('0', $documentId0->getAttribute('id')); + + $documentId0 = $database->findOne('documents', [ + query::equal('id', ['0']) + ]); +var_dump($documentId0); + $this->assertNotEmpty(true, $documentId0->getId()); + $this->assertIsString($documentId0->getAttribute('id')); + $this->assertEquals('0', $documentId0->getAttribute('id')); + + return $document; } From a78d77743f1b1e065b711431ce02c6ed12808227 Mon Sep 17 00:00:00 2001 From: fogelito Date: Sun, 13 Jul 2025 15:32:32 +0300 Subject: [PATCH 04/24] Remove var_dumps --- src/Database/Validator/Sequence.php | 5 +---- tests/e2e/Adapter/Scopes/DocumentTests.php | 9 ++++----- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/Database/Validator/Sequence.php b/src/Database/Validator/Sequence.php index ea3676ef6..ccfb72d4c 100644 --- a/src/Database/Validator/Sequence.php +++ b/src/Database/Validator/Sequence.php @@ -48,10 +48,7 @@ public function isValid($value): bool $type = gettype($value); -var_dump('========'); -var_dump($type); -var_dump($value); - if($type !== 'string'){ + if ($type !== 'string'){ return false; } diff --git a/tests/e2e/Adapter/Scopes/DocumentTests.php b/tests/e2e/Adapter/Scopes/DocumentTests.php index cc4dcd49f..572b2d868 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -253,17 +253,16 @@ public function testCreateDocument(): Document 'empty' => [], 'with-dash' => '', ])); + $this->assertNotEmpty(true, $documentIdNull->getSequence()); $this->assertNull($documentIdNull->getAttribute('id')); $documentIdNull = $database->getDocument('documents', $documentIdNull->getId()); $this->assertNotEmpty(true, $documentIdNull->getId()); $this->assertNull($documentIdNull->getAttribute('id')); - var_dump($documentIdNull); $documentIdNull = $database->findOne('documents', [ query::isNull('id') ]); - var_dump($documentIdNull); $this->assertNotEmpty(true, $documentIdNull->getId()); $this->assertNull($documentIdNull->getAttribute('id')); @@ -285,19 +284,19 @@ public function testCreateDocument(): Document 'empty' => [], 'with-dash' => '', ])); + $this->assertNotEmpty(true, $documentId0->getSequence()); $this->assertIsString($documentId0->getAttribute('id')); $this->assertEquals('0', $documentId0->getAttribute('id')); $documentId0 = $database->getDocument('documents', $documentId0->getId()); - $this->assertNotEmpty(true, $documentId0->getId()); + $this->assertNotEmpty(true, $documentId0->getSequence()); $this->assertIsString($documentId0->getAttribute('id')); $this->assertEquals('0', $documentId0->getAttribute('id')); $documentId0 = $database->findOne('documents', [ query::equal('id', ['0']) ]); -var_dump($documentId0); - $this->assertNotEmpty(true, $documentId0->getId()); + $this->assertNotEmpty(true, $documentId0->getSequence()); $this->assertIsString($documentId0->getAttribute('id')); $this->assertEquals('0', $documentId0->getAttribute('id')); From 0902764260c4501c81f3904aea7274e79f73c337 Mon Sep 17 00:00:00 2001 From: fogelito Date: Sun, 13 Jul 2025 15:41:40 +0300 Subject: [PATCH 05/24] Add attribute with schema --- tests/e2e/Adapter/Scopes/CollectionTests.php | 25 ++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/tests/e2e/Adapter/Scopes/CollectionTests.php b/tests/e2e/Adapter/Scopes/CollectionTests.php index 731525f81..7a4d818ff 100644 --- a/tests/e2e/Adapter/Scopes/CollectionTests.php +++ b/tests/e2e/Adapter/Scopes/CollectionTests.php @@ -107,6 +107,15 @@ public function testCreateCollectionWithSchema(): void 'array' => false, 'filters' => [], ]), + new Document([ + '$id' => ID::custom('attribute4'), + 'type' => Database::VAR_ID, + 'size' => 0, + 'required' => false, + 'signed' => false, + 'array' => false, + 'filters' => [], + ]), ]; $indexes = [ @@ -131,6 +140,13 @@ public function testCreateCollectionWithSchema(): void 'lengths' => [], 'orders' => ['DESC', 'ASC'], ]), + new Document([ + '$id' => ID::custom('index4'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['attribute4'], + 'lengths' => [], + 'orders' => ['DESC', 'ASC'], + ]), ]; $collection = $database->createCollection('withSchema', $attributes, $indexes); @@ -139,22 +155,27 @@ public function testCreateCollectionWithSchema(): void $this->assertEquals('withSchema', $collection->getId()); $this->assertIsArray($collection->getAttribute('attributes')); - $this->assertCount(3, $collection->getAttribute('attributes')); + $this->assertCount(4, $collection->getAttribute('attributes')); $this->assertEquals('attribute1', $collection->getAttribute('attributes')[0]['$id']); $this->assertEquals(Database::VAR_STRING, $collection->getAttribute('attributes')[0]['type']); $this->assertEquals('attribute2', $collection->getAttribute('attributes')[1]['$id']); $this->assertEquals(Database::VAR_INTEGER, $collection->getAttribute('attributes')[1]['type']); $this->assertEquals('attribute3', $collection->getAttribute('attributes')[2]['$id']); $this->assertEquals(Database::VAR_BOOLEAN, $collection->getAttribute('attributes')[2]['type']); + $this->assertEquals('attribute4', $collection->getAttribute('attributes')[3]['$id']); + $this->assertEquals(Database::VAR_ID, $collection->getAttribute('attributes')[3]['type']); $this->assertIsArray($collection->getAttribute('indexes')); - $this->assertCount(3, $collection->getAttribute('indexes')); + $this->assertCount(4, $collection->getAttribute('indexes')); $this->assertEquals('index1', $collection->getAttribute('indexes')[0]['$id']); $this->assertEquals(Database::INDEX_KEY, $collection->getAttribute('indexes')[0]['type']); $this->assertEquals('index2', $collection->getAttribute('indexes')[1]['$id']); $this->assertEquals(Database::INDEX_KEY, $collection->getAttribute('indexes')[1]['type']); $this->assertEquals('index3', $collection->getAttribute('indexes')[2]['$id']); $this->assertEquals(Database::INDEX_KEY, $collection->getAttribute('indexes')[2]['type']); + $this->assertEquals('index4', $collection->getAttribute('indexes')[3]['$id']); + $this->assertEquals(Database::INDEX_KEY, $collection->getAttribute('indexes')[3]['type']); + $database->deleteCollection('withSchema'); From 5eb97a56fe82c7a7d871e33ecf2eaea08ae9ce4a Mon Sep 17 00:00:00 2001 From: fogelito Date: Sun, 13 Jul 2025 17:49:18 +0300 Subject: [PATCH 06/24] Fix unit test --- src/Database/Adapter/Pool.php | 5 + src/Database/Validator/Sequence.php | 17 +-- tests/unit/Validator/StructureTest.php | 182 ++++++++++++++++++++++--- 3 files changed, 174 insertions(+), 30 deletions(-) diff --git a/src/Database/Adapter/Pool.php b/src/Database/Adapter/Pool.php index 302338aa9..1fe2c1d37 100644 --- a/src/Database/Adapter/Pool.php +++ b/src/Database/Adapter/Pool.php @@ -489,4 +489,9 @@ protected function execute(mixed $stmt): bool { return $this->delegate(__FUNCTION__, \func_get_args()); } + + public function getIdAttributeType(): string + { + return $this->delegate(__FUNCTION__, \func_get_args()); + } } diff --git a/src/Database/Validator/Sequence.php b/src/Database/Validator/Sequence.php index ccfb72d4c..1bcc5246d 100644 --- a/src/Database/Validator/Sequence.php +++ b/src/Database/Validator/Sequence.php @@ -33,7 +33,7 @@ public function isArray(): bool public function getType(): string { - if($this->idAttributeType === 'string'){ + if ($this->idAttributeType === 'string') { return self::TYPE_STRING; } @@ -48,25 +48,22 @@ public function isValid($value): bool $type = gettype($value); - if ($type !== 'string'){ + if ($type !== 'string') { return false; } if ($this->idAttributeType === 'string') { return preg_match('/^[a-f0-9]{24}$/i', $value) === 1; - } - else if ($this->idAttributeType === 'int') { - $value = (int)$value; - - $validator = new Integer(); - if (!$validator->isValid($value)){ + } elseif ($this->idAttributeType === 'int') { + $validator = new Integer(loose: true); + if (!$validator->isValid($value)) { return false; } - $start = ($this->primary) ? 1:0; + $start = ($this->primary) ? 1 : 0; $validator = new Range($start, Database::BIG_INT_MAX, Database::VAR_INTEGER); - if (!$validator->isValid($value)){ + if (!$validator->isValid($value)) { return false; } diff --git a/tests/unit/Validator/StructureTest.php b/tests/unit/Validator/StructureTest.php index a9f641038..6d0a11f2d 100644 --- a/tests/unit/Validator/StructureTest.php +++ b/tests/unit/Validator/StructureTest.php @@ -90,6 +90,16 @@ class StructureTest extends TestCase 'array' => true, 'filters' => [], ], + [ + '$id' => 'id', + 'type' => Database::VAR_ID, + 'format' => '', + 'size' => 0, + 'required' => false, + 'signed' => false, + 'array' => false, + 'filters' => [], + ], ], 'indexes' => [], ]; @@ -121,7 +131,10 @@ public function tearDown(): void public function testDocumentInstance(): void { - $validator = new Structure(new Document($this->collection)); + $validator = new Structure( + new Document($this->collection), + 'int' + ); $this->assertEquals(false, $validator->isValid('string')); $this->assertEquals(false, $validator->isValid(null)); @@ -133,7 +146,10 @@ public function testDocumentInstance(): void public function testCollectionAttribute(): void { - $validator = new Structure(new Document()); + $validator = new Structure( + new Document($this->collection), + 'int' + ); $this->assertEquals(false, $validator->isValid(new Document())); @@ -142,7 +158,10 @@ public function testCollectionAttribute(): void public function testCollection(): void { - $validator = new Structure(new Document()); + $validator = new Structure( + new Document(), + 'int' + ); $this->assertEquals(false, $validator->isValid(new Document([ '$collection' => ID::custom('posts'), @@ -160,7 +179,10 @@ public function testCollection(): void public function testRequiredKeys(): void { - $validator = new Structure(new Document($this->collection)); + $validator = new Structure( + new Document($this->collection), + 'int' + ); $this->assertEquals(false, $validator->isValid(new Document([ '$collection' => ID::custom('posts'), @@ -177,7 +199,10 @@ public function testRequiredKeys(): void public function testNullValues(): void { - $validator = new Structure(new Document($this->collection)); + $validator = new Structure( + new Document($this->collection), + 'int' + ); $this->assertEquals(true, $validator->isValid(new Document([ '$collection' => ID::custom('posts'), @@ -188,6 +213,7 @@ public function testNullValues(): void 'published' => true, 'tags' => ['dog', 'cat', 'mouse'], 'feedback' => 'team@appwrite.io', + 'id' => '1000', ]))); $this->assertEquals(true, $validator->isValid(new Document([ @@ -204,7 +230,10 @@ public function testNullValues(): void public function testUnknownKeys(): void { - $validator = new Structure(new Document($this->collection)); + $validator = new Structure( + new Document($this->collection), + 'int' + ); $this->assertEquals(false, $validator->isValid(new Document([ '$collection' => ID::custom('posts'), @@ -223,7 +252,10 @@ public function testUnknownKeys(): void public function testIntegerAsString(): void { - $validator = new Structure(new Document($this->collection)); + $validator = new Structure( + new Document($this->collection), + 'int' + ); $this->assertEquals(false, $validator->isValid(new Document([ '$collection' => ID::custom('posts'), @@ -241,7 +273,10 @@ public function testIntegerAsString(): void public function testValidDocument(): void { - $validator = new Structure(new Document($this->collection)); + $validator = new Structure( + new Document($this->collection), + 'int' + ); $this->assertEquals(true, $validator->isValid(new Document([ '$collection' => ID::custom('posts'), @@ -257,7 +292,10 @@ public function testValidDocument(): void public function testStringValidation(): void { - $validator = new Structure(new Document($this->collection)); + $validator = new Structure( + new Document($this->collection), + 'int' + ); $this->assertEquals(false, $validator->isValid(new Document([ '$collection' => ID::custom('posts'), @@ -275,7 +313,10 @@ public function testStringValidation(): void public function testArrayOfStringsValidation(): void { - $validator = new Structure(new Document($this->collection)); + $validator = new Structure( + new Document($this->collection), + 'int' + ); $this->assertEquals(false, $validator->isValid(new Document([ '$collection' => ID::custom('posts'), @@ -333,7 +374,10 @@ public function testArrayOfStringsValidation(): void */ public function testArrayAsObjectValidation(): void { - $validator = new Structure(new Document($this->collection)); + $validator = new Structure( + new Document($this->collection), + 'int' + ); $this->assertEquals(false, $validator->isValid(new Document([ '$collection' => ID::custom('posts'), @@ -349,7 +393,10 @@ public function testArrayAsObjectValidation(): void public function testArrayOfObjectsValidation(): void { - $validator = new Structure(new Document($this->collection)); + $validator = new Structure( + new Document($this->collection), + 'int' + ); $this->assertEquals(false, $validator->isValid(new Document([ '$collection' => ID::custom('posts'), @@ -365,7 +412,10 @@ public function testArrayOfObjectsValidation(): void public function testIntegerValidation(): void { - $validator = new Structure(new Document($this->collection)); + $validator = new Structure( + new Document($this->collection), + 'int' + ); $this->assertEquals(false, $validator->isValid(new Document([ '$collection' => ID::custom('posts'), @@ -396,7 +446,10 @@ public function testIntegerValidation(): void public function testArrayOfIntegersValidation(): void { - $validator = new Structure(new Document($this->collection)); + $validator = new Structure( + new Document($this->collection), + 'int' + ); $this->assertEquals(true, $validator->isValid(new Document([ '$collection' => ID::custom('posts'), @@ -451,7 +504,10 @@ public function testArrayOfIntegersValidation(): void public function testFloatValidation(): void { - $validator = new Structure(new Document($this->collection)); + $validator = new Structure( + new Document($this->collection), + 'int' + ); $this->assertEquals(false, $validator->isValid(new Document([ '$collection' => ID::custom('posts'), @@ -482,7 +538,10 @@ public function testFloatValidation(): void public function testBooleanValidation(): void { - $validator = new Structure(new Document($this->collection)); + $validator = new Structure( + new Document($this->collection), + 'int' + ); $this->assertEquals(false, $validator->isValid(new Document([ '$collection' => ID::custom('posts'), @@ -513,7 +572,10 @@ public function testBooleanValidation(): void public function testFormatValidation(): void { - $validator = new Structure(new Document($this->collection)); + $validator = new Structure( + new Document($this->collection), + 'int' + ); $this->assertEquals(false, $validator->isValid(new Document([ '$collection' => ID::custom('posts'), @@ -531,7 +593,10 @@ public function testFormatValidation(): void public function testIntegerMaxRange(): void { - $validator = new Structure(new Document($this->collection)); + $validator = new Structure( + new Document($this->collection), + 'int' + ); $this->assertEquals(false, $validator->isValid(new Document([ '$collection' => ID::custom('posts'), @@ -549,7 +614,10 @@ public function testIntegerMaxRange(): void public function testDoubleUnsigned(): void { - $validator = new Structure(new Document($this->collection)); + $validator = new Structure( + new Document($this->collection), + 'int' + ); $this->assertEquals(false, $validator->isValid(new Document([ '$collection' => ID::custom('posts'), @@ -567,7 +635,10 @@ public function testDoubleUnsigned(): void public function testDoubleMaxRange(): void { - $validator = new Structure(new Document($this->collection)); + $validator = new Structure( + new Document($this->collection), + 'int' + ); $this->assertEquals(false, $validator->isValid(new Document([ '$collection' => ID::custom('posts'), @@ -581,4 +652,75 @@ public function testDoubleMaxRange(): void ]))); } + public function testId(): void + { + $validator = new Structure( + new Document($this->collection), + 'int' + ); + + $id = '1000'; + $mongoid = '507f1f77bcf86cd799439011'; + + /** + * Sql + */ + + $this->assertEquals(true, $validator->isValid(new Document([ + '$collection' => ID::custom('posts'), + 'title' => 'My Title', + 'description' => null, + 'rating' => 5, + 'price' => 1.99, + 'published' => true, + 'tags' => ['dog', 'cat', 'mouse'], + 'feedback' => 'team@appwrite.io', + 'id' => $id, + ]))); + + $this->assertEquals(false, $validator->isValid(new Document([ + '$collection' => ID::custom('posts'), + 'title' => 'My Title', + 'description' => null, + 'rating' => 5, + 'price' => 1.99, + 'published' => true, + 'tags' => ['dog', 'cat', 'mouse'], + 'feedback' => 'team@appwrite.io', + 'id' => $mongoid, + ]))); + + /** + * Mongo + */ + $validator = new Structure( + new Document($this->collection), + 'string' + ); + + $this->assertEquals(true, $validator->isValid(new Document([ + '$collection' => ID::custom('posts'), + 'title' => 'My Title', + 'description' => null, + 'rating' => 5, + 'price' => 1.99, + 'published' => true, + 'tags' => ['dog', 'cat', 'mouse'], + 'feedback' => 'team@appwrite.io', + 'id' => $mongoid, + ]))); + + $this->assertEquals(true, $validator->isValid(new Document([ + '$collection' => ID::custom('posts'), + 'title' => 'My Title', + 'description' => null, + 'rating' => 5, + 'price' => 1.99, + 'published' => true, + 'tags' => ['dog', 'cat', 'mouse'], + 'feedback' => 'team@appwrite.io', + 'id' => $mongoid, + ]))); + } + } From c2e36795892f19558364890051a22b334d670bf3 Mon Sep 17 00:00:00 2001 From: fogelito Date: Sun, 13 Jul 2025 18:37:35 +0300 Subject: [PATCH 07/24] Fix index --- tests/e2e/Adapter/Scopes/CollectionTests.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/Adapter/Scopes/CollectionTests.php b/tests/e2e/Adapter/Scopes/CollectionTests.php index 7a4d818ff..346cfb5db 100644 --- a/tests/e2e/Adapter/Scopes/CollectionTests.php +++ b/tests/e2e/Adapter/Scopes/CollectionTests.php @@ -145,7 +145,7 @@ public function testCreateCollectionWithSchema(): void 'type' => Database::INDEX_KEY, 'attributes' => ['attribute4'], 'lengths' => [], - 'orders' => ['DESC', 'ASC'], + 'orders' => ['DESC'], ]), ]; From 41e6f760857fb0fcb3594d47c9b385fe55cb10ce Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 14 Jul 2025 10:16:49 +0300 Subject: [PATCH 08/24] Add $idAttributeType --- src/Database/Database.php | 5 ++ src/Database/Validator/Queries/Documents.php | 3 + src/Database/Validator/Query/Filter.php | 7 ++- tests/unit/Validator/DocumentsQueriesTest.php | 25 ++++++++- tests/unit/Validator/IndexedQueriesTest.php | 6 +- tests/unit/Validator/QueriesTest.php | 2 +- tests/unit/Validator/Query/FilterTest.php | 56 +++++++++---------- tests/unit/Validator/QueryTest.php | 20 +++---- 8 files changed, 77 insertions(+), 47 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 1c040d1f0..b2d5c0d55 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -4354,6 +4354,7 @@ public function updateDocuments( $validator = new DocumentsValidator( $attributes, $indexes, + $this->adapter->getIdAttributeType(), $this->maxQueryValues, $this->adapter->getMinDateTime(), $this->adapter->getMaxDateTime(), @@ -5804,6 +5805,7 @@ public function deleteDocuments( $validator = new DocumentsValidator( $attributes, $indexes, + $this->adapter->getIdAttributeType(), $this->maxQueryValues, $this->adapter->getMinDateTime(), $this->adapter->getMaxDateTime() @@ -5995,6 +5997,7 @@ public function find(string $collection, array $queries = [], string $forPermiss $validator = new DocumentsValidator( $attributes, $indexes, + $this->adapter->getIdAttributeType(), $this->maxQueryValues, $this->adapter->getMinDateTime(), $this->adapter->getMaxDateTime(), @@ -6248,6 +6251,7 @@ public function count(string $collection, array $queries = [], ?int $max = null) $validator = new DocumentsValidator( $attributes, $indexes, + $this->adapter->getIdAttributeType(), $this->maxQueryValues, $this->adapter->getMinDateTime(), $this->adapter->getMaxDateTime(), @@ -6298,6 +6302,7 @@ public function sum(string $collection, string $attribute, array $queries = [], $validator = new DocumentsValidator( $attributes, $indexes, + $this->adapter->getIdAttributeType(), $this->maxQueryValues, $this->adapter->getMinDateTime(), $this->adapter->getMaxDateTime(), diff --git a/src/Database/Validator/Queries/Documents.php b/src/Database/Validator/Queries/Documents.php index 2e1be4b37..9346b5ba7 100644 --- a/src/Database/Validator/Queries/Documents.php +++ b/src/Database/Validator/Queries/Documents.php @@ -19,12 +19,14 @@ class Documents extends IndexedQueries * Expression constructor * * @param array $attributes + * @param string $idAttributeType * @param array $indexes * @throws Exception */ public function __construct( array $attributes, array $indexes, + string $idAttributeType, int $maxValuesCount = 100, \DateTime $minAllowedDate = new \DateTime('0000-01-01'), \DateTime $maxAllowedDate = new \DateTime('9999-12-31'), @@ -60,6 +62,7 @@ public function __construct( new Cursor(), new Filter( $attributes, + $idAttributeType, $maxValuesCount, $minAllowedDate, $maxAllowedDate, diff --git a/src/Database/Validator/Query/Filter.php b/src/Database/Validator/Query/Filter.php index 95dd8ef4c..515782c53 100644 --- a/src/Database/Validator/Query/Filter.php +++ b/src/Database/Validator/Query/Filter.php @@ -26,7 +26,8 @@ class Filter extends Base * @param \DateTime $maxAllowedDate */ public function __construct( - array $attributes = [], + array $attributes, + private readonly string $idAttributeType, private readonly int $maxValuesCount = 100, private readonly \DateTime $minAllowedDate = new \DateTime('0000-01-01'), private readonly \DateTime $maxAllowedDate = new \DateTime('9999-12-31'), @@ -108,11 +109,11 @@ protected function isValidAttributeAndValues(string $attribute, array $values, s switch ($attributeType) { case Database::VAR_SEQUENCE: - $validator = new Sequence('int', true); + $validator = new Sequence($this->idAttributeType, true); break; case Database::VAR_ID: - $validator = new Sequence('int', false); + $validator = new Sequence($this->idAttributeType, false); break; case Database::VAR_STRING: diff --git a/tests/unit/Validator/DocumentsQueriesTest.php b/tests/unit/Validator/DocumentsQueriesTest.php index 45ae23933..9fe3e0fc7 100644 --- a/tests/unit/Validator/DocumentsQueriesTest.php +++ b/tests/unit/Validator/DocumentsQueriesTest.php @@ -76,6 +76,16 @@ public function setUp(): void 'signed' => false, 'array' => false, 'filters' => [], + ]), + new Document([ + '$id' => 'id', + 'key' => 'id', + 'type' => Database::VAR_ID, + 'size' => 0, + 'required' => false, + 'signed' => false, + 'array' => false, + 'filters' => [], ]) ], 'indexes' => [ @@ -113,9 +123,14 @@ public function tearDown(): void */ public function testValidQueries(): void { - $validator = new Documents($this->collection['attributes'], $this->collection['indexes']); + $validator = new Documents( + $this->collection['attributes'], + $this->collection['indexes'], + 'int' + ); $queries = [ + Query::notEqual('id', '1000000'), Query::equal('description', ['Best movie ever']), Query::equal('description', ['']), Query::equal('is_bool', [false]), @@ -146,7 +161,11 @@ public function testValidQueries(): void */ public function testInvalidQueries(): void { - $validator = new Documents($this->collection['attributes'], $this->collection['indexes']); + $validator = new Documents( + $this->collection['attributes'], + $this->collection['indexes'], + 'int' + ); $queries = ['{"method":"notEqual","attribute":"title","values":["Iron Man","Ant Man"]}']; $this->assertEquals(false, $validator->isValid($queries)); @@ -167,5 +186,7 @@ public function testInvalidQueries(): void $queries = [Query::equal('title', [])]; // empty array $this->assertEquals(false, $validator->isValid($queries)); $this->assertEquals('Invalid query: Equal queries require at least one value.', $validator->getDescription()); + + } } diff --git a/tests/unit/Validator/IndexedQueriesTest.php b/tests/unit/Validator/IndexedQueriesTest.php index 69ed9aeb1..cd312a192 100644 --- a/tests/unit/Validator/IndexedQueriesTest.php +++ b/tests/unit/Validator/IndexedQueriesTest.php @@ -80,7 +80,7 @@ public function testValid(): void $indexes, [ new Cursor(), - new Filter($attributes), + new Filter($attributes, 'int'), new Limit(), new Offset(), new Order($attributes) @@ -143,7 +143,7 @@ public function testMissingIndex(): void $indexes, [ new Cursor(), - new Filter($attributes), + new Filter($attributes, 'int'), new Limit(), new Offset(), new Order($attributes) @@ -196,7 +196,7 @@ public function testTwoAttributesFulltext(): void $indexes, [ new Cursor(), - new Filter($attributes), + new Filter($attributes, 'int'), new Limit(), new Offset(), new Order($attributes) diff --git a/tests/unit/Validator/QueriesTest.php b/tests/unit/Validator/QueriesTest.php index 86158014a..cc7fd08ce 100644 --- a/tests/unit/Validator/QueriesTest.php +++ b/tests/unit/Validator/QueriesTest.php @@ -63,7 +63,7 @@ public function testValid(): void $validator = new Queries( [ new Cursor(), - new Filter($attributes), + new Filter($attributes, 'int'), new Limit(), new Offset(), new Order($attributes) diff --git a/tests/unit/Validator/Query/FilterTest.php b/tests/unit/Validator/Query/FilterTest.php index 1388dbd7c..3b4f1c758 100644 --- a/tests/unit/Validator/Query/FilterTest.php +++ b/tests/unit/Validator/Query/FilterTest.php @@ -18,34 +18,34 @@ class FilterTest extends TestCase */ public function setUp(): void { - $this->validator = new Filter( - attributes: [ - new Document([ - '$id' => 'string', - 'key' => 'string', - 'type' => Database::VAR_STRING, - 'array' => false, - ]), - new Document([ - '$id' => 'string_array', - 'key' => 'string_array', - 'type' => Database::VAR_STRING, - 'array' => true, - ]), - new Document([ - '$id' => 'integer_array', - 'key' => 'integer_array', - 'type' => Database::VAR_INTEGER, - 'array' => true, - ]), - new Document([ - '$id' => 'integer', - 'key' => 'integer', - 'type' => Database::VAR_INTEGER, - 'array' => false, - ]), - ], - ); + $attributes = [ + new Document([ + '$id' => 'string', + 'key' => 'string', + 'type' => Database::VAR_STRING, + 'array' => false, + ]), + new Document([ + '$id' => 'string_array', + 'key' => 'string_array', + 'type' => Database::VAR_STRING, + 'array' => true, + ]), + new Document([ + '$id' => 'integer_array', + 'key' => 'integer_array', + 'type' => Database::VAR_INTEGER, + 'array' => true, + ]), + new Document([ + '$id' => 'integer', + 'key' => 'integer', + 'type' => Database::VAR_INTEGER, + 'array' => false, + ]), + ]; + + $this->validator = new Filter($attributes, 'int'); } public function testSuccess(): void diff --git a/tests/unit/Validator/QueryTest.php b/tests/unit/Validator/QueryTest.php index 7b4125145..0c533e578 100644 --- a/tests/unit/Validator/QueryTest.php +++ b/tests/unit/Validator/QueryTest.php @@ -108,7 +108,7 @@ public function tearDown(): void */ public function testQuery(): void { - $validator = new Documents($this->attributes, []); + $validator = new Documents($this->attributes, [], 'int'); $this->assertEquals(true, $validator->isValid([Query::equal('$id', ['Iron Man', 'Ant Man'])])); $this->assertEquals(true, $validator->isValid([Query::equal('$id', ['Iron Man'])])); @@ -138,7 +138,7 @@ public function testQuery(): void */ public function testAttributeNotFound(): void { - $validator = new Documents($this->attributes, []); + $validator = new Documents($this->attributes, [], 'int'); $response = $validator->isValid([Query::equal('name', ['Iron Man'])]); $this->assertEquals(false, $response); @@ -154,7 +154,7 @@ public function testAttributeNotFound(): void */ public function testAttributeWrongType(): void { - $validator = new Documents($this->attributes, []); + $validator = new Documents($this->attributes, [], 'int'); $response = $validator->isValid([Query::equal('title', [1776])]); $this->assertEquals(false, $response); @@ -166,7 +166,7 @@ public function testAttributeWrongType(): void */ public function testQueryDate(): void { - $validator = new Documents($this->attributes, []); + $validator = new Documents($this->attributes, [], 'int'); $response = $validator->isValid([Query::greaterThan('birthDay', '1960-01-01 10:10:10')]); $this->assertEquals(true, $response); @@ -177,7 +177,7 @@ public function testQueryDate(): void */ public function testQueryLimit(): void { - $validator = new Documents($this->attributes, []); + $validator = new Documents($this->attributes, [], 'int'); $response = $validator->isValid([Query::limit(25)]); $this->assertEquals(true, $response); @@ -191,7 +191,7 @@ public function testQueryLimit(): void */ public function testQueryOffset(): void { - $validator = new Documents($this->attributes, []); + $validator = new Documents($this->attributes, [], 'int'); $response = $validator->isValid([Query::offset(25)]); $this->assertEquals(true, $response); @@ -205,7 +205,7 @@ public function testQueryOffset(): void */ public function testQueryOrder(): void { - $validator = new Documents($this->attributes, []); + $validator = new Documents($this->attributes, [], 'int'); $response = $validator->isValid([Query::orderAsc('title')]); $this->assertEquals(true, $response); @@ -225,7 +225,7 @@ public function testQueryOrder(): void */ public function testQueryCursor(): void { - $validator = new Documents($this->attributes, []); + $validator = new Documents($this->attributes, [], 'int'); $response = $validator->isValid([Query::cursorAfter(new Document(['$id' => 'asdf']))]); $this->assertEquals(true, $response); @@ -255,7 +255,7 @@ public function testQueryGetByType(): void */ public function testQueryEmpty(): void { - $validator = new Documents($this->attributes, []); + $validator = new Documents($this->attributes, [], 'int'); $response = $validator->isValid([Query::equal('title', [''])]); $this->assertEquals(true, $response); @@ -284,7 +284,7 @@ public function testQueryEmpty(): void */ public function testOrQuery(): void { - $validator = new Documents($this->attributes, []); + $validator = new Documents($this->attributes, [], 'int'); $this->assertFalse($validator->isValid( [Query::or( From 1ef3855121a4130a611b2ddcae5a56ed18cdf6bc Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 14 Jul 2025 10:17:31 +0300 Subject: [PATCH 09/24] Order --- src/Database/Validator/Queries/Documents.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Validator/Queries/Documents.php b/src/Database/Validator/Queries/Documents.php index 9346b5ba7..c8406396d 100644 --- a/src/Database/Validator/Queries/Documents.php +++ b/src/Database/Validator/Queries/Documents.php @@ -19,8 +19,8 @@ class Documents extends IndexedQueries * Expression constructor * * @param array $attributes - * @param string $idAttributeType * @param array $indexes + * @param string $idAttributeType * @throws Exception */ public function __construct( From fd1b2888835984a037d43803f06aeca940a34852 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 14 Jul 2025 10:23:17 +0300 Subject: [PATCH 10/24] gettype --- src/Database/Validator/Sequence.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Database/Validator/Sequence.php b/src/Database/Validator/Sequence.php index 1bcc5246d..62417cb29 100644 --- a/src/Database/Validator/Sequence.php +++ b/src/Database/Validator/Sequence.php @@ -46,9 +46,7 @@ public function isValid($value): bool return false; } - $type = gettype($value); - - if ($type !== 'string') { + if (gettype($value) !== 'string') { return false; } From 98c22c12ae0289ac2e087b698b36d2c3539e905e Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 14 Jul 2025 10:28:17 +0300 Subject: [PATCH 11/24] lint --- tests/unit/Validator/Query/FilterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/Validator/Query/FilterTest.php b/tests/unit/Validator/Query/FilterTest.php index 3b4f1c758..6f31448fb 100644 --- a/tests/unit/Validator/Query/FilterTest.php +++ b/tests/unit/Validator/Query/FilterTest.php @@ -44,7 +44,7 @@ public function setUp(): void 'array' => false, ]), ]; - + $this->validator = new Filter($attributes, 'int'); } From 49fceb918cb74ad10c006b9c0b46b0b17f4601f3 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 14 Jul 2025 12:46:55 +0300 Subject: [PATCH 12/24] Use constant --- src/Database/Adapter/SQL.php | 2 +- src/Database/Database.php | 4 ++ src/Database/Validator/Sequence.php | 10 ++--- tests/unit/Validator/DocumentsQueriesTest.php | 4 +- tests/unit/Validator/IndexedQueriesTest.php | 6 +-- tests/unit/Validator/QueriesTest.php | 2 +- tests/unit/Validator/Query/FilterTest.php | 2 +- tests/unit/Validator/QueryTest.php | 20 ++++----- tests/unit/Validator/StructureTest.php | 44 +++++++++---------- 9 files changed, 47 insertions(+), 47 deletions(-) diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index 7324e789f..01161691a 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -1587,7 +1587,7 @@ public function getMaxVarcharLength(): int */ public function getIdAttributeType(): string { - return 'int'; + return Database::VAR_ID_INT; } /** diff --git a/src/Database/Database.php b/src/Database/Database.php index b2d5c0d55..8474e29d0 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -43,6 +43,10 @@ class Database public const VAR_ID = 'id'; public const VAR_SEQUENCE = 'sequence'; + // VAR_ID types + public const VAR_ID_INT = 'int'; + public const VAR_ID_MONGO = 'mongo'; + public const INT_MAX = 2147483647; public const BIG_INT_MAX = PHP_INT_MAX; public const DOUBLE_MAX = PHP_FLOAT_MAX; diff --git a/src/Database/Validator/Sequence.php b/src/Database/Validator/Sequence.php index 62417cb29..4332de6fa 100644 --- a/src/Database/Validator/Sequence.php +++ b/src/Database/Validator/Sequence.php @@ -33,11 +33,7 @@ public function isArray(): bool public function getType(): string { - if ($this->idAttributeType === 'string') { - return self::TYPE_STRING; - } - - return self::TYPE_INTEGER; + return self::TYPE_STRING; } public function isValid($value): bool @@ -50,9 +46,9 @@ public function isValid($value): bool return false; } - if ($this->idAttributeType === 'string') { + if ($this->idAttributeType === Database::VAR_ID_MONGO) { return preg_match('/^[a-f0-9]{24}$/i', $value) === 1; - } elseif ($this->idAttributeType === 'int') { + } elseif ($this->idAttributeType === Database::VAR_ID_INT) { $validator = new Integer(loose: true); if (!$validator->isValid($value)) { return false; diff --git a/tests/unit/Validator/DocumentsQueriesTest.php b/tests/unit/Validator/DocumentsQueriesTest.php index 9fe3e0fc7..e93b5fcc9 100644 --- a/tests/unit/Validator/DocumentsQueriesTest.php +++ b/tests/unit/Validator/DocumentsQueriesTest.php @@ -126,7 +126,7 @@ public function testValidQueries(): void $validator = new Documents( $this->collection['attributes'], $this->collection['indexes'], - 'int' + Database::VAR_ID_INT ); $queries = [ @@ -164,7 +164,7 @@ public function testInvalidQueries(): void $validator = new Documents( $this->collection['attributes'], $this->collection['indexes'], - 'int' + Database::VAR_ID_INT ); $queries = ['{"method":"notEqual","attribute":"title","values":["Iron Man","Ant Man"]}']; diff --git a/tests/unit/Validator/IndexedQueriesTest.php b/tests/unit/Validator/IndexedQueriesTest.php index cd312a192..e602a0454 100644 --- a/tests/unit/Validator/IndexedQueriesTest.php +++ b/tests/unit/Validator/IndexedQueriesTest.php @@ -80,7 +80,7 @@ public function testValid(): void $indexes, [ new Cursor(), - new Filter($attributes, 'int'), + new Filter($attributes, Database::VAR_ID_INT), new Limit(), new Offset(), new Order($attributes) @@ -143,7 +143,7 @@ public function testMissingIndex(): void $indexes, [ new Cursor(), - new Filter($attributes, 'int'), + new Filter($attributes, Database::VAR_ID_INT), new Limit(), new Offset(), new Order($attributes) @@ -196,7 +196,7 @@ public function testTwoAttributesFulltext(): void $indexes, [ new Cursor(), - new Filter($attributes, 'int'), + new Filter($attributes, Database::VAR_ID_INT), new Limit(), new Offset(), new Order($attributes) diff --git a/tests/unit/Validator/QueriesTest.php b/tests/unit/Validator/QueriesTest.php index cc7fd08ce..de991716f 100644 --- a/tests/unit/Validator/QueriesTest.php +++ b/tests/unit/Validator/QueriesTest.php @@ -63,7 +63,7 @@ public function testValid(): void $validator = new Queries( [ new Cursor(), - new Filter($attributes, 'int'), + new Filter($attributes, Database::VAR_ID_INT), new Limit(), new Offset(), new Order($attributes) diff --git a/tests/unit/Validator/Query/FilterTest.php b/tests/unit/Validator/Query/FilterTest.php index 6f31448fb..508573d03 100644 --- a/tests/unit/Validator/Query/FilterTest.php +++ b/tests/unit/Validator/Query/FilterTest.php @@ -45,7 +45,7 @@ public function setUp(): void ]), ]; - $this->validator = new Filter($attributes, 'int'); + $this->validator = new Filter($attributes, Database::VAR_ID_INT); } public function testSuccess(): void diff --git a/tests/unit/Validator/QueryTest.php b/tests/unit/Validator/QueryTest.php index 0c533e578..7ebacaed2 100644 --- a/tests/unit/Validator/QueryTest.php +++ b/tests/unit/Validator/QueryTest.php @@ -108,7 +108,7 @@ public function tearDown(): void */ public function testQuery(): void { - $validator = new Documents($this->attributes, [], 'int'); + $validator = new Documents($this->attributes, [], Database::VAR_ID_INT); $this->assertEquals(true, $validator->isValid([Query::equal('$id', ['Iron Man', 'Ant Man'])])); $this->assertEquals(true, $validator->isValid([Query::equal('$id', ['Iron Man'])])); @@ -138,7 +138,7 @@ public function testQuery(): void */ public function testAttributeNotFound(): void { - $validator = new Documents($this->attributes, [], 'int'); + $validator = new Documents($this->attributes, [], Database::VAR_ID_INT); $response = $validator->isValid([Query::equal('name', ['Iron Man'])]); $this->assertEquals(false, $response); @@ -154,7 +154,7 @@ public function testAttributeNotFound(): void */ public function testAttributeWrongType(): void { - $validator = new Documents($this->attributes, [], 'int'); + $validator = new Documents($this->attributes, [], Database::VAR_ID_INT); $response = $validator->isValid([Query::equal('title', [1776])]); $this->assertEquals(false, $response); @@ -166,7 +166,7 @@ public function testAttributeWrongType(): void */ public function testQueryDate(): void { - $validator = new Documents($this->attributes, [], 'int'); + $validator = new Documents($this->attributes, [], Database::VAR_ID_INT); $response = $validator->isValid([Query::greaterThan('birthDay', '1960-01-01 10:10:10')]); $this->assertEquals(true, $response); @@ -177,7 +177,7 @@ public function testQueryDate(): void */ public function testQueryLimit(): void { - $validator = new Documents($this->attributes, [], 'int'); + $validator = new Documents($this->attributes, [], Database::VAR_ID_INT); $response = $validator->isValid([Query::limit(25)]); $this->assertEquals(true, $response); @@ -191,7 +191,7 @@ public function testQueryLimit(): void */ public function testQueryOffset(): void { - $validator = new Documents($this->attributes, [], 'int'); + $validator = new Documents($this->attributes, [], Database::VAR_ID_INT); $response = $validator->isValid([Query::offset(25)]); $this->assertEquals(true, $response); @@ -205,7 +205,7 @@ public function testQueryOffset(): void */ public function testQueryOrder(): void { - $validator = new Documents($this->attributes, [], 'int'); + $validator = new Documents($this->attributes, [], Database::VAR_ID_INT); $response = $validator->isValid([Query::orderAsc('title')]); $this->assertEquals(true, $response); @@ -225,7 +225,7 @@ public function testQueryOrder(): void */ public function testQueryCursor(): void { - $validator = new Documents($this->attributes, [], 'int'); + $validator = new Documents($this->attributes, [], Database::VAR_ID_INT); $response = $validator->isValid([Query::cursorAfter(new Document(['$id' => 'asdf']))]); $this->assertEquals(true, $response); @@ -255,7 +255,7 @@ public function testQueryGetByType(): void */ public function testQueryEmpty(): void { - $validator = new Documents($this->attributes, [], 'int'); + $validator = new Documents($this->attributes, [], Database::VAR_ID_INT); $response = $validator->isValid([Query::equal('title', [''])]); $this->assertEquals(true, $response); @@ -284,7 +284,7 @@ public function testQueryEmpty(): void */ public function testOrQuery(): void { - $validator = new Documents($this->attributes, [], 'int'); + $validator = new Documents($this->attributes, [], Database::VAR_ID_INT); $this->assertFalse($validator->isValid( [Query::or( diff --git a/tests/unit/Validator/StructureTest.php b/tests/unit/Validator/StructureTest.php index 6d0a11f2d..26561d48e 100644 --- a/tests/unit/Validator/StructureTest.php +++ b/tests/unit/Validator/StructureTest.php @@ -133,7 +133,7 @@ public function testDocumentInstance(): void { $validator = new Structure( new Document($this->collection), - 'int' + Database::VAR_ID_INT ); $this->assertEquals(false, $validator->isValid('string')); @@ -148,7 +148,7 @@ public function testCollectionAttribute(): void { $validator = new Structure( new Document($this->collection), - 'int' + Database::VAR_ID_INT ); $this->assertEquals(false, $validator->isValid(new Document())); @@ -160,7 +160,7 @@ public function testCollection(): void { $validator = new Structure( new Document(), - 'int' + Database::VAR_ID_INT ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -181,7 +181,7 @@ public function testRequiredKeys(): void { $validator = new Structure( new Document($this->collection), - 'int' + Database::VAR_ID_INT ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -201,7 +201,7 @@ public function testNullValues(): void { $validator = new Structure( new Document($this->collection), - 'int' + Database::VAR_ID_INT ); $this->assertEquals(true, $validator->isValid(new Document([ @@ -232,7 +232,7 @@ public function testUnknownKeys(): void { $validator = new Structure( new Document($this->collection), - 'int' + Database::VAR_ID_INT ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -254,7 +254,7 @@ public function testIntegerAsString(): void { $validator = new Structure( new Document($this->collection), - 'int' + Database::VAR_ID_INT ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -275,7 +275,7 @@ public function testValidDocument(): void { $validator = new Structure( new Document($this->collection), - 'int' + Database::VAR_ID_INT ); $this->assertEquals(true, $validator->isValid(new Document([ @@ -294,7 +294,7 @@ public function testStringValidation(): void { $validator = new Structure( new Document($this->collection), - 'int' + Database::VAR_ID_INT ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -315,7 +315,7 @@ public function testArrayOfStringsValidation(): void { $validator = new Structure( new Document($this->collection), - 'int' + Database::VAR_ID_INT ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -376,7 +376,7 @@ public function testArrayAsObjectValidation(): void { $validator = new Structure( new Document($this->collection), - 'int' + Database::VAR_ID_INT ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -395,7 +395,7 @@ public function testArrayOfObjectsValidation(): void { $validator = new Structure( new Document($this->collection), - 'int' + Database::VAR_ID_INT ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -414,7 +414,7 @@ public function testIntegerValidation(): void { $validator = new Structure( new Document($this->collection), - 'int' + Database::VAR_ID_INT ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -448,7 +448,7 @@ public function testArrayOfIntegersValidation(): void { $validator = new Structure( new Document($this->collection), - 'int' + Database::VAR_ID_INT ); $this->assertEquals(true, $validator->isValid(new Document([ @@ -506,7 +506,7 @@ public function testFloatValidation(): void { $validator = new Structure( new Document($this->collection), - 'int' + Database::VAR_ID_INT ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -540,7 +540,7 @@ public function testBooleanValidation(): void { $validator = new Structure( new Document($this->collection), - 'int' + Database::VAR_ID_INT ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -574,7 +574,7 @@ public function testFormatValidation(): void { $validator = new Structure( new Document($this->collection), - 'int' + Database::VAR_ID_INT ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -595,7 +595,7 @@ public function testIntegerMaxRange(): void { $validator = new Structure( new Document($this->collection), - 'int' + Database::VAR_ID_INT ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -616,7 +616,7 @@ public function testDoubleUnsigned(): void { $validator = new Structure( new Document($this->collection), - 'int' + Database::VAR_ID_INT ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -637,7 +637,7 @@ public function testDoubleMaxRange(): void { $validator = new Structure( new Document($this->collection), - 'int' + Database::VAR_ID_INT ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -656,7 +656,7 @@ public function testId(): void { $validator = new Structure( new Document($this->collection), - 'int' + Database::VAR_ID_INT ); $id = '1000'; @@ -695,7 +695,7 @@ public function testId(): void */ $validator = new Structure( new Document($this->collection), - 'string' + Database::VAR_ID_MONGO ); $this->assertEquals(true, $validator->isValid(new Document([ From 549323a7e38742dd40bc50cf35ed736736bc9a51 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 14 Jul 2025 12:58:31 +0300 Subject: [PATCH 13/24] Use constant --- src/Database/Database.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 8474e29d0..f1a982a81 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -44,8 +44,8 @@ class Database public const VAR_SEQUENCE = 'sequence'; // VAR_ID types - public const VAR_ID_INT = 'int'; - public const VAR_ID_MONGO = 'mongo'; + public const VAR_ID_INT = 'integer_'; + public const VAR_ID_MONGO = 'mongo_'; public const INT_MAX = 2147483647; public const BIG_INT_MAX = PHP_INT_MAX; From e10670a5e9baed9fb7989a39f68a27677e152d7a Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 15 Jul 2025 08:45:59 +0300 Subject: [PATCH 14/24] Remove Sequence type --- src/Database/Adapter/SQL.php | 1 - src/Database/Database.php | 7 +++-- src/Database/Validator/Queries/Documents.php | 2 +- src/Database/Validator/Query/Filter.php | 6 +---- src/Database/Validator/Sequence.php | 28 +++++++++----------- src/Database/Validator/Structure.php | 8 ++---- 6 files changed, 20 insertions(+), 32 deletions(-) diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index 01161691a..2b95e45bd 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -1003,7 +1003,6 @@ public function getAttributeWidth(Document $collection): int } switch ($attribute['type']) { - case Database::VAR_SEQUENCE: case Database::VAR_ID: $total += 8; // BIGINT 8 bytes break; diff --git a/src/Database/Database.php b/src/Database/Database.php index f1a982a81..3b203f384 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -41,11 +41,10 @@ class Database public const VAR_BOOLEAN = 'boolean'; public const VAR_DATETIME = 'datetime'; public const VAR_ID = 'id'; - public const VAR_SEQUENCE = 'sequence'; // VAR_ID types - public const VAR_ID_INT = 'integer_'; - public const VAR_ID_MONGO = 'mongo_'; + public const VAR_ID_INT = '_integer'; + public const VAR_ID_MONGO = '_mongo'; public const INT_MAX = 2147483647; public const BIG_INT_MAX = PHP_INT_MAX; @@ -172,7 +171,7 @@ class Database ], [ '$id' => '$sequence', - 'type' => self::VAR_SEQUENCE, + 'type' => self::VAR_ID, 'size' => 0, 'required' => true, 'signed' => true, diff --git a/src/Database/Validator/Queries/Documents.php b/src/Database/Validator/Queries/Documents.php index c8406396d..289ccbe5b 100644 --- a/src/Database/Validator/Queries/Documents.php +++ b/src/Database/Validator/Queries/Documents.php @@ -40,7 +40,7 @@ public function __construct( $attributes[] = new Document([ '$id' => '$sequence', 'key' => '$sequence', - 'type' => Database::VAR_SEQUENCE, + 'type' => Database::VAR_ID, 'array' => false, ]); $attributes[] = new Document([ diff --git a/src/Database/Validator/Query/Filter.php b/src/Database/Validator/Query/Filter.php index 515782c53..9c2533558 100644 --- a/src/Database/Validator/Query/Filter.php +++ b/src/Database/Validator/Query/Filter.php @@ -108,12 +108,8 @@ protected function isValidAttributeAndValues(string $attribute, array $values, s $validator = null; switch ($attributeType) { - case Database::VAR_SEQUENCE: - $validator = new Sequence($this->idAttributeType, true); - break; - case Database::VAR_ID: - $validator = new Sequence($this->idAttributeType, false); + $validator = new Sequence($this->idAttributeType, $attribute === '$sequence'); break; case Database::VAR_STRING: diff --git a/src/Database/Validator/Sequence.php b/src/Database/Validator/Sequence.php index 4332de6fa..39b98a8f6 100644 --- a/src/Database/Validator/Sequence.php +++ b/src/Database/Validator/Sequence.php @@ -46,24 +46,22 @@ public function isValid($value): bool return false; } - if ($this->idAttributeType === Database::VAR_ID_MONGO) { - return preg_match('/^[a-f0-9]{24}$/i', $value) === 1; - } elseif ($this->idAttributeType === Database::VAR_ID_INT) { - $validator = new Integer(loose: true); - if (!$validator->isValid($value)) { - return false; - } + switch ($this->idAttributeType) { + case Database::VAR_ID_MONGO: + return preg_match('/^[a-f0-9]{24}$/i', $value) === 1; - $start = ($this->primary) ? 1 : 0; + case Database::VAR_ID_INT: + $validator = new Integer(loose: true); + if (!$validator->isValid($value)) { + return false; + } - $validator = new Range($start, Database::BIG_INT_MAX, Database::VAR_INTEGER); - if (!$validator->isValid($value)) { - return false; - } + $start = ($this->primary) ? 1 : 0; + $validator = new Range($start, Database::BIG_INT_MAX, Database::VAR_INTEGER); + return $validator->isValid($value); - return true; + default: + return false; } - - return false; } } diff --git a/src/Database/Validator/Structure.php b/src/Database/Validator/Structure.php index dd4156b58..adcc28441 100644 --- a/src/Database/Validator/Structure.php +++ b/src/Database/Validator/Structure.php @@ -32,7 +32,7 @@ class Structure extends Validator ], [ '$id' => '$sequence', - 'type' => Database::VAR_SEQUENCE, + 'type' => Database::VAR_ID, 'size' => 0, 'required' => false, 'signed' => true, @@ -316,12 +316,8 @@ protected function checkForInvalidAttributeValues(array $structure, array $keys) $validators = []; switch ($type) { - case Database::VAR_SEQUENCE: - $validators[] = new Sequence($this->idAttributeType, true); - break; - case Database::VAR_ID: - $validators[] = new Sequence($this->idAttributeType, false); + $validators[] = new Sequence($this->idAttributeType, $attribute['$id'] === '$sequence'); break; case Database::VAR_STRING: From bfce6cf730ac5e694ce939e3ab086c5d21a76a24 Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 15 Jul 2025 18:03:57 +0300 Subject: [PATCH 15/24] $tenant as VAR_ID --- src/Database/Database.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 3b203f384..7e2ad9659 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -189,7 +189,7 @@ class Database ], [ '$id' => '$tenant', - 'type' => self::VAR_INTEGER, + 'type' => self::VAR_ID, // Inconsistency with other VAR_ID since this is an INT 'size' => 0, 'required' => false, 'default' => null, From ae8b0473e32fd0991488497baf476cf77be0d384 Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 16 Jul 2025 08:15:44 +0300 Subject: [PATCH 16/24] $tenant as VAR_ID --- src/Database/Database.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 7e2ad9659..29c42db00 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -189,7 +189,8 @@ class Database ], [ '$id' => '$tenant', - 'type' => self::VAR_ID, // Inconsistency with other VAR_ID since this is an INT + 'type' => self::VAR_INTEGER, + //'type' => self::VAR_ID, // Inconsistency with other VAR_ID since this is an INT 'size' => 0, 'required' => false, 'default' => null, From df82400bbe72fd9e635bc2db3a8e0ca272a395af Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 16 Jul 2025 09:43:26 +0300 Subject: [PATCH 17/24] Cast internals --- src/Database/Database.php | 4 ++++ src/Database/Validator/Structure.php | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 676b5038a..e3ed6746c 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -6510,6 +6510,10 @@ public function casting(Document $collection, Document $document): Document foreach ($value as &$node) { switch ($type) { + case self::VAR_ID: + $node = (string)$node; + break; + case self::VAR_BOOLEAN: $node = (bool)$node; break; diff --git a/src/Database/Validator/Structure.php b/src/Database/Validator/Structure.php index adcc28441..117335cab 100644 --- a/src/Database/Validator/Structure.php +++ b/src/Database/Validator/Structure.php @@ -50,7 +50,7 @@ class Structure extends Validator ], [ '$id' => '$tenant', - 'type' => Database::VAR_INTEGER, // ????? + 'type' => Database::VAR_INTEGER, // ? VAR_ID 'size' => 8, 'required' => false, 'default' => null, From e4cd537843cf1db27dc47d1c253090770cd26094 Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 16 Jul 2025 10:04:46 +0300 Subject: [PATCH 18/24] Remove \n --- src/Database/Database.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index e3ed6746c..0a1d4e451 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -6513,7 +6513,6 @@ public function casting(Document $collection, Document $document): Document case self::VAR_ID: $node = (string)$node; break; - case self::VAR_BOOLEAN: $node = (bool)$node; break; From da47c32226bb8668abb609d9ad7bda9059de2461 Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 17 Jul 2025 15:58:40 +0300 Subject: [PATCH 19/24] stopOnFailure --- phpunit.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit.xml b/phpunit.xml index 34365d48d..2a0531cfd 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,7 +7,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="true" + stopOnFailure="false" > From d56038ee6ad6bdb4ba46d78883e760ae7eff37e4 Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 31 Jul 2025 17:11:58 +0300 Subject: [PATCH 20/24] Fix unit tests --- tests/unit/Validator/StructureTest.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/unit/Validator/StructureTest.php b/tests/unit/Validator/StructureTest.php index 8d62cbbfe..e76783f08 100644 --- a/tests/unit/Validator/StructureTest.php +++ b/tests/unit/Validator/StructureTest.php @@ -718,10 +718,6 @@ public function testId(): void $id = '1000'; $mongoid = '507f1f77bcf86cd799439011'; - /** - * Sql - */ - $this->assertEquals(true, $validator->isValid(new Document([ '$collection' => ID::custom('posts'), 'title' => 'My Title', @@ -731,6 +727,8 @@ public function testId(): void 'published' => true, 'tags' => ['dog', 'cat', 'mouse'], 'feedback' => 'team@appwrite.io', + '$createdAt' => '2000-04-01T12:00:00.000+00:00', + '$updatedAt' => '2000-04-01T12:00:00.000+00:00', 'id' => $id, ]))); @@ -743,6 +741,8 @@ public function testId(): void 'published' => true, 'tags' => ['dog', 'cat', 'mouse'], 'feedback' => 'team@appwrite.io', + '$createdAt' => '2000-04-01T12:00:00.000+00:00', + '$updatedAt' => '2000-04-01T12:00:00.000+00:00', 'id' => $mongoid, ]))); @@ -763,6 +763,8 @@ public function testId(): void 'published' => true, 'tags' => ['dog', 'cat', 'mouse'], 'feedback' => 'team@appwrite.io', + '$createdAt' => '2000-04-01T12:00:00.000+00:00', + '$updatedAt' => '2000-04-01T12:00:00.000+00:00', 'id' => $mongoid, ]))); @@ -775,6 +777,8 @@ public function testId(): void 'published' => true, 'tags' => ['dog', 'cat', 'mouse'], 'feedback' => 'team@appwrite.io', + '$createdAt' => '2000-04-01T12:00:00.000+00:00', + '$updatedAt' => '2000-04-01T12:00:00.000+00:00', 'id' => $mongoid, ]))); } From 50f5c56a868445f5b4df8ec66a52293bd8cca6e4 Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 31 Jul 2025 17:13:45 +0300 Subject: [PATCH 21/24] Fix unit tests --- tests/unit/Validator/StructureTest.php | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/tests/unit/Validator/StructureTest.php b/tests/unit/Validator/StructureTest.php index e76783f08..faa91ab09 100644 --- a/tests/unit/Validator/StructureTest.php +++ b/tests/unit/Validator/StructureTest.php @@ -715,8 +715,8 @@ public function testId(): void Database::VAR_ID_INT ); - $id = '1000'; - $mongoid = '507f1f77bcf86cd799439011'; + $sqlId = '1000'; + $mongoId = '507f1f77bcf86cd799439011'; $this->assertEquals(true, $validator->isValid(new Document([ '$collection' => ID::custom('posts'), @@ -729,7 +729,7 @@ public function testId(): void 'feedback' => 'team@appwrite.io', '$createdAt' => '2000-04-01T12:00:00.000+00:00', '$updatedAt' => '2000-04-01T12:00:00.000+00:00', - 'id' => $id, + 'id' => $sqlId, ]))); $this->assertEquals(false, $validator->isValid(new Document([ @@ -743,12 +743,9 @@ public function testId(): void 'feedback' => 'team@appwrite.io', '$createdAt' => '2000-04-01T12:00:00.000+00:00', '$updatedAt' => '2000-04-01T12:00:00.000+00:00', - 'id' => $mongoid, + 'id' => $mongoId, ]))); - /** - * Mongo - */ $validator = new Structure( new Document($this->collection), Database::VAR_ID_MONGO @@ -765,7 +762,7 @@ public function testId(): void 'feedback' => 'team@appwrite.io', '$createdAt' => '2000-04-01T12:00:00.000+00:00', '$updatedAt' => '2000-04-01T12:00:00.000+00:00', - 'id' => $mongoid, + 'id' => $mongoId, ]))); $this->assertEquals(true, $validator->isValid(new Document([ @@ -779,7 +776,7 @@ public function testId(): void 'feedback' => 'team@appwrite.io', '$createdAt' => '2000-04-01T12:00:00.000+00:00', '$updatedAt' => '2000-04-01T12:00:00.000+00:00', - 'id' => $mongoid, + 'id' => $mongoId, ]))); } From f7ba958ec82d3e138cbfeecd2c2cff81ecbaed0a Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Mon, 4 Aug 2025 21:20:34 +1200 Subject: [PATCH 22/24] Apply suggestions from code review --- src/Database/Adapter.php | 2 +- src/Database/Adapter/MariaDB.php | 2 +- src/Database/Adapter/Postgres.php | 2 +- src/Database/Adapter/SQL.php | 2 +- src/Database/Database.php | 8 ++++---- src/Database/Validator/Sequence.php | 11 +++-------- 6 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/Database/Adapter.php b/src/Database/Adapter.php index c4ce272ac..c55b3e9b9 100644 --- a/src/Database/Adapter.php +++ b/src/Database/Adapter.php @@ -863,7 +863,7 @@ abstract public function getMaxIndexLength(): int; abstract public function getMinDateTime(): \DateTime; /** - * Get Id's attribute primitive type int or string + * Get the primitive type of the primary key type for this adapter * * @return string */ diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 95a85188b..aa8276b90 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -1750,7 +1750,7 @@ protected function getSQLType(string $type, int $size, bool $signed = true, bool switch ($type) { case Database::VAR_ID: - return 'bigint UNSIGNED'; + return 'BIGINT UNSIGNED'; case Database::VAR_STRING: // $size = $size * 4; // Convert utf8mb4 size to bytes diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index 2ddf17df0..5430ba17f 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -1863,7 +1863,7 @@ protected function getSQLType(string $type, int $size, bool $signed = true, bool switch ($type) { case Database::VAR_ID: - return 'bigint'; + return 'BIGINT'; case Database::VAR_STRING: // $size = $size * 4; // Convert utf8mb4 size to bytes diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index 38f20de78..c7a4cf1b7 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -1629,7 +1629,7 @@ public function getMaxVarcharLength(): int */ public function getIdAttributeType(): string { - return Database::VAR_ID_INT; + return Database::VAR_INTEGER; } /** diff --git a/src/Database/Database.php b/src/Database/Database.php index 462cd7ddc..53545ae8c 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -42,10 +42,7 @@ class Database public const VAR_BOOLEAN = 'boolean'; public const VAR_DATETIME = 'datetime'; public const VAR_ID = 'id'; - - // VAR_ID types - public const VAR_ID_INT = '_integer'; - public const VAR_ID_MONGO = '_mongo'; + public const VAR_OBJECT_ID = 'objectId'; public const INT_MAX = 2147483647; public const BIG_INT_MAX = PHP_INT_MAX; @@ -6506,6 +6503,9 @@ public function casting(Document $collection, Document $document): Document foreach ($value as $index => $node) { switch ($type) { case self::VAR_ID: + // Disabled until Appwrite migrates to use real int ID's for MySQL + //$type = $this->adapter->getIdAttributeType(); + //\settype($node, $type); $node = (string)$node; break; case self::VAR_BOOLEAN: diff --git a/src/Database/Validator/Sequence.php b/src/Database/Validator/Sequence.php index 39b98a8f6..c74945190 100644 --- a/src/Database/Validator/Sequence.php +++ b/src/Database/Validator/Sequence.php @@ -42,20 +42,15 @@ public function isValid($value): bool return false; } - if (gettype($value) !== 'string') { + if (!\is_string($value)) { return false; } switch ($this->idAttributeType) { - case Database::VAR_ID_MONGO: + case Database::VAR_OBJECT_ID: return preg_match('/^[a-f0-9]{24}$/i', $value) === 1; - case Database::VAR_ID_INT: - $validator = new Integer(loose: true); - if (!$validator->isValid($value)) { - return false; - } - + case Database::VAR_INTEGER: $start = ($this->primary) ? 1 : 0; $validator = new Range($start, Database::BIG_INT_MAX, Database::VAR_INTEGER); return $validator->isValid($value); From 755a2f837c91cacf6d2585a3e55040b191e7ef7c Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Mon, 4 Aug 2025 21:21:38 +1200 Subject: [PATCH 23/24] Update tests --- tests/unit/Validator/DocumentsQueriesTest.php | 4 +- tests/unit/Validator/IndexedQueriesTest.php | 6 +-- tests/unit/Validator/QueriesTest.php | 2 +- tests/unit/Validator/Query/FilterTest.php | 2 +- tests/unit/Validator/QueryTest.php | 20 ++++----- tests/unit/Validator/StructureTest.php | 42 +++++++++---------- 6 files changed, 38 insertions(+), 38 deletions(-) diff --git a/tests/unit/Validator/DocumentsQueriesTest.php b/tests/unit/Validator/DocumentsQueriesTest.php index e93b5fcc9..6530ad299 100644 --- a/tests/unit/Validator/DocumentsQueriesTest.php +++ b/tests/unit/Validator/DocumentsQueriesTest.php @@ -126,7 +126,7 @@ public function testValidQueries(): void $validator = new Documents( $this->collection['attributes'], $this->collection['indexes'], - Database::VAR_ID_INT + Database::VAR_INTEGER ); $queries = [ @@ -164,7 +164,7 @@ public function testInvalidQueries(): void $validator = new Documents( $this->collection['attributes'], $this->collection['indexes'], - Database::VAR_ID_INT + Database::VAR_INTEGER ); $queries = ['{"method":"notEqual","attribute":"title","values":["Iron Man","Ant Man"]}']; diff --git a/tests/unit/Validator/IndexedQueriesTest.php b/tests/unit/Validator/IndexedQueriesTest.php index e602a0454..409fcf365 100644 --- a/tests/unit/Validator/IndexedQueriesTest.php +++ b/tests/unit/Validator/IndexedQueriesTest.php @@ -80,7 +80,7 @@ public function testValid(): void $indexes, [ new Cursor(), - new Filter($attributes, Database::VAR_ID_INT), + new Filter($attributes, Database::VAR_INTEGER), new Limit(), new Offset(), new Order($attributes) @@ -143,7 +143,7 @@ public function testMissingIndex(): void $indexes, [ new Cursor(), - new Filter($attributes, Database::VAR_ID_INT), + new Filter($attributes, Database::VAR_INTEGER), new Limit(), new Offset(), new Order($attributes) @@ -196,7 +196,7 @@ public function testTwoAttributesFulltext(): void $indexes, [ new Cursor(), - new Filter($attributes, Database::VAR_ID_INT), + new Filter($attributes, Database::VAR_INTEGER), new Limit(), new Offset(), new Order($attributes) diff --git a/tests/unit/Validator/QueriesTest.php b/tests/unit/Validator/QueriesTest.php index de991716f..265e9cbd0 100644 --- a/tests/unit/Validator/QueriesTest.php +++ b/tests/unit/Validator/QueriesTest.php @@ -63,7 +63,7 @@ public function testValid(): void $validator = new Queries( [ new Cursor(), - new Filter($attributes, Database::VAR_ID_INT), + new Filter($attributes, Database::VAR_INTEGER), new Limit(), new Offset(), new Order($attributes) diff --git a/tests/unit/Validator/Query/FilterTest.php b/tests/unit/Validator/Query/FilterTest.php index 508573d03..2465ea3e3 100644 --- a/tests/unit/Validator/Query/FilterTest.php +++ b/tests/unit/Validator/Query/FilterTest.php @@ -45,7 +45,7 @@ public function setUp(): void ]), ]; - $this->validator = new Filter($attributes, Database::VAR_ID_INT); + $this->validator = new Filter($attributes, Database::VAR_INTEGER); } public function testSuccess(): void diff --git a/tests/unit/Validator/QueryTest.php b/tests/unit/Validator/QueryTest.php index 7ebacaed2..dbe7a6b52 100644 --- a/tests/unit/Validator/QueryTest.php +++ b/tests/unit/Validator/QueryTest.php @@ -108,7 +108,7 @@ public function tearDown(): void */ public function testQuery(): void { - $validator = new Documents($this->attributes, [], Database::VAR_ID_INT); + $validator = new Documents($this->attributes, [], Database::VAR_INTEGER); $this->assertEquals(true, $validator->isValid([Query::equal('$id', ['Iron Man', 'Ant Man'])])); $this->assertEquals(true, $validator->isValid([Query::equal('$id', ['Iron Man'])])); @@ -138,7 +138,7 @@ public function testQuery(): void */ public function testAttributeNotFound(): void { - $validator = new Documents($this->attributes, [], Database::VAR_ID_INT); + $validator = new Documents($this->attributes, [], Database::VAR_INTEGER); $response = $validator->isValid([Query::equal('name', ['Iron Man'])]); $this->assertEquals(false, $response); @@ -154,7 +154,7 @@ public function testAttributeNotFound(): void */ public function testAttributeWrongType(): void { - $validator = new Documents($this->attributes, [], Database::VAR_ID_INT); + $validator = new Documents($this->attributes, [], Database::VAR_INTEGER); $response = $validator->isValid([Query::equal('title', [1776])]); $this->assertEquals(false, $response); @@ -166,7 +166,7 @@ public function testAttributeWrongType(): void */ public function testQueryDate(): void { - $validator = new Documents($this->attributes, [], Database::VAR_ID_INT); + $validator = new Documents($this->attributes, [], Database::VAR_INTEGER); $response = $validator->isValid([Query::greaterThan('birthDay', '1960-01-01 10:10:10')]); $this->assertEquals(true, $response); @@ -177,7 +177,7 @@ public function testQueryDate(): void */ public function testQueryLimit(): void { - $validator = new Documents($this->attributes, [], Database::VAR_ID_INT); + $validator = new Documents($this->attributes, [], Database::VAR_INTEGER); $response = $validator->isValid([Query::limit(25)]); $this->assertEquals(true, $response); @@ -191,7 +191,7 @@ public function testQueryLimit(): void */ public function testQueryOffset(): void { - $validator = new Documents($this->attributes, [], Database::VAR_ID_INT); + $validator = new Documents($this->attributes, [], Database::VAR_INTEGER); $response = $validator->isValid([Query::offset(25)]); $this->assertEquals(true, $response); @@ -205,7 +205,7 @@ public function testQueryOffset(): void */ public function testQueryOrder(): void { - $validator = new Documents($this->attributes, [], Database::VAR_ID_INT); + $validator = new Documents($this->attributes, [], Database::VAR_INTEGER); $response = $validator->isValid([Query::orderAsc('title')]); $this->assertEquals(true, $response); @@ -225,7 +225,7 @@ public function testQueryOrder(): void */ public function testQueryCursor(): void { - $validator = new Documents($this->attributes, [], Database::VAR_ID_INT); + $validator = new Documents($this->attributes, [], Database::VAR_INTEGER); $response = $validator->isValid([Query::cursorAfter(new Document(['$id' => 'asdf']))]); $this->assertEquals(true, $response); @@ -255,7 +255,7 @@ public function testQueryGetByType(): void */ public function testQueryEmpty(): void { - $validator = new Documents($this->attributes, [], Database::VAR_ID_INT); + $validator = new Documents($this->attributes, [], Database::VAR_INTEGER); $response = $validator->isValid([Query::equal('title', [''])]); $this->assertEquals(true, $response); @@ -284,7 +284,7 @@ public function testQueryEmpty(): void */ public function testOrQuery(): void { - $validator = new Documents($this->attributes, [], Database::VAR_ID_INT); + $validator = new Documents($this->attributes, [], Database::VAR_INTEGER); $this->assertFalse($validator->isValid( [Query::or( diff --git a/tests/unit/Validator/StructureTest.php b/tests/unit/Validator/StructureTest.php index faa91ab09..fa3cf9e0a 100644 --- a/tests/unit/Validator/StructureTest.php +++ b/tests/unit/Validator/StructureTest.php @@ -133,7 +133,7 @@ public function testDocumentInstance(): void { $validator = new Structure( new Document($this->collection), - Database::VAR_ID_INT + Database::VAR_INTEGER ); $this->assertEquals(false, $validator->isValid('string')); @@ -148,7 +148,7 @@ public function testCollectionAttribute(): void { $validator = new Structure( new Document($this->collection), - Database::VAR_ID_INT + Database::VAR_INTEGER ); $this->assertEquals(false, $validator->isValid(new Document())); @@ -160,7 +160,7 @@ public function testCollection(): void { $validator = new Structure( new Document(), - Database::VAR_ID_INT + Database::VAR_INTEGER ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -183,7 +183,7 @@ public function testRequiredKeys(): void { $validator = new Structure( new Document($this->collection), - Database::VAR_ID_INT + Database::VAR_INTEGER ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -205,7 +205,7 @@ public function testNullValues(): void { $validator = new Structure( new Document($this->collection), - Database::VAR_ID_INT + Database::VAR_INTEGER ); $this->assertEquals(true, $validator->isValid(new Document([ @@ -240,7 +240,7 @@ public function testUnknownKeys(): void { $validator = new Structure( new Document($this->collection), - Database::VAR_ID_INT + Database::VAR_INTEGER ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -264,7 +264,7 @@ public function testIntegerAsString(): void { $validator = new Structure( new Document($this->collection), - Database::VAR_ID_INT + Database::VAR_INTEGER ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -287,7 +287,7 @@ public function testValidDocument(): void { $validator = new Structure( new Document($this->collection), - Database::VAR_ID_INT + Database::VAR_INTEGER ); $this->assertEquals(true, $validator->isValid(new Document([ @@ -308,7 +308,7 @@ public function testStringValidation(): void { $validator = new Structure( new Document($this->collection), - Database::VAR_ID_INT + Database::VAR_INTEGER ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -331,7 +331,7 @@ public function testArrayOfStringsValidation(): void { $validator = new Structure( new Document($this->collection), - Database::VAR_ID_INT + Database::VAR_INTEGER ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -400,7 +400,7 @@ public function testArrayAsObjectValidation(): void { $validator = new Structure( new Document($this->collection), - Database::VAR_ID_INT + Database::VAR_INTEGER ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -421,7 +421,7 @@ public function testArrayOfObjectsValidation(): void { $validator = new Structure( new Document($this->collection), - Database::VAR_ID_INT + Database::VAR_INTEGER ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -442,7 +442,7 @@ public function testIntegerValidation(): void { $validator = new Structure( new Document($this->collection), - Database::VAR_ID_INT + Database::VAR_INTEGER ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -480,7 +480,7 @@ public function testArrayOfIntegersValidation(): void { $validator = new Structure( new Document($this->collection), - Database::VAR_ID_INT + Database::VAR_INTEGER ); $this->assertEquals(true, $validator->isValid(new Document([ @@ -546,7 +546,7 @@ public function testFloatValidation(): void { $validator = new Structure( new Document($this->collection), - Database::VAR_ID_INT + Database::VAR_INTEGER ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -584,7 +584,7 @@ public function testBooleanValidation(): void { $validator = new Structure( new Document($this->collection), - Database::VAR_ID_INT + Database::VAR_INTEGER ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -622,7 +622,7 @@ public function testFormatValidation(): void { $validator = new Structure( new Document($this->collection), - Database::VAR_ID_INT + Database::VAR_INTEGER ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -645,7 +645,7 @@ public function testIntegerMaxRange(): void { $validator = new Structure( new Document($this->collection), - Database::VAR_ID_INT + Database::VAR_INTEGER ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -668,7 +668,7 @@ public function testDoubleUnsigned(): void { $validator = new Structure( new Document($this->collection), - Database::VAR_ID_INT + Database::VAR_INTEGER ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -691,7 +691,7 @@ public function testDoubleMaxRange(): void { $validator = new Structure( new Document($this->collection), - Database::VAR_ID_INT + Database::VAR_INTEGER ); $this->assertEquals(false, $validator->isValid(new Document([ @@ -712,7 +712,7 @@ public function testId(): void { $validator = new Structure( new Document($this->collection), - Database::VAR_ID_INT + Database::VAR_INTEGER ); $sqlId = '1000'; From 7d77337814ad09c42e98fa46d376f4b4f8e98206 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Mon, 4 Aug 2025 21:26:20 +1200 Subject: [PATCH 24/24] Fix tests --- src/Database/Validator/Sequence.php | 1 - tests/unit/Validator/StructureTest.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Database/Validator/Sequence.php b/src/Database/Validator/Sequence.php index c74945190..305632727 100644 --- a/src/Database/Validator/Sequence.php +++ b/src/Database/Validator/Sequence.php @@ -4,7 +4,6 @@ use Utopia\Database\Database; use Utopia\Validator; -use Utopia\Validator\Integer; use Utopia\Validator\Range; class Sequence extends Validator diff --git a/tests/unit/Validator/StructureTest.php b/tests/unit/Validator/StructureTest.php index fa3cf9e0a..68fa73bf8 100644 --- a/tests/unit/Validator/StructureTest.php +++ b/tests/unit/Validator/StructureTest.php @@ -748,7 +748,7 @@ public function testId(): void $validator = new Structure( new Document($this->collection), - Database::VAR_ID_MONGO + Database::VAR_OBJECT_ID ); $this->assertEquals(true, $validator->isValid(new Document([