1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\FluidSchema; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Schema\Schema; |
6
|
|
|
use Doctrine\DBAL\Types\Type; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
class TdbmFluidTableTest extends TestCase |
10
|
|
|
{ |
11
|
|
View Code Duplication |
public function testColumn() |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
$schema = new Schema(); |
14
|
|
|
$fluid = new TdbmFluidSchema($schema); |
15
|
|
|
|
16
|
|
|
$posts = $fluid->table('posts'); |
17
|
|
|
|
18
|
|
|
$column = $posts->column('foo'); |
19
|
|
|
|
20
|
|
|
$this->assertTrue($schema->getTable('posts')->hasColumn('foo')); |
21
|
|
|
|
22
|
|
|
$this->assertSame($column, $posts->column('foo'), 'Failed asserting that the same instance is returned.'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testExistingColumn() |
26
|
|
|
{ |
27
|
|
|
$schema = new Schema(); |
28
|
|
|
$postsSchemaTable = $schema->createTable('posts'); |
29
|
|
|
$postsSchemaTable->addColumn('foo', 'string'); |
30
|
|
|
$fluid = new TdbmFluidSchema($schema); |
31
|
|
|
|
32
|
|
|
$posts = $fluid->table('posts'); |
33
|
|
|
|
34
|
|
|
$posts->column('foo')->integer(); |
35
|
|
|
|
36
|
|
|
$this->assertSame(Type::getType(Type::INTEGER), $schema->getTable('posts')->getColumn('foo')->getType()); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testIndex() |
40
|
|
|
{ |
41
|
|
|
$schema = new Schema(); |
42
|
|
|
$fluid = new TdbmFluidSchema($schema); |
43
|
|
|
|
44
|
|
|
$posts = $fluid->table('posts'); |
45
|
|
|
|
46
|
|
|
$posts->column('foo')->integer()->then()->index(['foo']); |
47
|
|
|
|
48
|
|
|
$this->assertCount(1, $schema->getTable('posts')->getIndexes()); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testUnique() |
52
|
|
|
{ |
53
|
|
|
$schema = new Schema(); |
54
|
|
|
$fluid = new TdbmFluidSchema($schema); |
55
|
|
|
|
56
|
|
|
$posts = $fluid->table('posts'); |
57
|
|
|
|
58
|
|
|
$posts->column('foo')->integer()->then()->unique(['foo']); |
59
|
|
|
|
60
|
|
|
$this->assertCount(1, $schema->getTable('posts')->getIndexes()); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
public function testPrimaryKey() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$schema = new Schema(); |
66
|
|
|
$fluid = new TdbmFluidSchema($schema); |
67
|
|
|
|
68
|
|
|
$posts = $fluid->table('posts'); |
69
|
|
|
|
70
|
|
|
$posts->column('id')->integer()->then()->primaryKey(['id'], 'pkname'); |
71
|
|
|
|
72
|
|
|
$this->assertTrue($schema->getTable('posts')->hasPrimaryKey()); |
73
|
|
|
$this->assertTrue($schema->getTable('posts')->hasIndex('pkname')); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testId() |
77
|
|
|
{ |
78
|
|
|
$schema = new Schema(); |
79
|
|
|
$fluid = new TdbmFluidSchema($schema); |
80
|
|
|
|
81
|
|
|
$posts = $fluid->table('posts'); |
82
|
|
|
|
83
|
|
|
$posts->id(); |
84
|
|
|
|
85
|
|
|
$this->assertTrue($schema->getTable('posts')->hasPrimaryKey()); |
86
|
|
|
$this->assertTrue($schema->getTable('posts')->hasColumn('id')); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
View Code Duplication |
public function testUuid() |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$schema = new Schema(); |
92
|
|
|
$fluid = new TdbmFluidSchema($schema); |
93
|
|
|
|
94
|
|
|
$posts = $fluid->table('posts'); |
95
|
|
|
|
96
|
|
|
$posts->uuid(); |
97
|
|
|
|
98
|
|
|
$this->assertTrue($schema->getTable('posts')->hasPrimaryKey()); |
99
|
|
|
$this->assertTrue($schema->getTable('posts')->hasColumn('uuid')); |
100
|
|
|
$this->assertSame("\n@UUID(\"v4\")", $schema->getTable('posts')->getColumn('uuid')->getComment()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testUuidBadType() |
104
|
|
|
{ |
105
|
|
|
$schema = new Schema(); |
106
|
|
|
$fluid = new TdbmFluidSchema($schema); |
107
|
|
|
|
108
|
|
|
$posts = $fluid->table('posts'); |
109
|
|
|
|
110
|
|
|
$this->expectException(FluidSchemaException::class); |
111
|
|
|
$posts->uuid('v2'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testCustomBeanName() |
115
|
|
|
{ |
116
|
|
|
$schema = new Schema(); |
117
|
|
|
$fluid = new TdbmFluidSchema($schema); |
118
|
|
|
|
119
|
|
|
$posts = $fluid->table('posts'); |
120
|
|
|
|
121
|
|
|
$posts->customBeanName('Article'); |
122
|
|
|
|
123
|
|
|
$this->assertSame("\n@Bean(name = \"Article\")", $schema->getTable('posts')->getOptions()['comment']); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
View Code Duplication |
public function testImplementsInterface() |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$schema = new Schema(); |
129
|
|
|
$fluid = new TdbmFluidSchema($schema); |
130
|
|
|
|
131
|
|
|
$posts = $fluid->table('posts'); |
132
|
|
|
|
133
|
|
|
$posts->implementsInterface('Foo\\Bar'); |
134
|
|
|
$posts->implementsInterface('Foo\\Bar2'); |
135
|
|
|
|
136
|
|
|
$this->assertSame("\n@AddInterface(name = \"Foo\\Bar\")\n@AddInterface(name = \"Foo\\Bar2\")", $schema->getTable('posts')->getOptions()['comment']); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testImplementsInterfaceOnDao() |
140
|
|
|
{ |
141
|
|
|
$schema = new Schema(); |
142
|
|
|
$fluid = new TdbmFluidSchema($schema); |
143
|
|
|
|
144
|
|
|
$posts = $fluid->table('posts'); |
145
|
|
|
|
146
|
|
|
$posts->implementsInterfaceOnDao('Foo\\Bar'); |
147
|
|
|
|
148
|
|
|
$this->assertSame("\n@AddInterfaceOnDao(name = \"Foo\\Bar\")", $schema->getTable('posts')->getOptions()['comment']); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
View Code Duplication |
public function testUseTrait() |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
$schema = new Schema(); |
154
|
|
|
$fluid = new TdbmFluidSchema($schema); |
155
|
|
|
|
156
|
|
|
$posts = $fluid->table('posts'); |
157
|
|
|
|
158
|
|
|
$posts->useTrait('Foo\\Bar'); |
159
|
|
|
$posts->useTrait('Foo\\Bar2'); |
160
|
|
|
|
161
|
|
|
$this->assertSame("\n@AddTrait(name = \"Foo\\Bar\", modifiers = {})\n@AddTrait(name = \"Foo\\Bar2\", modifiers = {})", $schema->getTable('posts')->getOptions()['comment']); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function testUseTraitOnDao() |
165
|
|
|
{ |
166
|
|
|
$schema = new Schema(); |
167
|
|
|
$fluid = new TdbmFluidSchema($schema); |
168
|
|
|
|
169
|
|
|
$posts = $fluid->table('posts'); |
170
|
|
|
|
171
|
|
|
$posts->useTraitOnDao('Foo\\Bar'); |
172
|
|
|
|
173
|
|
|
$this->assertSame("\n@AddTraitOnDao(name = \"Foo\\Bar\", modifiers = {})", $schema->getTable('posts')->getOptions()['comment']); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
View Code Duplication |
public function testTimestamps() |
|
|
|
|
177
|
|
|
{ |
178
|
|
|
if (defined('Doctrine\\DBAL\\Types\\Type::DATE_IMMUTABLE')) { |
179
|
|
|
$schema = new Schema(); |
180
|
|
|
$fluid = new TdbmFluidSchema($schema); |
181
|
|
|
|
182
|
|
|
$posts = $fluid->table('posts'); |
183
|
|
|
|
184
|
|
|
$posts->timestamps(); |
185
|
|
|
|
186
|
|
|
$this->assertTrue($schema->getTable('posts')->hasColumn('created_at')); |
187
|
|
|
$this->assertTrue($schema->getTable('posts')->hasColumn('updated_at')); |
188
|
|
|
} else { |
189
|
|
|
$this->markTestSkipped("Only available from Doctrine DBAL 2.6"); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
View Code Duplication |
public function testInherits() |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
$schema = new Schema(); |
196
|
|
|
$fluid = new TdbmFluidSchema($schema); |
197
|
|
|
|
198
|
|
|
$contacts = $fluid->table('contacts'); |
199
|
|
|
$contacts->id(); |
200
|
|
|
|
201
|
|
|
$fluid->table('users')->extends('contacts'); |
202
|
|
|
|
203
|
|
|
$dbalColumn = $schema->getTable('users')->getColumn('id'); |
204
|
|
|
|
205
|
|
|
$this->assertSame(Type::getType(Type::INTEGER), $dbalColumn->getType()); |
|
|
|
|
206
|
|
|
$fks = $schema->getTable('users')->getForeignKeys(); |
207
|
|
|
$this->assertCount(1, $fks); |
|
|
|
|
208
|
|
|
$fk = array_pop($fks); |
209
|
|
|
$this->assertSame('users', $fk->getLocalTableName()); |
210
|
|
|
$this->assertSame('contacts', $fk->getForeignTableName()); |
211
|
|
|
$this->assertSame(['id'], $fk->getLocalColumns()); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function testGetDbalTable() |
215
|
|
|
{ |
216
|
|
|
$schema = new Schema(); |
217
|
|
|
$fluid = new TdbmFluidSchema($schema); |
218
|
|
|
|
219
|
|
|
$contacts = $fluid->table('contacts'); |
220
|
|
|
$this->assertSame('contacts', $contacts->getDbalTable()->getName()); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.