Passed
Push — oracle_tests ( d942a0...cc00c1 )
by David
04:17 queued 01:48
created

TDBMAbstractServiceTest   B

Complexity

Total Complexity 17

Size/Duplication

Total Lines 397
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 17

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 397
rs 7.8571
c 1
b 0
f 0
wmc 17
lcom 1
cbo 17

9 Methods

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