|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
Copyright (C) 2006-2014 David Négrier - THE CODING MACHINE |
|
5
|
|
|
|
|
6
|
|
|
This program is free software; you can redistribute it and/or modify |
|
7
|
|
|
it under the terms of the GNU General Public License as published by |
|
8
|
|
|
the Free Software Foundation; either version 2 of the License, or |
|
9
|
|
|
(at your option) any later version. |
|
10
|
|
|
|
|
11
|
|
|
This program is distributed in the hope that it will be useful, |
|
12
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
GNU General Public License for more details. |
|
15
|
|
|
|
|
16
|
|
|
You should have received a copy of the GNU General Public License |
|
17
|
|
|
along with this program; if not, write to the Free Software |
|
18
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TheCodingMachine\TDBM; |
|
22
|
|
|
|
|
23
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
|
24
|
|
|
use Doctrine\DBAL\Connection; |
|
25
|
|
|
use Doctrine\DBAL\DriverManager; |
|
26
|
|
|
use TheCodingMachine\FluidSchema\FluidSchema; |
|
27
|
|
|
use TheCodingMachine\TDBM\Utils\DefaultNamingStrategy; |
|
28
|
|
|
use TheCodingMachine\TDBM\Utils\PathFinder\PathFinder; |
|
29
|
|
|
|
|
30
|
|
|
abstract class TDBMAbstractServiceTest extends \PHPUnit_Framework_TestCase |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var Connection |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $dbConnection; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var TDBMService |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $tdbmService; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var DummyGeneratorListener |
|
44
|
|
|
*/ |
|
45
|
|
|
private $dummyGeneratorListener; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var ConfigurationInterface |
|
49
|
|
|
*/ |
|
50
|
|
|
private $configuration; |
|
51
|
|
|
|
|
52
|
|
|
public static function setUpBeforeClass() |
|
53
|
|
|
{ |
|
54
|
|
|
$config = new \Doctrine\DBAL\Configuration(); |
|
55
|
|
|
|
|
56
|
|
|
$connectionParams = array( |
|
57
|
|
|
'user' => $GLOBALS['db_username'], |
|
58
|
|
|
'password' => $GLOBALS['db_password'], |
|
59
|
|
|
'host' => $GLOBALS['db_host'], |
|
60
|
|
|
'port' => $GLOBALS['db_port'], |
|
61
|
|
|
'driver' => $GLOBALS['db_driver'], |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
$adminConn = DriverManager::getConnection($connectionParams, $config); |
|
65
|
|
|
$adminConn->getSchemaManager()->dropAndCreateDatabase($GLOBALS['db_name']); |
|
66
|
|
|
|
|
67
|
|
|
$connectionParams['dbname'] = $GLOBALS['db_name']; |
|
68
|
|
|
|
|
69
|
|
|
$dbConnection = DriverManager::getConnection($connectionParams, $config); |
|
70
|
|
|
|
|
71
|
|
|
self::initSchema($dbConnection); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
protected function setUp() |
|
75
|
|
|
{ |
|
76
|
|
|
$this->tdbmService = new TDBMService($this->getConfiguration()); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function getDummyGeneratorListener() : DummyGeneratorListener |
|
80
|
|
|
{ |
|
81
|
|
|
if ($this->dummyGeneratorListener === null) { |
|
82
|
|
|
$this->dummyGeneratorListener = new DummyGeneratorListener(); |
|
83
|
|
|
} |
|
84
|
|
|
return $this->dummyGeneratorListener; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function getConfiguration() : ConfigurationInterface |
|
88
|
|
|
{ |
|
89
|
|
|
if ($this->configuration === null) { |
|
90
|
|
|
$config = new \Doctrine\DBAL\Configuration(); |
|
91
|
|
|
|
|
92
|
|
|
$connectionParams = array( |
|
93
|
|
|
'user' => $GLOBALS['db_username'], |
|
94
|
|
|
'password' => $GLOBALS['db_password'], |
|
95
|
|
|
'host' => $GLOBALS['db_host'], |
|
96
|
|
|
'port' => $GLOBALS['db_port'], |
|
97
|
|
|
'driver' => $GLOBALS['db_driver'], |
|
98
|
|
|
'dbname' => $GLOBALS['db_name'], |
|
99
|
|
|
); |
|
100
|
|
|
|
|
101
|
|
|
$this->dbConnection = DriverManager::getConnection($connectionParams, $config); |
|
102
|
|
|
$this->configuration = new Configuration('TheCodingMachine\\TDBM\\Test\\Dao\\Bean', 'TheCodingMachine\\TDBM\\Test\\Dao', $this->dbConnection, $this->getNamingStrategy(), new ArrayCache(), null, null, [$this->getDummyGeneratorListener()]); |
|
103
|
|
|
$this->configuration->setPathFinder(new PathFinder(null, dirname(__DIR__, 4))); |
|
104
|
|
|
} |
|
105
|
|
|
return $this->configuration; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
protected function getNamingStrategy() |
|
109
|
|
|
{ |
|
110
|
|
|
$strategy = new DefaultNamingStrategy(); |
|
111
|
|
|
$strategy->setBeanPrefix(''); |
|
112
|
|
|
$strategy->setBeanSuffix('Bean'); |
|
113
|
|
|
$strategy->setBaseBeanPrefix(''); |
|
114
|
|
|
$strategy->setBaseBeanSuffix('BaseBean'); |
|
115
|
|
|
$strategy->setDaoPrefix(''); |
|
116
|
|
|
$strategy->setDaoSuffix('Dao'); |
|
117
|
|
|
$strategy->setBaseDaoPrefix(''); |
|
118
|
|
|
$strategy->setBaseDaoSuffix('BaseDao'); |
|
119
|
|
|
|
|
120
|
|
|
return $strategy; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
private static function initSchema(Connection $connection): void |
|
124
|
|
|
{ |
|
125
|
|
|
$fromSchema = $connection->getSchemaManager()->createSchema(); |
|
126
|
|
|
$toSchema = clone $fromSchema; |
|
127
|
|
|
|
|
128
|
|
|
$db = new FluidSchema($toSchema); |
|
129
|
|
|
|
|
130
|
|
|
$db->table('country') |
|
131
|
|
|
->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
|
132
|
|
|
->column('label')->string(255); |
|
133
|
|
|
|
|
134
|
|
|
$db->table('person') |
|
135
|
|
|
->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
|
136
|
|
|
->column('name')->string(255); |
|
137
|
|
|
|
|
138
|
|
|
$toSchema->getTable('person') |
|
139
|
|
|
->addColumn( |
|
140
|
|
|
'created_at', |
|
141
|
|
|
'datetime', |
|
142
|
|
|
['columnDefinition' => 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP'] |
|
143
|
|
|
); |
|
144
|
|
|
|
|
145
|
|
|
$db->table('person') |
|
146
|
|
|
->column('modified_at')->datetime()->null() |
|
147
|
|
|
->column('order')->integer()->null(); |
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
$db->table('contact') |
|
151
|
|
|
->extends('person') |
|
152
|
|
|
->column('email')->string(255) |
|
153
|
|
|
->column('manager_id')->references('contact')->null(); |
|
154
|
|
|
|
|
155
|
|
|
$db->table('users') |
|
156
|
|
|
->extends('contact') |
|
157
|
|
|
->column('login')->string(255) |
|
158
|
|
|
->column('password')->string(255)->null() |
|
159
|
|
|
->column('status')->string(10)->null()->default(null) |
|
160
|
|
|
->column('country_id')->references('country'); |
|
161
|
|
|
|
|
162
|
|
|
$db->table('rights') |
|
163
|
|
|
->column('label')->string(255)->primaryKey()->comment('Non autoincrementable primary key'); |
|
164
|
|
|
|
|
165
|
|
|
$db->table('roles') |
|
166
|
|
|
->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
|
167
|
|
|
->column('name')->string(255) |
|
168
|
|
|
->column('created_at')->date()->null() |
|
169
|
|
|
->column('status')->boolean()->null()->default(1); |
|
170
|
|
|
|
|
171
|
|
|
$db->table('roles_rights') |
|
172
|
|
|
->column('role_id')->references('roles') |
|
173
|
|
|
->column('right_label')->references('rights')->then() |
|
174
|
|
|
->primaryKey(['role_id', 'right_label']); |
|
175
|
|
|
|
|
176
|
|
|
$db->table('users_roles') |
|
177
|
|
|
->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
|
178
|
|
|
->column('user_id')->references('users') |
|
179
|
|
|
->column('role_id')->references('roles'); |
|
180
|
|
|
|
|
181
|
|
|
$db->table('all_nullable') |
|
182
|
|
|
->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
|
183
|
|
|
->column('label')->string(255)->null() |
|
184
|
|
|
->column('country_id')->references('country')->null(); |
|
185
|
|
|
|
|
186
|
|
|
$db->table('animal') |
|
187
|
|
|
->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
|
188
|
|
|
->column('name')->string(45)->index() |
|
189
|
|
|
->column('order')->integer()->null(); |
|
190
|
|
|
|
|
191
|
|
|
$db->table('dog') |
|
192
|
|
|
->extends('animal') |
|
193
|
|
|
->column('race')->string(45)->null(); |
|
194
|
|
|
|
|
195
|
|
|
$db->table('cat') |
|
196
|
|
|
->extends('animal') |
|
197
|
|
|
->column('cuteness_level')->integer()->null(); |
|
198
|
|
|
|
|
199
|
|
|
$db->table('boats') |
|
200
|
|
|
->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
|
201
|
|
|
->column('name')->string(255) |
|
202
|
|
|
->column('anchorage_country')->references('country')->notNull(); |
|
203
|
|
|
|
|
204
|
|
|
$db->table('sailed_countries') |
|
205
|
|
|
->column('boat_id')->references('boats') |
|
206
|
|
|
->column('country_id')->references('country') |
|
207
|
|
|
->then()->primaryKey(['boat_id', 'country_id']); |
|
208
|
|
|
|
|
209
|
|
|
$db->table('category') |
|
210
|
|
|
->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
|
211
|
|
|
->column('label')->string(255) |
|
212
|
|
|
->column('parent_id')->references('category')->null(); |
|
213
|
|
|
|
|
214
|
|
|
$db->table('article') |
|
215
|
|
|
->column('id')->string(36)->primaryKey()->comment('@UUID') |
|
216
|
|
|
->column('content')->string(255); |
|
217
|
|
|
|
|
218
|
|
|
$db->table('article2') |
|
219
|
|
|
->column('id')->string(36)->primaryKey()->comment('@UUID v4') |
|
220
|
|
|
->column('content')->string(255); |
|
221
|
|
|
|
|
222
|
|
|
$toSchema->getTable('users') |
|
223
|
|
|
->addUniqueIndex(['login'], 'users_login_idx') |
|
224
|
|
|
->addUniqueIndex(['login'], 'users_login_idx_2') // We create the same index twice |
|
225
|
|
|
->addIndex(['status', 'country_id'], 'users_status_country_idx'); |
|
226
|
|
|
|
|
227
|
|
|
$sqlStmts = $toSchema->getMigrateFromSql($fromSchema, $connection->getDatabasePlatform()); |
|
228
|
|
|
|
|
229
|
|
|
foreach ($sqlStmts as $sqlStmt) { |
|
230
|
|
|
$connection->exec($sqlStmt); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
$connection->insert('country', [ |
|
234
|
|
|
'label' => 'France', |
|
235
|
|
|
]); |
|
236
|
|
|
$connection->insert('country', [ |
|
237
|
|
|
'label' => 'UK', |
|
238
|
|
|
]); |
|
239
|
|
|
$connection->insert('country', [ |
|
240
|
|
|
'label' => 'Jamaica', |
|
241
|
|
|
]); |
|
242
|
|
|
|
|
243
|
|
|
$connection->insert('person', [ |
|
244
|
|
|
'name' => 'John Smith', |
|
245
|
|
|
'created_at' => '2015-10-24 11:57:13', |
|
246
|
|
|
]); |
|
247
|
|
|
$connection->insert('person', [ |
|
248
|
|
|
'name' => 'Jean Dupont', |
|
249
|
|
|
'created_at' => '2015-10-24 11:57:13', |
|
250
|
|
|
]); |
|
251
|
|
|
$connection->insert('person', [ |
|
252
|
|
|
'name' => 'Robert Marley', |
|
253
|
|
|
'created_at' => '2015-10-24 11:57:13', |
|
254
|
|
|
]); |
|
255
|
|
|
$connection->insert('person', [ |
|
256
|
|
|
'name' => 'Bill Shakespeare', |
|
257
|
|
|
'created_at' => '2015-10-24 11:57:13', |
|
258
|
|
|
]); |
|
259
|
|
|
|
|
260
|
|
|
$connection->insert('contact', [ |
|
261
|
|
|
'id' => 1, |
|
262
|
|
|
'email' => '[email protected]', |
|
263
|
|
|
'manager_id' => null, |
|
264
|
|
|
]); |
|
265
|
|
|
$connection->insert('contact', [ |
|
266
|
|
|
'id' => 2, |
|
267
|
|
|
'email' => '[email protected]', |
|
268
|
|
|
'manager_id' => null, |
|
269
|
|
|
]); |
|
270
|
|
|
$connection->insert('contact', [ |
|
271
|
|
|
'id' => 3, |
|
272
|
|
|
'email' => '[email protected]', |
|
273
|
|
|
'manager_id' => null, |
|
274
|
|
|
]); |
|
275
|
|
|
$connection->insert('contact', [ |
|
276
|
|
|
'id' => 4, |
|
277
|
|
|
'email' => '[email protected]', |
|
278
|
|
|
'manager_id' => 1, |
|
279
|
|
|
]); |
|
280
|
|
|
|
|
281
|
|
|
$connection->insert('rights', [ |
|
282
|
|
|
'label' => 'CAN_SING', |
|
283
|
|
|
]); |
|
284
|
|
|
$connection->insert('rights', [ |
|
285
|
|
|
'label' => 'CAN_WRITE', |
|
286
|
|
|
]); |
|
287
|
|
|
|
|
288
|
|
|
$connection->insert('roles', [ |
|
289
|
|
|
'name' => 'Admins', |
|
290
|
|
|
'created_at' => '2015-10-24' |
|
291
|
|
|
]); |
|
292
|
|
|
$connection->insert('roles', [ |
|
293
|
|
|
'name' => 'Writers', |
|
294
|
|
|
'created_at' => '2015-10-24' |
|
295
|
|
|
]); |
|
296
|
|
|
$connection->insert('roles', [ |
|
297
|
|
|
'name' => 'Singers', |
|
298
|
|
|
'created_at' => '2015-10-24' |
|
299
|
|
|
]); |
|
300
|
|
|
|
|
301
|
|
|
$connection->insert('roles_rights', [ |
|
302
|
|
|
'role_id' => 1, |
|
303
|
|
|
'right_label' => 'CAN_SING' |
|
304
|
|
|
]); |
|
305
|
|
|
$connection->insert('roles_rights', [ |
|
306
|
|
|
'role_id' => 3, |
|
307
|
|
|
'right_label' => 'CAN_SING' |
|
308
|
|
|
]); |
|
309
|
|
|
$connection->insert('roles_rights', [ |
|
310
|
|
|
'role_id' => 1, |
|
311
|
|
|
'right_label' => 'CAN_WRITE' |
|
312
|
|
|
]); |
|
313
|
|
|
$connection->insert('roles_rights', [ |
|
314
|
|
|
'role_id' => 2, |
|
315
|
|
|
'right_label' => 'CAN_WRITE' |
|
316
|
|
|
]); |
|
317
|
|
|
|
|
318
|
|
|
$connection->insert('users', [ |
|
319
|
|
|
'id' => 1, |
|
320
|
|
|
'login' => 'john.smith', |
|
321
|
|
|
'password' => null, |
|
322
|
|
|
'status' => 'on', |
|
323
|
|
|
'country_id' => 2 |
|
324
|
|
|
]); |
|
325
|
|
|
$connection->insert('users', [ |
|
326
|
|
|
'id' => 2, |
|
327
|
|
|
'login' => 'jean.dupont', |
|
328
|
|
|
'password' => null, |
|
329
|
|
|
'status' => 'on', |
|
330
|
|
|
'country_id' => 1 |
|
331
|
|
|
]); |
|
332
|
|
|
$connection->insert('users', [ |
|
333
|
|
|
'id' => 3, |
|
334
|
|
|
'login' => 'robert.marley', |
|
335
|
|
|
'password' => null, |
|
336
|
|
|
'status' => 'off', |
|
337
|
|
|
'country_id' => 3 |
|
338
|
|
|
]); |
|
339
|
|
|
$connection->insert('users', [ |
|
340
|
|
|
'id' => 4, |
|
341
|
|
|
'login' => 'bill.shakespeare', |
|
342
|
|
|
'password' => null, |
|
343
|
|
|
'status' => 'off', |
|
344
|
|
|
'country_id' => 2 |
|
345
|
|
|
]); |
|
346
|
|
|
|
|
347
|
|
|
$connection->insert('users_roles', [ |
|
348
|
|
|
'user_id' => 1, |
|
349
|
|
|
'role_id' => 1, |
|
350
|
|
|
]); |
|
351
|
|
|
$connection->insert('users_roles', [ |
|
352
|
|
|
'user_id' => 2, |
|
353
|
|
|
'role_id' => 1, |
|
354
|
|
|
]); |
|
355
|
|
|
$connection->insert('users_roles', [ |
|
356
|
|
|
'user_id' => 3, |
|
357
|
|
|
'role_id' => 3, |
|
358
|
|
|
]); |
|
359
|
|
|
$connection->insert('users_roles', [ |
|
360
|
|
|
'user_id' => 4, |
|
361
|
|
|
'role_id' => 2, |
|
362
|
|
|
]); |
|
363
|
|
|
$connection->insert('users_roles', [ |
|
364
|
|
|
'user_id' => 3, |
|
365
|
|
|
'role_id' => 2, |
|
366
|
|
|
]); |
|
367
|
|
|
} |
|
368
|
|
|
} |
|
369
|
|
|
|