Passed
Push — oracle_tests ( 0a4664...8d5500 )
by David
02:39
created

TDBMAbstractServiceTest::initSchema()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 245
Code Lines 180

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 180
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 245
rs 8.2857

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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