diff --git a/.ci/test-matrix.yml b/.ci/test-matrix.yml index 466decd0f..b9248f190 100644 --- a/.ci/test-matrix.yml +++ b/.ci/test-matrix.yml @@ -3,6 +3,7 @@ STACK_VERSION: - 7.17-SNAPSHOT PHP_VERSION: + - 8.2-cli - 8.1-cli - 8.0-cli - 7.4-cli diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 95a8291b6..a4704fb5f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,43 +9,38 @@ jobs: strategy: matrix: - php-version: [7.3, 7.4, 8.0, 8.1] + php-version: [7.3, 7.4, 8.0, 8.1, 8.2] os: [ubuntu-latest] es-version: [7.17-SNAPSHOT] steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Use PHP ${{ matrix.php-version }} uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php-version }} - extensions: yaml + extensions: yaml, zip, curl + coverage: none env: COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }} - + - name: Get composer cache directory id: composercache - run: echo "::set-output name=dir::$(composer config cache-files-dir)" + run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT - name: Cache dependencies - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: ${{ steps.composercache.outputs.dir }} - key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }} - restore-keys: ${{ runner.os }}-composer- + key: ${{ runner.os }}-php-${{ matrix.php-version }}-${{ hashFiles('**/composer.json') }} + restore-keys: ${{ runner.os }}-php-${{ matrix.php-version }}- - name: Install dependencies - if: ${{ matrix.php-version != '8.0' }} run: | composer install --prefer-dist - - name: ignore ignore-platform-reqs if it is using php 8 - if: ${{ matrix.php-version == '8.0' }} - run: | - composer install --prefer-dist --ignore-platform-reqs - - name: PHP Coding Standards run: | composer run-script phpcs @@ -68,7 +63,7 @@ jobs: sudo sysctl -w vm.max_map_count=262144 - name: Runs Elasticsearch ${{ matrix.es-version }} - uses: elastic/elastic-github-actions/elasticsearch@master + uses: elastic/elastic-github-actions/elasticsearch@trial-license with: stack-version: ${{ matrix.es-version }} diff --git a/composer.json b/composer.json index c49a8113f..db3830754 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ "ext-yaml": "*", "ext-zip": "*", "mockery/mockery": "^1.2", - "phpstan/phpstan": "^0.12", + "phpstan/phpstan": "^1.10", "phpunit/phpunit": "^9.3", "squizlabs/php_codesniffer": "^3.4", "symfony/finder": "~4.0" @@ -50,7 +50,10 @@ } }, "config": { - "sort-packages": true + "sort-packages": true, + "allow-plugins": { + "php-http/discovery": true + } }, "scripts": { "phpcs": [ diff --git a/phpstan.neon b/phpstan.neon index 8d3ed4325..4aa9e4c09 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -7,3 +7,4 @@ parameters: - '#Constant JSON_THROW_ON_ERROR not found#' - '#Caught class JsonException not found#' - '#Call to method getCode\(\) on an unknown class JsonException#' + - '#Variable \$\w+ in isset\(\) always exists and is not nullable#' diff --git a/src/Elasticsearch/Connections/Connection.php b/src/Elasticsearch/Connections/Connection.php index e13501daf..acec0d002 100644 --- a/src/Elasticsearch/Connections/Connection.php +++ b/src/Elasticsearch/Connections/Connection.php @@ -372,7 +372,7 @@ function ($value) { $uri = $this->path . $uri; } - return $uri ?? ''; + return $uri; } public function getHeaders(): array diff --git a/src/Elasticsearch/Endpoints/AbstractEndpoint.php b/src/Elasticsearch/Endpoints/AbstractEndpoint.php index 489cde5ce..7934013b4 100644 --- a/src/Elasticsearch/Endpoints/AbstractEndpoint.php +++ b/src/Elasticsearch/Endpoints/AbstractEndpoint.php @@ -20,9 +20,7 @@ use Elasticsearch\Common\Exceptions\UnexpectedValueException; use Elasticsearch\Serializers\SerializerInterface; -use Elasticsearch\Transport; -use Exception; -use GuzzleHttp\Ring\Future\FutureArrayInterface; +use Elasticsearch\Utility; abstract class AbstractEndpoint { @@ -127,7 +125,7 @@ public function setIndex($index) $index = implode(",", $index); } - $this->index = urlencode($index); + $this->index = Utility::urlencode($index); return $this; } @@ -155,7 +153,7 @@ public function setType(?string $type) $type = implode(",", $type); } - $this->type = urlencode($type); + $this->type = Utility::urlencode($type); return $this; } @@ -175,7 +173,7 @@ public function setId($docID) $docID = (string) $docID; } - $this->id = urlencode($docID); + $this->id = Utility::urlencode($docID); return $this; } diff --git a/src/Elasticsearch/Serializers/SmartSerializer.php b/src/Elasticsearch/Serializers/SmartSerializer.php index 3a79ff188..f9750b0a4 100644 --- a/src/Elasticsearch/Serializers/SmartSerializer.php +++ b/src/Elasticsearch/Serializers/SmartSerializer.php @@ -93,10 +93,10 @@ private function decode(?string $data): array $result = json_decode($data, true, 512, JSON_THROW_ON_ERROR); return $result; } catch (JsonException $e) { - throw new JsonErrorException($e->getCode(), $data, $result ?? []); + throw new JsonErrorException($e->getCode(), $data, []); } } - throw new JsonErrorException($e->getCode(), $data, $result ?? []); + throw new JsonErrorException($e->getCode(), $data, []); } } diff --git a/src/Elasticsearch/Utility.php b/src/Elasticsearch/Utility.php new file mode 100644 index 000000000..5227c5d03 --- /dev/null +++ b/src/Elasticsearch/Utility.php @@ -0,0 +1,47 @@ +roundRobin = new Elasticsearch\ConnectionPool\Selectors\StickyRoundRobinSelector(); diff --git a/tests/Elasticsearch/Tests/Serializers/SmartSerializerTest.php b/tests/Elasticsearch/Tests/Serializers/SmartSerializerTest.php index 96ad30c56..2592a3ad9 100644 --- a/tests/Elasticsearch/Tests/Serializers/SmartSerializerTest.php +++ b/tests/Elasticsearch/Tests/Serializers/SmartSerializerTest.php @@ -20,7 +20,6 @@ use Elasticsearch\Common\Exceptions\Serializer\JsonErrorException; use Elasticsearch\Serializers\SmartSerializer; -use Mockery as m; use PHPUnit\Framework\TestCase; /** @@ -29,6 +28,11 @@ */ class SmartSerializerTest extends TestCase { + /** + * @var SmartSerializer + */ + protected $serializer; + public function setUp(): void { $this->serializer = new SmartSerializer(); diff --git a/tests/Elasticsearch/Tests/TransportTest.php b/tests/Elasticsearch/Tests/TransportTest.php index 7d8f4a59f..56a5b528b 100644 --- a/tests/Elasticsearch/Tests/TransportTest.php +++ b/tests/Elasticsearch/Tests/TransportTest.php @@ -32,6 +32,27 @@ class TransportTest extends TestCase { + /** + * @var LoggerInterface + */ + protected $logger; + /** + * @var LoggerInterface + */ + protected $trace; + /** + * @var SerializerInterface + */ + protected $serializer; + /** + * @var AbstractConnectionPool + */ + protected $connectionPool; + /** + * @var Connection + */ + protected $connection; + public function setUp(): void { $this->logger = $this->createMock(LoggerInterface::class); diff --git a/tests/Elasticsearch/Tests/UtilityTest.php b/tests/Elasticsearch/Tests/UtilityTest.php new file mode 100644 index 000000000..ec31121ec --- /dev/null +++ b/tests/Elasticsearch/Tests/UtilityTest.php @@ -0,0 +1,77 @@ +assertEquals('true', Utility::getEnv(Utility::ENV_URL_PLUS_AS_SPACE)); + } + + public function testGetEnvWithDollarEnv() + { + $_ENV[Utility::ENV_URL_PLUS_AS_SPACE] = 'true'; + $this->assertEquals('true', Utility::getEnv(Utility::ENV_URL_PLUS_AS_SPACE)); + } + + public function testGetEnvWithPutEnv() + { + putenv(Utility::ENV_URL_PLUS_AS_SPACE . '=true'); + $this->assertEquals('true', Utility::getEnv(Utility::ENV_URL_PLUS_AS_SPACE)); + } + + public function testUrlencodeWithDefault() + { + $url = Utility::urlencode('bar baz'); + $this->assertEquals('bar+baz', $url); + } + + public function testUrlencodeWithDollarServer() + { + $_SERVER[Utility::ENV_URL_PLUS_AS_SPACE] = 'true'; + $url = Utility::urlencode('bar baz'); + $this->assertEquals('bar%20baz', $url); + } + + public function testUrlencodeWithDollarEnv() + { + $_ENV[Utility::ENV_URL_PLUS_AS_SPACE] = 'true'; + $url = Utility::urlencode('bar baz'); + $this->assertEquals('bar%20baz', $url); + } + + public function testUrlencodeWithPutEnv() + { + putenv(Utility::ENV_URL_PLUS_AS_SPACE . '=true'); + $url = Utility::urlencode('bar baz'); + $this->assertEquals('bar%20baz', $url); + } +}