Completed
Pull Request — master (#2893)
by Šimon
61:40
created

filtersSequencesDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
rs 9.2
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Schema;
4
5
use Doctrine\DBAL\Configuration;
6
use Doctrine\DBAL\Platforms\PostgreSQL100Platform;
7
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
8
use Doctrine\DBAL\Schema\PostgreSqlSchemaManager;
9
use Doctrine\DBAL\Schema\Sequence;
10
use Doctrine\DBAL\Connection;
11
use Doctrine\DBAL\Driver;
12
13
class PostgreSQLSchemaManagerTest extends \PHPUnit\Framework\TestCase
14
{
15
    /**
16
     * @var \Doctrine\DBAL\Schema\PostgreSQLSchemaManager
17
     */
18
    private $schemaManager;
19
20
    /**
21
     * @var \Doctrine\DBAL\Connection|\PHPUnit_Framework_MockObject_MockObject
22
     */
23
    private $connection;
24
25
    private function setupConnectionAndManager(string $platformClass) : void
26
    {
27
        $driverMock = $this->createMock(Driver::class);
28
        $platform   = $this->createMock($platformClass);
29
30
        $this->connection = $this->getMockBuilder(Connection::class)
31
            ->setConstructorArgs([['platform' => $platform], $driverMock])
32
            ->getMock();
33
34
        $this->schemaManager = new PostgreSqlSchemaManager($this->connection, $platform);
35
    }
36
37
    /**
38
     * @group DBAL-474
39
     * @dataProvider filtersSequencesDataProvider
40
     */
41
    public function testFiltersSequences(string $platformClass, array $sequencesData, int $fetchAssocExpectationCount) : void
42
    {
43
        $this->setupConnectionAndManager($platformClass);
44
45
        $configuration = new Configuration();
46
        $configuration->setFilterSchemaAssetsExpression('/^schema/');
47
48
        $this->connection->expects($this->any())
49
            ->method('getConfiguration')
50
            ->will($this->returnValue($configuration));
51
52
        $this->connection->expects($this->at(0))
53
            ->method('fetchAll')
54
            ->will($this->returnValue($sequencesData));
55
56
        if ($fetchAssocExpectationCount !== 0) {
57
            $this->connection->expects($this->at(3))
58
                ->method('fetchAssoc')
59
                ->will($this->returnValue(['min_value' => 1, 'increment_by' => 1]));
60
61
            $this->connection->expects($this->at(2))
62
                ->method('fetchAssoc')
63
                ->will($this->returnValue(['min_value' => 2, 'increment_by' => 2]));
64
        }
65
66
        $this->connection->expects($this->exactly($fetchAssocExpectationCount))
67
            ->method('fetchAssoc');
68
69
        self::assertEquals(
70
            [
71
                new Sequence('schema.foo', 2, 2),
72
                new Sequence('schema.bar', 1, 1),
73
            ],
74
            $this->schemaManager->listSequences('database')
75
        );
76
    }
77
78
    public function filtersSequencesDataProvider() : array
79
    {
80
        return [
81
            [
82
                'platform' => PostgreSqlPlatform::class,
83
                'sequencesData' => [
84
                    ['relname' => 'foo', 'schemaname' => 'schema'],
85
                    ['relname' => 'bar', 'schemaname' => 'schema'],
86
                    ['relname' => 'baz', 'schemaname' => ''],
87
                    ['relname' => 'bloo', 'schemaname' => 'bloo_schema'],
88
                ],
89
                'fetchAssocExpectationCount' => 2
90
            ],
91
            [
92
                'platform' => PostgreSQL100Platform::class,
93
                'sequencesData' => [
94
                    ['relname' => 'foo', 'schemaname' => 'schema', 'min_value' => 2, 'increment_by' => 2],
95
                    ['relname' => 'bar', 'schemaname' => 'schema', 'min_value' => 1, 'increment_by' => 1],
96
                    ['relname' => 'baz', 'schemaname' => '', 'min_value' => 3, 'increment_by' => 3],
97
                    ['relname' => 'bloo', 'schemaname' => 'bloo_schema', 'min_value' => 4, 'increment_by' => 4],
98
                ],
99
                'fetchAssocExpectationCount' => 0
100
            ]
101
        ];
102
    }
103
}
104