-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathRegistryModifierTest.php
More file actions
236 lines (175 loc) · 8.58 KB
/
RegistryModifierTest.php
File metadata and controls
236 lines (175 loc) · 8.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
declare(strict_types=1);
namespace Cycle\ORM\Entity\Behavior\Tests\Functional\Driver\Common\Schema;
use Cycle\Database\ColumnInterface;
use Cycle\ORM\Entity\Behavior\Exception\BehaviorCompilationException;
use Cycle\ORM\Entity\Behavior\Schema\RegistryModifier;
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;
abstract class RegistryModifierTest extends BaseTest
{
private const ROLE_TEST = 'test';
protected RegistryModifier $modifier;
protected Registry $registry;
public function testAddDatetimeField(): void
{
$this->modifier->addDatetimeColumn('created_at', 'createdAt');
$entity = $this->registry->getEntity(self::ROLE_TEST);
$fields = $entity->getFields();
$this->assertTrue($fields->has('createdAt'));
$this->assertSame('datetime', $fields->get('createdAt')->getType());
$this->assertSame('created_at', $fields->get('createdAt')->getColumn());
}
public function testAddStringField(): void
{
$this->modifier->addStringColumn('version_str', 'version');
$entity = $this->registry->getEntity(self::ROLE_TEST);
$fields = $entity->getFields();
$this->assertTrue($fields->has('version'));
$this->assertSame(ColumnInterface::STRING, $fields->get('version')->getType());
$this->assertSame('version_str', $fields->get('version')->getColumn());
}
public function testAddIntegerField(): void
{
$this->modifier->addIntegerColumn('version_int', 'version');
$entity = $this->registry->getEntity(self::ROLE_TEST);
$fields = $entity->getFields();
$this->assertTrue($fields->has('version'));
$this->assertSame('integer', $fields->get('version')->getType());
$this->assertSame('version_int', $fields->get('version')->getColumn());
}
public function testAddBigIntegerField(): void
{
$this->modifier->addBigIntegerColumn('snowflake_column', 'snowflake');
$entity = $this->registry->getEntity(self::ROLE_TEST);
$fields = $entity->getFields();
$this->assertTrue($fields->has('snowflake'));
$this->assertSame('bigInteger', $fields->get('snowflake')->getType());
$this->assertSame('snowflake_column', $fields->get('snowflake')->getColumn());
}
public function testAddBigIntegerFieldThrowsException(): void
{
$this->modifier->addIntegerColumn('snowflake_column', 'snowflake');
$this->expectException(BehaviorCompilationException::class);
$this->expectExceptionMessage('Field snowflake must be of type big integer.');
$this->modifier->addBigIntegerColumn('snowflake_column', 'snowflake');
}
public function testAddSnowflakeField(): void
{
$this->modifier->addSnowflakeColumn('snowflake_column', 'snowflake');
$entity = $this->registry->getEntity(self::ROLE_TEST);
$fields = $entity->getFields();
$this->assertTrue($fields->has('snowflake'));
$this->assertSame('snowflake', $fields->get('snowflake')->getType());
$this->assertSame('snowflake_column', $fields->get('snowflake')->getColumn());
}
public function testAddSnowflakeFieldThrowsException(): void
{
$this->modifier->addStringColumn('snowflake_column', 'snowflake');
$this->expectException(BehaviorCompilationException::class);
$this->expectExceptionMessage('Field snowflake must be of type snowflake.');
$this->modifier->addSnowflakeColumn('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');
$entity = $this->registry->getEntity(self::ROLE_TEST);
$fields = $entity->getFields();
$this->assertTrue($fields->has('uuid'));
$this->assertSame('uuid', $fields->get('uuid')->getType());
$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');
$this->modifier->addIntegerColumn('counter_column', 'counter');
$this->modifier->addBigIntegerColumn('snowflake_column', 'snowflake');
$field1 = $this->registry->getEntity(self::ROLE_TEST)->getFields()->get('uuid');
$field2 = $this->registry->getEntity(self::ROLE_TEST)->getFields()->get('counter');
$field3 = $this->registry->getEntity(self::ROLE_TEST)->getFields()->get('snowflake');
$this->modifier->setTypecast($field1, [Uuid::class, 'fromString']);
$this->modifier->setTypecast($field2, 'int', CustomTypecast::class);
$this->modifier->setTypecast($field3, 'int', CustomTypecast::class);
// field has custom UUID typecast
$this->assertSame([Uuid::class, 'fromString'], $field1->getTypecast());
$this->assertSame('int', $field2->getTypecast());
$this->assertSame('int', $field3->getTypecast());
// entity has default typecast
$this->assertSame(
[Typecast::class, CustomTypecast::class],
$this->registry->getEntity(self::ROLE_TEST)->getTypecast(),
);
}
public function testAddTypecastEntityWithTypecast(): void
{
$this->registry->getEntity(self::ROLE_TEST)->setTypecast(CustomTypecast::class);
$this->modifier->addUuidColumn('uuid_column', 'uuid');
$field = $this->registry->getEntity(self::ROLE_TEST)->getFields()->get('uuid');
$this->modifier->setTypecast($field, [Uuid::class, 'fromString']);
// field has custom UUID typecast
$this->assertSame([Uuid::class, 'fromString'], $field->getTypecast());
// entity has default typecast and custom typecast
$this->assertSame(
[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');
$field = $this->registry->getEntity(self::ROLE_TEST)->getFields()->get('uuid');
$field->setTypecast(['foo', 'bar']);
$this->modifier->setTypecast($field, [Uuid::class, 'fromString']);
$this->assertSame(['foo', 'bar'], $field->getTypecast());
}
public function setUp(): void
{
parent::setUp();
$this->registry = new Registry($this->dbal);
$entity = (new Entity())->setRole(self::ROLE_TEST);
$this->registry->register($entity);
$this->registry->linkTable($entity, 'default', 'tests');
$this->modifier = new RegistryModifier($this->registry, self::ROLE_TEST);
}
}