Completed
Pull Request — 2.9 (#3692)
by
unknown
64:38 queued 13s
created

testTablesExistWhithFilteredFalseParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 5
c 2
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()  : void
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() : void
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 testTablesExistWhithFilteredFalseParameter() : void
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(true, $this->manager->allTablesExistInSchema('T_FOO'));
0 ignored issues
show
Bug introduced by
'T_FOO' of type string is incompatible with the type array expected by parameter $tableNames of Doctrine\DBAL\Schema\Abs...llTablesExistInSchema(). ( Ignorable by Annotation )

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

121
        self::assertEquals(true, $this->manager->allTablesExistInSchema(/** @scrutinizer ignore-type */ 'T_FOO'));
Loading history...
122
    }
123
124
    /**
125
     * @return void
126
     *
127
     * @group DBAL-2701
128
     */
129
    public function testAssetFilteringSetsACallable()
130
    {
131
        $filterExpression = '/^(?!T_)/';
132
        $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

132
        /** @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...
133
        $this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue([
134
            ['name' => 'FOO'],
135
            ['name' => 'T_FOO'],
136
            ['name' => 'BAR'],
137
            ['name' => 'T_BAR'],
138
        ]));
139
140
        self::assertSame(
141
            [
142
                'FOO',
143
                'BAR',
144
            ],
145
            $this->manager->listTableNames()
146
        );
147
148
        $callable = $this->conn->getConfiguration()->getSchemaAssetsFilter();
149
        $this->assertInternalType('callable', $callable);
150
151
        // BC check: Test that regexp expression is still preserved & accessible.
152
        $this->assertEquals($filterExpression, $this->conn->getConfiguration()->getFilterSchemaAssetsExpression());
153
    }
154
155
    /**
156
     * @return void
157
     */
158
    public function testListTableNamesFiltersAssetNamesCorrectlyWithCallable()
159
    {
160
        $accepted = ['T_FOO', 'T_BAR'];
161
        $this->conn->getConfiguration()->setSchemaAssetsFilter(static function ($assetName) use ($accepted) {
162
            return in_array($assetName, $accepted);
163
        });
164
        $this->conn->expects($this->any())->method('quote');
165
        $this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue([
166
            ['name' => 'FOO'],
167
            ['name' => 'T_FOO'],
168
            ['name' => 'BAR'],
169
            ['name' => 'T_BAR'],
170
        ]));
171
172
        self::assertSame(
173
            [
174
                'T_FOO',
175
                'T_BAR',
176
            ],
177
            $this->manager->listTableNames()
178
        );
179
180
        $this->assertNull($this->conn->getConfiguration()->getFilterSchemaAssetsExpression());
181
    }
182
183
    /**
184
     * @return void
185
     */
186
    public function testSettingNullExpressionWillResetCallable()
187
    {
188
        $accepted = ['T_FOO', 'T_BAR'];
189
        $this->conn->getConfiguration()->setSchemaAssetsFilter(static function ($assetName) use ($accepted) {
190
            return in_array($assetName, $accepted);
191
        });
192
        $this->conn->expects($this->any())->method('quote');
193
        $this->conn->expects($this->atLeastOnce())->method('fetchAll')->will($this->returnValue([
194
            ['name' => 'FOO'],
195
            ['name' => 'T_FOO'],
196
            ['name' => 'BAR'],
197
            ['name' => 'T_BAR'],
198
        ]));
199
200
        self::assertSame(
201
            [
202
                'T_FOO',
203
                'T_BAR',
204
            ],
205
            $this->manager->listTableNames()
206
        );
207
208
        $this->conn->getConfiguration()->setFilterSchemaAssetsExpression(null);
209
210
        self::assertSame(
211
            [
212
                'FOO',
213
                'T_FOO',
214
                'BAR',
215
                'T_BAR',
216
            ],
217
            $this->manager->listTableNames()
218
        );
219
220
        $this->assertNull($this->conn->getConfiguration()->getSchemaAssetsFilter());
221
    }
222
223
    /**
224
     * @return void
225
     */
226
    public function testSettingNullAsCallableClearsExpression()
227
    {
228
        $filterExpression = '/^(?!T_)/';
229
        $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

229
        /** @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...
230
231
        $this->conn->expects($this->exactly(2))->method('fetchAll')->will($this->returnValue([
232
            ['name' => 'FOO'],
233
            ['name' => 'T_FOO'],
234
            ['name' => 'BAR'],
235
            ['name' => 'T_BAR'],
236
        ]));
237
238
        self::assertSame(
239
            [
240
                'FOO',
241
                'BAR',
242
            ],
243
            $this->manager->listTableNames()
244
        );
245
246
        $this->conn->getConfiguration()->setSchemaAssetsFilter(null);
247
248
        self::assertSame(
249
            [
250
                'FOO',
251
                'T_FOO',
252
                'BAR',
253
                'T_BAR',
254
            ],
255
            $this->manager->listTableNames()
256
        );
257
258
        $this->assertNull($this->conn->getConfiguration()->getFilterSchemaAssetsExpression());
259
    }
260
}
261