Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16910 from mauriciofauth/database-name-vo
Add DatabaseName value object
- Loading branch information
Showing
5 changed files
with
101 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpMyAdmin\Dbal; | ||
|
||
use Webmozart\Assert\Assert; | ||
|
||
/** @psalm-immutable */ | ||
final class DatabaseName | ||
{ | ||
/** | ||
* @see https://dev.mysql.com/doc/refman/en/identifier-length.html | ||
* @see https://mariadb.com/kb/en/identifier-names/#maximum-length | ||
*/ | ||
private const MAX_LENGTH = 64; | ||
|
||
/** | ||
* @var string | ||
* @psalm-var non-empty-string | ||
*/ | ||
private $name; | ||
|
||
public function __construct(string $name) | ||
{ | ||
Assert::stringNotEmpty($name); | ||
Assert::maxLength($name, self::MAX_LENGTH); | ||
Assert::notEndsWith($name, ' '); | ||
$this->name = $name; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpMyAdmin\Tests\Dbal; | ||
|
||
use InvalidArgumentException; | ||
use PhpMyAdmin\Dbal\DatabaseName; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
use function str_repeat; | ||
|
||
class DatabaseNameTest extends TestCase | ||
{ | ||
public function testEmptyName(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('Expected a different value than "".'); | ||
new DatabaseName(''); | ||
} | ||
|
||
public function testNameWithTrailingWhitespace(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('Expected a value not to end with " ". Got: "a "'); | ||
new DatabaseName('a '); | ||
} | ||
|
||
public function testLongName(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage( | ||
'Expected a value to contain at most 64 characters. Got: ' | ||
. '"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"' | ||
); | ||
new DatabaseName(str_repeat('a', 65)); | ||
} | ||
|
||
public function testValidName(): void | ||
{ | ||
$name = new DatabaseName('name'); | ||
$this->assertEquals('name', $name->getName()); | ||
$this->assertEquals('name', (string) $name); | ||
} | ||
} |