Passed
Pull Request — master (#215)
by David
03:39
created

TDBMAbstractServiceTest::isMariaDb()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 1
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 Copyright (C) 2006-2014 David Négrier - THE CODING MACHINE
6
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
11
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
*/
21
22
namespace TheCodingMachine\TDBM;
23
24
use DateTime;
25
use DateTimeImmutable;
26
use Doctrine\Common\Cache\ArrayCache;
27
use Doctrine\Common\EventManager;
28
use Doctrine\DBAL\Connection;
29
use Doctrine\DBAL\DriverManager;
30
use Doctrine\DBAL\Event\Listeners\OracleSessionInit;
31
use Doctrine\DBAL\Platforms\MySqlPlatform;
32
use Doctrine\DBAL\Platforms\OraclePlatform;
33
use Doctrine\DBAL\Types\Type;
34
use PHPUnit\Framework\TestCase;
35
use TheCodingMachine\FluidSchema\FluidSchema;
36
use TheCodingMachine\FluidSchema\TdbmFluidSchema;
37
use TheCodingMachine\TDBM\Fixtures\Interfaces\TestUserDaoInterface;
38
use TheCodingMachine\TDBM\Fixtures\Interfaces\TestUserInterface;
39
use TheCodingMachine\TDBM\Fixtures\Traits\TestOtherUserTrait;
40
use TheCodingMachine\TDBM\Fixtures\Traits\TestUserDaoTrait;
41
use TheCodingMachine\TDBM\Fixtures\Traits\TestUserTrait;
42
use TheCodingMachine\TDBM\Utils\Annotation\AnnotationParser;
43
use TheCodingMachine\TDBM\Utils\Annotation\AddInterface;
44
use TheCodingMachine\TDBM\Utils\DefaultNamingStrategy;
45
use TheCodingMachine\TDBM\Utils\PathFinder\PathFinder;
46
use function stripos;
47
48
abstract class TDBMAbstractServiceTest extends TestCase
49
{
50
    /**
51
     * @var Connection
52
     */
53
    protected static $dbConnection;
54
55
    /**
56
     * @var TDBMService
57
     */
58
    protected $tdbmService;
59
60
    /**
61
     * @var DummyGeneratorListener
62
     */
63
    private $dummyGeneratorListener;
64
65
    /**
66
     * @var ConfigurationInterface
67
     */
68
    private $configuration;
69
70
    /**
71
     * @var ArrayCache
72
     */
73
    private $cache;
74
75
    public static function setUpBeforeClass(): void
76
    {
77
        self::resetConnection();
78
79
        $dbConnection = ConnectionFactory::resetDatabase(
80
            $GLOBALS['db_driver'],
81
            $GLOBALS['db_host'] ?? null,
82
            $GLOBALS['db_port'] ?? null,
83
            $GLOBALS['db_username'] ?? null,
84
            $GLOBALS['db_admin_username'] ?? null,
85
            $GLOBALS['db_password'] ?? null,
86
            $GLOBALS['db_name'] ?? null
87
        );
88
89
        self::initSchema($dbConnection);
90
    }
91
92
    private static function resetConnection(): void
93
    {
94
        if (self::$dbConnection !== null) {
95
            self::$dbConnection->close();
96
        }
97
        self::$dbConnection = null;
98
    }
99
100
    protected static function getConnection(): Connection
101
    {
102
        if (self::$dbConnection === null) {
103
            self::$dbConnection = ConnectionFactory::createConnection(
104
                $GLOBALS['db_driver'],
105
                $GLOBALS['db_host'] ?? null,
106
                $GLOBALS['db_port'] ?? null,
107
                $GLOBALS['db_username'] ?? null,
108
                $GLOBALS['db_password'] ?? null,
109
                $GLOBALS['db_name'] ?? null
110
            );
111
        }
112
        return self::$dbConnection;
113
    }
114
115
    protected function onlyMySql()
116
    {
117
        if (!self::getConnection()->getDatabasePlatform() instanceof MySqlPlatform) {
118
            $this->markTestSkipped('MySQL specific test');
119
        }
120
    }
121
122
    protected function setUp(): void
123
    {
124
        $this->tdbmService = new TDBMService($this->getConfiguration());
125
    }
126
127
    protected function getDummyGeneratorListener() : DummyGeneratorListener
128
    {
129
        if ($this->dummyGeneratorListener === null) {
130
            $this->dummyGeneratorListener = new DummyGeneratorListener();
131
        }
132
        return $this->dummyGeneratorListener;
133
    }
134
135
    protected function getCache(): ArrayCache
136
    {
137
        if ($this->cache === null) {
138
            $this->cache = new ArrayCache();
139
        }
140
        return $this->cache;
141
    }
142
143
    protected function getConfiguration() : ConfigurationInterface
144
    {
145
        if ($this->configuration === null) {
146
            $this->configuration = new Configuration('TheCodingMachine\\TDBM\\Test\\Dao\\Bean', 'TheCodingMachine\\TDBM\\Test\\Dao', self::getConnection(), $this->getNamingStrategy(), $this->getCache(), null, null, [$this->getDummyGeneratorListener()]);
147
            $this->configuration->setPathFinder(new PathFinder(null, dirname(__DIR__, 4)));
148
        }
149
        return $this->configuration;
150
    }
151
152
    protected function getNamingStrategy()
153
    {
154
        $strategy = new DefaultNamingStrategy(AnnotationParser::buildWithDefaultAnnotations([]), self::getConnection()->getSchemaManager());
155
        $strategy->setBeanPrefix('');
156
        $strategy->setBeanSuffix('Bean');
157
        $strategy->setBaseBeanPrefix('');
158
        $strategy->setBaseBeanSuffix('BaseBean');
159
        $strategy->setDaoPrefix('');
160
        $strategy->setDaoSuffix('Dao');
161
        $strategy->setBaseDaoPrefix('');
162
        $strategy->setBaseDaoSuffix('BaseDao');
163
164
        return $strategy;
165
    }
166
167
    private static function initSchema(Connection $connection): void
168
    {
169
        $fromSchema = $connection->getSchemaManager()->createSchema();
170
        $toSchema = clone $fromSchema;
171
172
        $db = new TdbmFluidSchema($toSchema, new \TheCodingMachine\FluidSchema\DefaultNamingStrategy($connection->getDatabasePlatform()));
173
174
        $db->table('country')
175
            ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement')
176
            ->column('label')->string(255)->unique();
177
178
        $db->table('person')
179
            ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement')
180
            ->column('name')->string(255);
181
182
        if ($connection->getDatabasePlatform() instanceof OraclePlatform) {
183
            $toSchema->getTable($connection->quoteIdentifier('person'))
184
                ->addColumn(
185
                    $connection->quoteIdentifier('created_at'),
186
                    'datetime',
187
                    ['columnDefinition' => 'TIMESTAMP(0) DEFAULT SYSDATE NOT NULL']
188
                );
189
        } else {
190
            $toSchema->getTable('person')
191
                ->addColumn(
192
                    $connection->quoteIdentifier('created_at'),
193
                    'datetime',
194
                    ['columnDefinition' => 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP']
195
                );
196
        }
197
198
        $db->table('person')
199
            ->column('modified_at')->datetime()->null()->index()
200
            ->column('order')->integer()->null();
201
202
203
        $db->table('contact')
204
            ->extends('person')
205
            ->column('email')->string(255)
206
            ->column('manager_id')->references('contact')->null();
207
208
        $db->table('users')
209
            ->addAnnotation('AddTrait', ['name'=>TestUserTrait::class], false)
210
            ->addAnnotation('AddTrait', ['name'=>TestOtherUserTrait::class, 'modifiers'=>['\\'.TestOtherUserTrait::class.'::method1 insteadof \\'.TestUserTrait::class, '\\'.TestUserTrait::class.'::method1 as method1renamed']], false)
211
            ->addAnnotation('AddTraitOnDao', ['name'=>TestUserDaoTrait::class], false)
212
            ->implementsInterface(TestUserInterface::class)
213
            ->implementsInterfaceOnDao(TestUserDaoInterface::class)
214
            ->extends('contact')
215
            ->column('login')->string(255)
216
            ->column('password')->string(255)->null()
217
            ->column('status')->string(10)->null()->default(null)
218
            ->column('country_id')->references('country')
219
            // Used to test generation for a column that starts with a digit
220
            ->then()->column('3d_view')->boolean()->default(true);
221
222
        $db->table('rights')
223
            ->column('label')->string(255)->primaryKey()->comment('Non autoincrementable primary key');
224
225
        $db->table('roles')
226
            ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement')
227
            ->column('name')->string(255)
228
            ->column('created_at')->date()->null()
229
            ->column('status')->boolean()->null()->default(1);
230
231
        $db->table('roles_rights')
232
            ->column('role_id')->references('roles')
233
            ->column('right_label')->references('rights')->then()
234
            ->primaryKey(['role_id', 'right_label']);
235
236
        $db->table('users_roles')
237
            ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement')
238
            ->column('user_id')->references('users')
239
            ->column('role_id')->references('roles');
240
241
        $db->table('all_nullable')
242
            ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement')
243
            ->column('label')->string(255)->null()
244
            ->column('country_id')->references('country')->null();
245
246
        $db->table('animal')
247
            ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement')
248
            ->column('name')->string(45)->index()
249
            ->column('UPPERCASE_COLUMN')->string(45)->null()
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('panda')
261
            ->extends('animal')
262
            ->column('weight')->float()->null();
263
264
        $db->table('boats')
265
            ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement')
266
            ->column('name')->string(255)
267
            ->column('anchorage_country')->references('country')->notNull()->then()
268
            ->column('current_country')->references('country')->null()->then()
269
            ->column('length')->decimal(10, 2)->null()->then()
270
            ->unique(['anchorage_country', 'name']);
271
272
        $db->table('sailed_countries')
273
            ->column('boat_id')->references('boats')
274
            ->column('country_id')->references('country')
275
            ->then()->primaryKey(['boat_id', 'country_id']);
276
277
        $db->table('category')
278
            ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement')
279
            ->column('label')->string(255)
280
            ->column('parent_id')->references('category')->null();
281
282
        $db->table('article')
283
            ->column('id')->string(36)->primaryKey()->comment('@UUID')
284
            ->column('content')->string(255)
285
            ->column('author_id')->references('users')->null()
286
            ->column('attachment')->blob()->null();
287
288
        $db->table('article2')
289
            ->column('id')->string(36)->primaryKey()->comment('@UUID v4')
290
            ->column('content')->string(255);
291
292
        $db->table('files')
293
            ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement')
294
            ->column('file')->blob()
295
            ->column('md5')->string()->null()->comment("@ProtectedGetter\n@ProtectedSetter")
296
            ->column('article_id')->references('article')->null()->comment("@ProtectedGetter\n@ProtectedSetter\n@ProtectedOneToMany");
297
298
        $toSchema->getTable('users')
299
            ->addUniqueIndex([$connection->quoteIdentifier('login')], 'users_login_idx')
300
            ->addIndex([$connection->quoteIdentifier('status'), $connection->quoteIdentifier('country_id')], 'users_status_country_idx');
301
302
        // We create the same index twice
303
        // except for Oracle that won't let us create twice the same index.
304
        if (!$connection->getDatabasePlatform() instanceof OraclePlatform) {
305
            $toSchema->getTable('users')
306
                ->addUniqueIndex([$connection->quoteIdentifier('login')], 'users_login_idx_2');
307
        }
308
309
        // A table with a foreign key that references a non primary key.
310
        $db->table('ref_no_prim_key')
311
            ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement')
312
            ->column('from')->string(50)
313
            ->column('to')->string(50)->unique();
314
315
        $toSchema->getTable($connection->quoteIdentifier('ref_no_prim_key'))->addForeignKeyConstraint($connection->quoteIdentifier('ref_no_prim_key'), [$connection->quoteIdentifier('from')], [$connection->quoteIdentifier('to')]);
316
317
        // A table with multiple primary keys.
318
        $db->table('states')
319
            ->column('country_id')->references('country')
320
            ->column('code')->string(3)
321
            ->column('name')->string(50)->then()
322
            ->primaryKey(['country_id', 'code']);
323
324
        // Tables using @Json annotations
325
        $db->table('accounts')
326
            ->column('id')->integer()->primaryKey()->autoIncrement()
327
            ->column('name')->string();
328
329
        $db->table('nodes')
330
            ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@JsonIgnore')
331
            ->column('alias_id')->references('nodes')->null()->comment('@JsonRecursive')
332
            ->column('parent_id')->references('nodes')->null()->comment('@JsonInclude')
333
            ->column('root_id')->references('nodes')->null()->comment('@JsonIgnore')
334
            ->column('owner_id')->references('accounts')->null()->comment('@JsonFormat(property="name") @JsonInclude')
335
            ->column('name')->string()->comment('@JsonKey("basename")')
336
            ->column('size')->integer()->notNull()->default(0)->comment('@JsonFormat(unit=" o")')
337
            ->column('weight')->float()->null()->comment('@JsonFormat(decimals=2,unit="g")')
338
            ->column('created_at')->date()->null()->comment('@JsonFormat("Y-m-d")');
339
340
        $db->table('nodes_guests')
341
            ->column('node_id')->references('nodes')->comment('@JsonIgnore')
342
            ->column('guest_id')->references('accounts')->comment('@JsonKey("guests") @JsonFormat(method="getName")');
343
344
        $db->table('node_entries')
345
            ->column('id')->integer()->primaryKey()->autoIncrement()
346
            ->column('node_id')->references('nodes')->comment('@JsonCollection("entries") @JsonFormat(property="entry")')
347
            ->column('entry')->string()->null();
348
349
        $db->table('artists')
350
            ->column('id')->integer()->primaryKey()->autoIncrement()
351
            ->column('children')->array()->null() //used to test conflicts with autopivot
352
            ->column('name')->string();
353
354
        $db->table('albums')
355
            ->column('id')->integer()->primaryKey()->autoIncrement()
356
            ->column('artist_id')->references('artists')->comment('@JsonCollection(key="discography")')
357
            ->column('account_id')->references('accounts')
358
            ->column('node_id')->references('nodes')->null()
359
            ->column('title')->string()
360
            ->then()->unique(['artist_id', 'account_id'])->unique(['artist_id', 'node_id']);
361
362
        $db->table('tracks')
363
            ->column('id')->integer()->primaryKey()->autoIncrement()
364
            ->column('album_id')->references('albums')->comment('@JsonCollection @JsonRecursive')
365
            ->column('title')->string()
366
            ->column('duration')->time()->comment('@JsonFormat("H:i:s")');
367
368
        $db->table('featuring')
369
            ->column('id')->integer()->primaryKey()->autoIncrement()
370
            ->column('track_id')->references('tracks')
371
            ->column('artist_id')->references('artists')->comment('@JsonKey("feat") @JsonInclude');
372
373
        $db->table('artists_relations') //used to test the auto pivot case
374
            ->column('id')->integer()->primaryKey()->autoIncrement()
375
            ->column('parent_id')->references('artists')
376
            ->column('child_id')->references('artists');
377
378
        $db->table('children') //used to test conflicts with autopivot
379
            ->column('id')->integer()->primaryKey()->autoIncrement()
380
            ->column('artist_id')->references('artists');
381
382
        $db->junctionTable('person', 'boats');
383
384
        $db->table('base_objects')
385
            ->column('id')->integer()->primaryKey()->autoIncrement()
386
            ->column('label')->string();
387
        $db->table('inherited_objects')
388
            ->column('id')->integer()->primaryKey()->autoIncrement()
389
            ->column('base_object_id')->references('base_objects')->unique()->comment('@JsonCollection');
390
391
        $targetTable = $db->table('composite_fk_target')
392
            ->column('id_1')->integer()
393
            ->column('id_2')->integer()
394
            ->then()->primaryKey(['id_1', 'id_2']);
395
        $db->table('composite_fk_source')
396
            ->column('id')->integer()->primaryKey()->autoIncrement()
397
            ->column('fk_1')->integer()
398
            ->column('fk_2')->integer()
399
            ->then()->getDbalTable()->addForeignKeyConstraint($targetTable->getDbalTable(), ['fk_1', 'fk_2'], ['id_1', 'id_2']);
400
401
        // Test case, the problem here is:
402
        // - `inheritance_agency` have an FK to `inheritance_society.**id_entity**`
403
        // - `inheritance_society` have an FK to `inheritance_entity.**id**`
404
        $db->table('inheritance_entity')
405
            ->column('id')->integer()->primaryKey()->autoIncrement();
406
        $db->table('inheritance_society')
407
            ->column('id_entity')->references('inheritance_entity')->primaryKey()
408
            ->then();
409
        $db->table('inheritance_agency')
410
            ->column('id')->integer()->primaryKey()->autoIncrement()
411
            ->column('id_parent_society')->references('inheritance_society');
412
413
        $sqlStmts = $toSchema->getMigrateFromSql($fromSchema, $connection->getDatabasePlatform());
414
415
        foreach ($sqlStmts as $sqlStmt) {
416
            $connection->exec($sqlStmt);
417
        }
418
419
        // Let's generate computed columns
420
        if ($connection->getDatabasePlatform() instanceof MySqlPlatform && !self::isMariaDb($connection)) {
421
            $connection->exec('CREATE TABLE `players` (
422
               `id` INT UNSIGNED AUTO_INCREMENT NOT NULL,
423
               `player_and_games` JSON NOT NULL,
424
               `names_virtual` VARCHAR(20) GENERATED ALWAYS AS (`player_and_games` ->> \'$.name\') NOT NULL COMMENT \'@Generated\',
425
               `animal_id` INT COMMENT \'@Generated\',
426
               PRIMARY KEY (`id`),
427
               FOREIGN KEY (animal_id) REFERENCES animal(id)
428
            );
429
            ');
430
        }
431
432
        self::insert($connection, 'country', [
433
            'label' => 'France',
434
        ]);
435
        self::insert($connection, 'country', [
436
            'label' => 'UK',
437
        ]);
438
        self::insert($connection, 'country', [
439
            'label' => 'Jamaica',
440
        ]);
441
442
        self::insert($connection, 'person', [
443
            'name' => 'John Smith',
444
            'created_at' => '2015-10-24 11:57:13',
445
        ]);
446
        self::insert($connection, 'person', [
447
            'name' => 'Jean Dupont',
448
            'created_at' => '2015-10-24 11:57:13',
449
        ]);
450
        self::insert($connection, 'person', [
451
            'name' => 'Robert Marley',
452
            'created_at' => '2015-10-24 11:57:13',
453
        ]);
454
        self::insert($connection, 'person', [
455
            'name' => 'Bill Shakespeare',
456
            'created_at' => '2015-10-24 11:57:13',
457
        ]);
458
459
        self::insert($connection, 'contact', [
460
            'id' => 1,
461
            'email' => '[email protected]',
462
            'manager_id' => null,
463
        ]);
464
        self::insert($connection, 'contact', [
465
            'id' => 2,
466
            'email' => '[email protected]',
467
            'manager_id' => null,
468
        ]);
469
        self::insert($connection, 'contact', [
470
            'id' => 3,
471
            'email' => '[email protected]',
472
            'manager_id' => null,
473
        ]);
474
        self::insert($connection, 'contact', [
475
            'id' => 4,
476
            'email' => '[email protected]',
477
            'manager_id' => 1,
478
        ]);
479
480
        self::insert($connection, 'rights', [
481
            'label' => 'CAN_SING',
482
        ]);
483
        self::insert($connection, 'rights', [
484
            'label' => 'CAN_WRITE',
485
        ]);
486
487
        self::insert($connection, 'roles', [
488
            'name' => 'Admins',
489
            'created_at' => '2015-10-24'
490
        ]);
491
        self::insert($connection, 'roles', [
492
            'name' => 'Writers',
493
            'created_at' => '2015-10-24'
494
        ]);
495
        self::insert($connection, 'roles', [
496
            'name' => 'Singers',
497
            'created_at' => '2015-10-24'
498
        ]);
499
500
        self::insert($connection, 'roles_rights', [
501
            'role_id' => 1,
502
            'right_label' => 'CAN_SING'
503
        ]);
504
        self::insert($connection, 'roles_rights', [
505
            'role_id' => 3,
506
            'right_label' => 'CAN_SING'
507
        ]);
508
        self::insert($connection, 'roles_rights', [
509
            'role_id' => 1,
510
            'right_label' => 'CAN_WRITE'
511
        ]);
512
        self::insert($connection, 'roles_rights', [
513
            'role_id' => 2,
514
            'right_label' => 'CAN_WRITE'
515
        ]);
516
517
        self::insert($connection, 'users', [
518
            'id' => 1,
519
            'login' => 'john.smith',
520
            'password' => null,
521
            'status' => 'on',
522
            'country_id' => 2
523
        ]);
524
        self::insert($connection, 'users', [
525
            'id' => 2,
526
            'login' => 'jean.dupont',
527
            'password' => null,
528
            'status' => 'on',
529
            'country_id' => 1
530
        ]);
531
        self::insert($connection, 'users', [
532
            'id' => 3,
533
            'login' => 'robert.marley',
534
            'password' => null,
535
            'status' => 'off',
536
            'country_id' => 3
537
        ]);
538
        self::insert($connection, 'users', [
539
            'id' => 4,
540
            'login' => 'bill.shakespeare',
541
            'password' => null,
542
            'status' => 'off',
543
            'country_id' => 2
544
        ]);
545
546
        self::insert($connection, 'users_roles', [
547
            'user_id' => 1,
548
            'role_id' => 1,
549
        ]);
550
        self::insert($connection, 'users_roles', [
551
            'user_id' => 2,
552
            'role_id' => 1,
553
        ]);
554
        self::insert($connection, 'users_roles', [
555
            'user_id' => 3,
556
            'role_id' => 3,
557
        ]);
558
        self::insert($connection, 'users_roles', [
559
            'user_id' => 4,
560
            'role_id' => 2,
561
        ]);
562
        self::insert($connection, 'users_roles', [
563
            'user_id' => 3,
564
            'role_id' => 2,
565
        ]);
566
567
        self::insert($connection, 'ref_no_prim_key', [
568
            'from' => 'foo',
569
            'to' => 'foo',
570
        ]);
571
572
        self::insert($connection, 'accounts', [
573
            'id' => 1,
574
            'name' => 'root'
575
        ]);
576
        self::insert($connection, 'accounts', [
577
            'id' => 2,
578
            'name' => 'user'
579
        ]);
580
        self::insert($connection, 'accounts', [
581
            'id' => 3,
582
            'name' => 'www'
583
        ]);
584
        self::insert($connection, 'nodes', [
585
            'id' => 1,
586
            'owner_id' => 1,
587
            'name' => '/',
588
            'created_at' => (new DateTime('last year'))->format('Y-m-d H:i:s'),
589
        ]);
590
        self::insert($connection, 'nodes', [
591
            'id' => 2,
592
            'name' => 'private',
593
            'created_at' => (new DateTime('last year'))->format('Y-m-d H:i:s'),
594
            'parent_id' => 1,
595
        ]);
596
        self::insert($connection, 'nodes', [
597
            'id' => 3,
598
            'name' => 'var',
599
            'created_at' => (new DateTime('last year'))->format('Y-m-d H:i:s'),
600
            'parent_id' => 2,
601
        ]);
602
        self::insert($connection, 'nodes', [
603
            'id' => 4,
604
            'name' => 'var',
605
            'created_at' => (new DateTime('last year'))->format('Y-m-d H:i:s'),
606
            'parent_id' => 1,
607
            'alias_id' => 3
608
        ]);
609
        self::insert($connection, 'nodes', [
610
            'id' => 5,
611
            'name' => 'www',
612
            'created_at' => (new DateTime('last week'))->format('Y-m-d H:i:s'),
613
            'parent_id' => 4
614
        ]);
615
        self::insert($connection, 'nodes', [
616
            'id' => 6,
617
            'owner_id' => 2,
618
            'name' => 'index.html',
619
            'created_at' => (new DateTime('now'))->format('Y-m-d H:i:s'),
620
            'size' => 512,
621
            'weight' => 42.5,
622
            'parent_id' => 5
623
        ]);
624
        self::insert($connection, 'nodes', [
625
            'id' => 7,
626
            'name' => 'index.html',
627
            'created_at' => (new DateTime('now'))->format('Y-m-d H:i:s'),
628
            'alias_id' => 6,
629
            'parent_id' => 1
630
        ]);
631
        self::insert($connection, 'nodes', [
632
            'id' => 8,
633
            'name' => 'index.htm',
634
            'created_at' => (new DateTime('now'))->format('Y-m-d H:i:s'),
635
            'alias_id' => 7,
636
            'parent_id' => 1
637
        ]);
638
        self::insert($connection, 'nodes_guests', [
639
            'node_id' => 6,
640
            'guest_id' => 1
641
        ]);
642
        self::insert($connection, 'nodes_guests', [
643
            'node_id' => 6,
644
            'guest_id' => 3
645
        ]);
646
        self::insert($connection, 'node_entries', [
647
            'node_id' => 6,
648
            'entry' => '<h1>'
649
        ]);
650
        self::insert($connection, 'node_entries', [
651
            'node_id' => 6,
652
            'entry' => 'Hello, World'
653
        ]);
654
        self::insert($connection, 'node_entries', [
655
            'node_id' => 6,
656
            'entry' => '</h1>'
657
        ]);
658
659
        self::insert($connection, 'artists', [
660
            'id' => 1,
661
            'name' => 'Pink Floyd'
662
        ]);
663
        self::insert($connection, 'artists', [
664
            'id' => 2,
665
            'name' => 'Roger Waters'
666
        ]);
667
        self::insert($connection, 'artists', [
668
            'id' => 3,
669
            'name' => 'David Gilmour'
670
        ]);
671
        self::insert($connection, 'albums', [
672
            'id' => 1,
673
            'artist_id' => 1,
674
            'account_id' => 1,
675
            'title' => 'Animals'
676
        ]);
677
        self::insert($connection, 'artists_relations', [
678
            'parent_id' => 1,
679
            'child_id' => 2
680
        ]);
681
682
        $timeType = Type::getType(Type::TIME_IMMUTABLE);
683
684
        self::insert($connection, 'tracks', [
685
            'album_id' => 1,
686
            'title' =>'Pigs on the Wing 1',
687
            // Note: Oracle does not have a TIME column type
688
            'duration' => $timeType->convertToDatabaseValue(new DateTimeImmutable('1970-01-01 00:01:25'), $connection->getDatabasePlatform()),
689
        ]);
690
        self::insert($connection, 'tracks', [
691
            'album_id' => 1,
692
            'title' => 'Dogs',
693
            'duration' => $timeType->convertToDatabaseValue(new DateTimeImmutable('1970-01-01 00:17:04'), $connection->getDatabasePlatform()),
694
        ]);
695
        self::insert($connection, 'tracks', [
696
            'album_id' => 1,
697
            'title' => 'Pigs (Three Different Ones)',
698
            'duration' => $timeType->convertToDatabaseValue(new DateTimeImmutable('1970-01-01 00:11:22'), $connection->getDatabasePlatform()),
699
        ]);
700
        self::insert($connection, 'tracks', [
701
            'album_id' => 1,
702
            'title' => 'Sheep',
703
            'duration' => $timeType->convertToDatabaseValue(new DateTimeImmutable('1970-01-01 00:10:24'), $connection->getDatabasePlatform()),
704
        ]);
705
        self::insert($connection, 'tracks', [
706
            'album_id' => 1,
707
            'title' => 'Pigs on the Wing 2',
708
            'duration' => $timeType->convertToDatabaseValue(new DateTimeImmutable('1970-01-01 00:01:26'), $connection->getDatabasePlatform()),
709
        ]);
710
        self::insert($connection, 'featuring', [
711
            'track_id' => 1,
712
            'artist_id' => 2
713
        ]);
714
        self::insert($connection, 'featuring', [
715
            'track_id' => 2,
716
            'artist_id' => 3
717
        ]);
718
        self::insert($connection, 'featuring', [
719
            'track_id' => 2,
720
            'artist_id' => 2
721
        ]);
722
        self::insert($connection, 'featuring', [
723
            'track_id' => 3,
724
            'artist_id' => 2
725
        ]);
726
        self::insert($connection, 'featuring', [
727
            'track_id' => 4,
728
            'artist_id' => 2
729
        ]);
730
        self::insert($connection, 'featuring', [
731
            'track_id' => 5,
732
            'artist_id' => 2
733
        ]);
734
735
        self::insert($connection, 'boats', [
736
            'name' => 'RoseBud',
737
            'anchorage_country' => 1,
738
            'current_country' => 1,
739
            'length' => '13.5',
740
        ]);
741
742
        self::insert($connection, 'person_boats', [
743
            'person_id' => 1,
744
            'boat_id' => 1,
745
        ]);
746
        self::insert($connection, 'composite_fk_target', [
747
            'id_1' => 1,
748
            'id_2' => 1
749
        ]);
750
        self::insert($connection, 'composite_fk_source', [
751
            'id' => 1,
752
            'fk_1' => 1,
753
            'fk_2' => 1
754
        ]);
755
    }
756
757
    public static function insert(Connection $connection, string $tableName, array $data): void
758
    {
759
        $quotedData = [];
760
        foreach ($data as $id => $value) {
761
            $quotedData[$connection->quoteIdentifier($id)] = $value;
762
        }
763
        $connection->insert($connection->quoteIdentifier($tableName), $quotedData);
764
    }
765
766
    protected static function delete(Connection $connection, string $tableName, array $data): void
767
    {
768
        $quotedData = [];
769
        foreach ($data as $id => $value) {
770
            $quotedData[$connection->quoteIdentifier($id)] = $value;
771
        }
772
        $connection->delete($connection->quoteIdentifier($tableName), $quotedData);
773
    }
774
775
    protected static function isMariaDb(Connection $connection): bool
776
    {
777
        if (!$connection->getDatabasePlatform() instanceof MySqlPlatform) {
778
            return false;
779
        }
780
        $version = $connection->fetchColumn('SELECT VERSION()');
781
        return stripos($version, 'maria') !== false;
0 ignored issues
show
Bug introduced by
It seems like $version can also be of type false; however, parameter $haystack of stripos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

781
        return stripos(/** @scrutinizer ignore-type */ $version, 'maria') !== false;
Loading history...
782
    }
783
}
784