Completed
Pull Request — 2.9 (#3692)
by
unknown
61:17 queued 10s
created

testTablesExistWhithFilteredTrueParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
rs 10
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
14
use function in_array;
15
16
/**
17
 * @covers \Doctrine\DBAL\Schema\DB2SchemaManager
18
 */
19
final class DB2SchemaManagerTest extends TestCase
20
{
21
    /** @var Connection|PHPUnit_Framework_MockObject_MockObject */
22
    private $conn;
23
24
    /** @var DB2SchemaManager */
25
    private $manager;
26
27
    protected function setUp()
28
    {
29
        $eventManager  = new EventManager();
30
        $driverMock    = $this->createMock(Driver::class);
31
        $platform      = $this->createMock(DB2Platform::class);
32
        $this->conn    = $this
33
            ->getMockBuilder(Connection::class)
34
            ->setMethods(['fetchAll', 'quote'])
35
            ->setConstructorArgs([['platform' => $platform], $driverMock, new Configuration(), $eventManager])
36
            ->getMock();
37
        $this->manager = new DB2SchemaManager($this->conn);
38
    }
39
40
    /**
41
     * @see https://github.com/doctrine/dbal/issues/2701
42
     *
43
     * @return void
44
     *
45
     * @group DBAL-2701
46
     */
47
    public function testListTableNamesFiltersAssetNamesCorrectly()
48
    {
49
        $this->conn->getConfiguration()->setFilterSchemaAssetsExpression('/^(?!T_)/');
1 ignored issue
show
Deprecated Code introduced by
The function Doctrine\DBAL\Configurat...chemaAssetsExpression() has been deprecated: Use Configuration::setSchemaAssetsFilter() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

49
        /** @scrutinizer ignore-deprecated */ $this->conn->getConfiguration()->setFilterSchemaAssetsExpression('/^(?!T_)/');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
50
        $this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue([
51
            ['name' => 'FOO'],
52
            ['name' => 'T_FOO'],
53
            ['name' => 'BAR'],
54
            ['name' => 'T_BAR'],
55
        ]));
56
57
        self::assertSame(
58
            [
59
                'FOO',
60
                'BAR',
61
            ],
62
            $this->manager->listTableNames()
63
        );
64
    }
65
66
    /**
67
     * @return void
68
     *
69
     * @group DBAL-3692
70
     */
71
    public function testListAllTableNamesFiltersAssetNamesCorrectly()
72
    {
73
        $this->conn->getConfiguration()->setFilterSchemaAssetsExpression('/^(?!T_)/');
1 ignored issue
show
Deprecated Code introduced by
The function Doctrine\DBAL\Configurat...chemaAssetsExpression() has been deprecated: Use Configuration::setSchemaAssetsFilter() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

73
        /** @scrutinizer ignore-deprecated */ $this->conn->getConfiguration()->setFilterSchemaAssetsExpression('/^(?!T_)/');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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
                'T_FOO',
85
                'BAR',
86
                'T_BAR',
87
            ],
88
            $this->manager->listAllTableNames()
89
        );
90
    }
91
92
    /**
93
     * @return void
94
     *
95
     * @group DBAL-3692
96
     */
97
    public function testTablesExistWithoutFilteredParameter()
98
    {
99
        $this->conn->getConfiguration()->setFilterSchemaAssetsExpression('/^(?!T_)/');
1 ignored issue
show
Deprecated Code introduced by
The function Doctrine\DBAL\Configurat...chemaAssetsExpression() has been deprecated: Use Configuration::setSchemaAssetsFilter() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

99
        /** @scrutinizer ignore-deprecated */ $this->conn->getConfiguration()->setFilterSchemaAssetsExpression('/^(?!T_)/');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
100
        $this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue([
101
            ['name' => 'FOO'],
102
            ['name' => 'T_FOO'],
103
        ]));
104
105
        self::assertEquals(false, $this->manager->tablesExist('T_FOO'));
106
    }
107
108
    /**
109
     * @return void
110
     *
111
     * @group DBAL-3692
112
     */
113
    public function testTablesExistWhithFilteredTrueParameter()
114
    {
115
        $this->conn->getConfiguration()->setFilterSchemaAssetsExpression('/^(?!T_)/');
1 ignored issue
show
Deprecated Code introduced by
The function Doctrine\DBAL\Configurat...chemaAssetsExpression() has been deprecated: Use Configuration::setSchemaAssetsFilter() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

115
        /** @scrutinizer ignore-deprecated */ $this->conn->getConfiguration()->setFilterSchemaAssetsExpression('/^(?!T_)/');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
116
        $this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue([
117
            ['name' => 'FOO'],
118
            ['name' => 'T_FOO'],
119
        ]));
120
121
        self::assertEquals(false, $this->manager->tablesExist('T_FOO', true));
122
    }
123
124
    /**
125
     * @return void
126
     *
127
     * @group DBAL-3692
128
     */
129
    public function testTablesExistWhithFilteredFalseParameter()
130
    {
131
        $this->conn->getConfiguration()->setFilterSchemaAssetsExpression('/^(?!T_)/');
1 ignored issue
show
Deprecated Code introduced by
The function Doctrine\DBAL\Configurat...chemaAssetsExpression() has been deprecated: Use Configuration::setSchemaAssetsFilter() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

131
        /** @scrutinizer ignore-deprecated */ $this->conn->getConfiguration()->setFilterSchemaAssetsExpression('/^(?!T_)/');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
132
        $this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue([
133
            ['name' => 'FOO'],
134
            ['name' => 'T_FOO'],
135
        ]));
136
137
        self::assertEquals(true, $this->manager->tablesExist('T_FOO', false));
138
    }
139
140
    /**
141
     * @return void
142
     *
143
     * @group DBAL-2701
144
     */
145
    public function testAssetFilteringSetsACallable()
146
    {
147
        $filterExpression = '/^(?!T_)/';
148
        $this->conn->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression);
1 ignored issue
show
Deprecated Code introduced by
The function Doctrine\DBAL\Configurat...chemaAssetsExpression() has been deprecated: Use Configuration::setSchemaAssetsFilter() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

148
        /** @scrutinizer ignore-deprecated */ $this->conn->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
149
        $this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue([
150
            ['name' => 'FOO'],
151
            ['name' => 'T_FOO'],
152
            ['name' => 'BAR'],
153
            ['name' => 'T_BAR'],
154
        ]));
155
156
        self::assertSame(
157
            [
158
                'FOO',
159
                'BAR',
160
            ],
161
            $this->manager->listTableNames()
162
        );
163
164
        $callable = $this->conn->getConfiguration()->getSchemaAssetsFilter();
165
        $this->assertInternalType('callable', $callable);
166
167
        // BC check: Test that regexp expression is still preserved & accessible.
168
        $this->assertEquals($filterExpression, $this->conn->getConfiguration()->getFilterSchemaAssetsExpression());
169
    }
170
171
    /**
172
     * @return void
173
     */
174
    public function testListTableNamesFiltersAssetNamesCorrectlyWithCallable()
175
    {
176
        $accepted = ['T_FOO', 'T_BAR'];
177
        $this->conn->getConfiguration()->setSchemaAssetsFilter(static function ($assetName) use ($accepted) {
178
            return in_array($assetName, $accepted);
179
        });
180
        $this->conn->expects($this->any())->method('quote');
181
        $this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue([
182
            ['name' => 'FOO'],
183
            ['name' => 'T_FOO'],
184
            ['name' => 'BAR'],
185
            ['name' => 'T_BAR'],
186
        ]));
187
188
        self::assertSame(
189
            [
190
                'T_FOO',
191
                'T_BAR',
192
            ],
193
            $this->manager->listTableNames()
194
        );
195
196
        $this->assertNull($this->conn->getConfiguration()->getFilterSchemaAssetsExpression());
197
    }
198
199
    /**
200
     * @return void
201
     */
202
    public function testSettingNullExpressionWillResetCallable()
203
    {
204
        $accepted = ['T_FOO', 'T_BAR'];
205
        $this->conn->getConfiguration()->setSchemaAssetsFilter(static function ($assetName) use ($accepted) {
206
            return in_array($assetName, $accepted);
207
        });
208
        $this->conn->expects($this->any())->method('quote');
209
        $this->conn->expects($this->atLeastOnce())->method('fetchAll')->will($this->returnValue([
210
            ['name' => 'FOO'],
211
            ['name' => 'T_FOO'],
212
            ['name' => 'BAR'],
213
            ['name' => 'T_BAR'],
214
        ]));
215
216
        self::assertSame(
217
            [
218
                'T_FOO',
219
                'T_BAR',
220
            ],
221
            $this->manager->listTableNames()
222
        );
223
224
        $this->conn->getConfiguration()->setFilterSchemaAssetsExpression(null);
225
226
        self::assertSame(
227
            [
228
                'FOO',
229
                'T_FOO',
230
                'BAR',
231
                'T_BAR',
232
            ],
233
            $this->manager->listTableNames()
234
        );
235
236
        $this->assertNull($this->conn->getConfiguration()->getSchemaAssetsFilter());
237
    }
238
239
    /**
240
     * @return void
241
     */
242
    public function testSettingNullAsCallableClearsExpression()
243
    {
244
        $filterExpression = '/^(?!T_)/';
245
        $this->conn->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression);
1 ignored issue
show
Deprecated Code introduced by
The function Doctrine\DBAL\Configurat...chemaAssetsExpression() has been deprecated: Use Configuration::setSchemaAssetsFilter() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

245
        /** @scrutinizer ignore-deprecated */ $this->conn->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
246
247
        $this->conn->expects($this->exactly(2))->method('fetchAll')->will($this->returnValue([
248
            ['name' => 'FOO'],
249
            ['name' => 'T_FOO'],
250
            ['name' => 'BAR'],
251
            ['name' => 'T_BAR'],
252
        ]));
253
254
        self::assertSame(
255
            [
256
                'FOO',
257
                'BAR',
258
            ],
259
            $this->manager->listTableNames()
260
        );
261
262
        $this->conn->getConfiguration()->setSchemaAssetsFilter(null);
263
264
        self::assertSame(
265
            [
266
                'FOO',
267
                'T_FOO',
268
                'BAR',
269
                'T_BAR',
270
            ],
271
            $this->manager->listTableNames()
272
        );
273
274
        $this->assertNull($this->conn->getConfiguration()->getFilterSchemaAssetsExpression());
275
    }
276
}
277