Failed Conditions
Pull Request — master (#2893)
by Šimon
64:55
created

setupConnectionAndManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Schema;
4
5
use Doctrine\DBAL\Configuration;
6
use Doctrine\DBAL\Schema\PostgreSqlSchemaManager;
7
use Doctrine\DBAL\Schema\Sequence;
8
9
class PostgreSQLSchemaManagerTest extends \PHPUnit\Framework\TestCase
10
{
11
    /**
12
     * @var \Doctrine\DBAL\Schema\PostgreSQLSchemaManager
13
     */
14
    private $schemaManager;
15
16
    /**
17
     * @var \Doctrine\DBAL\Connection|\PHPUnit_Framework_MockObject_MockObject
18
     */
19
    private $connection;
20
21
    private function setupConnectionAndManager($platformClass)
22
    {
23
        $driverMock = $this->createMock('Doctrine\DBAL\Driver');
24
        $platform = $this->createMock($platformClass);
25
26
        $this->connection = $this->getMockBuilder('Doctrine\DBAL\Connection')
27
            ->setConstructorArgs([['platform' => $platform], $driverMock])
28
            ->getMock();
29
30
        $this->schemaManager = new PostgreSqlSchemaManager($this->connection, $platform);
31
    }
32
33
    /**
34
     * @group DBAL-474
35
     * @dataProvider filtersSequencesDataProvider
36
     */
37
    public function testFiltersSequences(string $platformClass)
38
    {
39
        $this->setupConnectionAndManager($platformClass);
40
41
        $configuration = new Configuration();
42
        $configuration->setFilterSchemaAssetsExpression('/^schema/');
43
44
        $sequences = [
45
            ['relname' => 'foo', 'schemaname' => 'schema'],
46
            ['relname' => 'bar', 'schemaname' => 'schema'],
47
            ['relname' => 'baz', 'schemaname' => ''],
48
            ['relname' => 'bloo', 'schemaname' => 'bloo_schema'],
49
        ];
50
51
52
        $this->connection->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\DBAL\Connection.

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...
53
            ->method('getConfiguration')
54
            ->will($this->returnValue($configuration));
55
56
        $this->connection->expects($this->at(0))
57
            ->method('fetchAll')
58
            ->will($this->returnValue($sequences));
59
60
        $this->connection->expects($this->at(2))
61
            ->method('fetchAll')
62
            ->will($this->returnValue([['min_value' => 1, 'increment_by' => 1]]));
63
64
        $this->connection->expects($this->at(3))
65
            ->method('fetchAll')
66
            ->will($this->returnValue([['min_value' => 2, 'increment_by' => 2]]));
67
68
        $this->connection->expects($this->exactly(3))
69
            ->method('fetchAll');
70
71
        self::assertEquals(
72
            [
73
                new Sequence('schema.foo', 1, 1),
74
                new Sequence('schema.bar', 2, 2),
75
            ],
76
            $this->schemaManager->listSequences('database')
77
        );
78
    }
79
80
    public function filtersSequencesDataProvider()
81
    {
82
        return [
83
            ['Doctrine\DBAL\Platforms\PostgreSqlPlatform'],
84
            ['Doctrine\DBAL\Platforms\PostgreSQL100Platform']
85
        ];
86
    }
87
}
88