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\Common\EventManager; |
25
|
|
|
use Doctrine\DBAL\Connection; |
26
|
|
|
use Doctrine\DBAL\DriverManager; |
27
|
|
|
use Doctrine\DBAL\Event\Listeners\OracleSessionInit; |
28
|
|
|
use Doctrine\DBAL\Platforms\MySqlPlatform; |
29
|
|
|
use Doctrine\DBAL\Platforms\OraclePlatform; |
30
|
|
|
use TheCodingMachine\FluidSchema\FluidSchema; |
31
|
|
|
use TheCodingMachine\TDBM\Utils\DefaultNamingStrategy; |
32
|
|
|
use TheCodingMachine\TDBM\Utils\PathFinder\PathFinder; |
33
|
|
|
|
34
|
|
|
abstract class TDBMAbstractServiceTest extends \PHPUnit_Framework_TestCase |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var Connection |
38
|
|
|
*/ |
39
|
|
|
protected static $dbConnection; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var TDBMService |
43
|
|
|
*/ |
44
|
|
|
protected $tdbmService; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var DummyGeneratorListener |
48
|
|
|
*/ |
49
|
|
|
private $dummyGeneratorListener; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var ConfigurationInterface |
53
|
|
|
*/ |
54
|
|
|
private $configuration; |
55
|
|
|
|
56
|
|
|
public static function setUpBeforeClass() |
57
|
|
|
{ |
58
|
|
|
self::resetConnection(); |
59
|
|
|
|
60
|
|
|
$config = new \Doctrine\DBAL\Configuration(); |
61
|
|
|
|
62
|
|
|
$dbDriver = $GLOBALS['db_driver']; |
63
|
|
|
|
64
|
|
|
if ($dbDriver === 'pdo_sqlite') { |
65
|
|
|
$dbConnection = self::getConnection(); |
66
|
|
|
$dbConnection->exec('PRAGMA foreign_keys = ON;'); |
67
|
|
|
} elseif ($dbDriver === 'oci8') { |
68
|
|
|
$connectionParams = array( |
69
|
|
|
'servicename' => 'XE', |
70
|
|
|
'user' => $GLOBALS['db_admin_username'], |
71
|
|
|
'password' => $GLOBALS['db_admin_password'], |
72
|
|
|
'host' => $GLOBALS['db_host'], |
73
|
|
|
'port' => $GLOBALS['db_port'], |
74
|
|
|
'driver' => $GLOBALS['db_driver'], |
75
|
|
|
'dbname' => $GLOBALS['db_admin_username'], |
76
|
|
|
'charset' => 'utf-8', |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$adminConn = DriverManager::getConnection($connectionParams, $config); |
80
|
|
|
$adminConn->getSchemaManager()->dropAndCreateDatabase($GLOBALS['db_name']); |
81
|
|
|
|
82
|
|
|
$dbConnection = self::getConnection(); |
83
|
|
|
} else { |
84
|
|
|
$connectionParams = array( |
85
|
|
|
'user' => $GLOBALS['db_username'], |
86
|
|
|
'password' => $GLOBALS['db_password'], |
87
|
|
|
'host' => $GLOBALS['db_host'], |
88
|
|
|
'port' => $GLOBALS['db_port'], |
89
|
|
|
'driver' => $dbDriver, |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$adminConn = DriverManager::getConnection($connectionParams, $config); |
93
|
|
|
$adminConn->getSchemaManager()->dropAndCreateDatabase($GLOBALS['db_name']); |
94
|
|
|
|
95
|
|
|
$connectionParams['dbname'] = $GLOBALS['db_name']; |
96
|
|
|
|
97
|
|
|
$dbConnection = DriverManager::getConnection($connectionParams, $config); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
self::initSchema($dbConnection); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private static function resetConnection(): void |
105
|
|
|
{ |
106
|
|
|
self::$dbConnection = null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected static function getConnection(): Connection |
110
|
|
|
{ |
111
|
|
|
if (self::$dbConnection === null) { |
112
|
|
|
$config = new \Doctrine\DBAL\Configuration(); |
113
|
|
|
|
114
|
|
|
$dbDriver = $GLOBALS['db_driver']; |
115
|
|
|
|
116
|
|
|
if ($dbDriver === 'pdo_sqlite') { |
117
|
|
|
$connectionParams = array( |
118
|
|
|
'memory' => true, |
119
|
|
|
'driver' => 'pdo_sqlite', |
120
|
|
|
); |
121
|
|
|
self::$dbConnection = DriverManager::getConnection($connectionParams, $config); |
122
|
|
|
} elseif ($dbDriver === 'oci8') { |
123
|
|
|
$evm = new EventManager(); |
124
|
|
|
$evm->addEventSubscriber(new OracleSessionInit(array( |
125
|
|
|
'NLS_TIME_FORMAT' => 'HH24:MI:SS', |
126
|
|
|
'NLS_DATE_FORMAT' => 'YYYY-MM-DD HH24:MI:SS', |
127
|
|
|
'NLS_TIMESTAMP_FORMAT' => 'YYYY-MM-DD HH24:MI:SS', |
128
|
|
|
))); |
129
|
|
|
|
130
|
|
|
$connectionParams = array( |
131
|
|
|
'servicename' => 'XE', |
132
|
|
|
'user' => $GLOBALS['db_username'], |
133
|
|
|
'password' => $GLOBALS['db_password'], |
134
|
|
|
'host' => $GLOBALS['db_host'], |
135
|
|
|
'port' => $GLOBALS['db_port'], |
136
|
|
|
'driver' => $GLOBALS['db_driver'], |
137
|
|
|
'dbname' => $GLOBALS['db_name'], |
138
|
|
|
'charset' => 'utf-8', |
139
|
|
|
); |
140
|
|
|
self::$dbConnection = DriverManager::getConnection($connectionParams, $config, $evm); |
141
|
|
|
self::$dbConnection->setAutoCommit(true); |
142
|
|
|
|
143
|
|
|
} else { |
144
|
|
|
$connectionParams = array( |
145
|
|
|
'user' => $GLOBALS['db_username'], |
146
|
|
|
'password' => $GLOBALS['db_password'], |
147
|
|
|
'host' => $GLOBALS['db_host'], |
148
|
|
|
'port' => $GLOBALS['db_port'], |
149
|
|
|
'driver' => $GLOBALS['db_driver'], |
150
|
|
|
'dbname' => $GLOBALS['db_name'], |
151
|
|
|
); |
152
|
|
|
self::$dbConnection = DriverManager::getConnection($connectionParams, $config); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
return self::$dbConnection; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
protected function onlyMySql() |
160
|
|
|
{ |
161
|
|
|
if (!self::getConnection()->getDatabasePlatform() instanceof MySqlPlatform) { |
162
|
|
|
$this->markTestSkipped('MySQL specific test'); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
protected function setUp() |
167
|
|
|
{ |
168
|
|
|
$this->tdbmService = new TDBMService($this->getConfiguration()); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
protected function getDummyGeneratorListener() : DummyGeneratorListener |
172
|
|
|
{ |
173
|
|
|
if ($this->dummyGeneratorListener === null) { |
174
|
|
|
$this->dummyGeneratorListener = new DummyGeneratorListener(); |
175
|
|
|
} |
176
|
|
|
return $this->dummyGeneratorListener; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
protected function getConfiguration() : ConfigurationInterface |
180
|
|
|
{ |
181
|
|
|
if ($this->configuration === null) { |
182
|
|
|
|
183
|
|
|
$this->configuration = new Configuration('TheCodingMachine\\TDBM\\Test\\Dao\\Bean', 'TheCodingMachine\\TDBM\\Test\\Dao', self::getConnection(), $this->getNamingStrategy(), new ArrayCache(), null, null, [$this->getDummyGeneratorListener()]); |
184
|
|
|
$this->configuration->setPathFinder(new PathFinder(null, dirname(__DIR__, 4))); |
185
|
|
|
} |
186
|
|
|
return $this->configuration; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
protected function getNamingStrategy() |
190
|
|
|
{ |
191
|
|
|
$strategy = new DefaultNamingStrategy(); |
192
|
|
|
$strategy->setBeanPrefix(''); |
193
|
|
|
$strategy->setBeanSuffix('Bean'); |
194
|
|
|
$strategy->setBaseBeanPrefix(''); |
195
|
|
|
$strategy->setBaseBeanSuffix('BaseBean'); |
196
|
|
|
$strategy->setDaoPrefix(''); |
197
|
|
|
$strategy->setDaoSuffix('Dao'); |
198
|
|
|
$strategy->setBaseDaoPrefix(''); |
199
|
|
|
$strategy->setBaseDaoSuffix('BaseDao'); |
200
|
|
|
|
201
|
|
|
return $strategy; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
private static function initSchema(Connection $connection): void |
205
|
|
|
{ |
206
|
|
|
$fromSchema = $connection->getSchemaManager()->createSchema(); |
207
|
|
|
$toSchema = clone $fromSchema; |
208
|
|
|
|
209
|
|
|
$db = new FluidSchema($toSchema); |
210
|
|
|
|
211
|
|
|
$db->table('country') |
212
|
|
|
->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
213
|
|
|
->column('label')->string(255); |
214
|
|
|
|
215
|
|
|
$db->table('person') |
216
|
|
|
->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
217
|
|
|
->column('name')->string(255); |
218
|
|
|
|
219
|
|
|
if ($connection->getDatabasePlatform() instanceof OraclePlatform) { |
220
|
|
|
$toSchema->getTable('person') |
221
|
|
|
->addColumn( |
222
|
|
|
'created_at', |
223
|
|
|
'datetime', |
224
|
|
|
['columnDefinition' => 'TIMESTAMP(0) DEFAULT SYSDATE NOT NULL'] |
225
|
|
|
); |
226
|
|
|
} else { |
227
|
|
|
$toSchema->getTable('person') |
228
|
|
|
->addColumn( |
229
|
|
|
'created_at', |
230
|
|
|
'datetime', |
231
|
|
|
['columnDefinition' => 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP'] |
232
|
|
|
); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$db->table('person') |
236
|
|
|
->column('modified_at')->datetime()->null() |
237
|
|
|
->column('order')->integer()->null(); |
238
|
|
|
|
239
|
|
|
|
240
|
|
|
$db->table('contact') |
241
|
|
|
->extends('person') |
242
|
|
|
->column('email')->string(255) |
243
|
|
|
->column('manager_id')->references('contact')->null(); |
244
|
|
|
|
245
|
|
|
$db->table('users') |
246
|
|
|
->extends('contact') |
247
|
|
|
->column('login')->string(255) |
248
|
|
|
->column('password')->string(255)->null() |
249
|
|
|
->column('status')->string(10)->null()->default(null) |
250
|
|
|
->column('country_id')->references('country'); |
251
|
|
|
|
252
|
|
|
$db->table('rights') |
253
|
|
|
->column('label')->string(255)->primaryKey()->comment('Non autoincrementable primary key'); |
254
|
|
|
|
255
|
|
|
$db->table('roles') |
256
|
|
|
->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
257
|
|
|
->column('name')->string(255) |
258
|
|
|
->column('created_at')->date()->null() |
259
|
|
|
->column('status')->boolean()->null()->default(1); |
260
|
|
|
|
261
|
|
|
$db->table('roles_rights') |
262
|
|
|
->column('role_id')->references('roles') |
263
|
|
|
->column('right_label')->references('rights')->then() |
264
|
|
|
->primaryKey(['role_id', 'right_label']); |
265
|
|
|
|
266
|
|
|
$db->table('users_roles') |
267
|
|
|
->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
268
|
|
|
->column('user_id')->references('users') |
269
|
|
|
->column('role_id')->references('roles'); |
270
|
|
|
|
271
|
|
|
$db->table('all_nullable') |
272
|
|
|
->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
273
|
|
|
->column('label')->string(255)->null() |
274
|
|
|
->column('country_id')->references('country')->null(); |
275
|
|
|
|
276
|
|
|
$db->table('animal') |
277
|
|
|
->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
278
|
|
|
->column('name')->string(45)->index() |
279
|
|
|
->column('order')->integer()->null(); |
280
|
|
|
|
281
|
|
|
$db->table('dog') |
282
|
|
|
->extends('animal') |
283
|
|
|
->column('race')->string(45)->null(); |
284
|
|
|
|
285
|
|
|
$db->table('cat') |
286
|
|
|
->extends('animal') |
287
|
|
|
->column('cuteness_level')->integer()->null(); |
288
|
|
|
|
289
|
|
|
$db->table('boats') |
290
|
|
|
->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
291
|
|
|
->column('name')->string(255) |
292
|
|
|
->column('anchorage_country')->references('country')->notNull(); |
293
|
|
|
|
294
|
|
|
$db->table('sailed_countries') |
295
|
|
|
->column('boat_id')->references('boats') |
296
|
|
|
->column('country_id')->references('country') |
297
|
|
|
->then()->primaryKey(['boat_id', 'country_id']); |
298
|
|
|
|
299
|
|
|
$db->table('category') |
300
|
|
|
->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
301
|
|
|
->column('label')->string(255) |
302
|
|
|
->column('parent_id')->references('category')->null(); |
303
|
|
|
|
304
|
|
|
$db->table('article') |
305
|
|
|
->column('id')->string(36)->primaryKey()->comment('@UUID') |
306
|
|
|
->column('content')->string(255); |
307
|
|
|
|
308
|
|
|
$db->table('article2') |
309
|
|
|
->column('id')->string(36)->primaryKey()->comment('@UUID v4') |
310
|
|
|
->column('content')->string(255); |
311
|
|
|
|
312
|
|
|
$toSchema->getTable('users') |
313
|
|
|
->addUniqueIndex(['login'], 'users_login_idx') |
314
|
|
|
->addIndex(['status', 'country_id'], 'users_status_country_idx'); |
315
|
|
|
|
316
|
|
|
// We create the same index twice |
317
|
|
|
// except for Oracle that won't let us create twice the same index. |
318
|
|
|
if (!$connection->getDatabasePlatform() instanceof OraclePlatform) { |
319
|
|
|
$toSchema->getTable('users') |
320
|
|
|
->addUniqueIndex(['login'], 'users_login_idx_2'); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
|
324
|
|
|
$sqlStmts = $toSchema->getMigrateFromSql($fromSchema, $connection->getDatabasePlatform()); |
325
|
|
|
|
326
|
|
|
foreach ($sqlStmts as $sqlStmt) { |
327
|
|
|
$connection->exec($sqlStmt); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
$connection->insert('country', [ |
331
|
|
|
'label' => 'France', |
332
|
|
|
]); |
333
|
|
|
$connection->insert('country', [ |
334
|
|
|
'label' => 'UK', |
335
|
|
|
]); |
336
|
|
|
$connection->insert('country', [ |
337
|
|
|
'label' => 'Jamaica', |
338
|
|
|
]); |
339
|
|
|
|
340
|
|
|
$connection->insert('person', [ |
341
|
|
|
'name' => 'John Smith', |
342
|
|
|
'created_at' => '2015-10-24 11:57:13', |
343
|
|
|
]); |
344
|
|
|
$connection->insert('person', [ |
345
|
|
|
'name' => 'Jean Dupont', |
346
|
|
|
'created_at' => '2015-10-24 11:57:13', |
347
|
|
|
]); |
348
|
|
|
$connection->insert('person', [ |
349
|
|
|
'name' => 'Robert Marley', |
350
|
|
|
'created_at' => '2015-10-24 11:57:13', |
351
|
|
|
]); |
352
|
|
|
$connection->insert('person', [ |
353
|
|
|
'name' => 'Bill Shakespeare', |
354
|
|
|
'created_at' => '2015-10-24 11:57:13', |
355
|
|
|
]); |
356
|
|
|
|
357
|
|
|
$connection->insert('contact', [ |
358
|
|
|
'id' => 1, |
359
|
|
|
'email' => '[email protected]', |
360
|
|
|
'manager_id' => null, |
361
|
|
|
]); |
362
|
|
|
$connection->insert('contact', [ |
363
|
|
|
'id' => 2, |
364
|
|
|
'email' => '[email protected]', |
365
|
|
|
'manager_id' => null, |
366
|
|
|
]); |
367
|
|
|
$connection->insert('contact', [ |
368
|
|
|
'id' => 3, |
369
|
|
|
'email' => '[email protected]', |
370
|
|
|
'manager_id' => null, |
371
|
|
|
]); |
372
|
|
|
$connection->insert('contact', [ |
373
|
|
|
'id' => 4, |
374
|
|
|
'email' => '[email protected]', |
375
|
|
|
'manager_id' => 1, |
376
|
|
|
]); |
377
|
|
|
|
378
|
|
|
$connection->insert('rights', [ |
379
|
|
|
'label' => 'CAN_SING', |
380
|
|
|
]); |
381
|
|
|
$connection->insert('rights', [ |
382
|
|
|
'label' => 'CAN_WRITE', |
383
|
|
|
]); |
384
|
|
|
|
385
|
|
|
$connection->insert('roles', [ |
386
|
|
|
'name' => 'Admins', |
387
|
|
|
'created_at' => '2015-10-24' |
388
|
|
|
]); |
389
|
|
|
$connection->insert('roles', [ |
390
|
|
|
'name' => 'Writers', |
391
|
|
|
'created_at' => '2015-10-24' |
392
|
|
|
]); |
393
|
|
|
$connection->insert('roles', [ |
394
|
|
|
'name' => 'Singers', |
395
|
|
|
'created_at' => '2015-10-24' |
396
|
|
|
]); |
397
|
|
|
|
398
|
|
|
$connection->insert('roles_rights', [ |
399
|
|
|
'role_id' => 1, |
400
|
|
|
'right_label' => 'CAN_SING' |
401
|
|
|
]); |
402
|
|
|
$connection->insert('roles_rights', [ |
403
|
|
|
'role_id' => 3, |
404
|
|
|
'right_label' => 'CAN_SING' |
405
|
|
|
]); |
406
|
|
|
$connection->insert('roles_rights', [ |
407
|
|
|
'role_id' => 1, |
408
|
|
|
'right_label' => 'CAN_WRITE' |
409
|
|
|
]); |
410
|
|
|
$connection->insert('roles_rights', [ |
411
|
|
|
'role_id' => 2, |
412
|
|
|
'right_label' => 'CAN_WRITE' |
413
|
|
|
]); |
414
|
|
|
|
415
|
|
|
$connection->insert('users', [ |
416
|
|
|
'id' => 1, |
417
|
|
|
'login' => 'john.smith', |
418
|
|
|
'password' => null, |
419
|
|
|
'status' => 'on', |
420
|
|
|
'country_id' => 2 |
421
|
|
|
]); |
422
|
|
|
$connection->insert('users', [ |
423
|
|
|
'id' => 2, |
424
|
|
|
'login' => 'jean.dupont', |
425
|
|
|
'password' => null, |
426
|
|
|
'status' => 'on', |
427
|
|
|
'country_id' => 1 |
428
|
|
|
]); |
429
|
|
|
$connection->insert('users', [ |
430
|
|
|
'id' => 3, |
431
|
|
|
'login' => 'robert.marley', |
432
|
|
|
'password' => null, |
433
|
|
|
'status' => 'off', |
434
|
|
|
'country_id' => 3 |
435
|
|
|
]); |
436
|
|
|
$connection->insert('users', [ |
437
|
|
|
'id' => 4, |
438
|
|
|
'login' => 'bill.shakespeare', |
439
|
|
|
'password' => null, |
440
|
|
|
'status' => 'off', |
441
|
|
|
'country_id' => 2 |
442
|
|
|
]); |
443
|
|
|
|
444
|
|
|
$connection->insert('users_roles', [ |
445
|
|
|
'user_id' => 1, |
446
|
|
|
'role_id' => 1, |
447
|
|
|
]); |
448
|
|
|
$connection->insert('users_roles', [ |
449
|
|
|
'user_id' => 2, |
450
|
|
|
'role_id' => 1, |
451
|
|
|
]); |
452
|
|
|
$connection->insert('users_roles', [ |
453
|
|
|
'user_id' => 3, |
454
|
|
|
'role_id' => 3, |
455
|
|
|
]); |
456
|
|
|
$connection->insert('users_roles', [ |
457
|
|
|
'user_id' => 4, |
458
|
|
|
'role_id' => 2, |
459
|
|
|
]); |
460
|
|
|
$connection->insert('users_roles', [ |
461
|
|
|
'user_id' => 3, |
462
|
|
|
'role_id' => 2, |
463
|
|
|
]); |
464
|
|
|
} |
465
|
|
|
} |
466
|
|
|
|