Skip to content

Commit

Permalink
Add the ability to check schema defaults (#30)
Browse files Browse the repository at this point in the history
Co-authored-by: Aleksei Gagarin <roxblnfk@ya.ru>
  • Loading branch information
msmakouz and roxblnfk authored May 19, 2023
1 parent d148455 commit 891fd97
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 17 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci-mssql.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
on:
- pull_request
- push
pull_request:
push:
branches:
- '*.*'

name: ci-mssql

Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/ci-mysql.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
on:
- pull_request
- push
pull_request:
push:
branches:
- '*.*'

name: ci-mysql

Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/ci-pgsql.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
on:
- pull_request
- push
pull_request:
push:
branches:
- '*.*'

name: ci-pgsql

Expand Down
8 changes: 6 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: build
on:
pull_request:
push:
branches:
- '*.*'

on: [push, pull_request]
name: build

jobs:
test:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
22 changes: 15 additions & 7 deletions src/Schema/RegistryModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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');
Expand Down

0 comments on commit 891fd97

Please sign in to comment.