Skip to content

Commit 6e2967b

Browse files
authored
Added ELASTIC_CLIENT_URL_PLUS_AS_SPACE env variable for URL encode (#1303)
* Added ES_URL_PLUS_AS_SPACE env variable for URL encode * Updated phpstan + fixed minor issues * Changed env variable in ELASTIC_CLIENT_URL_PLUS_AS_SPACE * Refactored the test job for github action * Removed duplicate task * Revert ELASTICSEARCH_URL parameter * Added PHP 8.2 in CI
1 parent ea074f2 commit 6e2967b

File tree

13 files changed

+180
-28
lines changed

13 files changed

+180
-28
lines changed

.ci/test-matrix.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ STACK_VERSION:
33
- 7.17-SNAPSHOT
44

55
PHP_VERSION:
6+
- 8.2-cli
67
- 8.1-cli
78
- 8.0-cli
89
- 7.4-cli

.github/workflows/test.yml

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,38 @@ jobs:
99

1010
strategy:
1111
matrix:
12-
php-version: [7.3, 7.4, 8.0, 8.1]
12+
php-version: [7.3, 7.4, 8.0, 8.1, 8.2]
1313
os: [ubuntu-latest]
1414
es-version: [7.17-SNAPSHOT]
1515

1616
steps:
1717
- name: Checkout
18-
uses: actions/checkout@v2
18+
uses: actions/checkout@v3
1919

2020
- name: Use PHP ${{ matrix.php-version }}
2121
uses: shivammathur/setup-php@v2
2222
with:
2323
php-version: ${{ matrix.php-version }}
24-
extensions: yaml
24+
extensions: yaml, zip, curl
25+
coverage: none
2526
env:
2627
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
27-
28+
2829
- name: Get composer cache directory
2930
id: composercache
30-
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
31+
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
3132

3233
- name: Cache dependencies
33-
uses: actions/cache@v2
34+
uses: actions/cache@v3
3435
with:
3536
path: ${{ steps.composercache.outputs.dir }}
36-
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
37-
restore-keys: ${{ runner.os }}-composer-
37+
key: ${{ runner.os }}-php-${{ matrix.php-version }}-${{ hashFiles('**/composer.json') }}
38+
restore-keys: ${{ runner.os }}-php-${{ matrix.php-version }}-
3839

3940
- name: Install dependencies
40-
if: ${{ matrix.php-version != '8.0' }}
4141
run: |
4242
composer install --prefer-dist
4343
44-
- name: ignore ignore-platform-reqs if it is using php 8
45-
if: ${{ matrix.php-version == '8.0' }}
46-
run: |
47-
composer install --prefer-dist --ignore-platform-reqs
48-
4944
- name: PHP Coding Standards
5045
run: |
5146
composer run-script phpcs
@@ -68,7 +63,7 @@ jobs:
6863
sudo sysctl -w vm.max_map_count=262144
6964
7065
- name: Runs Elasticsearch ${{ matrix.es-version }}
71-
uses: elastic/elastic-github-actions/elasticsearch@master
66+
uses: elastic/elastic-github-actions/elasticsearch@trial-license
7267
with:
7368
stack-version: ${{ matrix.es-version }}
7469

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"ext-yaml": "*",
2626
"ext-zip": "*",
2727
"mockery/mockery": "^1.2",
28-
"phpstan/phpstan": "^0.12",
28+
"phpstan/phpstan": "^1.10",
2929
"phpunit/phpunit": "^9.3",
3030
"squizlabs/php_codesniffer": "^3.4",
3131
"symfony/finder": "~4.0"
@@ -50,7 +50,10 @@
5050
}
5151
},
5252
"config": {
53-
"sort-packages": true
53+
"sort-packages": true,
54+
"allow-plugins": {
55+
"php-http/discovery": true
56+
}
5457
},
5558
"scripts": {
5659
"phpcs": [

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ parameters:
77
- '#Constant JSON_THROW_ON_ERROR not found#'
88
- '#Caught class JsonException not found#'
99
- '#Call to method getCode\(\) on an unknown class JsonException#'
10+
- '#Variable \$\w+ in isset\(\) always exists and is not nullable#'

src/Elasticsearch/Connections/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ function ($value) {
372372
$uri = $this->path . $uri;
373373
}
374374

375-
return $uri ?? '';
375+
return $uri;
376376
}
377377

378378
public function getHeaders(): array

src/Elasticsearch/Endpoints/AbstractEndpoint.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020

2121
use Elasticsearch\Common\Exceptions\UnexpectedValueException;
2222
use Elasticsearch\Serializers\SerializerInterface;
23-
use Elasticsearch\Transport;
24-
use Exception;
25-
use GuzzleHttp\Ring\Future\FutureArrayInterface;
23+
use Elasticsearch\Utility;
2624

2725
abstract class AbstractEndpoint
2826
{
@@ -127,7 +125,7 @@ public function setIndex($index)
127125
$index = implode(",", $index);
128126
}
129127

130-
$this->index = urlencode($index);
128+
$this->index = Utility::urlencode($index);
131129

132130
return $this;
133131
}
@@ -155,7 +153,7 @@ public function setType(?string $type)
155153
$type = implode(",", $type);
156154
}
157155

158-
$this->type = urlencode($type);
156+
$this->type = Utility::urlencode($type);
159157

160158
return $this;
161159
}
@@ -175,7 +173,7 @@ public function setId($docID)
175173
$docID = (string) $docID;
176174
}
177175

178-
$this->id = urlencode($docID);
176+
$this->id = Utility::urlencode($docID);
179177

180178
return $this;
181179
}

src/Elasticsearch/Serializers/SmartSerializer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ private function decode(?string $data): array
9393
$result = json_decode($data, true, 512, JSON_THROW_ON_ERROR);
9494
return $result;
9595
} catch (JsonException $e) {
96-
throw new JsonErrorException($e->getCode(), $data, $result ?? []);
96+
throw new JsonErrorException($e->getCode(), $data, []);
9797
}
9898
}
99-
throw new JsonErrorException($e->getCode(), $data, $result ?? []);
99+
throw new JsonErrorException($e->getCode(), $data, []);
100100
}
101101
}
102102

src/Elasticsearch/Utility.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
8+
* @license https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
9+
*
10+
* Licensed to Elasticsearch B.V under one or more agreements.
11+
* Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
12+
* the GNU Lesser General Public License, Version 2.1, at your option.
13+
* See the LICENSE file in the project root for more information.
14+
*/
15+
declare(strict_types = 1);
16+
17+
namespace Elasticsearch;
18+
19+
class Utility
20+
{
21+
const ENV_URL_PLUS_AS_SPACE = 'ELASTIC_CLIENT_URL_PLUS_AS_SPACE';
22+
23+
/**
24+
* Get the ENV variable with a thread safe fallback criteria
25+
* @see https://github.com/elastic/elasticsearch-php/issues/1237
26+
*
27+
* @return string | false
28+
*/
29+
public static function getEnv(string $env)
30+
{
31+
return $_SERVER[$env] ?? $_ENV[$env] ?? getenv($env);
32+
}
33+
34+
/**
35+
* Encode a string in URL using urlencode() or rawurlencode()
36+
* according to env variable ES_URL_PLUS_AS_SPACE.
37+
* If ES_URL_PLUS_AS_SPACE is true use urlencode(), otherwise rawurlencode()
38+
*
39+
* @see https://github.com/elastic/elasticsearch-php/issues/1278
40+
*/
41+
public static function urlencode(string $url): string
42+
{
43+
return self::getEnv(self::ENV_URL_PLUS_AS_SPACE) === 'true'
44+
? rawurlencode($url)
45+
: urlencode($url);
46+
}
47+
}

tests/Elasticsearch/Tests/ConnectionPool/Selectors/RoundRobinSelectorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use Elasticsearch\Connections\ConnectionInterface;
2323

2424
/**
25-
* Class SnifferTest
25+
* Class RoundRobinSelectorTest
2626
*
2727
* @subpackage Tests\ConnectionPool\RoundRobinSelectorTest
2828
*/

tests/Elasticsearch/Tests/ConnectionPool/Selectors/StickyRoundRobinSelectorTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
*/
3030
class StickyRoundRobinSelectorTest extends \PHPUnit\Framework\TestCase
3131
{
32+
/**
33+
* @var Elasticsearch\ConnectionPool\Selectors\StickyRoundRobinSelector
34+
*/
35+
protected $roundRobin;
36+
3237
public function setUp(): void
3338
{
3439
$this->roundRobin = new Elasticsearch\ConnectionPool\Selectors\StickyRoundRobinSelector();

tests/Elasticsearch/Tests/Serializers/SmartSerializerTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
use Elasticsearch\Common\Exceptions\Serializer\JsonErrorException;
2222
use Elasticsearch\Serializers\SmartSerializer;
23-
use Mockery as m;
2423
use PHPUnit\Framework\TestCase;
2524

2625
/**
@@ -29,6 +28,11 @@
2928
*/
3029
class SmartSerializerTest extends TestCase
3130
{
31+
/**
32+
* @var SmartSerializer
33+
*/
34+
protected $serializer;
35+
3236
public function setUp(): void
3337
{
3438
$this->serializer = new SmartSerializer();

tests/Elasticsearch/Tests/TransportTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,27 @@
3232

3333
class TransportTest extends TestCase
3434
{
35+
/**
36+
* @var LoggerInterface
37+
*/
38+
protected $logger;
39+
/**
40+
* @var LoggerInterface
41+
*/
42+
protected $trace;
43+
/**
44+
* @var SerializerInterface
45+
*/
46+
protected $serializer;
47+
/**
48+
* @var AbstractConnectionPool
49+
*/
50+
protected $connectionPool;
51+
/**
52+
* @var Connection
53+
*/
54+
protected $connection;
55+
3556
public function setUp(): void
3657
{
3758
$this->logger = $this->createMock(LoggerInterface::class);
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
8+
* @license https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
9+
*
10+
* Licensed to Elasticsearch B.V under one or more agreements.
11+
* Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
12+
* the GNU Lesser General Public License, Version 2.1, at your option.
13+
* See the LICENSE file in the project root for more information.
14+
*/
15+
16+
17+
declare(strict_types = 1);
18+
19+
namespace Elasticsearch\Tests;
20+
21+
use Elasticsearch\Utility;
22+
use PHPUnit\Framework\TestCase;
23+
24+
class UtilityTest extends TestCase
25+
{
26+
public function tearDown(): void
27+
{
28+
unset($_SERVER[Utility::ENV_URL_PLUS_AS_SPACE]);
29+
unset($_ENV[Utility::ENV_URL_PLUS_AS_SPACE]);
30+
putenv(Utility::ENV_URL_PLUS_AS_SPACE);
31+
}
32+
33+
public function testGetEnvWithDollarServer()
34+
{
35+
$_SERVER[Utility::ENV_URL_PLUS_AS_SPACE] = 'true';
36+
$this->assertEquals('true', Utility::getEnv(Utility::ENV_URL_PLUS_AS_SPACE));
37+
}
38+
39+
public function testGetEnvWithDollarEnv()
40+
{
41+
$_ENV[Utility::ENV_URL_PLUS_AS_SPACE] = 'true';
42+
$this->assertEquals('true', Utility::getEnv(Utility::ENV_URL_PLUS_AS_SPACE));
43+
}
44+
45+
public function testGetEnvWithPutEnv()
46+
{
47+
putenv(Utility::ENV_URL_PLUS_AS_SPACE . '=true');
48+
$this->assertEquals('true', Utility::getEnv(Utility::ENV_URL_PLUS_AS_SPACE));
49+
}
50+
51+
public function testUrlencodeWithDefault()
52+
{
53+
$url = Utility::urlencode('bar baz');
54+
$this->assertEquals('bar+baz', $url);
55+
}
56+
57+
public function testUrlencodeWithDollarServer()
58+
{
59+
$_SERVER[Utility::ENV_URL_PLUS_AS_SPACE] = 'true';
60+
$url = Utility::urlencode('bar baz');
61+
$this->assertEquals('bar%20baz', $url);
62+
}
63+
64+
public function testUrlencodeWithDollarEnv()
65+
{
66+
$_ENV[Utility::ENV_URL_PLUS_AS_SPACE] = 'true';
67+
$url = Utility::urlencode('bar baz');
68+
$this->assertEquals('bar%20baz', $url);
69+
}
70+
71+
public function testUrlencodeWithPutEnv()
72+
{
73+
putenv(Utility::ENV_URL_PLUS_AS_SPACE . '=true');
74+
$url = Utility::urlencode('bar baz');
75+
$this->assertEquals('bar%20baz', $url);
76+
}
77+
}

0 commit comments

Comments
 (0)