Skip to content

Commit b00e2fb

Browse files
committed
Added the true/false conversion to string for query parameters
1 parent ef7f015 commit b00e2fb

File tree

3 files changed

+121
-4
lines changed

3 files changed

+121
-4
lines changed

src/Traits/EndpointTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected function addQueryString(string $url, array $params, array $keys): stri
3636
$queryParams = [];
3737
foreach ($keys as $k) {
3838
if (isset($params[$k])) {
39-
$queryParams[$k] = $params[$k];
39+
$queryParams[$k] = is_bool($params[$k]) ? ($params[$k] ? 'true' : 'false') : $params[$k];
4040
}
4141
}
4242
if (empty($queryParams)) {

tests/Integration/BasicTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,15 @@ public function testIndexDocuments(string $date, string $open, string $high, str
5858
{
5959
$response = $this->client->index([
6060
'index' => 'stocks',
61-
'refresh' => 'true',
61+
'refresh' => true,
6262
'body' => [
6363
'date' => $date,
6464
'open' => $open,
6565
'high' => $high,
6666
'low' => $low,
6767
'close' => $close,
6868
'volume' => $volume,
69-
'name' => $name,
70-
'refresh' => 'true'
69+
'name' => $name
7170
]
7271
]);
7372

tests/Traits/EndpointTraitTest.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
/**
3+
* Elasticsearch PHP Client
4+
*
5+
* @link https://github.com/elastic/elasticsearch-php
6+
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
7+
* @license https://opensource.org/licenses/MIT MIT License
8+
*
9+
* Licensed to Elasticsearch B.V under one or more agreements.
10+
* Elasticsearch B.V licenses this file to you under the MIT License.
11+
* See the LICENSE file in the project root for more information.
12+
*/
13+
declare(strict_types = 1);
14+
15+
namespace Elastic\Elasticsearch\Tests\Traits;
16+
17+
use Elastic\Elasticsearch\Exception\ContentTypeException;
18+
use Elastic\Elasticsearch\Exception\MissingParameterException;
19+
use Elastic\Elasticsearch\Traits\EndpointTrait;
20+
use PHPUnit\Framework\TestCase;
21+
22+
class EndpointTraitTest extends TestCase
23+
{
24+
use EndpointTrait;
25+
26+
public function getQueryStrings(): array
27+
{
28+
return [
29+
['http://localhost:9200', ['refresh' => true], ['refresh'], 'http://localhost:9200?refresh=true'],
30+
['http://localhost:9200', ['refresh' => 'true'], ['refresh'], 'http://localhost:9200?refresh=true'],
31+
['http://localhost', ['refresh' => 'true'], ['refresh', 'pretty'], 'http://localhost?refresh=true'],
32+
['http://localhost', ['body' => [], 'foo' => 'bar'], ['refresh'], 'http://localhost']
33+
];
34+
}
35+
36+
/**
37+
* @dataProvider getQueryStrings
38+
*/
39+
public function testAddQueryString(string $url, array $params, array $keys, string $expected)
40+
{
41+
$result = $this->addQueryString($url, $params, $keys);
42+
$this->assertEquals($expected, $result);
43+
}
44+
45+
public function getBodySerialized(): array
46+
{
47+
return [
48+
[[ 'foo' => 'bar'], 'application/json', '{"foo":"bar"}'],
49+
[[[ 'foo' => 'bar'], ['bar' => 'baz']], 'application/x-ndjson', "{\"foo\":\"bar\"}\n{\"bar\":\"baz\"}\n"]
50+
];
51+
}
52+
53+
/**
54+
* @dataProvider getBodySerialized
55+
*/
56+
public function testBodySerialize(array $body, string $contentType, string $expected)
57+
{
58+
$result = $this->bodySerialize($body, $contentType);
59+
$this->assertEquals($expected, $result);
60+
}
61+
62+
public function testBodySerializeUnknownContentTypeThrowsException()
63+
{
64+
$this->expectException(ContentTypeException::class);
65+
$this->bodySerialize(['foo' => 'bar'], 'Unknown-content-type');
66+
}
67+
68+
public function getRequestParts(): array
69+
{
70+
return [
71+
[ 'GET', 'http://localhost:9200', ['Foo' => 'bar', 'Content-Type' => 'application/json'], []],
72+
[ 'GET', 'http://localhost', ['Foo' => 'bar', 'Content-Type' => 'application/json'], []],
73+
[ 'POST', 'http://localhost:9200', ['Content-Type' => 'application/json'], ['foo' => 'bar']],
74+
[ 'POST', 'http://localhost:9200', ['Content-Type' => 'application/x-ndjson'], [[ 'foo' => 'bar'], ['bar' => 'baz']]]
75+
];
76+
}
77+
78+
/**
79+
* @dataProvider getRequestParts
80+
*/
81+
public function testCreateRequest(string $method, string $url, array $headers, array $body)
82+
{
83+
$request = $this->createRequest($method, $url, $headers, $body);
84+
$this->assertEquals($method, $request->getMethod());
85+
$this->assertEquals($url, (string) $request->getUri());
86+
$host = parse_url($url, PHP_URL_HOST);
87+
$port = parse_url($url, PHP_URL_PORT);
88+
$this->assertEquals(empty($port) ? $host : $host . ':' . $port, $request->getHeader('Host')[0]);
89+
foreach ($headers as $name => $value) {
90+
$header = $request->getHeader($name);
91+
$this->assertEquals($value, implode(',', $header));
92+
}
93+
if (!empty($body)) {
94+
$this->assertEquals($this->bodySerialize($body, $headers['Content-Type']), (string) $request->getBody());
95+
}
96+
}
97+
98+
public function getRequiredParams(): array
99+
{
100+
return [
101+
[['index'], ['index' => '1'], false],
102+
[['index'], ['foo' => 'bar'], true]
103+
];
104+
}
105+
106+
/**
107+
* @dataProvider getRequiredParams
108+
*/
109+
public function testCheckRequiredParameters(array $required, array $params, bool $exception)
110+
{
111+
if (!$exception) {
112+
$this->assertNull($this->checkRequiredParameters($required, $params));
113+
} else {
114+
$this->expectException(MissingParameterException::class);
115+
$this->checkRequiredParameters($required, $params);
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)