Completed
Pull Request — master (#1808)
by Tobias
06:21
created

ClientConfigurationTest::testFromEmptyArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
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
73
    public function testFromDsnWithPool(): void
74
    {
75
        $configuration = ClientConfiguration::fromDsn('pool(http://[email protected] http://127.0.0.2/bar?timeout=4)?extra=abc&username=tobias');
76
        $expected = [
77
            'host' => null,
78
            'port' => null,
79
            'path' => null,
80
            'url' => null,
81
            'proxy' => null,
82
            'transport' => null,
83
            'persistent' => true,
84
            'timeout' => null,
85
            'connections' => [
86
                ['host' => '127.0.0.1', 'transport' => 'http', 'username' => 'nicolas'],
87
                ['host' => '127.0.0.2', 'path' => '/bar', 'transport' => 'http', 'timeout' => 4],
88
            ],
89
            'roundRobin' => false,
90
            'retryOnConflict' => 0,
91
            'bigintConversion' => false,
92
            'username' => 'tobias',
93
            'password' => null,
94
            'auth_type' => null,
95
            'extra' => 'abc',
96
        ];
97
98
        $this->assertEquals($expected, $configuration->getAll());
99
    }
100
101
    public function testFromEmptyArray(): void
102
    {
103
        $configuration = ClientConfiguration::fromArray([]);
104
105
        $expected = [
106
            'host' => null,
107
            'port' => null,
108
            'path' => null,
109
            'url' => null,
110
            'proxy' => null,
111
            'transport' => null,
112
            'persistent' => true,
113
            'timeout' => null,
114
            'connections' => [], // host, port, path, timeout, transport, compression, persistent, timeout, username, password, config -> (curl, headers, url)
115
            'roundRobin' => false,
116
            'retryOnConflict' => 0,
117
            'bigintConversion' => false,
118
            'username' => null,
119
            'password' => null,
120
            'auth_type' => null,
121
        ];
122
123
        $this->assertEquals($expected, $configuration->getAll());
124
    }
125
126
    public function testFromArray(): void
127
    {
128
        $configuration = ClientConfiguration::fromArray([
129
            'username' => 'Jdoe',
130
            'extra' => 'abc',
131
        ]);
132
133
        $expected = [
134
            'host' => null,
135
            'port' => null,
136
            'path' => null,
137
            'url' => null,
138
            'proxy' => null,
139
            'transport' => null,
140
            'persistent' => true,
141
            'timeout' => null,
142
            'connections' => [], // host, port, path, timeout, transport, compression, persistent, timeout, username, password, config -> (curl, headers, url)
143
            'roundRobin' => false,
144
            'retryOnConflict' => 0,
145
            'bigintConversion' => false,
146
            'username' => 'Jdoe',
147
            'password' => null,
148
            'auth_type' => null,
149
            'extra' => 'abc',
150
        ];
151
152
        $this->assertEquals($expected, $configuration->getAll());
153
    }
154
155
    public function testHas(): void
156
    {
157
        $configuration = new ClientConfiguration();
158
        $this->assertTrue($configuration->has('host'));
159
        $this->assertFalse($configuration->has('inexistantKey'));
160
    }
161
162
    public function testGet(): void
163
    {
164
        $configuration = new ClientConfiguration();
165
        $this->assertTrue($configuration->get('persistent'));
166
167
        $expected = [
168
            'host' => null,
169
            'port' => null,
170
            'path' => null,
171
            'url' => null,
172
            'proxy' => null,
173
            'transport' => null,
174
            'persistent' => true,
175
            'timeout' => null,
176
            'connections' => [],
177
            'roundRobin' => false,
178
            'retryOnConflict' => 0,
179
            'bigintConversion' => false,
180
            'username' => null,
181
            'password' => null,
182
            'auth_type' => null,
183
        ];
184
185
        $this->assertEquals($expected, $configuration->get(''));
186
187
        $this->expectException(\Elastica\Exception\InvalidException::class);
188
        $configuration->get('invalidKey');
189
    }
190
191
    public function testAdd(): void
192
    {
193
        $keyName = 'myKey';
194
195
        $configuration = new ClientConfiguration();
196
        $this->assertFalse($configuration->has($keyName));
197
198
        $configuration->add($keyName, 'FirstValue');
199
        $this->assertEquals(['FirstValue'], $configuration->get($keyName));
200
201
        $configuration->add($keyName, 'SecondValue');
202
        $this->assertEquals(['FirstValue', 'SecondValue'], $configuration->get($keyName));
203
204
        $configuration->set('otherKey', 'value');
205
        $this->assertEquals('value', $configuration->get('otherKey'));
206
        $configuration->add('otherKey', 'nextValue');
207
        $this->assertEquals(['value', 'nextValue'], $configuration->get('otherKey'));
208
    }
209
}
210