Completed
Pull Request — master (#1808)
by Tobias
04:15 queued 01:32
created

ClientConfigurationTest::testInvalidDsnPortOnly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Elastica\Test;
4
5
use Elastica\ClientConfiguration;
6
use PHPUnit\Framework\TestCase;
7
8
/**
9
 * @group unit
10
 *
11
 * @internal
12
 */
13
class ClientConfigurationTest extends TestCase
14
{
15
    public function testInvalidDsn(): void
16
    {
17
        $this->expectException(\Elastica\Exception\InvalidException::class);
18
        $this->expectExceptionMessage('DSN "test:foo" is invalid.');
19
20
        ClientConfiguration::fromDsn('test:foo');
21
    }
22
23
    public function testInvalidDsnPortOnly(): void
24
    {
25
        $this->expectException(\Elastica\Exception\InvalidException::class);
26
        $this->expectExceptionMessage('DSN ":0" is invalid.');
27
28
        ClientConfiguration::fromDsn(':0');
29
    }
30
31
    public function testFromSimpleDsn(): void
32
    {
33
        $configuration = ClientConfiguration::fromDsn('192.168.1.1:9201');
34
35
        $expected = [
36
            'host' => '192.168.1.1',
37
            'port' => 9201,
38
            'path' => null,
39
            'url' => null,
40
            'proxy' => null,
41
            'transport' => null,
42
            'persistent' => true,
43
            'timeout' => null,
44
            'connections' => [],
45
            'roundRobin' => false,
46
            'retryOnConflict' => 0,
47
            'bigintConversion' => false,
48
            'username' => null,
49
            'password' => null,
50
            'auth_type' => null,
51
        ];
52
53
        $this->assertEquals($expected, $configuration->getAll());
54
    }
55
56
    public function testFromDsnWithParameters(): void
57
    {
58
        $configuration = ClientConfiguration::fromDsn('https://user:[email protected]:9201/my-path?proxy=https://proxy.com&persistent=false&timeout=45&roundRobin=true&retryOnConflict=2&bigintConversion=true&extra=abc');
59
        $expected = [
60
            'host' => 'foo.com',
61
            'port' => 9201,
62
            'path' => '/my-path',
63
            'url' => null,
64
            'proxy' => 'https://proxy.com',
65
            'transport' => 'https',
66
            'persistent' => false,
67
            'timeout' => 45,
68
            'connections' => [],
69
            'roundRobin' => true,
70
            'retryOnConflict' => 2,
71
            'bigintConversion' => true,
72
            'username' => 'user',
73
            'password' => 'p4ss',
74
            'auth_type' => 'basic',
75
            'extra' => 'abc',
76
        ];
77
78
        $this->assertEquals($expected, $configuration->getAll());
79
    }
80
81
    public function testFromDsnWithPool(): void
82
    {
83
        $configuration = ClientConfiguration::fromDsn('pool(http://[email protected] http://127.0.0.2/bar?timeout=4)?extra=abc&username=tobias');
84
        $expected = [
85
            'host' => null,
86
            'port' => null,
87
            'path' => null,
88
            'url' => null,
89
            'proxy' => null,
90
            'transport' => null,
91
            'persistent' => true,
92
            'timeout' => null,
93
            'connections' => [
94
                ['host' => '127.0.0.1', 'transport' => 'http', 'username' => 'nicolas'],
95
                ['host' => '127.0.0.2', 'path' => '/bar', 'transport' => 'http', 'timeout' => 4],
96
            ],
97
            'roundRobin' => false,
98
            'retryOnConflict' => 0,
99
            'bigintConversion' => false,
100
            'username' => 'tobias',
101
            'password' => null,
102
            'auth_type' => null,
103
            'extra' => 'abc',
104
        ];
105
106
        $this->assertEquals($expected, $configuration->getAll());
107
    }
108
109
    public function testFromEmptyArray(): void
110
    {
111
        $configuration = ClientConfiguration::fromArray([]);
112
113
        $expected = [
114
            'host' => null,
115
            'port' => null,
116
            'path' => null,
117
            'url' => null,
118
            'proxy' => null,
119
            'transport' => null,
120
            'persistent' => true,
121
            'timeout' => null,
122
            'connections' => [], // host, port, path, timeout, transport, compression, persistent, timeout, username, password, config -> (curl, headers, url)
123
            'roundRobin' => false,
124
            'retryOnConflict' => 0,
125
            'bigintConversion' => false,
126
            'username' => null,
127
            'password' => null,
128
            'auth_type' => null,
129
        ];
130
131
        $this->assertEquals($expected, $configuration->getAll());
132
    }
133
134
    public function testFromArray(): void
135
    {
136
        $configuration = ClientConfiguration::fromArray([
137
            'username' => 'Jdoe',
138
            'extra' => 'abc',
139
        ]);
140
141
        $expected = [
142
            'host' => null,
143
            'port' => null,
144
            'path' => null,
145
            'url' => null,
146
            'proxy' => null,
147
            'transport' => null,
148
            'persistent' => true,
149
            'timeout' => null,
150
            'connections' => [], // host, port, path, timeout, transport, compression, persistent, timeout, username, password, config -> (curl, headers, url)
151
            'roundRobin' => false,
152
            'retryOnConflict' => 0,
153
            'bigintConversion' => false,
154
            'username' => 'Jdoe',
155
            'password' => null,
156
            'auth_type' => null,
157
            'extra' => 'abc',
158
        ];
159
160
        $this->assertEquals($expected, $configuration->getAll());
161
    }
162
163
    public function testHas(): void
164
    {
165
        $configuration = new ClientConfiguration();
166
        $this->assertTrue($configuration->has('host'));
167
        $this->assertFalse($configuration->has('inexistantKey'));
168
    }
169
170
    public function testGet(): void
171
    {
172
        $configuration = new ClientConfiguration();
173
        $this->assertTrue($configuration->get('persistent'));
174
175
        $expected = [
176
            'host' => null,
177
            'port' => null,
178
            'path' => null,
179
            'url' => null,
180
            'proxy' => null,
181
            'transport' => null,
182
            'persistent' => true,
183
            'timeout' => null,
184
            'connections' => [],
185
            'roundRobin' => false,
186
            'retryOnConflict' => 0,
187
            'bigintConversion' => false,
188
            'username' => null,
189
            'password' => null,
190
            'auth_type' => null,
191
        ];
192
193
        $this->assertEquals($expected, $configuration->get(''));
194
195
        $this->expectException(\Elastica\Exception\InvalidException::class);
196
        $configuration->get('invalidKey');
197
    }
198
199
    public function testAdd(): void
200
    {
201
        $keyName = 'myKey';
202
203
        $configuration = new ClientConfiguration();
204
        $this->assertFalse($configuration->has($keyName));
205
206
        $configuration->add($keyName, 'FirstValue');
207
        $this->assertEquals(['FirstValue'], $configuration->get($keyName));
208
209
        $configuration->add($keyName, 'SecondValue');
210
        $this->assertEquals(['FirstValue', 'SecondValue'], $configuration->get($keyName));
211
212
        $configuration->set('otherKey', 'value');
213
        $this->assertEquals('value', $configuration->get('otherKey'));
214
        $configuration->add('otherKey', 'nextValue');
215
        $this->assertEquals(['value', 'nextValue'], $configuration->get('otherKey'));
216
    }
217
}
218