Skip to content

Commit 891fd97

Browse files
msmakouzroxblnfk
andauthored
Add the ability to check schema defaults (#30)
Co-authored-by: Aleksei Gagarin <[email protected]>
1 parent d148455 commit 891fd97

File tree

7 files changed

+55
-17
lines changed

7 files changed

+55
-17
lines changed

.github/workflows/ci-mssql.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
on:
2-
- pull_request
3-
- push
2+
pull_request:
3+
push:
4+
branches:
5+
- '*.*'
46

57
name: ci-mssql
68

.github/workflows/ci-mysql.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
on:
2-
- pull_request
3-
- push
2+
pull_request:
3+
push:
4+
branches:
5+
- '*.*'
46

57
name: ci-mysql
68

.github/workflows/ci-pgsql.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
on:
2-
- pull_request
3-
- push
2+
pull_request:
3+
push:
4+
branches:
5+
- '*.*'
46

57
name: ci-pgsql
68

.github/workflows/main.yml

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
name: build
1+
on:
2+
pull_request:
3+
push:
4+
branches:
5+
- '*.*'
26

3-
on: [push, pull_request]
7+
name: build
48

59
jobs:
610
test:

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"php": ">=8.0",
77
"psr/event-dispatcher": "^1",
88
"cycle/orm": "^2.0",
9-
"cycle/schema-builder": "^2.0",
9+
"cycle/schema-builder": "^2.5",
1010
"psr/container": "^1.0|^2.0",
1111
"yiisoft/injector": "^1.0"
1212
},

src/Schema/RegistryModifier.php

+15-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Cycle\ORM\Entity\Behavior\Exception\BehaviorCompilationException;
1010
use Cycle\ORM\Parser\Typecast;
1111
use Cycle\ORM\Parser\TypecastInterface;
12+
use Cycle\ORM\SchemaInterface;
13+
use Cycle\Schema\Defaults;
1214
use Cycle\Schema\Definition\Entity;
1315
use Cycle\Schema\Definition\Field;
1416
use Cycle\Schema\Definition\Map\FieldMap;
@@ -39,12 +41,14 @@ class RegistryModifier
3941
protected FieldMap $fields;
4042
protected AbstractTable $table;
4143
protected Entity $entity;
44+
protected Defaults $defaults;
4245

4346
public function __construct(Registry $registry, string $role)
4447
{
4548
$this->entity = $registry->getEntity($role);
4649
$this->fields = $this->entity->getFields();
4750
$this->table = $registry->getTableSchema($this->entity);
51+
$this->defaults = $registry->getDefaults();
4852
}
4953

5054
public function addDatetimeColumn(string $columnName, string $fieldName): AbstractColumn
@@ -135,15 +139,19 @@ public function setTypecast(Field $field, array|string|null $rule, string $handl
135139
$field->setTypecast($rule);
136140
}
137141

138-
$handlers = $this->entity->getTypecast();
139-
if ($handlers === null) {
140-
$this->entity->setTypecast($handler);
141-
return $field;
142+
$defaultHandlers = $this->defaults[SchemaInterface::TYPECAST_HANDLER] ?? [];
143+
if (!\is_array($defaultHandlers)) {
144+
$defaultHandlers = [$defaultHandlers];
142145
}
143146

144-
$handlers = (array) $handlers;
145-
$handlers[] = $handler;
146-
$this->entity->setTypecast(array_unique($handlers));
147+
$handlers = $this->entity->getTypecast() ?? [];
148+
if (!is_array($handlers)) {
149+
$handlers = [$handlers];
150+
}
151+
152+
if (!\in_array($handler, $handlers, true) && !\in_array($handler, $defaultHandlers, true)) {
153+
$this->entity->setTypecast(\array_merge($handlers, [$handler]));
154+
}
147155

148156
return $field;
149157
}

tests/Behavior/Functional/Driver/Common/Schema/RegistryModifierTest.php

+21-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Cycle\ORM\Entity\Behavior\Tests\Fixtures\CustomTypecast;
1010
use Cycle\ORM\Entity\Behavior\Tests\Functional\Driver\Common\BaseTest;
1111
use Cycle\ORM\Parser\Typecast;
12+
use Cycle\ORM\SchemaInterface;
1213
use Cycle\Schema\Definition\Entity;
1314
use Cycle\Schema\Registry;
1415
use Ramsey\Uuid\Uuid;
@@ -102,7 +103,7 @@ public function testAddTypecast(): void
102103
);
103104
}
104105

105-
public function testAddCustomTypecast(): void
106+
public function testAddTypecastEntityWithTypecast(): void
106107
{
107108
$this->registry->getEntity(self::ROLE_TEST)->setTypecast(CustomTypecast::class);
108109

@@ -121,6 +122,25 @@ public function testAddCustomTypecast(): void
121122
);
122123
}
123124

125+
public function testAddTypecastShouldBeSkipped(): void
126+
{
127+
$this->registry->getEntity(self::ROLE_TEST);
128+
129+
$this->modifier->addUuidColumn('uuid_column', 'uuid');
130+
$this->registry->getDefaults()->offsetSet(SchemaInterface::TYPECAST_HANDLER, Typecast::class);
131+
132+
$this->assertNull($this->registry->getEntity(self::ROLE_TEST)->getTypecast());
133+
}
134+
135+
public function testAddTypecastShouldBeDuplicated(): void
136+
{
137+
$this->registry->getEntity(self::ROLE_TEST)->setTypecast(CustomTypecast::class);
138+
139+
$this->modifier->addUuidColumn('uuid_column', 'uuid');
140+
141+
$this->assertSame(CustomTypecast::class, $this->registry->getEntity(self::ROLE_TEST)->getTypecast());
142+
}
143+
124144
public function testCustomTypecastNotOverridden(): void
125145
{
126146
$this->modifier->addUuidColumn('uuid_column', 'uuid');

0 commit comments

Comments
 (0)