Completed
Push — master ( ed25dc...18a804 )
by Sergei
35:26 queued 32:37
created

DB2SchemaManagerTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 11
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Schema;
4
5
use Doctrine\Common\EventManager;
6
use Doctrine\DBAL\Configuration;
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\Driver;
9
use Doctrine\DBAL\Platforms\DB2Platform;
10
use Doctrine\DBAL\Schema\DB2SchemaManager;
11
use PHPUnit\Framework\TestCase;
12
use PHPUnit_Framework_MockObject_MockObject;
13
use function in_array;
14
15
/**
16
 * @covers \Doctrine\DBAL\Schema\DB2SchemaManager
17
 */
18
final class DB2SchemaManagerTest extends TestCase
19
{
20
    /** @var Connection|PHPUnit_Framework_MockObject_MockObject */
21
    private $conn;
22
23
    /** @var DB2SchemaManager */
24
    private $manager;
25
26
    protected function setUp()
27
    {
28
        $eventManager  = new EventManager();
29
        $driverMock    = $this->createMock(Driver::class);
30
        $platform      = $this->createMock(DB2Platform::class);
31
        $this->conn    = $this
32
            ->getMockBuilder(Connection::class)
33
            ->setMethods(['fetchAll'])
34
            ->setConstructorArgs([['platform' => $platform], $driverMock, new Configuration(), $eventManager])
35
            ->getMock();
36
        $this->manager = new DB2SchemaManager($this->conn);
37
    }
38
39
    /**
40
     * @see https://github.com/doctrine/dbal/issues/2701
41
     *
42
     * @return void
43
     *
44
     * @group DBAL-2701
45
     */
46
    public function testListTableNamesFiltersAssetNamesCorrectly()
47
    {
48
        $this->conn->getConfiguration()->setFilterSchemaAssetsExpression('/^(?!T_)/');
49
        $this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue([
50
            ['name' => 'FOO'],
51
            ['name' => 'T_FOO'],
52
            ['name' => 'BAR'],
53
            ['name' => 'T_BAR'],
54
        ]));
55
56
        self::assertSame(
57
            [
58
                'FOO',
59
                'BAR',
60
            ],
61
            $this->manager->listTableNames()
62
        );
63
    }
64
65
    /**
66
     * @return void
67
     *
68
     * @group DBAL-2701
69
     */
70
    public function testAssetFilteringSetsACallable()
71
    {
72
        $filterExpression = '/^(?!T_)/';
73
        $this->conn->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression);
74
        $this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue([
75
            ['name' => 'FOO'],
76
            ['name' => 'T_FOO'],
77
            ['name' => 'BAR'],
78
            ['name' => 'T_BAR'],
79
        ]));
80
81
        self::assertSame(
82
            [
83
                'FOO',
84
                'BAR',
85
            ],
86
            $this->manager->listTableNames()
87
        );
88
89
        $callable = $this->conn->getConfiguration()->getSchemaAssetsFilter();
90
        $this->assertInternalType('callable', $callable);
91
92
        // BC check: Test that regexp expression is still preserved & accessible.
93
        $this->assertEquals($filterExpression, $this->conn->getConfiguration()->getFilterSchemaAssetsExpression());
94
    }
95
96
    /**
97
     * @return void
98
     */
99
    public function testListTableNamesFiltersAssetNamesCorrectlyWithCallable()
100
    {
101
        $accepted = ['T_FOO', 'T_BAR'];
102
        $this->conn->getConfiguration()->setSchemaAssetsFilter(static function ($assetName) use ($accepted) {
103
            return in_array($assetName, $accepted);
104
        });
105
        $this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue([
106
            ['name' => 'FOO'],
107
            ['name' => 'T_FOO'],
108
            ['name' => 'BAR'],
109
            ['name' => 'T_BAR'],
110
        ]));
111
112
        self::assertSame(
113
            [
114
                'T_FOO',
115
                'T_BAR',
116
            ],
117
            $this->manager->listTableNames()
118
        );
119
120
        $this->assertNull($this->conn->getConfiguration()->getFilterSchemaAssetsExpression());
121
    }
122
123
    /**
124
     * @return void
125
     */
126
    public function testSettingNullExpressionWillResetCallable()
127
    {
128
        $accepted = ['T_FOO', 'T_BAR'];
129
        $this->conn->getConfiguration()->setSchemaAssetsFilter(static function ($assetName) use ($accepted) {
130
            return in_array($assetName, $accepted);
131
        });
132
        $this->conn->expects($this->atLeastOnce())->method('fetchAll')->will($this->returnValue([
133
            ['name' => 'FOO'],
134
            ['name' => 'T_FOO'],
135
            ['name' => 'BAR'],
136
            ['name' => 'T_BAR'],
137
        ]));
138
139
        self::assertSame(
140
            [
141
                'T_FOO',
142
                'T_BAR',
143
            ],
144
            $this->manager->listTableNames()
145
        );
146
147
        $this->conn->getConfiguration()->setFilterSchemaAssetsExpression(null);
148
149
        self::assertSame(
150
            [
151
                'FOO',
152
                'T_FOO',
153
                'BAR',
154
                'T_BAR',
155
            ],
156
            $this->manager->listTableNames()
157
        );
158
159
        $this->assertNull($this->conn->getConfiguration()->getSchemaAssetsFilter());
160
    }
161
162
    /**
163
     * @return void
164
     */
165
    public function testSettingNullAsCallableClearsExpression()
166
    {
167
        $filterExpression = '/^(?!T_)/';
168
        $this->conn->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression);
169
170
        $this->conn->expects($this->exactly(2))->method('fetchAll')->will($this->returnValue([
171
            ['name' => 'FOO'],
172
            ['name' => 'T_FOO'],
173
            ['name' => 'BAR'],
174
            ['name' => 'T_BAR'],
175
        ]));
176
177
        self::assertSame(
178
            [
179
                'FOO',
180
                'BAR',
181
            ],
182
            $this->manager->listTableNames()
183
        );
184
185
        $this->conn->getConfiguration()->setSchemaAssetsFilter(null);
186
187
        self::assertSame(
188
            [
189
                'FOO',
190
                'T_FOO',
191
                'BAR',
192
                'T_BAR',
193
            ],
194
            $this->manager->listTableNames()
195
        );
196
197
        $this->assertNull($this->conn->getConfiguration()->getFilterSchemaAssetsExpression());
198
    }
199
}
200