Skip to content

Commit aeebd69

Browse files
committed
Fix tests
1 parent 329eb9e commit aeebd69

File tree

6 files changed

+1059
-32
lines changed

6 files changed

+1059
-32
lines changed

src/Database/Adapter/MariaDB.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1716,7 +1716,7 @@ protected function getSQLType(string $type, int $size, bool $signed = true, bool
17161716
return 'DATETIME(3)';
17171717

17181718
default:
1719-
throw new DatabaseException('Unknown type: ' . $type . '. Must be one of ' . Database::VAR_STRING . ', ' . Database::VAR_VARCHAR . ', ' . Database::VAR_TEXT . ', ' . Database::VAR_MEDIUMTEXT . ', ' . Database::VAR_LONGTEXT . ', ' . Database::VAR_INTEGER . ', ' . Database::VAR_FLOAT . ', ' . Database::VAR_BOOLEAN . ', ' . Database::VAR_DATETIME . ', ' . Database::VAR_RELATIONSHIP . ', ' . ', ' . Database::VAR_POINT . ', ' . Database::VAR_LINESTRING . ', ' . Database::VAR_POLYGON);
1719+
throw new DatabaseException('Unknown type: ' . $type . '. Must be one of ' . Database::VAR_STRING . ', ' . Database::VAR_VARCHAR . ', ' . Database::VAR_TEXT . ', ' . Database::VAR_MEDIUMTEXT . ', ' . Database::VAR_LONGTEXT . ', ' . Database::VAR_INTEGER . ', ' . Database::VAR_FLOAT . ', ' . Database::VAR_BOOLEAN . ', ' . Database::VAR_DATETIME . ', ' . Database::VAR_RELATIONSHIP . ', ' . Database::VAR_POINT . ', ' . Database::VAR_LINESTRING . ', ' . Database::VAR_POLYGON);
17201720
}
17211721
}
17221722

src/Database/Adapter/Mongo.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2698,6 +2698,17 @@ public function getLimitForString(): int
26982698
return 2147483647;
26992699
}
27002700

2701+
/**
2702+
* Get max VARCHAR limit
2703+
* MongoDB doesn't distinguish between string types, so using same as string limit
2704+
*
2705+
* @return int
2706+
*/
2707+
public function getMaxVarcharLength(): int
2708+
{
2709+
return 2147483647;
2710+
}
2711+
27012712
/**
27022713
* Get max INT limit
27032714
*

src/Database/Validator/Attribute.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ public function checkDefaultValue(Document $attribute): bool
453453
$default = $attribute->getAttribute('default');
454454
$required = $attribute->getAttribute('required', false);
455455
$type = $attribute->getAttribute('type');
456+
$array = $attribute->getAttribute('array', false);
456457

457458
if (\is_null($default)) {
458459
return true;
@@ -463,6 +464,12 @@ public function checkDefaultValue(Document $attribute): bool
463464
throw new DatabaseException($this->message);
464465
}
465466

467+
// Reject array defaults for non-array attributes (except vectors and spatial types which use arrays internally)
468+
if (\is_array($default) && !$array && !\in_array($type, [Database::VAR_VECTOR, ...Database::SPATIAL_TYPES], true)) {
469+
$this->message = 'Cannot set an array default value for a non-array attribute';
470+
throw new DatabaseException($this->message);
471+
}
472+
466473
$this->validateDefaultTypes($type, $default);
467474

468475
return true;

src/Database/Validator/Query/Filter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ protected function isValidAttributeAndValues(string $attribute, array $values, s
142142
break;
143143

144144
case Database::VAR_STRING:
145+
case Database::VAR_VARCHAR:
146+
case Database::VAR_TEXT:
147+
case Database::VAR_MEDIUMTEXT:
148+
case Database::VAR_LONGTEXT:
145149
$validator = new Text(0, 0);
146150
break;
147151

tests/e2e/Adapter/Scopes/AttributeTests.php

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2312,38 +2312,40 @@ public function testStringTypeAttributes(): void
23122312
$this->assertEquals(['test1', 'test2', 'test3'], $doc3->getAttribute('varchar_array'));
23132313
$this->assertEquals([\str_repeat('x', 1000), \str_repeat('y', 2000)], $doc3->getAttribute('text_array'));
23142314

2315-
// Test VARCHAR size constraint (should fail)
2316-
try {
2317-
$database->createDocument('stringTypes', new Document([
2318-
'$id' => ID::custom('doc4'),
2319-
'$permissions' => [
2320-
Permission::read(Role::any()),
2321-
Permission::create(Role::any()),
2322-
Permission::update(Role::any()),
2323-
Permission::delete(Role::any()),
2324-
],
2325-
'varchar_field' => \str_repeat('a', 256), // Too long for VARCHAR(255)
2326-
]));
2327-
$this->fail('Failed to throw exception for VARCHAR size violation');
2328-
} catch (Exception $e) {
2329-
$this->assertInstanceOf(StructureException::class, $e);
2330-
}
2315+
// Test VARCHAR size constraint (should fail) - only for adapters that support attributes
2316+
if ($database->getAdapter()->getSupportForAttributes()) {
2317+
try {
2318+
$database->createDocument('stringTypes', new Document([
2319+
'$id' => ID::custom('doc4'),
2320+
'$permissions' => [
2321+
Permission::read(Role::any()),
2322+
Permission::create(Role::any()),
2323+
Permission::update(Role::any()),
2324+
Permission::delete(Role::any()),
2325+
],
2326+
'varchar_field' => \str_repeat('a', 256), // Too long for VARCHAR(255)
2327+
]));
2328+
$this->fail('Failed to throw exception for VARCHAR size violation');
2329+
} catch (Exception $e) {
2330+
$this->assertInstanceOf(StructureException::class, $e);
2331+
}
23312332

2332-
// Test TEXT size constraint (should fail)
2333-
try {
2334-
$database->createDocument('stringTypes', new Document([
2335-
'$id' => ID::custom('doc5'),
2336-
'$permissions' => [
2337-
Permission::read(Role::any()),
2338-
Permission::create(Role::any()),
2339-
Permission::update(Role::any()),
2340-
Permission::delete(Role::any()),
2341-
],
2342-
'text_field' => \str_repeat('a', 65536), // Too long for TEXT(65535)
2343-
]));
2344-
$this->fail('Failed to throw exception for TEXT size violation');
2345-
} catch (Exception $e) {
2346-
$this->assertInstanceOf(StructureException::class, $e);
2333+
// Test TEXT size constraint (should fail)
2334+
try {
2335+
$database->createDocument('stringTypes', new Document([
2336+
'$id' => ID::custom('doc5'),
2337+
'$permissions' => [
2338+
Permission::read(Role::any()),
2339+
Permission::create(Role::any()),
2340+
Permission::update(Role::any()),
2341+
Permission::delete(Role::any()),
2342+
],
2343+
'text_field' => \str_repeat('a', 65536), // Too long for TEXT(65535)
2344+
]));
2345+
$this->fail('Failed to throw exception for TEXT size violation');
2346+
} catch (Exception $e) {
2347+
$this->assertInstanceOf(StructureException::class, $e);
2348+
}
23472349
}
23482350

23492351
// Test querying by VARCHAR field

0 commit comments

Comments
 (0)