diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 06a83b4..6200c7f 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,138 +1,137 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - command]]> - command]]> - command]]> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + command]]> + command]]> + command]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Schema/RegistryModifier.php b/src/Schema/RegistryModifier.php index 1bff41e..ed523de 100644 --- a/src/Schema/RegistryModifier.php +++ b/src/Schema/RegistryModifier.php @@ -41,6 +41,7 @@ class RegistryModifier protected const STRING_COLUMN = AbstractColumn::STRING; protected const BIG_INTEGER_COLUMN = 'bigInteger'; protected const DATETIME_COLUMN = 'datetime'; + protected const ULID_COLUMN = 'ulid'; protected const UUID_COLUMN = 'uuid'; protected FieldMap $fields; @@ -84,11 +85,18 @@ public static function isStringType(string $type): bool return $matches['type'] === 'string'; } + public static function isUlidType(string $type): bool + { + \preg_match(self::DEFINITION, $type, $matches); + + return $matches['type'] === self::ULID_COLUMN; + } + public static function isUuidType(string $type): bool { \preg_match(self::DEFINITION, $type, $matches); - return $matches['type'] === 'uuid'; + return $matches['type'] === self::UUID_COLUMN; } public function addDatetimeColumn(string $columnName, string $fieldName, int|null $generated = null): AbstractColumn @@ -179,6 +187,29 @@ public function addStringColumn(string $columnName, string $fieldName, int|null return $this->table->column($columnName)->type(self::STRING_COLUMN); } + /** + * @throws BehaviorCompilationException + */ + public function addUlidColumn(string $columnName, string $fieldName, int|null $generated = null): AbstractColumn + { + if ($this->fields->has($fieldName)) { + if (!static::isUlidType($this->fields->get($fieldName)->getType())) { + throw new BehaviorCompilationException( + \sprintf('Field %s must be of type %s.', $fieldName, self::ULID_COLUMN), + ); + } + $this->validateColumnName($fieldName, $columnName); + $this->fields->get($fieldName)->setGenerated($generated); + + return $this->table->column($columnName); + } + + $field = (new Field())->setColumn($columnName)->setType(self::ULID_COLUMN)->setGenerated($generated); + $this->fields->set($fieldName, $field); + + return $this->table->column($columnName)->type(self::ULID_COLUMN); + } + /** * @throws BehaviorCompilationException */ @@ -186,7 +217,9 @@ public function addUuidColumn(string $columnName, string $fieldName, int|null $g { if ($this->fields->has($fieldName)) { if (!static::isUuidType($this->fields->get($fieldName)->getType())) { - throw new BehaviorCompilationException(\sprintf('Field %s must be of type uuid.', $fieldName)); + throw new BehaviorCompilationException( + \sprintf('Field %s must be of type %s.', $fieldName, self::UUID_COLUMN), + ); } $this->validateColumnName($fieldName, $columnName); $this->fields->get($fieldName)->setGenerated($generated); @@ -194,7 +227,7 @@ public function addUuidColumn(string $columnName, string $fieldName, int|null $g return $this->table->column($columnName); } - $field = (new Field())->setColumn($columnName)->setType('uuid')->setGenerated($generated); + $field = (new Field())->setColumn($columnName)->setType(self::UUID_COLUMN)->setGenerated($generated); $this->fields->set($fieldName, $field); return $this->table->column($columnName)->type(self::UUID_COLUMN); @@ -257,6 +290,10 @@ protected function validateColumnName(string $fieldName, string $columnName): vo /** * @deprecated since v1.2 + * + * @param non-empty-string $type + * @param non-empty-string $fieldName + * @param non-empty-string $columnName */ protected function isType(string $type, string $fieldName, string $columnName): bool { diff --git a/tests/Behavior/Functional/Driver/Common/Schema/RegistryModifierTest.php b/tests/Behavior/Functional/Driver/Common/Schema/RegistryModifierTest.php index 6e16118..04f43b5 100644 --- a/tests/Behavior/Functional/Driver/Common/Schema/RegistryModifierTest.php +++ b/tests/Behavior/Functional/Driver/Common/Schema/RegistryModifierTest.php @@ -80,6 +80,28 @@ public function testAddBigIntegerFieldThrowsException(): void $this->modifier->addBigIntegerColumn('snowflake_column', 'snowflake'); } + public function testAddUlidField(): void + { + $this->modifier->addUlidColumn('ulid_column', 'ulid'); + + $entity = $this->registry->getEntity(self::ROLE_TEST); + $fields = $entity->getFields(); + + $this->assertTrue($fields->has('ulid')); + $this->assertSame('ulid', $fields->get('ulid')->getType()); + $this->assertSame('ulid_column', $fields->get('ulid')->getColumn()); + } + + public function testAddUlidFieldThrowsException(): void + { + $this->modifier->addIntegerColumn('ulid_column', 'ulid'); + + $this->expectException(BehaviorCompilationException::class); + $this->expectExceptionMessage('Field ulid must be of type ulid.'); + + $this->modifier->addUlidColumn('ulid_column', 'ulid'); + } + public function testAddUuidField(): void { $this->modifier->addUuidColumn('uuid_column', 'uuid'); @@ -92,6 +114,16 @@ public function testAddUuidField(): void $this->assertSame('uuid_column', $fields->get('uuid')->getColumn()); } + public function testAddUuidFieldThrowsException(): void + { + $this->modifier->addIntegerColumn('uuid_column', 'uuid'); + + $this->expectException(BehaviorCompilationException::class); + $this->expectExceptionMessage('Field uuid must be of type uuid.'); + + $this->modifier->addUuidColumn('uuid_column', 'uuid'); + } + public function testAddTypecast(): void { $this->modifier->addUuidColumn('uuid_column', 'uuid'); diff --git a/tests/Behavior/Unit/Schema/RegistryModifierTest.php b/tests/Behavior/Unit/Schema/RegistryModifierTest.php index d93fe5c..fa8cafc 100644 --- a/tests/Behavior/Unit/Schema/RegistryModifierTest.php +++ b/tests/Behavior/Unit/Schema/RegistryModifierTest.php @@ -121,6 +121,22 @@ public function testIsStringTypeFalse(mixed $type): void $this->assertFalse(RegistryModifier::isStringType($type)); } + public function testIsUlidTypeTrue(): void + { + $this->assertTrue(RegistryModifier::isUlidType('ulid')); + } + + /** + * @dataProvider integerDataProvider + * @dataProvider datetimeDataProvider + * @dataProvider invalidDataProvider + * @dataProvider stringDataProvider + */ + public function testIsUlidTypeFalse(mixed $type): void + { + $this->assertFalse(RegistryModifier::isUlidType($type)); + } + public function testIsUuidTypeTrue(): void { $this->assertTrue(RegistryModifier::isUuidType('uuid'));