1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Licensed to CRATE Technology GmbH("Crate") under one or more contributor |
4
|
|
|
* license agreements. See the NOTICE file distributed with this work for |
5
|
|
|
* additional information regarding copyright ownership. Crate licenses |
6
|
|
|
* this file to you under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. You may |
8
|
|
|
* obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
15
|
|
|
* License for the specific language governing permissions and limitations |
16
|
|
|
* under the License. |
17
|
|
|
* |
18
|
|
|
* However, if you have executed another commercial license agreement |
19
|
|
|
* with Crate these terms will supersede the license and you may use the |
20
|
|
|
* software solely pursuant to the terms of the relevant commercial agreement. |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace CrateTest\PDO; |
24
|
|
|
|
25
|
|
|
use Crate\PDO\Exception\PDOException; |
26
|
|
|
use Crate\PDO\PDO; |
27
|
|
|
use PHPUnit_Framework_TestCase; |
28
|
|
|
use ReflectionMethod; |
29
|
|
|
|
30
|
|
|
class PDOParseDSNTest extends PHPUnit_Framework_TestCase |
31
|
|
|
{ |
32
|
|
|
private function parseDSN($dsn) |
33
|
|
|
{ |
34
|
|
|
$method = new ReflectionMethod('Crate\PDO\PDO', 'parseDSN'); |
35
|
|
|
$method->setAccessible(true); |
36
|
|
|
|
37
|
|
|
return $method->invoke(null, $dsn); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testParseDSNSingleHost() |
41
|
|
|
{ |
42
|
|
|
$dsn = 'crate:localhost:4200'; |
43
|
|
|
$servers = $this->parseDSN($dsn); |
44
|
|
|
|
45
|
|
|
$this->assertEquals(1, count($servers)); |
46
|
|
|
$this->assertEquals('localhost:4200', $servers[0]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testParseDSNMissingName() |
50
|
|
|
{ |
51
|
|
|
$dsn = 'localhost:4200'; |
52
|
|
|
|
53
|
|
|
$this->setExpectedException('Crate\PDO\Exception\PDOException', sprintf('Invalid DSN %s', $dsn)); |
54
|
|
|
$this->parseDSN($dsn); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testParseDSNEmpty() |
58
|
|
|
{ |
59
|
|
|
$this->setExpectedException('Crate\PDO\Exception\PDOException', sprintf('Invalid DSN %s', '')); |
60
|
|
|
$this->parseDSN(''); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testParseDSNInvalid() |
64
|
|
|
{ |
65
|
|
|
$dsn = 'crate:localhost,demo.crate.io'; |
66
|
|
|
|
67
|
|
|
$this->setExpectedException('Crate\PDO\Exception\PDOException', sprintf('Invalid DSN %s', $dsn)); |
68
|
|
|
$this->parseDSN($dsn); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testParseDSNSingleHostWithSchema() |
72
|
|
|
{ |
73
|
|
|
$dsn = 'crate:localhost:4200/my_schema'; |
74
|
|
|
$servers = $this->parseDSN($dsn); |
75
|
|
|
|
76
|
|
|
$this->assertEquals(2, count($servers)); |
77
|
|
|
$this->assertEquals('my_schema', $servers[1]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testParseDSNInvalidSchema() |
81
|
|
|
{ |
82
|
|
|
$dsn = array( |
83
|
|
|
'crate:localhost:4200/österreich', |
84
|
|
|
'crate:localhost:4200/mk++je', |
85
|
|
|
'crate:localhost:4200/copyright©' |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
foreach ($dsn as &$val) { |
89
|
|
|
try { |
90
|
|
|
$this->parseDSN($val); |
91
|
|
|
} catch (PDOException $e) { |
92
|
|
|
$this->assertEquals(substr( $e->getMessage(), 0, 11 ), 'Invalid DSN'); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|