Failed Conditions
Pull Request — master (#2996)
by Sergei
17:14
created

DriverManagerTest::testDatabaseUrl()   C

Complexity

Conditions 8
Paths 72

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 17
nc 72
nop 2
1
<?php
2
3
namespace Doctrine\Tests\DBAL;
4
5
use Doctrine\DBAL\DBALException;
6
7
class DriverManagerTest extends \Doctrine\Tests\DbalTestCase
8
{
9
    /**
10
     * @requires extension pdo_sqlite
11
     * @expectedException \Doctrine\DBAL\DBALException
12
     */
13
    public function testInvalidPdoInstance()
14
    {
15
        $options = array(
16
            'pdo' => 'test'
17
        );
18
        $test = \Doctrine\DBAL\DriverManager::getConnection($options);
19
    }
20
21
    /**
22
     * @requires extension pdo_sqlite
23
     */
24
    public function testValidPdoInstance()
25
    {
26
        $options = array(
27
            'pdo' => new \PDO('sqlite::memory:')
28
        );
29
        $conn = \Doctrine\DBAL\DriverManager::getConnection($options);
30
        self::assertEquals('sqlite', $conn->getDatabasePlatform()->getName());
31
    }
32
33
    /**
34
     * @group DBAL-32
35
     * @requires extension pdo_sqlite
36
     */
37
    public function testPdoInstanceSetErrorMode()
38
    {
39
        $pdo = new \PDO('sqlite::memory:');
40
        $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT);
41
        $options = array(
42
            'pdo' => $pdo
43
        );
44
45
        $conn = \Doctrine\DBAL\DriverManager::getConnection($options);
46
        self::assertEquals(\PDO::ERRMODE_EXCEPTION, $pdo->getAttribute(\PDO::ATTR_ERRMODE));
47
    }
48
49
    /**
50
     * @expectedException \Doctrine\DBAL\DBALException
51
     */
52
    public function testCheckParams()
53
    {
54
        $conn = \Doctrine\DBAL\DriverManager::getConnection(array());
55
    }
56
57
    /**
58
     * @expectedException \Doctrine\DBAL\DBALException
59
     */
60
    public function testInvalidDriver()
61
    {
62
        $conn = \Doctrine\DBAL\DriverManager::getConnection(array('driver' => 'invalid_driver'));
63
    }
64
65
    /**
66
     * @requires extension pdo_sqlite
67
     */
68
    public function testCustomPlatform()
69
    {
70
        $mockPlatform = new \Doctrine\Tests\DBAL\Mocks\MockPlatform();
71
        $options = array(
72
            'pdo' => new \PDO('sqlite::memory:'),
73
            'platform' => $mockPlatform
74
        );
75
76
        $conn = \Doctrine\DBAL\DriverManager::getConnection($options);
77
        self::assertSame($mockPlatform, $conn->getDatabasePlatform());
78
    }
79
80
    /**
81
     * @requires extension pdo_sqlite
82
     */
83
    public function testCustomWrapper()
84
    {
85
        $wrapperClass = 'Doctrine\Tests\Mocks\ConnectionMock';
86
87
        $options = array(
88
            'pdo' => new \PDO('sqlite::memory:'),
89
            'wrapperClass' => $wrapperClass,
90
        );
91
92
        $conn = \Doctrine\DBAL\DriverManager::getConnection($options);
93
        self::assertInstanceOf($wrapperClass, $conn);
94
    }
95
96
    /**
97
     * @requires extension pdo_sqlite
98
     */
99
    public function testInvalidWrapperClass()
100
    {
101
        $this->expectException(DBALException::class);
102
103
        $options = array(
104
            'pdo' => new \PDO('sqlite::memory:'),
105
            'wrapperClass' => 'stdClass',
106
        );
107
108
        $conn = \Doctrine\DBAL\DriverManager::getConnection($options);
109
    }
110
111
    public function testInvalidDriverClass()
112
    {
113
        $this->expectException(DBALException::class);
114
115
        $options = array(
116
            'driverClass' => 'stdClass'
117
        );
118
119
        $conn = \Doctrine\DBAL\DriverManager::getConnection($options);
120
    }
121
122
    public function testValidDriverClass()
123
    {
124
        $options = array(
125
            'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
126
        );
127
128
        $conn = \Doctrine\DBAL\DriverManager::getConnection($options);
129
        self::assertInstanceOf('Doctrine\DBAL\Driver\PDOMySql\Driver', $conn->getDriver());
130
    }
131
132
    /**
133
     * @dataProvider databaseUrls
134
     */
135
    public function testDatabaseUrl($url, $expected)
136
    {
137
        $options = is_array($url) ? $url : array(
138
            'url' => $url,
139
        );
140
141
        if (isset($options['pdo'])) {
142
            if (! extension_loaded('pdo')) {
143
                $this->markTestSkipped('PDO is not installed');
144
            }
145
146
            $options['pdo'] = $this->createMock(\PDO::class);
147
        }
148
149
        $options = is_array($url) ? $url : array(
150
            'url' => $url,
151
        );
152
153
        if ($expected === false) {
154
            $this->expectException(DBALException::class);
155
        }
156
157
        $conn = \Doctrine\DBAL\DriverManager::getConnection($options);
158
159
        $params = $conn->getParams();
160
        foreach ($expected as $key => $value) {
161
            if (in_array($key, array('pdo', 'driver', 'driverClass'), true)) {
162
                self::assertInstanceOf($value, $conn->getDriver());
163
            } else {
164
                self::assertEquals($value, $params[$key]);
165
            }
166
        }
167
    }
168
169
    public function databaseUrls()
170
    {
171
        return array(
172
            'simple URL' => array(
173
                'mysql://foo:bar@localhost/baz',
174
                array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'),
175
            ),
176
            'simple URL with port' => array(
177
                'mysql://foo:bar@localhost:11211/baz',
178
                array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'port' => 11211, 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'),
179
            ),
180
            'sqlite relative URL with host' => array(
181
                'sqlite://localhost/foo/dbname.sqlite',
182
                array('path' => 'foo/dbname.sqlite', 'driver' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver'),
183
            ),
184
            'sqlite absolute URL with host' => array(
185
                'sqlite://localhost//tmp/dbname.sqlite',
186
                array('path' => '/tmp/dbname.sqlite', 'driver' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver'),
187
            ),
188
            'sqlite relative URL without host' => array(
189
                'sqlite:///foo/dbname.sqlite',
190
                array('path' => 'foo/dbname.sqlite', 'driver' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver'),
191
            ),
192
            'sqlite absolute URL without host' => array(
193
                'sqlite:////tmp/dbname.sqlite',
194
                array('path' => '/tmp/dbname.sqlite', 'driver' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver'),
195
            ),
196
            'sqlite memory' => array(
197
                'sqlite:///:memory:',
198
                array('memory' => true, 'driver' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver'),
199
            ),
200
            'sqlite memory with host' => array(
201
                'sqlite://localhost/:memory:',
202
                array('memory' => true, 'driver' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver'),
203
            ),
204
            'params parsed from URL override individual params' => array(
205
                array('url' => 'mysql://foo:bar@localhost/baz', 'password' => 'lulz'),
206
                array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'),
207
            ),
208
            'params not parsed from URL but individual params are preserved' => array(
209
                array('url' => 'mysql://foo:bar@localhost/baz', 'port' => 1234),
210
                array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'port' => 1234, 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'),
211
            ),
212
            'query params from URL are used as extra params' => array(
213
                'url' => 'mysql://foo:bar@localhost/dbname?charset=UTF-8',
214
                array('charset' => 'UTF-8'),
215
            ),
216
            'simple URL with fallthrough scheme not defined in map' => array(
217
                'sqlsrv://foo:bar@localhost/baz',
218
                array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\SQLSrv\Driver'),
219
            ),
220
            'simple URL with fallthrough scheme containing underscores fails' => array(
221
                'drizzle_pdo_mysql://foo:bar@localhost/baz',
222
                false,
223
            ),
224
            'simple URL with fallthrough scheme containing dashes works' => array(
225
                'drizzle-pdo-mysql://foo:bar@localhost/baz',
226
                array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\DrizzlePDOMySql\Driver'),
227
            ),
228
            'simple URL with percent encoding' => array(
229
                'mysql://foo%3A:bar%2F@localhost/baz+baz%40',
230
                array('user' => 'foo:', 'password' => 'bar/', 'host' => 'localhost', 'dbname' => 'baz+baz@', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'),
231
            ),
232
            'simple URL with percent sign in password' => array(
233
                'mysql://foo:bar%25bar@localhost/baz',
234
                array('user' => 'foo', 'password' => 'bar%bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'),
235
            ),
236
237
            // DBAL-1234
238
            'URL without scheme and without any driver information' => array(
239
                array('url' => '//foo:bar@localhost/baz'),
240
                false,
241
            ),
242
            'URL without scheme but default PDO driver' => array(
243
                array('url' => '//foo:bar@localhost/baz', 'pdo' => true),
244
                false,
245
            ),
246
            'URL without scheme but default driver' => array(
247
                array('url' => '//foo:bar@localhost/baz', 'driver' => 'pdo_mysql'),
248
                array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'),
249
            ),
250
            'URL without scheme but custom driver' => array(
251
                array('url' => '//foo:bar@localhost/baz', 'driverClass' => 'Doctrine\Tests\Mocks\DriverMock'),
252
                array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driverClass' => 'Doctrine\Tests\Mocks\DriverMock'),
253
            ),
254
            'URL without scheme but default PDO driver and default driver' => array(
255
                array('url' => '//foo:bar@localhost/baz', 'pdo' => true, 'driver' => 'pdo_mysql'),
256
                array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'),
257
            ),
258
            'URL without scheme but driver and custom driver' => array(
259
                array('url' => '//foo:bar@localhost/baz', 'driver' => 'pdo_mysql', 'driverClass' => 'Doctrine\Tests\Mocks\DriverMock'),
260
                array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driverClass' => 'Doctrine\Tests\Mocks\DriverMock'),
261
            ),
262
            'URL with default PDO driver' => array(
263
                array('url' => 'mysql://foo:bar@localhost/baz', 'pdo' => true),
264
                array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'),
265
            ),
266
            'URL with default driver' => array(
267
                array('url' => 'mysql://foo:bar@localhost/baz', 'driver' => 'sqlite'),
268
                array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'),
269
            ),
270
            'URL with default custom driver' => array(
271
                array('url' => 'mysql://foo:bar@localhost/baz', 'driverClass' => 'Doctrine\Tests\Mocks\DriverMock'),
272
                array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'),
273
            ),
274
            'URL with default PDO driver and default driver' => array(
275
                array('url' => 'mysql://foo:bar@localhost/baz', 'pdo' => true, 'driver' => 'sqlite'),
276
                array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'),
277
            ),
278
            'URL with default driver and default custom driver' => array(
279
                array('url' => 'mysql://foo:bar@localhost/baz', 'driver' => 'sqlite',  'driverClass' => 'Doctrine\Tests\Mocks\DriverMock'),
280
                array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'),
281
            ),
282
            'URL with default PDO driver and default driver and default custom driver' => array(
283
                array('url' => 'mysql://foo:bar@localhost/baz', 'pdo' => true, 'driver' => 'sqlite', 'driverClass' => 'Doctrine\Tests\Mocks\DriverMock'),
284
                array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'),
285
            ),
286
        );
287
    }
288
}
289