2023-06-23 23:07:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Tests\Unit\Utopia\Database\Validator;
|
|
|
|
|
|
|
|
|
|
use Appwrite\Utopia\Database\Validator\ProjectId;
|
2026-01-15 03:14:53 +00:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
2023-06-23 23:07:58 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
|
|
class ProjectIdTest extends TestCase
|
|
|
|
|
{
|
2024-01-09 02:21:43 +00:00
|
|
|
protected ?ProjectId $object = null;
|
2023-06-23 23:07:58 +00:00
|
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
|
{
|
|
|
|
|
$this->object = new ProjectId();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function tearDown(): void
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-15 03:14:53 +00:00
|
|
|
public static function provideTest(): array
|
2023-06-23 23:07:58 +00:00
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'unique()' => ['unique()', true],
|
|
|
|
|
'dashes' => ['as12-df34', true],
|
|
|
|
|
'36 chars' => [\str_repeat('a', 36), true],
|
|
|
|
|
'uppercase' => ['ABC', false],
|
|
|
|
|
'underscore' => ['under_score', false],
|
|
|
|
|
'leading dash' => ['-dash', false],
|
|
|
|
|
'too long' => [\str_repeat('a', 37), false],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-15 03:14:53 +00:00
|
|
|
#[DataProvider('provideTest')]
|
2023-06-23 23:07:58 +00:00
|
|
|
public function testValues(string $input, bool $expected): void
|
|
|
|
|
{
|
|
|
|
|
$this->assertEquals($this->object->isValid($input), $expected);
|
|
|
|
|
}
|
2025-10-21 02:31:46 +00:00
|
|
|
|
|
|
|
|
public function testCustomMaxLength(): void
|
|
|
|
|
{
|
|
|
|
|
// Test with MongoDB max length (255)
|
|
|
|
|
$validator = new ProjectId(255);
|
|
|
|
|
$this->assertTrue($validator->isValid(\str_repeat('a', 255)));
|
|
|
|
|
$this->assertFalse($validator->isValid(\str_repeat('a', 256)));
|
|
|
|
|
|
|
|
|
|
// Test with smaller custom length
|
|
|
|
|
$validator = new ProjectId(10);
|
|
|
|
|
$this->assertTrue($validator->isValid(\str_repeat('a', 10)));
|
|
|
|
|
$this->assertFalse($validator->isValid(\str_repeat('a', 11)));
|
|
|
|
|
|
|
|
|
|
// Verify description updates
|
|
|
|
|
$this->assertStringContainsString('10 chars', $validator->getDescription());
|
|
|
|
|
}
|
2023-06-23 23:07:58 +00:00
|
|
|
}
|