Passed
Branch main (45b422)
by Andreas
01:40
created

PDOParseDSNTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 89
c 0
b 0
f 0
wmc 11
lcom 1
cbo 2
rs 10
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 PHPUnit\Framework\TestCase;
27
use ReflectionMethod;
28
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