|
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
|
|
|
return $method->invoke(null, $dsn); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
private function serversFromDsnParts(array $dsnParts) |
|
40
|
|
|
{ |
|
41
|
|
|
$method = new ReflectionMethod('Crate\PDO\PDO', 'serversFromDsnParts'); |
|
42
|
|
|
$method->setAccessible(true); |
|
43
|
|
|
return $method->invoke(null, $dsnParts); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testParseDSNSingleHost() |
|
47
|
|
|
{ |
|
48
|
|
|
$parts = $this->parseDSN('crate:localhost:4200'); |
|
49
|
|
|
$servers = $this->serversFromDsnParts($parts); |
|
50
|
|
|
|
|
51
|
|
|
$this->assertEquals(1, count($servers)); |
|
52
|
|
|
$this->assertEquals('localhost:4200', $servers[0]); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testParseDSNMultipleHosts() |
|
56
|
|
|
{ |
|
57
|
|
|
$parts = $this->parseDSN('crate:crate1.example.com:4200,crate2.example.com:4200'); |
|
58
|
|
|
$servers = $this->serversFromDsnParts($parts); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertEquals(2, count($servers)); |
|
61
|
|
|
$this->assertEquals('crate1.example.com:4200', $servers[0]); |
|
62
|
|
|
$this->assertEquals('crate2.example.com:4200', $servers[1]); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testParseDSNMissingName() |
|
66
|
|
|
{ |
|
67
|
|
|
$dsn = 'localhost:4200'; |
|
68
|
|
|
|
|
69
|
|
|
$this->setExpectedException('Crate\PDO\Exception\PDOException', sprintf('Invalid DSN %s', $dsn)); |
|
70
|
|
|
$this->parseDSN($dsn); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function testParseDSNEmpty() |
|
74
|
|
|
{ |
|
75
|
|
|
$this->setExpectedException('Crate\PDO\Exception\PDOException', sprintf('Invalid DSN %s', '')); |
|
76
|
|
|
$this->parseDSN(''); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testParseDSNInvalid() |
|
80
|
|
|
{ |
|
81
|
|
|
$dsn = 'crate:localhost,demo.crate.io'; |
|
82
|
|
|
|
|
83
|
|
|
$this->setExpectedException('Crate\PDO\Exception\PDOException', sprintf('Invalid DSN %s', $dsn)); |
|
84
|
|
|
$this->parseDSN($dsn); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function testParseDSNSingleHostWithSchema() |
|
88
|
|
|
{ |
|
89
|
|
|
$dsn = 'crate:localhost:4200/my_schema'; |
|
90
|
|
|
$servers = $this->parseDSN($dsn); |
|
91
|
|
|
|
|
92
|
|
|
$this->assertEquals(2, count($servers)); |
|
93
|
|
|
$this->assertEquals('my_schema', $servers[1]); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function testParseDSNInvalidSchema() |
|
97
|
|
|
{ |
|
98
|
|
|
$dsn = array( |
|
99
|
|
|
'crate:localhost:4200/österreich', |
|
100
|
|
|
'crate:localhost:4200/mk++je', |
|
101
|
|
|
'crate:localhost:4200/copyright©' |
|
102
|
|
|
); |
|
103
|
|
|
|
|
104
|
|
|
foreach ($dsn as &$val) { |
|
105
|
|
|
try { |
|
106
|
|
|
$this->parseDSN($val); |
|
107
|
|
|
} catch (PDOException $e) { |
|
108
|
|
|
$this->assertEquals(substr( $e->getMessage(), 0, 11 ), 'Invalid DSN'); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|