Completed
Pull Request — master (#3610)
by Sergei
03:03
created

SchemaDiffTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 111
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testSchemaDiffToSql() 0 11 1
A testSchemaDiffToSaveSql() 0 11 1
B createPlatform() 0 61 4
A createSchemaDiff() 0 19 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\DBAL\Schema;
6
7
use Doctrine\DBAL\Platforms\AbstractPlatform;
8
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
9
use Doctrine\DBAL\Schema\SchemaDiff;
10
use Doctrine\DBAL\Schema\Sequence;
11
use Doctrine\DBAL\Schema\Table;
12
use Doctrine\DBAL\Schema\TableDiff;
13
use PHPUnit\Framework\MockObject\MockObject;
14
use PHPUnit\Framework\TestCase;
15
16
class SchemaDiffTest extends TestCase
17
{
18
    public function testSchemaDiffToSql() : void
19
    {
20
        $diff     = $this->createSchemaDiff();
21
        $platform = $this->createPlatform(true);
22
23
        $sql = $diff->toSql($platform);
0 ignored issues
show
Bug introduced by
It seems like $platform defined by $this->createPlatform(true) on line 21 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Doctrine\DBAL\Schema\SchemaDiff::toSql() does only seem to accept object<Doctrine\DBAL\Platforms\AbstractPlatform>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
24
25
        $expected = ['create_schema', 'drop_orphan_fk', 'alter_seq', 'drop_seq', 'create_seq', 'create_table', 'create_foreign_key', 'drop_table', 'alter_table'];
26
27
        self::assertEquals($expected, $sql);
28
    }
29
30
    public function testSchemaDiffToSaveSql() : void
31
    {
32
        $diff     = $this->createSchemaDiff();
33
        $platform = $this->createPlatform(false);
34
35
        $sql = $diff->toSaveSql($platform);
0 ignored issues
show
Bug introduced by
It seems like $platform defined by $this->createPlatform(false) on line 33 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Doctrine\DBAL\Schema\SchemaDiff::toSaveSql() does only seem to accept object<Doctrine\DBAL\Platforms\AbstractPlatform>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
36
37
        $expected = ['create_schema', 'alter_seq', 'create_seq', 'create_table', 'create_foreign_key', 'alter_table'];
38
39
        self::assertEquals($expected, $sql);
40
    }
41
42
    /**
43
     * @return AbstractPlatform|MockObject
44
     */
45
    private function createPlatform(bool $unsafe)
46
    {
47
        /** @var AbstractPlatform|MockObject $platform */
48
        $platform = $this->createMock(AbstractPlatform::class);
49
        $platform->expects($this->exactly(1))
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Doctrine\DBAL\Platforms\AbstractPlatform.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
50
            ->method('getCreateSchemaSQL')
51
            ->with('foo_ns')
52
            ->will($this->returnValue('create_schema'));
53
        if ($unsafe) {
54
            $platform->expects($this->exactly(1))
55
                 ->method('getDropSequenceSql')
56
                 ->with($this->isInstanceOf(Sequence::class))
57
                 ->will($this->returnValue('drop_seq'));
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
        $platform->expects($this->exactly(1))
74
                 ->method('getCreateTableSql')
75
                 ->with($this->isInstanceOf(Table::class))
76
                 ->will($this->returnValue(['create_table']));
77
        $platform->expects($this->exactly(1))
78
                 ->method('getCreateForeignKeySQL')
79
                 ->with($this->isInstanceOf(ForeignKeyConstraint::class))
80
                 ->will($this->returnValue('create_foreign_key'));
81
        $platform->expects($this->exactly(1))
82
                 ->method('getAlterTableSql')
83
                 ->with($this->isInstanceOf(TableDiff::class))
84
                 ->will($this->returnValue(['alter_table']));
85
        if ($unsafe) {
86
            $platform->expects($this->exactly(1))
87
                     ->method('getDropForeignKeySql')
88
                     ->with(
89
                         $this->isInstanceOf(ForeignKeyConstraint::class),
90
                         $this->isInstanceOf(Table::class)
91
                     )
92
                     ->will($this->returnValue('drop_orphan_fk'));
93
        }
94
        $platform->expects($this->exactly(1))
95
                ->method('supportsSchemas')
96
                ->will($this->returnValue(true));
97
        $platform->expects($this->exactly(1))
98
                ->method('supportsSequences')
99
                ->will($this->returnValue(true));
100
        $platform->expects($this->exactly(2))
101
                ->method('supportsForeignKeyConstraints')
102
                ->will($this->returnValue(true));
103
104
        return $platform;
105
    }
106
107
    public function createSchemaDiff() : SchemaDiff
108
    {
109
        $diff                              = new SchemaDiff();
110
        $diff->newNamespaces['foo_ns']     = 'foo_ns';
111
        $diff->removedNamespaces['bar_ns'] = 'bar_ns';
112
        $diff->changedSequences[]          = new Sequence('foo_seq');
113
        $diff->newSequences[]              = new Sequence('bar_seq');
114
        $diff->removedSequences[]          = new Sequence('baz_seq');
115
        $diff->newTables['foo_table']      = new Table('foo_table');
116
        $diff->removedTables['bar_table']  = new Table('bar_table');
117
        $diff->changedTables['baz_table']  = new TableDiff('baz_table');
118
        $diff->newTables['foo_table']->addColumn('foreign_id', 'integer');
119
        $diff->newTables['foo_table']->addForeignKeyConstraint('foreign_table', ['foreign_id'], ['id']);
120
        $fk = new ForeignKeyConstraint(['id'], 'foreign_table', ['id']);
121
        $fk->setLocalTable(new Table('local_table'));
122
        $diff->orphanedForeignKeys[] = $fk;
123
124
        return $diff;
125
    }
126
}
127