Passed
Pull Request — master (#3233)
by Sergey
12:25
created

SchemaTest::testConstructWithView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 13
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Schema;
4
5
use Doctrine\DBAL\Schema\Schema;
6
use Doctrine\DBAL\Schema\SchemaConfig;
7
use Doctrine\DBAL\Schema\SchemaException;
8
use Doctrine\DBAL\Schema\Sequence;
9
use Doctrine\DBAL\Schema\Table;
10
use Doctrine\DBAL\Schema\View;
11
use Doctrine\DBAL\Schema\Visitor\AbstractVisitor;
12
use Doctrine\DBAL\Schema\Visitor\Visitor;
13
use PHPUnit\Framework\TestCase;
14
use function current;
15
use function strlen;
16
17
class SchemaTest extends TestCase
18
{
19
    public function testAddTable()
20
    {
21
        $tableName = 'public.foo';
22
        $table     = new Table($tableName);
23
24
        $schema = new Schema([$table]);
25
26
        self::assertTrue($schema->hasTable($tableName));
27
28
        $tables = $schema->getTables();
29
        self::assertArrayHasKey($tableName, $tables);
30
        self::assertSame($table, $tables[$tableName]);
31
        self::assertSame($table, $schema->getTable($tableName));
32
        self::assertTrue($schema->hasTable($tableName));
33
    }
34
35
    public function testTableMatchingCaseInsensitive()
36
    {
37
        $table = new Table('Foo');
38
39
        $schema = new Schema([$table]);
40
        self::assertTrue($schema->hasTable('foo'));
41
        self::assertTrue($schema->hasTable('FOO'));
42
43
        self::assertSame($table, $schema->getTable('FOO'));
44
        self::assertSame($table, $schema->getTable('foo'));
45
        self::assertSame($table, $schema->getTable('Foo'));
46
    }
47
48
    public function testGetUnknownTableThrowsException()
49
    {
50
        $this->expectException(SchemaException::class);
51
52
        $schema = new Schema();
53
        $schema->getTable('unknown');
54
    }
55
56
    public function testCreateTableTwiceThrowsException()
57
    {
58
        $this->expectException(SchemaException::class);
59
60
        $tableName = 'foo';
61
        $table     = new Table($tableName);
62
        $tables    = [$table, $table];
63
64
        $schema = new Schema($tables);
0 ignored issues
show
Unused Code introduced by
The assignment to $schema is dead and can be removed.
Loading history...
65
    }
66
67
    public function testRenameTable()
68
    {
69
        $tableName = 'foo';
70
        $table     = new Table($tableName);
71
        $schema    = new Schema([$table]);
72
73
        self::assertTrue($schema->hasTable('foo'));
74
        $schema->renameTable('foo', 'bar');
75
        self::assertFalse($schema->hasTable('foo'));
76
        self::assertTrue($schema->hasTable('bar'));
77
        self::assertSame($table, $schema->getTable('bar'));
78
    }
79
80
    public function testDropTable()
81
    {
82
        $tableName = 'foo';
83
        $table     = new Table($tableName);
84
        $schema    = new Schema([$table]);
85
86
        self::assertTrue($schema->hasTable('foo'));
87
88
        $schema->dropTable('foo');
89
90
        self::assertFalse($schema->hasTable('foo'));
91
    }
92
93
    public function testCreateTable()
94
    {
95
        $schema = new Schema();
96
97
        self::assertFalse($schema->hasTable('foo'));
98
99
        $table = $schema->createTable('foo');
100
101
        self::assertInstanceOf(Table::class, $table);
102
        self::assertEquals('foo', $table->getName());
103
        self::assertTrue($schema->hasTable('foo'));
104
    }
105
106
    public function testAddSequences()
107
    {
108
        $sequence = new Sequence('a_seq', 1, 1);
109
110
        $schema = new Schema([], [$sequence]);
111
112
        self::assertTrue($schema->hasSequence('a_seq'));
113
        self::assertInstanceOf(Sequence::class, $schema->getSequence('a_seq'));
114
115
        $sequences = $schema->getSequences();
116
        self::assertArrayHasKey('public.a_seq', $sequences);
117
    }
118
119
    public function testSequenceAccessCaseInsensitive()
120
    {
121
        $sequence = new Sequence('a_Seq');
122
123
        $schema = new Schema([], [$sequence]);
124
        self::assertTrue($schema->hasSequence('a_seq'));
125
        self::assertTrue($schema->hasSequence('a_Seq'));
126
        self::assertTrue($schema->hasSequence('A_SEQ'));
127
128
        self::assertEquals($sequence, $schema->getSequence('a_seq'));
129
        self::assertEquals($sequence, $schema->getSequence('a_Seq'));
130
        self::assertEquals($sequence, $schema->getSequence('A_SEQ'));
131
    }
132
133
    public function testGetUnknownSequenceThrowsException()
134
    {
135
        $this->expectException(SchemaException::class);
136
137
        $schema = new Schema();
138
        $schema->getSequence('unknown');
139
    }
140
141
    public function testCreateSequence()
142
    {
143
        $schema   = new Schema();
144
        $sequence = $schema->createSequence('a_seq', 10, 20);
145
146
        self::assertEquals('a_seq', $sequence->getName());
147
        self::assertEquals(10, $sequence->getAllocationSize());
148
        self::assertEquals(20, $sequence->getInitialValue());
149
150
        self::assertTrue($schema->hasSequence('a_seq'));
151
        self::assertInstanceOf(Sequence::class, $schema->getSequence('a_seq'));
152
153
        $sequences = $schema->getSequences();
154
        self::assertArrayHasKey('public.a_seq', $sequences);
155
    }
156
157
    public function testDropSequence()
158
    {
159
        $sequence = new Sequence('a_seq', 1, 1);
160
161
        $schema = new Schema([], [$sequence]);
162
163
        $schema->dropSequence('a_seq');
164
        self::assertFalse($schema->hasSequence('a_seq'));
165
    }
166
167
    public function testAddSequenceTwiceThrowsException()
168
    {
169
        $this->expectException(SchemaException::class);
170
171
        $sequence = new Sequence('a_seq', 1, 1);
172
173
        $schema = new Schema([], [$sequence, $sequence]);
0 ignored issues
show
Unused Code introduced by
The assignment to $schema is dead and can be removed.
Loading history...
174
    }
175
176
    public function testConfigMaxIdentifierLength()
177
    {
178
        $schemaConfig = new SchemaConfig();
179
        $schemaConfig->setMaxIdentifierLength(5);
180
181
        $schema = new Schema([], [], $schemaConfig);
182
        $table  = $schema->createTable('smalltable');
183
        $table->addColumn('long_id', 'integer');
184
        $table->addIndex(['long_id']);
185
186
        $index = current($table->getIndexes());
187
        self::assertEquals(5, strlen($index->getName()));
188
    }
189
190
    public function testDeepClone()
191
    {
192
        $schema   = new Schema();
193
        $sequence = $schema->createSequence('baz');
194
        $view     = $schema->createView('foz', 'SELECT * FROM `foo`');
195
196
        $tableA = $schema->createTable('foo');
197
        $tableA->addColumn('id', 'integer');
198
199
        $tableB = $schema->createTable('bar');
200
        $tableB->addColumn('id', 'integer');
201
        $tableB->addColumn('foo_id', 'integer');
202
        $tableB->addForeignKeyConstraint($tableA, ['foo_id'], ['id']);
203
204
        $schemaNew = clone $schema;
205
206
        self::assertNotSame($sequence, $schemaNew->getSequence('baz'));
207
208
        self::assertNotSame($view, $schemaNew->getView('foz'));
209
210
        self::assertNotSame($tableA, $schemaNew->getTable('foo'));
211
        self::assertNotSame($tableA->getColumn('id'), $schemaNew->getTable('foo')->getColumn('id'));
212
213
        self::assertNotSame($tableB, $schemaNew->getTable('bar'));
214
        self::assertNotSame($tableB->getColumn('id'), $schemaNew->getTable('bar')->getColumn('id'));
215
216
        $fk = $schemaNew->getTable('bar')->getForeignKeys();
217
        $fk = current($fk);
218
        self::assertSame($schemaNew->getTable('bar'), $this->readAttribute($fk, '_localTable'));
219
    }
220
221
    /**
222
     * @group DBAL-219
223
     */
224
    public function testHasTableForQuotedAsset()
225
    {
226
        $schema = new Schema();
227
228
        $tableA = $schema->createTable('foo');
229
        $tableA->addColumn('id', 'integer');
230
231
        self::assertTrue($schema->hasTable('`foo`'));
232
    }
233
234
    /**
235
     * @group DBAL-669
236
     */
237
    public function testHasNamespace()
238
    {
239
        $schema = new Schema();
240
241
        self::assertFalse($schema->hasNamespace('foo'));
242
243
        $schema->createTable('foo');
244
245
        self::assertFalse($schema->hasNamespace('foo'));
246
247
        $schema->createTable('bar.baz');
248
249
        self::assertFalse($schema->hasNamespace('baz'));
250
        self::assertTrue($schema->hasNamespace('bar'));
251
        self::assertFalse($schema->hasNamespace('tab'));
252
253
        $schema->createTable('tab.taz');
254
255
        self::assertTrue($schema->hasNamespace('tab'));
256
    }
257
258
    /**
259
     * @group DBAL-669
260
     */
261
    public function testCreatesNamespace()
262
    {
263
        $schema = new Schema();
264
265
        self::assertFalse($schema->hasNamespace('foo'));
266
267
        $schema->createNamespace('foo');
268
269
        self::assertTrue($schema->hasNamespace('foo'));
270
        self::assertTrue($schema->hasNamespace('FOO'));
271
        self::assertTrue($schema->hasNamespace('`foo`'));
272
        self::assertTrue($schema->hasNamespace('`FOO`'));
273
274
        $schema->createNamespace('`bar`');
275
276
        self::assertTrue($schema->hasNamespace('bar'));
277
        self::assertTrue($schema->hasNamespace('BAR'));
278
        self::assertTrue($schema->hasNamespace('`bar`'));
279
        self::assertTrue($schema->hasNamespace('`BAR`'));
280
281
        self::assertSame(['foo' => 'foo', 'bar' => '`bar`'], $schema->getNamespaces());
282
    }
283
284
    /**
285
     * @group DBAL-669
286
     * @expectedException \Doctrine\DBAL\Schema\SchemaException
287
     */
288
    public function testThrowsExceptionOnCreatingNamespaceTwice()
289
    {
290
        $schema = new Schema();
291
292
        $schema->createNamespace('foo');
293
        $schema->createNamespace('foo');
294
    }
295
296
    /**
297
     * @group DBAL-669
298
     */
299
    public function testCreatesNamespaceThroughAddingTableImplicitly()
300
    {
301
        $schema = new Schema();
302
303
        self::assertFalse($schema->hasNamespace('foo'));
304
305
        $schema->createTable('baz');
306
307
        self::assertFalse($schema->hasNamespace('foo'));
308
        self::assertFalse($schema->hasNamespace('baz'));
309
310
        $schema->createTable('foo.bar');
311
312
        self::assertTrue($schema->hasNamespace('foo'));
313
        self::assertFalse($schema->hasNamespace('bar'));
314
315
        $schema->createTable('`baz`.bloo');
316
317
        self::assertTrue($schema->hasNamespace('baz'));
318
        self::assertFalse($schema->hasNamespace('bloo'));
319
320
        $schema->createTable('`baz`.moo');
321
322
        self::assertTrue($schema->hasNamespace('baz'));
323
        self::assertFalse($schema->hasNamespace('moo'));
324
    }
325
326
    /**
327
     * @group DBAL-669
328
     */
329
    public function testCreatesNamespaceThroughAddingSequenceImplicitly()
330
    {
331
        $schema = new Schema();
332
333
        self::assertFalse($schema->hasNamespace('foo'));
334
335
        $schema->createSequence('baz');
336
337
        self::assertFalse($schema->hasNamespace('foo'));
338
        self::assertFalse($schema->hasNamespace('baz'));
339
340
        $schema->createSequence('foo.bar');
341
342
        self::assertTrue($schema->hasNamespace('foo'));
343
        self::assertFalse($schema->hasNamespace('bar'));
344
345
        $schema->createSequence('`baz`.bloo');
346
347
        self::assertTrue($schema->hasNamespace('baz'));
348
        self::assertFalse($schema->hasNamespace('bloo'));
349
350
        $schema->createSequence('`baz`.moo');
351
352
        self::assertTrue($schema->hasNamespace('baz'));
353
        self::assertFalse($schema->hasNamespace('moo'));
354
    }
355
356
    /**
357
     * @group DBAL-669
358
     */
359
    public function testVisitsVisitor()
360
    {
361
        $schema  = new Schema();
362
        $visitor = $this->createMock(Visitor::class);
363
364
        $schema->createNamespace('foo');
365
        $schema->createNamespace('bar');
366
367
        $schema->createTable('baz');
368
        $schema->createTable('bla.bloo');
369
370
        $schema->createSequence('moo');
371
        $schema->createSequence('war');
372
373
        $visitor->expects($this->once())
374
            ->method('acceptSchema')
375
            ->with($schema);
376
377
        $visitor->expects($this->at(1))
378
            ->method('acceptTable')
379
            ->with($schema->getTable('baz'));
380
381
        $visitor->expects($this->at(2))
382
            ->method('acceptTable')
383
            ->with($schema->getTable('bla.bloo'));
384
385
        $visitor->expects($this->exactly(2))
386
            ->method('acceptTable');
387
388
        $visitor->expects($this->at(3))
389
            ->method('acceptSequence')
390
            ->with($schema->getSequence('moo'));
391
392
        $visitor->expects($this->at(4))
393
            ->method('acceptSequence')
394
            ->with($schema->getSequence('war'));
395
396
        $visitor->expects($this->exactly(2))
397
            ->method('acceptSequence');
398
399
        self::assertNull($schema->visit($visitor));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $schema->visit($visitor) targeting Doctrine\DBAL\Schema\Schema::visit() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
400
    }
401
402
    /**
403
     * @group DBAL-669
404
     */
405
    public function testVisitsNamespaceVisitor()
406
    {
407
        $schema  = new Schema();
408
        $visitor = $this->createMock(AbstractVisitor::class);
409
410
        $schema->createNamespace('foo');
411
        $schema->createNamespace('bar');
412
413
        $schema->createTable('baz');
414
        $schema->createTable('bla.bloo');
415
416
        $schema->createSequence('moo');
417
        $schema->createSequence('war');
418
419
        $schema->createView('foz', 'SELECT * FROM baz');
420
421
        $visitor->expects($this->once())
422
            ->method('acceptSchema')
423
            ->with($schema);
424
425
        $visitor->expects($this->at(1))
426
            ->method('acceptNamespace')
427
            ->with('foo');
428
429
        $visitor->expects($this->at(2))
430
            ->method('acceptNamespace')
431
            ->with('bar');
432
433
        $visitor->expects($this->at(3))
434
            ->method('acceptNamespace')
435
            ->with('bla');
436
437
        $visitor->expects($this->exactly(3))
438
            ->method('acceptNamespace');
439
440
        $visitor->expects($this->at(4))
441
            ->method('acceptTable')
442
            ->with($schema->getTable('baz'));
443
444
        $visitor->expects($this->at(5))
445
            ->method('acceptTable')
446
            ->with($schema->getTable('bla.bloo'));
447
448
        $visitor->expects($this->exactly(2))
449
            ->method('acceptTable');
450
451
        $visitor->expects($this->at(6))
452
            ->method('acceptSequence')
453
            ->with($schema->getSequence('moo'));
454
455
        $visitor->expects($this->at(7))
456
            ->method('acceptSequence')
457
            ->with($schema->getSequence('war'));
458
459
        $visitor->expects($this->exactly(2))
460
            ->method('acceptSequence');
461
462
        $visitor->expects($this->at(8))
463
            ->method('acceptView')
464
            ->with($schema->getView('foz'));
465
466
        self::assertNull($schema->visit($visitor));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $schema->visit($visitor) targeting Doctrine\DBAL\Schema\Schema::visit() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
467
    }
468
469
    public function testConstructWithView()
470
    {
471
        $viewName = 'public.foo';
472
        $view     = new View($viewName, 'SELECT * FROM `bar`');
473
474
        $schema = new Schema([], [], null, [], [$view]);
475
476
        self::assertTrue($schema->hasView($viewName));
477
478
        $views = $schema->getViews();
479
        self::assertArrayHasKey($viewName, $views);
480
        self::assertSame($view, $views[$viewName]);
481
        self::assertSame($view, $schema->getView($viewName));
482
    }
483
484
    public function testViewMatchingCaseInsensitive()
485
    {
486
        $view = new View('Foo', 'SELECT * FROM `bar`');
487
488
        $schema = new Schema([], [], null, [], [$view]);
489
        self::assertTrue($schema->hasView('foo'));
490
        self::assertTrue($schema->hasView('FOO'));
491
492
        self::assertSame($view, $schema->getView('FOO'));
493
        self::assertSame($view, $schema->getView('foo'));
494
        self::assertSame($view, $schema->getView('Foo'));
495
    }
496
497
    /**
498
     * @expectedException \Doctrine\DBAL\Schema\SchemaException
499
     */
500
    public function testGetUnknownViewThrowsException()
501
    {
502
        $schema = new Schema();
503
        $schema->getView('unknown');
504
    }
505
506
    /**
507
     * @expectedException \Doctrine\DBAL\Schema\SchemaException
508
     */
509
    public function testCreateViewTwiceThrowsException()
510
    {
511
        $viewName = 'foo';
512
        $view     = new View($viewName, 'SELECT * FROM `bar`');
513
        $views    = [$view, $view];
514
515
        $schema = new Schema([], [], null, [], $views);
0 ignored issues
show
Unused Code introduced by
The assignment to $schema is dead and can be removed.
Loading history...
516
    }
517
518
    public function testHasView()
519
    {
520
        $schema = new Schema();
521
522
        self::assertFalse($schema->hasView('foo'));
523
524
        $schema->createView('foo', 'SELECT * FROM `bar`');
525
526
        self::assertTrue($schema->hasView('foo'));
527
    }
528
529
    public function testCreatesView()
530
    {
531
        $schema = new Schema();
532
533
        self::assertFalse($schema->hasView('foo'));
534
535
        $view = $schema->createView('foo', 'SELECT * FROM `bar`');
536
537
        self::assertInstanceOf('Doctrine\DBAL\Schema\View', $view);
538
        self::assertEquals('foo', $view->getName());
539
        self::assertEquals('SELECT * FROM `bar`', $view->getSql());
540
        self::assertTrue($schema->hasView('foo'));
541
    }
542
543
    public function testDropView()
544
    {
545
        $schema = new Schema();
546
547
        $schema->dropView('foo');
548
        self::assertFalse($schema->hasView('foo'));
549
550
        $schema->createView('foo', 'SELECT * FROM `bar`');
551
552
        $schema->dropView('foo');
553
        self::assertFalse($schema->hasView('foo'));
554
    }
555
}
556