Completed
Pull Request — master (#3610)
by Sergei
03:27 queued 10s
created

SchemaTest::testVisitsNamespaceVisitor()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 8.8945
c 0
b 0
f 0
cc 1
nc 1
nop 0

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
declare(strict_types=1);
4
5
namespace Doctrine\Tests\DBAL\Schema;
6
7
use Doctrine\DBAL\Schema\Schema;
8
use Doctrine\DBAL\Schema\SchemaConfig;
9
use Doctrine\DBAL\Schema\SchemaException;
10
use Doctrine\DBAL\Schema\Sequence;
11
use Doctrine\DBAL\Schema\Table;
12
use Doctrine\DBAL\Schema\Visitor\AbstractVisitor;
13
use Doctrine\DBAL\Schema\Visitor\Visitor;
14
use PHPUnit\Framework\TestCase;
15
use ReflectionProperty;
16
use function current;
17
use function strlen;
18
19
class SchemaTest extends TestCase
20
{
21
    public function testAddTable() : void
22
    {
23
        $tableName = 'public.foo';
24
        $table     = new Table($tableName);
25
26
        $schema = new Schema([$table]);
27
28
        self::assertTrue($schema->hasTable($tableName));
29
30
        $tables = $schema->getTables();
31
        self::assertArrayHasKey($tableName, $tables);
32
        self::assertSame($table, $tables[$tableName]);
33
        self::assertSame($table, $schema->getTable($tableName));
34
        self::assertTrue($schema->hasTable($tableName));
35
    }
36
37 View Code Duplication
    public function testTableMatchingCaseInsensitive() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39
        $table = new Table('Foo');
40
41
        $schema = new Schema([$table]);
42
        self::assertTrue($schema->hasTable('foo'));
43
        self::assertTrue($schema->hasTable('FOO'));
44
45
        self::assertSame($table, $schema->getTable('FOO'));
46
        self::assertSame($table, $schema->getTable('foo'));
47
        self::assertSame($table, $schema->getTable('Foo'));
48
    }
49
50
    public function testGetUnknownTableThrowsException() : void
51
    {
52
        $this->expectException(SchemaException::class);
53
54
        $schema = new Schema();
55
        $schema->getTable('unknown');
56
    }
57
58
    public function testCreateTableTwiceThrowsException() : void
59
    {
60
        $this->expectException(SchemaException::class);
61
62
        $tableName = 'foo';
63
        $table     = new Table($tableName);
64
        $tables    = [$table, $table];
65
66
        $schema = new Schema($tables);
0 ignored issues
show
Unused Code introduced by
$schema is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
67
    }
68
69 View Code Duplication
    public function testRenameTable() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
    {
71
        $tableName = 'foo';
72
        $table     = new Table($tableName);
73
        $schema    = new Schema([$table]);
74
75
        self::assertTrue($schema->hasTable('foo'));
76
        $schema->renameTable('foo', 'bar');
77
        self::assertFalse($schema->hasTable('foo'));
78
        self::assertTrue($schema->hasTable('bar'));
79
        self::assertSame($table, $schema->getTable('bar'));
80
    }
81
82
    public function testDropTable() : void
83
    {
84
        $tableName = 'foo';
85
        $table     = new Table($tableName);
86
        $schema    = new Schema([$table]);
87
88
        self::assertTrue($schema->hasTable('foo'));
89
90
        $schema->dropTable('foo');
91
92
        self::assertFalse($schema->hasTable('foo'));
93
    }
94
95
    public function testCreateTable() : void
96
    {
97
        $schema = new Schema();
98
99
        self::assertFalse($schema->hasTable('foo'));
100
101
        $table = $schema->createTable('foo');
102
103
        self::assertInstanceOf(Table::class, $table);
104
        self::assertEquals('foo', $table->getName());
105
        self::assertTrue($schema->hasTable('foo'));
106
    }
107
108
    public function testAddSequences() : void
109
    {
110
        $sequence = new Sequence('a_seq', 1, 1);
111
112
        $schema = new Schema([], [$sequence]);
113
114
        self::assertTrue($schema->hasSequence('a_seq'));
115
        self::assertInstanceOf(Sequence::class, $schema->getSequence('a_seq'));
116
117
        $sequences = $schema->getSequences();
118
        self::assertArrayHasKey('public.a_seq', $sequences);
119
    }
120
121 View Code Duplication
    public function testSequenceAccessCaseInsensitive() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
    {
123
        $sequence = new Sequence('a_Seq');
124
125
        $schema = new Schema([], [$sequence]);
126
        self::assertTrue($schema->hasSequence('a_seq'));
127
        self::assertTrue($schema->hasSequence('a_Seq'));
128
        self::assertTrue($schema->hasSequence('A_SEQ'));
129
130
        self::assertEquals($sequence, $schema->getSequence('a_seq'));
131
        self::assertEquals($sequence, $schema->getSequence('a_Seq'));
132
        self::assertEquals($sequence, $schema->getSequence('A_SEQ'));
133
    }
134
135
    public function testGetUnknownSequenceThrowsException() : void
136
    {
137
        $this->expectException(SchemaException::class);
138
139
        $schema = new Schema();
140
        $schema->getSequence('unknown');
141
    }
142
143
    public function testCreateSequence() : void
144
    {
145
        $schema   = new Schema();
146
        $sequence = $schema->createSequence('a_seq', 10, 20);
147
148
        self::assertEquals('a_seq', $sequence->getName());
149
        self::assertEquals(10, $sequence->getAllocationSize());
150
        self::assertEquals(20, $sequence->getInitialValue());
151
152
        self::assertTrue($schema->hasSequence('a_seq'));
153
        self::assertInstanceOf(Sequence::class, $schema->getSequence('a_seq'));
154
155
        $sequences = $schema->getSequences();
156
        self::assertArrayHasKey('public.a_seq', $sequences);
157
    }
158
159
    public function testDropSequence() : void
160
    {
161
        $sequence = new Sequence('a_seq', 1, 1);
162
163
        $schema = new Schema([], [$sequence]);
164
165
        $schema->dropSequence('a_seq');
166
        self::assertFalse($schema->hasSequence('a_seq'));
167
    }
168
169
    public function testAddSequenceTwiceThrowsException() : void
170
    {
171
        $this->expectException(SchemaException::class);
172
173
        $sequence = new Sequence('a_seq', 1, 1);
174
175
        $schema = new Schema([], [$sequence, $sequence]);
0 ignored issues
show
Unused Code introduced by
$schema is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
176
    }
177
178 View Code Duplication
    public function testConfigMaxIdentifierLength() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
179
    {
180
        $schemaConfig = new SchemaConfig();
181
        $schemaConfig->setMaxIdentifierLength(5);
182
183
        $schema = new Schema([], [], $schemaConfig);
184
        $table  = $schema->createTable('smalltable');
185
        $table->addColumn('long_id', 'integer');
186
        $table->addIndex(['long_id']);
187
188
        $index = current($table->getIndexes());
189
        self::assertEquals(5, strlen($index->getName()));
190
    }
191
192
    public function testDeepClone() : void
193
    {
194
        $schema   = new Schema();
195
        $sequence = $schema->createSequence('baz');
196
197
        $tableA = $schema->createTable('foo');
198
        $tableA->addColumn('id', 'integer');
199
200
        $tableB = $schema->createTable('bar');
201
        $tableB->addColumn('id', 'integer');
202
        $tableB->addColumn('foo_id', 'integer');
203
        $tableB->addForeignKeyConstraint($tableA, ['foo_id'], ['id']);
204
205
        $schemaNew = clone $schema;
206
207
        self::assertNotSame($sequence, $schemaNew->getSequence('baz'));
208
209
        self::assertNotSame($tableA, $schemaNew->getTable('foo'));
210
        self::assertNotSame($tableA->getColumn('id'), $schemaNew->getTable('foo')->getColumn('id'));
211
212
        self::assertNotSame($tableB, $schemaNew->getTable('bar'));
213
        self::assertNotSame($tableB->getColumn('id'), $schemaNew->getTable('bar')->getColumn('id'));
214
215
        $fk = $schemaNew->getTable('bar')->getForeignKeys();
216
        $fk = current($fk);
217
218
        $re = new ReflectionProperty($fk, '_localTable');
219
        $re->setAccessible(true);
220
221
        self::assertSame($schemaNew->getTable('bar'), $re->getValue($fk));
222
    }
223
224
    /**
225
     * @group DBAL-219
226
     */
227
    public function testHasTableForQuotedAsset() : void
228
    {
229
        $schema = new Schema();
230
231
        $tableA = $schema->createTable('foo');
232
        $tableA->addColumn('id', 'integer');
233
234
        self::assertTrue($schema->hasTable('`foo`'));
235
    }
236
237
    /**
238
     * @group DBAL-669
239
     */
240
    public function testHasNamespace() : void
241
    {
242
        $schema = new Schema();
243
244
        self::assertFalse($schema->hasNamespace('foo'));
245
246
        $schema->createTable('foo');
247
248
        self::assertFalse($schema->hasNamespace('foo'));
249
250
        $schema->createTable('bar.baz');
251
252
        self::assertFalse($schema->hasNamespace('baz'));
253
        self::assertTrue($schema->hasNamespace('bar'));
254
        self::assertFalse($schema->hasNamespace('tab'));
255
256
        $schema->createTable('tab.taz');
257
258
        self::assertTrue($schema->hasNamespace('tab'));
259
    }
260
261
    /**
262
     * @group DBAL-669
263
     */
264
    public function testCreatesNamespace() : void
265
    {
266
        $schema = new Schema();
267
268
        self::assertFalse($schema->hasNamespace('foo'));
269
270
        $schema->createNamespace('foo');
271
272
        self::assertTrue($schema->hasNamespace('foo'));
273
        self::assertTrue($schema->hasNamespace('FOO'));
274
        self::assertTrue($schema->hasNamespace('`foo`'));
275
        self::assertTrue($schema->hasNamespace('`FOO`'));
276
277
        $schema->createNamespace('`bar`');
278
279
        self::assertTrue($schema->hasNamespace('bar'));
280
        self::assertTrue($schema->hasNamespace('BAR'));
281
        self::assertTrue($schema->hasNamespace('`bar`'));
282
        self::assertTrue($schema->hasNamespace('`BAR`'));
283
284
        self::assertSame(['foo' => 'foo', 'bar' => '`bar`'], $schema->getNamespaces());
285
    }
286
287
    /**
288
     * @group DBAL-669
289
     */
290
    public function testThrowsExceptionOnCreatingNamespaceTwice() : void
291
    {
292
        $schema = new Schema();
293
294
        $schema->createNamespace('foo');
295
296
        $this->expectException(SchemaException::class);
297
298
        $schema->createNamespace('foo');
299
    }
300
301
    /**
302
     * @group DBAL-669
303
     */
304 View Code Duplication
    public function testCreatesNamespaceThroughAddingTableImplicitly() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
305
    {
306
        $schema = new Schema();
307
308
        self::assertFalse($schema->hasNamespace('foo'));
309
310
        $schema->createTable('baz');
311
312
        self::assertFalse($schema->hasNamespace('foo'));
313
        self::assertFalse($schema->hasNamespace('baz'));
314
315
        $schema->createTable('foo.bar');
316
317
        self::assertTrue($schema->hasNamespace('foo'));
318
        self::assertFalse($schema->hasNamespace('bar'));
319
320
        $schema->createTable('`baz`.bloo');
321
322
        self::assertTrue($schema->hasNamespace('baz'));
323
        self::assertFalse($schema->hasNamespace('bloo'));
324
325
        $schema->createTable('`baz`.moo');
326
327
        self::assertTrue($schema->hasNamespace('baz'));
328
        self::assertFalse($schema->hasNamespace('moo'));
329
    }
330
331
    /**
332
     * @group DBAL-669
333
     */
334 View Code Duplication
    public function testCreatesNamespaceThroughAddingSequenceImplicitly() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
335
    {
336
        $schema = new Schema();
337
338
        self::assertFalse($schema->hasNamespace('foo'));
339
340
        $schema->createSequence('baz');
341
342
        self::assertFalse($schema->hasNamespace('foo'));
343
        self::assertFalse($schema->hasNamespace('baz'));
344
345
        $schema->createSequence('foo.bar');
346
347
        self::assertTrue($schema->hasNamespace('foo'));
348
        self::assertFalse($schema->hasNamespace('bar'));
349
350
        $schema->createSequence('`baz`.bloo');
351
352
        self::assertTrue($schema->hasNamespace('baz'));
353
        self::assertFalse($schema->hasNamespace('bloo'));
354
355
        $schema->createSequence('`baz`.moo');
356
357
        self::assertTrue($schema->hasNamespace('baz'));
358
        self::assertFalse($schema->hasNamespace('moo'));
359
    }
360
361
    /**
362
     * @group DBAL-669
363
     */
364
    public function testVisitsVisitor() : void
365
    {
366
        $schema  = new Schema();
367
        $visitor = $this->createMock(Visitor::class);
368
369
        $schema->createNamespace('foo');
370
        $schema->createNamespace('bar');
371
372
        $schema->createTable('baz');
373
        $schema->createTable('bla.bloo');
374
375
        $schema->createSequence('moo');
376
        $schema->createSequence('war');
377
378
        $visitor->expects($this->once())
379
            ->method('acceptSchema')
380
            ->with($schema);
381
382
        $visitor->expects($this->at(1))
383
            ->method('acceptTable')
384
            ->with($schema->getTable('baz'));
385
386
        $visitor->expects($this->at(2))
387
            ->method('acceptTable')
388
            ->with($schema->getTable('bla.bloo'));
389
390
        $visitor->expects($this->exactly(2))
391
            ->method('acceptTable');
392
393
        $visitor->expects($this->at(3))
394
            ->method('acceptSequence')
395
            ->with($schema->getSequence('moo'));
396
397
        $visitor->expects($this->at(4))
398
            ->method('acceptSequence')
399
            ->with($schema->getSequence('war'));
400
401
        $visitor->expects($this->exactly(2))
402
            ->method('acceptSequence');
403
404
        $schema->visit($visitor);
0 ignored issues
show
Documentation introduced by
$visitor is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\DBAL\Schema\Visitor\Visitor>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
405
    }
406
407
    /**
408
     * @group DBAL-669
409
     */
410
    public function testVisitsNamespaceVisitor() : void
411
    {
412
        $schema  = new Schema();
413
        $visitor = $this->createMock(AbstractVisitor::class);
414
415
        $schema->createNamespace('foo');
416
        $schema->createNamespace('bar');
417
418
        $schema->createTable('baz');
419
        $schema->createTable('bla.bloo');
420
421
        $schema->createSequence('moo');
422
        $schema->createSequence('war');
423
424
        $visitor->expects($this->once())
425
            ->method('acceptSchema')
426
            ->with($schema);
427
428
        $visitor->expects($this->at(1))
429
            ->method('acceptNamespace')
430
            ->with('foo');
431
432
        $visitor->expects($this->at(2))
433
            ->method('acceptNamespace')
434
            ->with('bar');
435
436
        $visitor->expects($this->at(3))
437
            ->method('acceptNamespace')
438
            ->with('bla');
439
440
        $visitor->expects($this->exactly(3))
441
            ->method('acceptNamespace');
442
443
        $visitor->expects($this->at(4))
444
            ->method('acceptTable')
445
            ->with($schema->getTable('baz'));
446
447
        $visitor->expects($this->at(5))
448
            ->method('acceptTable')
449
            ->with($schema->getTable('bla.bloo'));
450
451
        $visitor->expects($this->exactly(2))
452
            ->method('acceptTable');
453
454
        $visitor->expects($this->at(6))
455
            ->method('acceptSequence')
456
            ->with($schema->getSequence('moo'));
457
458
        $visitor->expects($this->at(7))
459
            ->method('acceptSequence')
460
            ->with($schema->getSequence('war'));
461
462
        $visitor->expects($this->exactly(2))
463
            ->method('acceptSequence');
464
465
        $schema->visit($visitor);
0 ignored issues
show
Documentation introduced by
$visitor is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\DBAL\Schema\Visitor\Visitor>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
466
467
        self::doesNotPerformAssertions(); // FIXME
0 ignored issues
show
Unused Code introduced by
The call to the method Doctrine\Tests\DBAL\Sche...sNotPerformAssertions() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
468
    }
469
}
470