Failed Conditions
Pull Request — 2.10.x (#3983)
by Grégoire
63:38
created

SchemaDiffTest::createPlatform()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 63
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 53
dl 0
loc 63
rs 8.7143
c 0
b 0
f 0
cc 5
nc 8
nop 1

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
namespace Doctrine\Tests\DBAL\Schema;
4
5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
7
use Doctrine\DBAL\Schema\SchemaDiff;
8
use Doctrine\DBAL\Schema\Sequence;
9
use Doctrine\DBAL\Schema\Table;
10
use Doctrine\DBAL\Schema\TableDiff;
11
use PHPUnit\Framework\MockObject\MockObject;
12
use PHPUnit\Framework\TestCase;
13
use function assert;
14
15
class SchemaDiffTest extends TestCase
16
{
17
    public function testSchemaDiffToSql() : void
18
    {
19
        $diff     = $this->createSchemaDiff();
20
        $platform = $this->createPlatform(true);
21
22
        $sql = $diff->toSql($platform);
23
24
        $expected = ['create_schema', 'drop_orphan_fk', 'alter_seq', 'drop_seq', 'create_seq', 'create_table', 'create_foreign_key', 'drop_table', 'alter_table'];
25
26
        self::assertEquals($expected, $sql);
27
    }
28
29
    public function testSchemaDiffToSaveSql() : void
30
    {
31
        $diff     = $this->createSchemaDiff();
32
        $platform = $this->createPlatform(false);
33
34
        $sql = $diff->toSaveSql($platform);
35
36
        $expected = ['create_schema', 'alter_seq', 'create_seq', 'create_table', 'create_foreign_key', 'alter_table'];
37
38
        self::assertEquals($expected, $sql);
39
    }
40
41
    /**
42
     * @return AbstractPlatform|MockObject
43
     */
44
    private function createPlatform(bool $unsafe)
45
    {
46
        $platform = $this->createMock(AbstractPlatform::class);
47
        assert($platform instanceof AbstractPlatform || $platform instanceof MockObject);
48
        $platform->expects($this->exactly(1))
49
            ->method('getCreateSchemaSQL')
50
            ->with('foo_ns')
51
            ->will($this->returnValue('create_schema'));
52
        if ($unsafe) {
53
            $platform->expects($this->exactly(1))
54
                 ->method('getDropSequenceSql')
55
                 ->with($this->isInstanceOf(Sequence::class))
56
                 ->will($this->returnValue('drop_seq'));
57
        }
58
59
        $platform->expects($this->exactly(1))
60
                 ->method('getAlterSequenceSql')
61
                 ->with($this->isInstanceOf(Sequence::class))
62
                 ->will($this->returnValue('alter_seq'));
63
        $platform->expects($this->exactly(1))
64
                 ->method('getCreateSequenceSql')
65
                 ->with($this->isInstanceOf(Sequence::class))
66
                 ->will($this->returnValue('create_seq'));
67
        if ($unsafe) {
68
            $platform->expects($this->exactly(1))
69
                     ->method('getDropTableSql')
70
                     ->with($this->isInstanceOf(Table::class))
71
                     ->will($this->returnValue('drop_table'));
72
        }
73
74
        $platform->expects($this->exactly(1))
75
                 ->method('getCreateTableSql')
76
                 ->with($this->isInstanceOf(Table::class))
77
                 ->will($this->returnValue(['create_table']));
78
        $platform->expects($this->exactly(1))
79
                 ->method('getCreateForeignKeySQL')
80
                 ->with($this->isInstanceOf(ForeignKeyConstraint::class))
81
                 ->will($this->returnValue('create_foreign_key'));
82
        $platform->expects($this->exactly(1))
83
                 ->method('getAlterTableSql')
84
                 ->with($this->isInstanceOf(TableDiff::class))
85
                 ->will($this->returnValue(['alter_table']));
86
        if ($unsafe) {
87
            $platform->expects($this->exactly(1))
88
                     ->method('getDropForeignKeySql')
89
                     ->with(
90
                         $this->isInstanceOf(ForeignKeyConstraint::class),
91
                         $this->isInstanceOf(Table::class)
92
                     )
93
                     ->will($this->returnValue('drop_orphan_fk'));
94
        }
95
96
        $platform->expects($this->exactly(1))
97
                ->method('supportsSchemas')
98
                ->will($this->returnValue(true));
99
        $platform->expects($this->exactly(1))
100
                ->method('supportsSequences')
101
                ->will($this->returnValue(true));
102
        $platform->expects($this->exactly(2))
103
                ->method('supportsForeignKeyConstraints')
104
                ->will($this->returnValue(true));
105
106
        return $platform;
107
    }
108
109
    public function createSchemaDiff() : SchemaDiff
110
    {
111
        $diff                              = new SchemaDiff();
112
        $diff->newNamespaces['foo_ns']     = 'foo_ns';
113
        $diff->removedNamespaces['bar_ns'] = 'bar_ns';
114
        $diff->changedSequences['foo_seq'] = new Sequence('foo_seq');
115
        $diff->newSequences['bar_seq']     = new Sequence('bar_seq');
116
        $diff->removedSequences['baz_seq'] = new Sequence('baz_seq');
117
        $diff->newTables['foo_table']      = new Table('foo_table');
118
        $diff->removedTables['bar_table']  = new Table('bar_table');
119
        $diff->changedTables['baz_table']  = new TableDiff('baz_table');
120
        $diff->newTables['foo_table']->addColumn('foreign_id', 'integer');
121
        $diff->newTables['foo_table']->addForeignKeyConstraint('foreign_table', ['foreign_id'], ['id']);
122
        $fk = new ForeignKeyConstraint(['id'], 'foreign_table', ['id']);
123
        $fk->setLocalTable(new Table('local_table'));
124
        $diff->orphanedForeignKeys[] = $fk;
125
126
        return $diff;
127
    }
128
}
129