Failed Conditions
Push — develop ( c067f0...c4478a )
by Sergei
10:16
created

testSettingNullAsCallableClearsExpression()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 33
rs 9.6333
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', 'quote'])
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_)/');
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

48
        /** @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...
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);
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($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...
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->any())->method('quote');
106
        $this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue([
107
            ['name' => 'FOO'],
108
            ['name' => 'T_FOO'],
109
            ['name' => 'BAR'],
110
            ['name' => 'T_BAR'],
111
        ]));
112
113
        self::assertSame(
114
            [
115
                'T_FOO',
116
                'T_BAR',
117
            ],
118
            $this->manager->listTableNames()
119
        );
120
121
        $this->assertNull($this->conn->getConfiguration()->getFilterSchemaAssetsExpression());
122
    }
123
124
    /**
125
     * @return void
126
     */
127
    public function testSettingNullExpressionWillResetCallable()
128
    {
129
        $accepted = ['T_FOO', 'T_BAR'];
130
        $this->conn->getConfiguration()->setSchemaAssetsFilter(static function ($assetName) use ($accepted) {
131
            return in_array($assetName, $accepted);
132
        });
133
        $this->conn->expects($this->any())->method('quote');
134
        $this->conn->expects($this->atLeastOnce())->method('fetchAll')->will($this->returnValue([
135
            ['name' => 'FOO'],
136
            ['name' => 'T_FOO'],
137
            ['name' => 'BAR'],
138
            ['name' => 'T_BAR'],
139
        ]));
140
141
        self::assertSame(
142
            [
143
                'T_FOO',
144
                'T_BAR',
145
            ],
146
            $this->manager->listTableNames()
147
        );
148
149
        $this->conn->getConfiguration()->setFilterSchemaAssetsExpression(null);
150
151
        self::assertSame(
152
            [
153
                'FOO',
154
                'T_FOO',
155
                'BAR',
156
                'T_BAR',
157
            ],
158
            $this->manager->listTableNames()
159
        );
160
161
        $this->assertNull($this->conn->getConfiguration()->getSchemaAssetsFilter());
162
    }
163
164
    /**
165
     * @return void
166
     */
167
    public function testSettingNullAsCallableClearsExpression()
168
    {
169
        $filterExpression = '/^(?!T_)/';
170
        $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

170
        /** @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...
171
172
        $this->conn->expects($this->exactly(2))->method('fetchAll')->will($this->returnValue([
173
            ['name' => 'FOO'],
174
            ['name' => 'T_FOO'],
175
            ['name' => 'BAR'],
176
            ['name' => 'T_BAR'],
177
        ]));
178
179
        self::assertSame(
180
            [
181
                'FOO',
182
                'BAR',
183
            ],
184
            $this->manager->listTableNames()
185
        );
186
187
        $this->conn->getConfiguration()->setSchemaAssetsFilter(null);
188
189
        self::assertSame(
190
            [
191
                'FOO',
192
                'T_FOO',
193
                'BAR',
194
                'T_BAR',
195
            ],
196
            $this->manager->listTableNames()
197
        );
198
199
        $this->assertNull($this->conn->getConfiguration()->getFilterSchemaAssetsExpression());
200
    }
201
}
202