diff --git a/.github/workflows/ci-mssql.yml b/.github/workflows/ci-mssql.yml index f3c9fd4..6d170b7 100644 --- a/.github/workflows/ci-mssql.yml +++ b/.github/workflows/ci-mssql.yml @@ -1,6 +1,8 @@ on: - - pull_request - - push + pull_request: + push: + branches: + - '*.*' name: ci-mssql diff --git a/.github/workflows/ci-mysql.yml b/.github/workflows/ci-mysql.yml index c69859b..3b48916 100644 --- a/.github/workflows/ci-mysql.yml +++ b/.github/workflows/ci-mysql.yml @@ -1,6 +1,8 @@ on: - - pull_request - - push + pull_request: + push: + branches: + - '*.*' name: ci-mysql diff --git a/.github/workflows/ci-pgsql.yml b/.github/workflows/ci-pgsql.yml index 38aa820..469429a 100644 --- a/.github/workflows/ci-pgsql.yml +++ b/.github/workflows/ci-pgsql.yml @@ -1,6 +1,8 @@ on: - - pull_request - - push + pull_request: + push: + branches: + - '*.*' name: ci-pgsql diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d0c966f..754ea0c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,6 +1,10 @@ -name: build +on: + pull_request: + push: + branches: + - '*.*' -on: [push, pull_request] +name: build jobs: test: diff --git a/composer.json b/composer.json index 965dbc4..7ba69cc 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "php": ">=8.0", "psr/event-dispatcher": "^1", "cycle/orm": "^2.0", - "cycle/schema-builder": "^2.0", + "cycle/schema-builder": "^2.5", "psr/container": "^1.0|^2.0", "yiisoft/injector": "^1.0" }, diff --git a/src/Schema/RegistryModifier.php b/src/Schema/RegistryModifier.php index bacf047..86b0a32 100644 --- a/src/Schema/RegistryModifier.php +++ b/src/Schema/RegistryModifier.php @@ -9,6 +9,8 @@ use Cycle\ORM\Entity\Behavior\Exception\BehaviorCompilationException; use Cycle\ORM\Parser\Typecast; use Cycle\ORM\Parser\TypecastInterface; +use Cycle\ORM\SchemaInterface; +use Cycle\Schema\Defaults; use Cycle\Schema\Definition\Entity; use Cycle\Schema\Definition\Field; use Cycle\Schema\Definition\Map\FieldMap; @@ -39,12 +41,14 @@ class RegistryModifier protected FieldMap $fields; protected AbstractTable $table; protected Entity $entity; + protected Defaults $defaults; public function __construct(Registry $registry, string $role) { $this->entity = $registry->getEntity($role); $this->fields = $this->entity->getFields(); $this->table = $registry->getTableSchema($this->entity); + $this->defaults = $registry->getDefaults(); } public function addDatetimeColumn(string $columnName, string $fieldName): AbstractColumn @@ -135,15 +139,19 @@ public function setTypecast(Field $field, array|string|null $rule, string $handl $field->setTypecast($rule); } - $handlers = $this->entity->getTypecast(); - if ($handlers === null) { - $this->entity->setTypecast($handler); - return $field; + $defaultHandlers = $this->defaults[SchemaInterface::TYPECAST_HANDLER] ?? []; + if (!\is_array($defaultHandlers)) { + $defaultHandlers = [$defaultHandlers]; } - $handlers = (array) $handlers; - $handlers[] = $handler; - $this->entity->setTypecast(array_unique($handlers)); + $handlers = $this->entity->getTypecast() ?? []; + if (!is_array($handlers)) { + $handlers = [$handlers]; + } + + if (!\in_array($handler, $handlers, true) && !\in_array($handler, $defaultHandlers, true)) { + $this->entity->setTypecast(\array_merge($handlers, [$handler])); + } return $field; } diff --git a/tests/Behavior/Functional/Driver/Common/Schema/RegistryModifierTest.php b/tests/Behavior/Functional/Driver/Common/Schema/RegistryModifierTest.php index b4e4af9..4efe292 100644 --- a/tests/Behavior/Functional/Driver/Common/Schema/RegistryModifierTest.php +++ b/tests/Behavior/Functional/Driver/Common/Schema/RegistryModifierTest.php @@ -9,6 +9,7 @@ use Cycle\ORM\Entity\Behavior\Tests\Fixtures\CustomTypecast; use Cycle\ORM\Entity\Behavior\Tests\Functional\Driver\Common\BaseTest; use Cycle\ORM\Parser\Typecast; +use Cycle\ORM\SchemaInterface; use Cycle\Schema\Definition\Entity; use Cycle\Schema\Registry; use Ramsey\Uuid\Uuid; @@ -102,7 +103,7 @@ public function testAddTypecast(): void ); } - public function testAddCustomTypecast(): void + public function testAddTypecastEntityWithTypecast(): void { $this->registry->getEntity(self::ROLE_TEST)->setTypecast(CustomTypecast::class); @@ -121,6 +122,25 @@ public function testAddCustomTypecast(): void ); } + public function testAddTypecastShouldBeSkipped(): void + { + $this->registry->getEntity(self::ROLE_TEST); + + $this->modifier->addUuidColumn('uuid_column', 'uuid'); + $this->registry->getDefaults()->offsetSet(SchemaInterface::TYPECAST_HANDLER, Typecast::class); + + $this->assertNull($this->registry->getEntity(self::ROLE_TEST)->getTypecast()); + } + + public function testAddTypecastShouldBeDuplicated(): void + { + $this->registry->getEntity(self::ROLE_TEST)->setTypecast(CustomTypecast::class); + + $this->modifier->addUuidColumn('uuid_column', 'uuid'); + + $this->assertSame(CustomTypecast::class, $this->registry->getEntity(self::ROLE_TEST)->getTypecast()); + } + public function testCustomTypecastNotOverridden(): void { $this->modifier->addUuidColumn('uuid_column', 'uuid');