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