Completed
Pull Request — master (#1808)
by Tobias
05:20
created

ClientConfigurationTest::testFromDsnWithPool()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

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