Passed
Push — phpstan-tests ( 6a2b78...974220 )
by Michael
61:35 queued 23s
created

testSettingNullExpressionWillResetCallable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 35
rs 9.6
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\DBAL\Schema;
6
7
use Doctrine\Common\EventManager;
8
use Doctrine\DBAL\Configuration;
9
use Doctrine\DBAL\Connection;
10
use Doctrine\DBAL\Driver;
11
use Doctrine\DBAL\Platforms\DB2Platform;
12
use Doctrine\DBAL\Schema\DB2SchemaManager;
13
use PHPUnit\Framework\MockObject\MockObject;
14
use PHPUnit\Framework\TestCase;
15
use function in_array;
16
17
/**
18
 * @covers \Doctrine\DBAL\Schema\DB2SchemaManager
19
 */
20
final class DB2SchemaManagerTest extends TestCase
21
{
22
    /** @var Connection|MockObject */
23
    private $conn;
24
25
    /** @var DB2SchemaManager */
26
    private $manager;
27
28
    protected function setUp() : void
29
    {
30
        $eventManager = new EventManager();
31
        $driverMock   = $this->createMock(Driver::class);
32
        $platform     = $this->createMock(DB2Platform::class);
33
        $this->conn   = $this
34
            ->getMockBuilder(Connection::class)
35
            ->setMethods(['fetchAll', 'getUsername'])
36
            ->setConstructorArgs([['platform' => $platform], $driverMock, new Configuration(), $eventManager])
37
            ->getMock();
38
39
        $this->conn->expects($this->any())
40
            ->method('getUsername')
41
            ->willReturn('db2inst1');
42
43
        $this->manager = new DB2SchemaManager($this->conn);
44
    }
45
46
    /**
47
     * @see https://github.com/doctrine/dbal/issues/2701
48
     *
49
     * @return void
50
     *
51
     * @group DBAL-2701
52
     */
53
    public function testListTableNamesFiltersAssetNamesCorrectly()
54
    {
55
        $this->conn->getConfiguration()->setFilterSchemaAssetsExpression('/^(?!T_)/');
0 ignored issues
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

55
        /** @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...
Bug introduced by
The method getConfiguration() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

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

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

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
        $this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue([
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Doctrine\DBAL\Connection. ( Ignorable by Annotation )

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

56
        $this->conn->/** @scrutinizer ignore-call */ 
57
                     expects($this->once())->method('fetchAll')->will($this->returnValue([

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
            ['name' => 'FOO'],
58
            ['name' => 'T_FOO'],
59
            ['name' => 'BAR'],
60
            ['name' => 'T_BAR'],
61
        ]));
62
63
        self::assertSame(
64
            [
65
                'FOO',
66
                'BAR',
67
            ],
68
            $this->manager->listTableNames()
69
        );
70
    }
71
72
    /**
73
     * @return void
74
     *
75
     * @group DBAL-2701
76
     */
77
    public function testAssetFilteringSetsACallable()
78
    {
79
        $filterExpression = '/^(?!T_)/';
80
        $this->conn->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression);
0 ignored issues
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

80
        /** @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...
81
        $this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue([
82
            ['name' => 'FOO'],
83
            ['name' => 'T_FOO'],
84
            ['name' => 'BAR'],
85
            ['name' => 'T_BAR'],
86
        ]));
87
88
        self::assertSame(
89
            [
90
                'FOO',
91
                'BAR',
92
            ],
93
            $this->manager->listTableNames()
94
        );
95
96
        $callable = $this->conn->getConfiguration()->getSchemaAssetsFilter();
97
        self::assertIsCallable($callable);
98
99
        // BC check: Test that regexp expression is still preserved & accessible.
100
        $this->assertEquals($filterExpression, $this->conn->getConfiguration()->getFilterSchemaAssetsExpression());
101
    }
102
103
    /**
104
     * @return void
105
     */
106
    public function testListTableNamesFiltersAssetNamesCorrectlyWithCallable()
107
    {
108
        $accepted = ['T_FOO', 'T_BAR'];
109
        $this->conn->getConfiguration()->setSchemaAssetsFilter(static function ($assetName) use ($accepted) {
110
            return in_array($assetName, $accepted);
111
        });
112
113
        $this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue([
114
            ['name' => 'FOO'],
115
            ['name' => 'T_FOO'],
116
            ['name' => 'BAR'],
117
            ['name' => 'T_BAR'],
118
        ]));
119
120
        self::assertSame(
121
            [
122
                'T_FOO',
123
                'T_BAR',
124
            ],
125
            $this->manager->listTableNames()
126
        );
127
128
        $this->assertNull($this->conn->getConfiguration()->getFilterSchemaAssetsExpression());
129
    }
130
131
    /**
132
     * @return void
133
     */
134
    public function testSettingNullExpressionWillResetCallable()
135
    {
136
        $accepted = ['T_FOO', 'T_BAR'];
137
        $this->conn->getConfiguration()->setSchemaAssetsFilter(static function ($assetName) use ($accepted) {
138
            return in_array($assetName, $accepted);
139
        });
140
141
        $this->conn->expects($this->atLeastOnce())->method('fetchAll')->will($this->returnValue([
142
            ['name' => 'FOO'],
143
            ['name' => 'T_FOO'],
144
            ['name' => 'BAR'],
145
            ['name' => 'T_BAR'],
146
        ]));
147
148
        self::assertSame(
149
            [
150
                'T_FOO',
151
                'T_BAR',
152
            ],
153
            $this->manager->listTableNames()
154
        );
155
156
        $this->conn->getConfiguration()->setFilterSchemaAssetsExpression(null);
157
158
        self::assertSame(
159
            [
160
                'FOO',
161
                'T_FOO',
162
                'BAR',
163
                'T_BAR',
164
            ],
165
            $this->manager->listTableNames()
166
        );
167
168
        $this->assertNull($this->conn->getConfiguration()->getSchemaAssetsFilter());
169
    }
170
171
    /**
172
     * @return void
173
     */
174
    public function testSettingNullAsCallableClearsExpression()
175
    {
176
        $filterExpression = '/^(?!T_)/';
177
        $this->conn->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression);
0 ignored issues
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

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