Passed
Pull Request — 2.9 (#3692)
by
unknown
75:50 queued 13:38
created

testTablesExistWithoutFilteredParameter()   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\MockObject\MockObject;
12
use PHPUnit\Framework\TestCase;
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|MockObject */
22
    private $conn;
23
24
    /** @var DB2SchemaManager */
25
    private $manager;
26
27
    protected function setUp() : void
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
            ->onlyMethods(['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
     * @group DBAL-2701
44
     */
45
    public function testListTableNamesFiltersAssetNamesCorrectly() : void
46
    {
47
        $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

47
        /** @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...
48
        $this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue([
49
            ['name' => 'FOO'],
50
            ['name' => 'T_FOO'],
51
            ['name' => 'BAR'],
52
            ['name' => 'T_BAR'],
53
        ]));
54
55
        self::assertSame(
56
            [
57
                'FOO',
58
                'BAR',
59
            ],
60
            $this->manager->listTableNames()
61
        );
62
    }
63
64
    /**
65
     * @return void
66
     *
67
     * @group DBAL-3692
68
     */
69
    public function testListAllTableNamesFiltersAssetNamesCorrectly()  : void
70
    {
71
        $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

71
        /** @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...
72
        $this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue([
73
            ['name' => 'FOO'],
74
            ['name' => 'T_FOO'],
75
            ['name' => 'BAR'],
76
            ['name' => 'T_BAR'],
77
        ]));
78
79
        self::assertSame(
80
            [
81
                'FOO',
82
                'T_FOO',
83
                'BAR',
84
                'T_BAR',
85
            ],
86
            $this->manager->listAllTableNames()
87
        );
88
    }
89
90
    /**
91
     * @return void
92
     *
93
     * @group DBAL-3692
94
     */
95
    public function testTablesExistWithoutFilteredParameter() : void
96
    {
97
        $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

97
        /** @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...
98
        $this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue([
99
            ['name' => 'FOO'],
100
            ['name' => 'T_FOO'],
101
        ]));
102
103
        self::assertEquals(false, $this->manager->tablesExist('T_FOO'));
104
    }
105
106
    /**
107
     * @return void
108
     *
109
     * @group DBAL-3692
110
     */
111
    public function testTablesExistWhithFilteredFalseParameter() : void
112
    {
113
        $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

113
        /** @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...
114
        $this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue([
115
            ['name' => 'FOO'],
116
            ['name' => 'T_FOO'],
117
        ]));
118
119
        self::assertEquals(true, $this->manager->allTablesExistInSchema(['T_FOO']));
120
    }
121
122
    /**
123
     * @group DBAL-2701
124
     */
125
    public function testAssetFilteringSetsACallable() : void
126
    {
127
        $filterExpression = '/^(?!T_)/';
128
        $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

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

216
        /** @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...
217
218
        $this->conn->expects($this->exactly(2))->method('fetchAll')->will($this->returnValue([
219
            ['name' => 'FOO'],
220
            ['name' => 'T_FOO'],
221
            ['name' => 'BAR'],
222
            ['name' => 'T_BAR'],
223
        ]));
224
225
        self::assertSame(
226
            [
227
                'FOO',
228
                'BAR',
229
            ],
230
            $this->manager->listTableNames()
231
        );
232
233
        $this->conn->getConfiguration()->setSchemaAssetsFilter(null);
234
235
        self::assertSame(
236
            [
237
                'FOO',
238
                'T_FOO',
239
                'BAR',
240
                'T_BAR',
241
            ],
242
            $this->manager->listTableNames()
243
        );
244
245
        $this->assertNull($this->conn->getConfiguration()->getFilterSchemaAssetsExpression());
246
    }
247
}
248