-
Notifications
You must be signed in to change notification settings - Fork 982
/
Copy pathClientTest.php
248 lines (208 loc) · 9 KB
/
ClientTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/**
* Elasticsearch PHP Client
*
* @link https://github.com/elastic/elasticsearch-php
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
* @license https://opensource.org/licenses/MIT MIT License
*
* Licensed to Elasticsearch B.V under one or more agreements.
* Elasticsearch B.V licenses this file to you under the MIT License.
* See the LICENSE file in the project root for more information.
*/
declare(strict_types = 1);
namespace Elastic\Elasticsearch\Tests;
use Elastic\Elasticsearch\Client;
use Elastic\Elasticsearch\Exception\ProductCheckException;
use Elastic\Elasticsearch\Exception\ClientResponseException;
use Elastic\Elasticsearch\Exception\ServerResponseException;
use Elastic\Elasticsearch\Response\Elasticsearch;
use Elastic\Transport\Transport;
use Elastic\Transport\TransportBuilder;
use Http\Mock\Client as MockClient;
use Http\Promise\Promise;
use Nyholm\Psr7\Factory\Psr17Factory;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
class ClientTest extends TestCase
{
protected LoggerInterface $logger;
protected MockClient $httpClient;
protected Transport $transport;
protected Psr17Factory $psr17Factory;
protected Client $client;
public function setUp(): void
{
$this->logger = $this->createStub(LoggerInterface::class);
$this->httpClient = new MockClient();
$this->transport = TransportBuilder::create()
->setClient($this->httpClient)
->build();
$this->psr17Factory = new Psr17Factory();
$this->client = new Client($this->transport, $this->logger);
}
public function testGetTransport()
{
$this->assertEquals($this->transport, $this->client->getTransport());
}
public function testGetLogger()
{
$this->assertEquals($this->logger, $this->client->getLogger());
}
public function testDefaultAsyncIsFalse()
{
$this->assertFalse($this->client->getAsync());
}
public function testSetGetAsync()
{
$async = $this->client->getAsync();
$this->client->setAsync(!$async);
$this->assertEquals(!$async, $this->client->getAsync());
}
public function testSetGetElasticMetaHeader()
{
$this->client->setElasticMetaHeader(false);
$this->assertFalse($this->client->getElasticMetaHeader());
}
public function testSetGetResponseException()
{
$this->client->setResponseException(false);
$this->assertFalse($this->client->getResponseException());
}
public function testSendRequest()
{
$request = $this->psr17Factory->createRequest('GET', 'localhost:9200');
$response = $this->psr17Factory->createResponse(200)->withHeader('X-Elastic-Product', 'Elasticsearch');
$this->httpClient->addResponse($response);
$result = $this->client->sendRequest($request);
$this->assertInstanceOf(Elasticsearch::class, $result);
}
public function testSendRequestToNotElasticsearchThrowsException()
{
$request = $this->psr17Factory->createRequest('GET', 'localhost:9200');
$response = $this->psr17Factory->createResponse(200);
$this->httpClient->addResponse($response);
$this->expectException(ProductCheckException::class);
$result = $this->client->sendRequest($request);
}
public function testSendRequestWith400ResponseThrowsException()
{
$request = $this->psr17Factory->createRequest('GET', 'localhost:9200');
$response = $this->psr17Factory->createResponse(400);
$this->httpClient->addResponse($response);
$this->expectException(ClientResponseException::class);
$result = $this->client->sendRequest($request);
}
public function testSendRequestWith400ThrowsExceptionWithResponse()
{
$request = $this->psr17Factory->createRequest('GET', 'localhost:9200');
$response = $this->psr17Factory->createResponse(400);
$this->httpClient->addResponse($response);
try {
$result = $this->client->sendRequest($request);
} catch (ClientResponseException $e) {
$this->assertEquals($response, $e->getResponse());
}
}
public function testSendRequestWith400DoesNotThrowsExceptionWithSetResponseExceptionToFalse()
{
$request = $this->psr17Factory->createRequest('GET', 'localhost:9200');
$response = $this->psr17Factory->createResponse(400);
$this->httpClient->addResponse($response);
$this->client->setResponseException(false);
$result = $this->client->sendRequest($request);
$this->assertEquals(400, $result->getStatusCode());
}
public function testSendRequestWith500ResponseThrowsException()
{
$request = $this->psr17Factory->createRequest('GET', 'localhost:9200');
$response = $this->psr17Factory->createResponse(500);
$this->httpClient->addResponse($response);
$this->expectException(ServerResponseException::class);
$result = $this->client->sendRequest($request);
}
public function testSendRequestWith500ThrowsExceptionWithResponse()
{
$request = $this->psr17Factory->createRequest('GET', 'localhost:9200');
$response = $this->psr17Factory->createResponse(500);
$this->httpClient->addResponse($response);
try {
$result = $this->client->sendRequest($request);
} catch (ServerResponseException $e) {
$this->assertEquals($response, $e->getResponse());
}
}
public function testSendRequestWith500DoesNotThrowsExceptionWithSetResponseExceptionToFalse()
{
$request = $this->psr17Factory->createRequest('GET', 'localhost:9200');
$response = $this->psr17Factory->createResponse(500);
$this->httpClient->addResponse($response);
$this->client->setResponseException(false);
$result = $this->client->sendRequest($request);
$this->assertEquals(500, $result->getStatusCode());
}
public function testSendRequestWithAsync()
{
$request = $this->psr17Factory->createRequest('GET', 'localhost:9200');
$response = $this->psr17Factory->createResponse(200)->withHeader('X-Elastic-Product', 'Elasticsearch');
$this->httpClient->addResponse($response);
$this->client->setAsync(true);
$result = $this->client->sendRequest($request);
$this->assertInstanceOf(Promise::class, $result);
}
public function testSendRequestWithAsyncWillReturnElasticsearch()
{
$request = $this->psr17Factory->createRequest('GET', 'localhost:9200');
$response = $this->psr17Factory->createResponse(200)->withHeader('X-Elastic-Product', 'Elasticsearch');
$this->httpClient->addResponse($response);
$this->client->setAsync(true);
$result = $this->client->sendRequest($request);
$this->assertInstanceOf(Promise::class, $result);
$this->assertInstanceOf(Elasticsearch::class, $result->wait());
}
public function testSetServerless()
{
$this->client->setServerless(true);
$this->assertTrue($this->client->getServerless());
}
public function testServerlessIsDefaultFalse()
{
$this->assertFalse($this->client->getServerless());
}
public function testIsServerlessTrueWhenReponseFromServerless()
{
$request = $this->psr17Factory->createRequest('GET', 'localhost:9200');
$response = $this->psr17Factory->createResponse(200)
->withHeader('X-Elastic-Product', 'Elasticsearch')
->withHeader(Client::API_VERSION_HEADER, Client::API_VERSION);
$this->httpClient->addResponse($response);
$result = $this->client->sendRequest($request);
$this->assertTrue($this->client->getServerless());
}
public function testIsServerlessTrueWhenReponseFromServerlessWithAsyncOnSuccess()
{
$request = $this->psr17Factory->createRequest('GET', 'localhost:9200');
$response = $this->psr17Factory->createResponse(200)
->withHeader('X-Elastic-Product', 'Elasticsearch')
->withHeader(Client::API_VERSION_HEADER, Client::API_VERSION);
$this->httpClient->addResponse($response);
$this->client->setAsync(true);
$result = $this->client->sendRequest($request);
$this->assertInstanceOf(Promise::class, $result);
$result->wait();
$this->assertTrue($this->client->getServerless());
}
public function testIsServerlessTrueWhenReponseFromServerlessWithAsyncOnSuccessNoException()
{
$request = $this->psr17Factory->createRequest('HEAD', 'localhost:9200');
$response = $this->psr17Factory->createResponse(200)
->withHeader('X-Elastic-Product', 'Elasticsearch')
->withHeader(Client::API_VERSION_HEADER, Client::API_VERSION);
$this->httpClient->addResponse($response);
$this->client->setAsync(true);
$result = $this->client->sendRequest($request);
$this->assertInstanceOf(Promise::class, $result);
$result->wait();
$this->assertTrue($this->client->getServerless());
}
}