3 declare(strict_types
=1);
5 namespace PhpMyAdmin\Tests\Identifiers
;
7 use PhpMyAdmin\Identifiers\InvalidTableName
;
8 use PhpMyAdmin\Identifiers\TableName
;
9 use PHPUnit\Framework\Attributes\CoversClass
;
10 use PHPUnit\Framework\Attributes\DataProvider
;
11 use PHPUnit\Framework\TestCase
;
13 use function str_repeat
;
15 #[CoversClass(TableName::class)]
16 #[CoversClass(InvalidTableName::class)]
17 class TableNameTest
extends TestCase
19 #[DataProvider('providerForTestValidNames')]
20 public function testValidName(string $validName): void
22 $name = TableName
::from($validName);
23 $this->assertEquals($validName, $name->getName());
24 $this->assertEquals($validName, (string) $name);
27 #[DataProvider('providerForTestValidNames')]
28 public function testTryFromValueValidName(string $validName): void
30 $name = TableName
::tryFrom($validName);
31 $this->assertNotNull($name);
32 $this->assertEquals($validName, $name->getName());
33 $this->assertEquals($validName, (string) $name);
36 /** @return iterable<int, string[]> */
37 public static function providerForTestValidNames(): iterable
41 yield
[str_repeat('a', 64)];
44 #[DataProvider('providerForTestInvalidNames')]
45 public function testInvalidNames(mixed $name, string $exceptionMessage): void
47 $this->assertNull(TableName
::tryFrom($name));
48 $this->expectException(InvalidTableName
::class);
49 $this->expectExceptionMessage($exceptionMessage);
50 TableName
::from($name);
54 * @return iterable<string, mixed[]>
55 * @psalm-return iterable<string, array{mixed, non-empty-string}>
57 public static function providerForTestInvalidNames(): iterable
59 yield
'null' => [null, 'The table name must be a non-empty string.'];
60 yield
'integer' => [1, 'The table name must be a non-empty string.'];
61 yield
'array' => [['table'], 'The table name must be a non-empty string.'];
62 yield
'empty string' => ['', 'The table name must be a non-empty string.'];
63 yield
'too long name' => [str_repeat('a', 65), 'The table name cannot be longer than 64 characters.'];
64 yield
'trailing space' => ['a ', 'The table name cannot end with a space character.'];