-
Notifications
You must be signed in to change notification settings - Fork 982
/
Copy pathElasticsearchTest.php
211 lines (180 loc) · 7.01 KB
/
ElasticsearchTest.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
<?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\Response;
use Elastic\Elasticsearch\Exception\ArrayAccessException;
use Elastic\Elasticsearch\Exception\ClientResponseException;
use Elastic\Elasticsearch\Exception\ProductCheckException;
use Elastic\Elasticsearch\Exception\ServerResponseException;
use Elastic\Elasticsearch\Response\Elasticsearch;
use Nyholm\Psr7\Factory\Psr17Factory;
use PHPUnit\Framework\TestCase;
class ElasticsearchTest extends TestCase
{
public function setUp(): void
{
$this->psr17Factory = new Psr17Factory();
$this->elasticsearch = new Elasticsearch();
$this->response200 = $this->psr17Factory->createResponse(200)
->withHeader('X-Elastic-Product', 'Elasticsearch')
->withHeader('Content-Type', 'application/json');
$this->response400 = $this->psr17Factory->createResponse(400)
->withHeader('X-Elastic-Product', 'Elasticsearch')
->withHeader('Content-Type', 'application/json');
$this->response500 = $this->psr17Factory->createResponse(500)
->withHeader('X-Elastic-Product', 'Elasticsearch')
->withHeader('Content-Type', 'application/json');
}
public function testAsArray()
{
$array = ['foo' => 'bar'];
$body = $this->psr17Factory->createStream(json_encode($array));
$this->elasticsearch->setResponse($this->response200->withBody($body));
$this->assertEquals($array, $this->elasticsearch->asArray());
}
public function testAsString()
{
$json = json_encode(['foo' => 'bar']);
$body = $this->psr17Factory->createStream($json);
$this->elasticsearch->setResponse($this->response200->withBody($body));
$this->assertEquals($json, $this->elasticsearch->asString());
}
public function testAsObject()
{
$json = json_encode(['foo' => 'bar']);
$body = $this->psr17Factory->createStream($json);
$this->elasticsearch->setResponse($this->response200->withBody($body));
$obj = $this->elasticsearch->asObject();
$this->assertIsObject($obj);
$this->assertEquals('bar', $obj->foo);
}
public function testAsBoolIsTrueWith200()
{
$this->elasticsearch->setResponse($this->response200);
$this->assertTrue($this->elasticsearch->asBool());
}
public function testAsBoolIsFalseWith400()
{
try {
$this->elasticsearch->setResponse($this->response400);
} catch (ClientResponseException $e) {
$this->assertFalse($this->elasticsearch->asBool());
}
}
/**
* @covers Elastic\Elasticsearch\Response\Elasticsearch::__toString()
*/
public function testSerializeAsString()
{
$json = json_encode(['foo' => 'bar']);
$body = $this->psr17Factory->createStream($json);
$this->elasticsearch->setResponse($this->response200->withBody($body));
$this->assertEquals($json, (string) $this->elasticsearch);
}
/**
* @doesNotPerformAssertions
*/
public function testSetResponse()
{
$this->elasticsearch->setResponse($this->response200);
}
public function testSetResponseFromUnknownSourceThrowProductCheckException()
{
$response = $this->psr17Factory->createResponse(200)
->withHeader('Content-Type', 'application/json');
$this->expectException(ProductCheckException::class);
$this->elasticsearch->setResponse($response);
}
public function testSetResponseWith400ThrowException()
{
$this->expectException(ClientResponseException::class);
$this->elasticsearch->setResponse($this->response400);
}
public function testSetResponseWith500ThrowException()
{
$this->expectException(ServerResponseException::class);
$this->elasticsearch->setResponse($this->response500);
}
/**
* @doesNotPerformAssertions
*/
public function testSetResponseWith400AndThrowFalseDoesNotThrowException()
{
$this->elasticsearch->setResponse($this->response400, false);
}
/**
* @doesNotPerformAssertions
*/
public function testSetResponseWith500AndThrowFalseDoesNotThrowException()
{
$this->elasticsearch->setResponse($this->response500, false);
}
/**
* @covers Elastic\Elasticsearch\Response\Elasticsearch::offsetGet
*/
public function testAccessAsArray()
{
$array = ['foo' => 'bar'];
$body = $this->psr17Factory->createStream(json_encode($array));
$this->elasticsearch->setResponse($this->response200->withBody($body));
$this->assertEquals($array['foo'], $this->elasticsearch['foo']);
}
/**
* @covers Elastic\Elasticsearch\Response\Elasticsearch::offsetExists
*/
public function testIsSetArrayAccess()
{
$array = ['foo' => 'bar'];
$body = $this->psr17Factory->createStream(json_encode($array));
$this->elasticsearch->setResponse($this->response200->withBody($body));
$this->assertTrue(isset($this->elasticsearch['foo']));
}
/**
* @covers Elastic\Elasticsearch\Response\Elasticsearch::offsetSet
*/
public function testSetArrayAccessThrowException()
{
$array = ['foo' => 'bar'];
$body = $this->psr17Factory->createStream(json_encode($array));
$this->elasticsearch->setResponse($this->response200->withBody($body));
$this->expectException(ArrayAccessException::class);
$this->elasticsearch['foo'] = 'test';
}
/**
* @covers Elastic\Elasticsearch\Response\Elasticsearch::offsetSet
*/
public function testUnsetArrayAccessThrowException()
{
$array = ['foo' => 'bar'];
$body = $this->psr17Factory->createStream(json_encode($array));
$this->elasticsearch->setResponse($this->response200->withBody($body));
$this->expectException(ArrayAccessException::class);
unset($this->elasticsearch['foo']);
}
/**
* @covers Elastic\Elasticsearch\Response\Elasticsearch::__get()
*/
public function testAccessAsObject()
{
$array = ['foo' => 'bar'];
$body = $this->psr17Factory->createStream(json_encode($array));
$this->elasticsearch->setResponse($this->response200->withBody($body));
$this->assertEquals($array['foo'], $this->elasticsearch->foo);
}
public function testWithStatusForPsr7Version1And2Compatibility()
{
$this->elasticsearch->setResponse($this->response200);
$this->elasticsearch = $this->elasticsearch->withStatus(400);
$this->assertEquals(400, $this->elasticsearch->getStatusCode());
}
}