From ecd541d1d29b61a40d49a34165447cb31ec737ee Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Fri, 19 May 2023 14:32:51 +0300 Subject: [PATCH 1/5] Add the ability to check schema defaults --- composer.json | 2 +- src/Schema/RegistryModifier.php | 16 +++++++++------- .../Common/Schema/RegistryModifierTest.php | 17 ++++++++++++----- 3 files changed, 22 insertions(+), 13 deletions(-) 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..5cbd75b 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,13 @@ 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; + $handlers = $this->defaults[SchemaInterface::TYPECAST_HANDLER] ?? []; + if (!is_array($handlers)) { + $handlers = [$handlers]; } - - $handlers = (array) $handlers; $handlers[] = $handler; - $this->entity->setTypecast(array_unique($handlers)); + + $this->defaults[SchemaInterface::TYPECAST_HANDLER] = \array_unique($handlers); return $field; } diff --git a/tests/Behavior/Functional/Driver/Common/Schema/RegistryModifierTest.php b/tests/Behavior/Functional/Driver/Common/Schema/RegistryModifierTest.php index b4e4af9..a8007c9 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; @@ -95,14 +96,14 @@ public function testAddTypecast(): void $this->assertSame([Uuid::class, 'fromString'], $field1->getTypecast()); $this->assertSame('int', $field2->getTypecast()); - // entity has default typecast + // registry has default typecasts $this->assertSame( [Typecast::class, CustomTypecast::class], - $this->registry->getEntity(self::ROLE_TEST)->getTypecast() + $this->registry->getDefaults()[SchemaInterface::TYPECAST_HANDLER] ); } - public function testAddCustomTypecast(): void + public function testAddTypecastEntityWithTypecast(): void { $this->registry->getEntity(self::ROLE_TEST)->setTypecast(CustomTypecast::class); @@ -114,9 +115,15 @@ public function testAddCustomTypecast(): void // field has custom UUID typecast $this->assertSame([Uuid::class, 'fromString'], $field->getTypecast()); - // entity has default typecast and custom typecast + // registry has default typecast + $this->assertSame( + [Typecast::class], + $this->registry->getDefaults()[SchemaInterface::TYPECAST_HANDLER] + ); + + // entity has custom typecast $this->assertSame( - [CustomTypecast::class, Typecast::class], + CustomTypecast::class, $this->registry->getEntity(self::ROLE_TEST)->getTypecast() ); } From 8de5f02af41da0f9de7b14095fff39c0fa85acd6 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Fri, 19 May 2023 15:32:10 +0300 Subject: [PATCH 2/5] Add typecast to the entity --- src/Schema/RegistryModifier.php | 12 +++++-- .../Common/Schema/RegistryModifierTest.php | 31 +++++++++++++------ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/Schema/RegistryModifier.php b/src/Schema/RegistryModifier.php index 5cbd75b..ca3dda9 100644 --- a/src/Schema/RegistryModifier.php +++ b/src/Schema/RegistryModifier.php @@ -139,13 +139,19 @@ public function setTypecast(Field $field, array|string|null $rule, string $handl $field->setTypecast($rule); } - $handlers = $this->defaults[SchemaInterface::TYPECAST_HANDLER] ?? []; + $defaultHandlers = $this->defaults[SchemaInterface::TYPECAST_HANDLER] ?? []; + if (!is_array($defaultHandlers)) { + $defaultHandlers = [$defaultHandlers]; + } + + $handlers = $this->entity->getTypecast() ?? []; if (!is_array($handlers)) { $handlers = [$handlers]; } - $handlers[] = $handler; - $this->defaults[SchemaInterface::TYPECAST_HANDLER] = \array_unique($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 a8007c9..e7d7331 100644 --- a/tests/Behavior/Functional/Driver/Common/Schema/RegistryModifierTest.php +++ b/tests/Behavior/Functional/Driver/Common/Schema/RegistryModifierTest.php @@ -96,10 +96,10 @@ public function testAddTypecast(): void $this->assertSame([Uuid::class, 'fromString'], $field1->getTypecast()); $this->assertSame('int', $field2->getTypecast()); - // registry has default typecasts + // entity has default typecast $this->assertSame( [Typecast::class, CustomTypecast::class], - $this->registry->getDefaults()[SchemaInterface::TYPECAST_HANDLER] + $this->registry->getEntity(self::ROLE_TEST)->getTypecast() ); } @@ -115,19 +115,32 @@ public function testAddTypecastEntityWithTypecast(): void // field has custom UUID typecast $this->assertSame([Uuid::class, 'fromString'], $field->getTypecast()); - // registry has default typecast - $this->assertSame( - [Typecast::class], - $this->registry->getDefaults()[SchemaInterface::TYPECAST_HANDLER] - ); - // entity has custom typecast $this->assertSame( - CustomTypecast::class, + [CustomTypecast::class, Typecast::class], $this->registry->getEntity(self::ROLE_TEST)->getTypecast() ); } + 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'); From 5fa973b041c274b1da0655ecab15b71f881d2f66 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Fri, 19 May 2023 15:34:13 +0300 Subject: [PATCH 3/5] Revert comment --- .../Functional/Driver/Common/Schema/RegistryModifierTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Behavior/Functional/Driver/Common/Schema/RegistryModifierTest.php b/tests/Behavior/Functional/Driver/Common/Schema/RegistryModifierTest.php index e7d7331..4efe292 100644 --- a/tests/Behavior/Functional/Driver/Common/Schema/RegistryModifierTest.php +++ b/tests/Behavior/Functional/Driver/Common/Schema/RegistryModifierTest.php @@ -115,7 +115,7 @@ public function testAddTypecastEntityWithTypecast(): void // field has custom UUID typecast $this->assertSame([Uuid::class, 'fromString'], $field->getTypecast()); - // entity has custom typecast + // entity has default typecast and custom typecast $this->assertSame( [CustomTypecast::class, Typecast::class], $this->registry->getEntity(self::ROLE_TEST)->getTypecast() From afb725207a0c50135eb3689e5466d9684e0bd18a Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Fri, 19 May 2023 15:43:04 +0300 Subject: [PATCH 4/5] Fix gh actions --- .github/workflows/ci-mssql.yml | 6 ++++-- .github/workflows/ci-mysql.yml | 6 ++++-- .github/workflows/ci-pgsql.yml | 6 ++++-- .github/workflows/main.yml | 8 ++++++-- 4 files changed, 18 insertions(+), 8 deletions(-) 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: From 93a8e2f470b8f6f0634246b13a442760fa579e21 Mon Sep 17 00:00:00 2001 From: Aleksei Gagarin Date: Fri, 19 May 2023 16:53:42 +0300 Subject: [PATCH 5/5] Apply suggestions from code review --- src/Schema/RegistryModifier.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Schema/RegistryModifier.php b/src/Schema/RegistryModifier.php index ca3dda9..86b0a32 100644 --- a/src/Schema/RegistryModifier.php +++ b/src/Schema/RegistryModifier.php @@ -140,7 +140,7 @@ public function setTypecast(Field $field, array|string|null $rule, string $handl } $defaultHandlers = $this->defaults[SchemaInterface::TYPECAST_HANDLER] ?? []; - if (!is_array($defaultHandlers)) { + if (!\is_array($defaultHandlers)) { $defaultHandlers = [$defaultHandlers]; } @@ -149,7 +149,7 @@ public function setTypecast(Field $field, array|string|null $rule, string $handl $handlers = [$handlers]; } - if (!\in_array($handler, $handlers, true) && !in_array($handler, $defaultHandlers, true)) { + if (!\in_array($handler, $handlers, true) && !\in_array($handler, $defaultHandlers, true)) { $this->entity->setTypecast(\array_merge($handlers, [$handler])); }