diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 6469370..9ebbe1f 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -10,6 +10,6 @@ updates: # Disable automatic rebasing to prevent overloading our CICD system rebase-strategy: "disabled" versioning-strategy: auto - # By default Dependabot has a limit of the number of open PR for version updates + # By default, Dependabot has a limit of the number of open PR for version updates # and security updates. # See other defaults in here: https://docs.github.com/en/github/administering-a-repository/configuration-options-for-dependency-updates diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index ea8167b..9e6c071 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -1,81 +1,84 @@ name: Continuous Integration on: - push: - branches: - - master - - 6.0 - - 5.0 - - 4.0 - - 3.0 - - 2.5 - pull_request: - types: [ opened, synchronize, reopened ] + push: + branches: + - master + - '6.0' + - '5.0' + - '4.0' + - '3.0' + - '2.5' + + pull_request: + types: [ opened, synchronize, reopened ] env: - fail-fast: true + fail-fast: true jobs: - build: - name: PHPUnit - runs-on: ubuntu-20.04 - strategy: - matrix: - php-version: - - 8.0 - dependencies: - - lowest - - highest + build: + name: PHPUnit + runs-on: ubuntu-22.04 + strategy: + matrix: + php-version: + - 8.1 + - 8.2 + - 8.3 + dependencies: + - lowest + - highest - steps: - - name: Checkout - uses: actions/checkout@v2 + steps: + - name: Checkout + uses: actions/checkout@v4 - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-version }} - coverage: xdebug - extensions: ${{ matrix.extensions }} + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-version }} + coverage: xdebug + extensions: ${{ matrix.extensions }} - - name: Install Composer Dependencies - uses: ramsey/composer-install@v1 - with: - dependency-versions: ${{ matrix.dependencies }} - composer-options: "--prefer-stable" + - name: Install Composer Dependencies + uses: ramsey/composer-install@v3 + with: + dependency-versions: ${{ matrix.dependencies }} + composer-options: "--prefer-stable" - - name: Run PHPUnit - run: vendor/bin/phpunit + - name: Run PHPUnit + run: vendor/bin/phpunit - - name: Upload coverage files - uses: actions/upload-artifact@v2 - with: - name: ${{ github.job }}-${{ matrix.php-version }}-coverage - path: tests/.results/ + - name: Upload coverage files + uses: actions/upload-artifact@v4 + with: + name: ${{ github.job }}-${{ matrix.php-version }}-${{ matrix.dependencies }}-coverage + path: tests/.results/ - sonarcloud: - needs: build - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - with: - # Disabling shallow clone is recommended for improving relevancy of reporting - fetch-depth: 0 + sonarcloud: + needs: build + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + # Disabling shallow clone is recommended for improving relevancy of reporting + fetch-depth: 0 - - uses: actions/download-artifact@v2 - with: - name: build-8-coverage - path: tests/.results/ + - uses: actions/download-artifact@v4 + with: + name: build-8.1-highest-coverage + path: tests/.results/ - - name: Fix Code Coverage Paths - working-directory: tests/.results/ - run: | - sed -i 's@'$GITHUB_WORKSPACE'@/github/workspace/@g' tests-clover.xml - sed -i 's@'$GITHUB_WORKSPACE'@/github/workspace/@g' tests-junit.xml + - name: Fix Code Coverage Paths + working-directory: tests/.results/ + run: | + sed -i 's@'$GITHUB_WORKSPACE'@/github/workspace/@g' tests-clover.xml + sed -i 's@'$GITHUB_WORKSPACE'@/github/workspace/@g' tests-junit.xml - - name: SonarCloud Scan - uses: sonarsource/sonarcloud-github-action@master - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + - name: SonarCloud Scan + uses: sonarsource/sonarcloud-github-action@master + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} diff --git a/.gitignore b/.gitignore index 0470d61..b725c4b 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,9 @@ deploy.prop log/ .env .env.test -.phpunit.result.cache +.phpunit.cache +.phpunit.result tests/.results composer.lock .php-cs-fixer.cache +phpunit.log diff --git a/CHANGELOG.md b/CHANGELOG.md index 2027328..26613bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,26 @@ to [Semantic Versioning](http://semver.org/). ### Deprecated +## [7.0.0](https://github.com/kununu/elasticsearch/compare/v7.0.0...v6.0.1) + +### Backward Compatibility Breaks + +* Drop support for PHP 8.0 - PHP 8.1 is now the minimum version. + +### Bugfixes + +### Added + +### Improvements + +* Upgraded PHPUnit to version 10.5 +* Refactor some code to take advantage of PHP 8.1 features (read-only properties) +* Fix tests that used deprecated/removed features on PHPUnit 10.5 +* Refactor tests to be more PHPUnit 10.5 compliant (e.g. using attributes for data providers, make all data providers static, etc.) +* Updated versions of continuous integration components + +### Deprecated + ## [6.0.1](https://github.com/kununu/elasticsearch/compare/v6.0.1...v6.0.0) ### Backward Compatibility Breaks diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6b5907e..0bbe222 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -2,13 +2,13 @@ Contributions are more than **welcome** and will be fully **credited**. -We accept contributions via Pull Requests on [Github](https://github.com/kununu/elasticsearch). +We accept contributions via Pull Requests on [GitHub](https://github.com/kununu/elasticsearch). ## Pull Requests -- **[PSR-12 Coding Standard](https://www.php-fig.org/psr/psr-12/)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). +- **[kununu Coding Standards](https://github.com/kununu/kununu-scripts)** - The kununu coding standards are an extension of the [PSR-12](https://www.php-fig.org/psr/psr-12/). Check out [kununu Coding Standards](https://github.com/kununu/kununu-scripts) for more details. In development mode the package [kununu-scripts](https://github.com/kununu/kununu-scripts) is already a dependency that expose commands that you can use to meet our standards. -- **Add tests!** - to ensure a high quality code base, your code patch can't be accepted if it doesn't have tests. +- **Add tests!** - to ensure a high quality code base, your code patch can't be accepted if it does not have tests. - **Document any change in behaviour** - Make sure the [CHANGELOG.md](CHANGELOG.md), [README.md](README.md) and any other relevant documentation are kept up-to-date. diff --git a/README.md b/README.md index 9132d0d..74d6b44 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,26 @@ # kununu/elasticsearch ## Purpose + This package aims to 1. reduce cognitive load when interacting with Elasticsearch by providing an intuitive query language with a fluent interface while staying very close to Elasticsearch terminology 2. make your project independent of the underlying client library ## Quickstart It does not take a lot to get you up and running with Elasticsearch. All that's required is a `Repository` which can be used to execute requests (e.g. to save a document, query for documents, etc.) + ```php +build(); +$client = ClientBuilder::create()->build(); // create a new repository and bind it to my_index/my_type $repository = new Repository( @@ -43,6 +54,7 @@ $repository->delete('the_document_id'); ``` ## Technical Concept + The key features of this package are: - Repositories - Queries @@ -50,9 +62,9 @@ The key features of this package are: ### Repositories -Very similar -to [Entity Repositories in Doctrine](https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/working-with-objects.html) -, a `Repository` in this package is a class which capsules Elasticsearch specific logic - for a specific index. +Very similar to [Entity Repositories in Doctrine](https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/working-with-objects.html), a `Repository` in this package is a class which encapsulates +Elasticsearch specific logic - for a specific index. + Repositories execute queries against the index (and type) they are bound to. [More explanation and examples](doc/REPOSITORY.md) @@ -73,7 +85,7 @@ This package contains two implementations of the `QueryInterface`: ### IndexManager -This package provides easy access to a a few commonly used Elasticsearch index management features like creating indices +This package provides easy access to a few commonly used Elasticsearch index management features like creating indices and managing aliases. [More explanation and examples](doc/INDEX_MANAGER.md) @@ -90,6 +102,7 @@ library. Any other version may or may not work. | 4.x | 7.x | 7.9 | \>=8.0 | | 5.x | 7.x | 7.9 | \>=8.0 | | 6.x | 7.x | 7.9 | \>=8.0 | +| 7.x | 7.x | 7.9 | \>=8.1 | See also https://github.com/elastic/elasticsearch-php#version-matrix diff --git a/SECURITY.md b/SECURITY.md index 2e1318e..c891b35 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -1,20 +1,24 @@ # Security Policy +## For external contributors -## Reporting a Vulnerability +If you want to report any security vulnerabilities please do so by creating an [issue](https://github.com/kununu/elasticsearch/issues). +Additionally, if you have a fix then please create a pull request and link it to the issue you have created. -Jira is our entrypoint to report security vulnerabilities. Having this mind a KUNSECU user story (type Vulnerability) needs to be created. +## For kununu developers -### How to fill the user story? +### Reporting a Vulnerability -Follow this [documentation](https://confluence.xing.hh/pages/viewpage.action?pageId=381133070). +Jira is our entrypoint to report security vulnerabilities. Having this mind a KUNSECU user story (type Vulnerability) needs to be created. +#### How to fill the user story? -### To which team do I assign the user story? +Follow this [documentation](https://new-work.atlassian.net/wiki/spaces/kununu/pages/47846323/Vulnerability+Issue+Type+Jira). -Follow the [component ownership matrix](https://confluence.xing.hh/display/kununu/Component+ownership+and+support) and assign it to the corresponding team. +#### To which team do I assign the user story? +Follow the [domain ownership matrix](https://new-work.atlassian.net/wiki/spaces/kununu/pages/113148000/Domain+ownership+matrix) and assign it to the corresponding team. ## Reporting the update of dependencies -This is the benefit of having Dependabot. It will open pull requests for security and version updates. For more information check the Github [documentation](https://docs.github.com/en/github/administering-a-repository/managing-pull-requests-for-dependency-updates). +This is the benefit of having Dependabot. It will open pull requests for security and version updates. For more information check the GitHub [documentation](https://docs.github.com/en/github/administering-a-repository/managing-pull-requests-for-dependency-updates). diff --git a/composer.json b/composer.json index 2b54075..f4e8b8a 100644 --- a/composer.json +++ b/composer.json @@ -5,17 +5,17 @@ "license": "proprietary", "minimum-stability": "stable", "require": { - "php": ">=8.0", + "php": ">=8.1", "ext-ctype": "*", "ext-iconv": "*", "ext-json": "*", "ext-mbstring": "*", - "elasticsearch/elasticsearch": "^7.0.0", - "psr/log": "~1.0" + "elasticsearch/elasticsearch": "^7.0", + "psr/log": "^1.0|^2.0|^3.0" }, "require-dev": { - "kununu/scripts": ">=4.0", - "phpunit/phpunit": "^9.6" + "kununu/scripts": ">=5.0", + "phpunit/phpunit": "^10.5" }, "autoload": { "psr-4": { @@ -28,8 +28,8 @@ } }, "scripts": { - "test": "phpunit --no-coverage tests", - "test-coverage": "XDEBUG_MODE=coverage phpunit --coverage-clover tests/.results/coverage.xml --coverage-html tests/.results/html/ tests" + "test": "phpunit --log-events-text phpunit.log --no-coverage --no-logging --no-progress --testsuite Full", + "test-coverage": "XDEBUG_MODE=coverage phpunit --log-events-text phpunit.log --no-progress --testsuite Full" }, "scripts-descriptions": { "test": "Run all tests", @@ -39,9 +39,6 @@ "allow-plugins": { "kununu/scripts": true }, - "preferred-install": { - "*": "dist" - }, "sort-packages": true } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index c52793f..3ef9522 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,12 +1,9 @@ - + - - - src - + xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="vendor/autoload.php" + colors="true" beStrictAboutChangesToGlobalState="true" cacheDirectory=".phpunit.cache" testdox="true"> + @@ -17,11 +14,16 @@ - + tests + + + src + + diff --git a/src/Exception/BulkException.php b/src/Exception/BulkException.php index e2d35af..d18b853 100644 --- a/src/Exception/BulkException.php +++ b/src/Exception/BulkException.php @@ -7,8 +7,11 @@ class BulkException extends WriteOperationException { - public function __construct(string $message = '', ?Throwable $previous = null, protected ?array $operations = null) - { + public function __construct( + string $message = '', + ?Throwable $previous = null, + protected readonly ?array $operations = null + ) { parent::__construct($message, $previous); } diff --git a/src/Exception/DeleteException.php b/src/Exception/DeleteException.php index 9261ff5..ab4b94d 100644 --- a/src/Exception/DeleteException.php +++ b/src/Exception/DeleteException.php @@ -7,8 +7,11 @@ class DeleteException extends RepositoryException { - public function __construct(string $message = '', ?Throwable $previous = null, protected mixed $documentId = null) - { + public function __construct( + string $message = '', + ?Throwable $previous = null, + protected readonly mixed $documentId = null + ) { parent::__construct($message, $previous); } diff --git a/src/Exception/ReadOperationException.php b/src/Exception/ReadOperationException.php index 4e08ecf..27010a9 100644 --- a/src/Exception/ReadOperationException.php +++ b/src/Exception/ReadOperationException.php @@ -7,8 +7,11 @@ class ReadOperationException extends RepositoryException { - public function __construct(string $message = '', ?Throwable $previous = null, protected mixed $query = null) - { + public function __construct( + string $message = '', + ?Throwable $previous = null, + protected readonly mixed $query = null + ) { parent::__construct($message, $previous); } diff --git a/src/Exception/UpdateException.php b/src/Exception/UpdateException.php index fab200c..32e34f8 100644 --- a/src/Exception/UpdateException.php +++ b/src/Exception/UpdateException.php @@ -10,8 +10,8 @@ class UpdateException extends WriteOperationException public function __construct( string $message = '', ?Throwable $previous = null, - protected mixed $documentId = null, - protected ?array $document = null + protected readonly mixed $documentId = null, + protected readonly ?array $document = null ) { parent::__construct($message, $previous); } diff --git a/src/Exception/UpsertException.php b/src/Exception/UpsertException.php index 4e7a506..e4e16e2 100644 --- a/src/Exception/UpsertException.php +++ b/src/Exception/UpsertException.php @@ -10,8 +10,8 @@ class UpsertException extends WriteOperationException public function __construct( string $message = '', ?Throwable $previous = null, - protected mixed $documentId = null, - protected ?array $document = null + protected readonly mixed $documentId = null, + protected readonly ?array $document = null ) { parent::__construct($message, $previous); } diff --git a/src/IndexManagement/IndexManager.php b/src/IndexManagement/IndexManager.php index c490792..9211852 100644 --- a/src/IndexManagement/IndexManager.php +++ b/src/IndexManagement/IndexManager.php @@ -18,7 +18,7 @@ class IndexManager implements IndexManagerInterface, LoggerAwareInterface { use LoggerAwareTrait; - public function __construct(protected Client $client) + public function __construct(protected readonly Client $client) { } diff --git a/src/Query/Aggregation.php b/src/Query/Aggregation.php index cb50da2..cad5ff2 100644 --- a/src/Query/Aggregation.php +++ b/src/Query/Aggregation.php @@ -15,24 +15,21 @@ class Aggregation implements AggregationInterface */ public const GLOBAL = 'global'; + protected readonly string $name; /** @var AggregationInterface[] */ protected array $nestedAggregations = []; public function __construct( - protected ?string $field, - protected string $type, - protected string $name = '', - protected array $options = [] + protected readonly ?string $field, + protected readonly string $type, + string $name = '', + protected readonly array $options = [] ) { if (!Metric::hasConstant($type) && !Bucket::hasConstant($type) && $type !== static::GLOBAL) { throw new InvalidArgumentException('Unknown type "' . $type . '" given'); } - if (empty($name)) { - $name = spl_object_hash($this); - } - - $this->name = $name; + $this->name = empty($name) ? spl_object_hash($this) : $name; } public static function create(string $field, string $type, string $name = '', array $options = []): Aggregation diff --git a/src/Query/Criteria/Filter.php b/src/Query/Criteria/Filter.php index 13a7ba2..24577c4 100644 --- a/src/Query/Criteria/Filter.php +++ b/src/Query/Criteria/Filter.php @@ -17,10 +17,10 @@ class Filter implements FilterInterface { public function __construct( - protected string $field, - protected mixed $value, - protected ?string $operator = null, - protected array $options = [] + protected readonly string $field, + protected readonly mixed $value, + protected readonly ?string $operator = null, + protected readonly array $options = [] ) { if ($operator !== null && !Operator::hasConstant($operator)) { throw new InvalidArgumentException('Unknown operator "' . $operator . '" given'); diff --git a/src/Query/Criteria/Search.php b/src/Query/Criteria/Search.php index 91947c9..14a52fb 100644 --- a/src/Query/Criteria/Search.php +++ b/src/Query/Criteria/Search.php @@ -25,10 +25,10 @@ class Search implements SearchInterface public const TERM = TermQuery::KEYWORD; public function __construct( - protected array $fields, - protected string $queryString, - protected string $type = self::QUERY_STRING, - protected array $options = [] + protected readonly array $fields, + protected readonly string $queryString, + protected readonly string $type = self::QUERY_STRING, + protected readonly array $options = [] ) { if (empty($fields)) { throw new InvalidArgumentException('No fields given'); diff --git a/src/Query/RawQuery.php b/src/Query/RawQuery.php index d61430a..5af1415 100644 --- a/src/Query/RawQuery.php +++ b/src/Query/RawQuery.php @@ -5,7 +5,7 @@ class RawQuery extends AbstractQuery { - public function __construct(protected array $body = []) + public function __construct(protected readonly array $body = []) { } diff --git a/src/Repository/Repository.php b/src/Repository/Repository.php index 2626ac2..cbd08fd 100644 --- a/src/Repository/Repository.php +++ b/src/Repository/Repository.php @@ -30,9 +30,9 @@ class Repository implements RepositoryInterface, LoggerAwareInterface protected const EXCEPTION_PREFIX = 'Elasticsearch exception: '; - protected RepositoryConfiguration $config; + protected readonly RepositoryConfiguration $config; - public function __construct(protected Client $client, array $config) + public function __construct(protected readonly Client $client, array $config) { $this->config = new RepositoryConfiguration($config); } @@ -141,7 +141,7 @@ public function findByQuery(QueryInterface $query): ResultIteratorInterface public function findScrollableByQuery( QueryInterface $query, - string|null $scrollContextKeepalive = null + ?string $scrollContextKeepalive = null ): ResultIteratorInterface { return $this->executeRead( function() use ($query, $scrollContextKeepalive) { @@ -157,7 +157,7 @@ function() use ($query, $scrollContextKeepalive) { public function findByScrollId( string $scrollId, - string|null $scrollContextKeepalive = null + ?string $scrollContextKeepalive = null ): ResultIteratorInterface { return $this->executeRead( fn() => $this->parseRawSearchResponse( diff --git a/src/Result/AggregationResult.php b/src/Result/AggregationResult.php index fcdfe26..8e0df87 100644 --- a/src/Result/AggregationResult.php +++ b/src/Result/AggregationResult.php @@ -5,7 +5,7 @@ class AggregationResult implements AggregationResultInterface { - public function __construct(protected string $name, protected array $fields = []) + public function __construct(protected readonly string $name, protected readonly array $fields = []) { } diff --git a/tests/IndexManagement/IndexManagerTest.php b/tests/IndexManagement/IndexManagerTest.php index b1be774..2113c94 100644 --- a/tests/IndexManagement/IndexManagerTest.php +++ b/tests/IndexManagement/IndexManagerTest.php @@ -12,6 +12,7 @@ use Kununu\Elasticsearch\Exception\NoIndexForAliasException; use Kununu\Elasticsearch\IndexManagement\IndexManager; use Kununu\Elasticsearch\IndexManagement\IndexManagerInterface; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; @@ -19,25 +20,25 @@ final class IndexManagerTest extends TestCase { - protected const INDEX = 'my_index'; - protected const ALIAS = 'my_alias'; - protected const MAPPING = [ + private const INDEX = 'my_index'; + private const ALIAS = 'my_alias'; + private const MAPPING = [ 'properties' => [ 'field_a' => ['type' => 'text'], ], ]; - protected Client|MockObject $clientMock; - protected IndicesNamespace|MockObject $indicesMock; - protected LoggerInterface|MockObject $loggerMock; + private MockObject&Client $clientMock; + private MockObject&IndicesNamespace $indicesMock; + private MockObject&LoggerInterface $loggerMock; public static function notAcknowledgedResponseDataProvider(): array { return [ - 'acknowledged false' => [ + 'acknowledged_false' => [ 'response' => ['acknowledged' => false], ], - 'acknowledged field missing' => [ + 'acknowledged_field_missing' => [ 'response' => [], ], ]; @@ -48,7 +49,7 @@ public function testAddAlias(): void $this->setUpIndexOperation(); $this->indicesMock - ->expects($this->once()) + ->expects(self::once()) ->method('putAlias') ->with( [ @@ -59,7 +60,7 @@ public function testAddAlias(): void ->willReturn(['acknowledged' => true]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $this->getManager()->addAlias( @@ -68,13 +69,13 @@ public function testAddAlias(): void ); } - /** @dataProvider notAcknowledgedResponseDataProvider */ + #[DataProvider('notAcknowledgedResponseDataProvider')] public function testAddAliasFails(array $response): void { $this->setUpIndexOperation(); $this->indicesMock - ->expects($this->once()) + ->expects(self::once()) ->method('putAlias') ->with( [ @@ -85,7 +86,7 @@ public function testAddAliasFails(array $response): void ->willReturn($response); $this->loggerMock - ->expects($this->once()) + ->expects(self::once()) ->method('error') ->with( 'Elasticsearch exception: Could not add alias for index', @@ -106,7 +107,7 @@ public function testRemoveAlias(): void $this->setUpIndexOperation(); $this->indicesMock - ->expects($this->once()) + ->expects(self::once()) ->method('deleteAlias') ->with( [ @@ -117,7 +118,7 @@ public function testRemoveAlias(): void ->willReturn(['acknowledged' => true]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $this->getManager()->removeAlias( @@ -126,13 +127,13 @@ public function testRemoveAlias(): void ); } - /** @dataProvider notAcknowledgedResponseDataProvider */ + #[DataProvider('notAcknowledgedResponseDataProvider')] public function testRemoveAliasFails(array $response): void { $this->setUpIndexOperation(); $this->indicesMock - ->expects($this->once()) + ->expects(self::once()) ->method('deleteAlias') ->with( [ @@ -143,7 +144,7 @@ public function testRemoveAliasFails(array $response): void ->willReturn($response); $this->loggerMock - ->expects($this->once()) + ->expects(self::once()) ->method('error') ->with( 'Elasticsearch exception: Could not remove alias for index', @@ -167,7 +168,7 @@ public function testSwitchAlias(): void $toIndex = 'to_ ' . self::INDEX; $this->indicesMock - ->expects($this->once()) + ->expects(self::once()) ->method('updateAliases') ->with( [ @@ -182,7 +183,7 @@ public function testSwitchAlias(): void ->willReturn(['acknowledged' => true]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $this->getManager()->switchAlias( @@ -192,7 +193,7 @@ public function testSwitchAlias(): void ); } - /** @dataProvider notAcknowledgedResponseDataProvider */ + #[DataProvider('notAcknowledgedResponseDataProvider')] public function testSwitchAliasFails(array $response): void { $this->setUpIndexOperation(); @@ -201,7 +202,7 @@ public function testSwitchAliasFails(array $response): void $toIndex = 'to_ ' . self::INDEX; $this->indicesMock - ->expects($this->once()) + ->expects(self::once()) ->method('updateAliases') ->with( [ @@ -216,7 +217,7 @@ public function testSwitchAliasFails(array $response): void ->willReturn($response); $this->loggerMock - ->expects($this->once()) + ->expects(self::once()) ->method('error') ->with( 'Elasticsearch exception: Could not switch alias for index', @@ -243,7 +244,7 @@ public static function createIndexDataProvider(): array $settings = ['index' => ['number_of_shards' => 5, 'number_of_replicas' => 1]]; return [ - 'completely blank' => [ + 'completely_blank' => [ 'input' => [ self::INDEX, ], @@ -251,7 +252,7 @@ public static function createIndexDataProvider(): array 'index' => self::INDEX, ], ], - 'no aliases, no settings' => [ + 'no_aliases_no_settings' => [ 'input' => [ self::INDEX, self::MAPPING, @@ -261,7 +262,7 @@ public static function createIndexDataProvider(): array 'body' => ['mappings' => self::MAPPING], ], ], - 'with alias, no settings' => [ + 'with_alias_no_settings' => [ 'input' => [ self::INDEX, self::MAPPING, @@ -272,7 +273,7 @@ public static function createIndexDataProvider(): array 'body' => ['mappings' => self::MAPPING, 'aliases' => [self::ALIAS => new stdClass()]], ], ], - 'no aliases, with settings' => [ + 'no_aliases_with_settings' => [ 'input' => [ self::INDEX, self::MAPPING, @@ -284,7 +285,7 @@ public static function createIndexDataProvider(): array 'body' => ['mappings' => self::MAPPING, 'settings' => $settings], ], ], - 'with alias and settings' => [ + 'with_alias_and_settings' => [ 'input' => [ self::INDEX, self::MAPPING, @@ -303,36 +304,36 @@ public static function createIndexDataProvider(): array ]; } - /** @dataProvider createIndexDataProvider */ + #[DataProvider('createIndexDataProvider')] public function testCreateIndex(array $input, array $expectedRequestBody): void { $this->setUpIndexOperation(); $this->indicesMock - ->expects($this->once()) + ->expects(self::once()) ->method('create') ->with($expectedRequestBody) ->willReturn(['acknowledged' => true]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $this->getManager()->createIndex(...$input); } - /** @dataProvider notAcknowledgedResponseDataProvider */ + #[DataProvider('notAcknowledgedResponseDataProvider')] public function testCreateIndexFails(array $response): void { $this->setUpIndexOperation(); $this->indicesMock - ->expects($this->once()) + ->expects(self::once()) ->method('create') ->willReturn($response); $this->loggerMock - ->expects($this->once()) + ->expects(self::once()) ->method('error') ->with( 'Elasticsearch exception: Could not create index', @@ -358,7 +359,7 @@ public function testDeleteIndex(): void $this->setUpIndexOperation(); $this->indicesMock - ->expects($this->once()) + ->expects(self::once()) ->method('delete') ->with( ['index' => self::INDEX] @@ -366,19 +367,19 @@ public function testDeleteIndex(): void ->willReturn(['acknowledged' => true]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $this->getManager()->deleteIndex(self::INDEX); } - /** @dataProvider notAcknowledgedResponseDataProvider */ + #[DataProvider('notAcknowledgedResponseDataProvider')] public function testDeleteIndexFails(array $response): void { $this->setUpIndexOperation(); $this->indicesMock - ->expects($this->once()) + ->expects(self::once()) ->method('delete') ->with( ['index' => self::INDEX] @@ -386,7 +387,7 @@ public function testDeleteIndexFails(array $response): void ->willReturn($response); $this->loggerMock - ->expects($this->once()) + ->expects(self::once()) ->method('error') ->with( 'Elasticsearch exception: Could not delete index', @@ -404,7 +405,7 @@ public function testPutMapping(): void $this->setUpIndexOperation(); $this->indicesMock - ->expects($this->once()) + ->expects(self::once()) ->method('putMapping') ->with( [ @@ -416,19 +417,19 @@ public function testPutMapping(): void ->willReturn(['acknowledged' => true]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $this->getManager()->putMapping(self::INDEX, self::MAPPING, ['extra_param' => true]); } - /** @dataProvider notAcknowledgedResponseDataProvider */ + #[DataProvider('notAcknowledgedResponseDataProvider')] public function testPutMappingFails(array $response): void { $this->setUpIndexOperation(); $this->indicesMock - ->expects($this->once()) + ->expects(self::once()) ->method('putMapping') ->with( [ @@ -439,7 +440,7 @@ public function testPutMappingFails(array $response): void ->willReturn($response); $this->loggerMock - ->expects($this->once()) + ->expects(self::once()) ->method('error') ->with( 'Elasticsearch exception: Could not put mapping', @@ -459,28 +460,28 @@ public function testPutMappingFails(array $response): void public static function indicesByAliasDataProvider(): array { return [ - 'no indices mapped to alias' => [ + 'no_indices_mapped_to_alias' => [ 'es_response' => [], 'expected_result' => [], ], - 'one index mapped to alias' => [ + 'one_index_mapped_to_alias' => [ 'es_response' => [self::INDEX => ['foo' => 'bar']], 'expected_result' => [self::INDEX], ], - 'multiple indices mapped to alias' => [ + 'multiple_indices_mapped_to_alias' => [ 'es_response' => [self::INDEX => ['foo' => 'bar'], 'another_index' => []], 'expected_result' => [self::INDEX, 'another_index'], ], ]; } - /** @dataProvider indicesByAliasDataProvider */ + #[DataProvider('indicesByAliasDataProvider')] public function testGetIndicesByAlias(array $esResponse, array $expectedResult): void { $this->setUpIndexOperation(); $this->indicesMock - ->expects($this->once()) + ->expects(self::once()) ->method('getAlias') ->with( ['name' => self::ALIAS] @@ -488,10 +489,10 @@ public function testGetIndicesByAlias(array $esResponse, array $expectedResult): ->willReturn($esResponse); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); - $this->assertEquals($expectedResult, $this->getManager()->getIndicesByAlias(self::ALIAS)); + self::assertEquals($expectedResult, $this->getManager()->getIndicesByAlias(self::ALIAS)); } public function testGetIndicesByAliasFails(): void @@ -499,12 +500,12 @@ public function testGetIndicesByAliasFails(): void $this->setUpIndexOperation(); $this->indicesMock - ->expects($this->once()) + ->expects(self::once()) ->method('getAlias') ->willThrowException(new Exception('something happened')); $this->loggerMock - ->expects($this->once()) + ->expects(self::once()) ->method('error') ->with( 'Elasticsearch exception: Unable to get indices by alias', @@ -525,33 +526,33 @@ public function testGetIndicesByAliasCatches404(): void $this->setUpIndexOperation(); $this->indicesMock - ->expects($this->once()) + ->expects(self::once()) ->method('getAlias') ->willThrowException(new Missing404Exception()); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); - $this->assertEquals([], $this->getManager()->getIndicesByAlias(self::ALIAS)); + self::assertEquals([], $this->getManager()->getIndicesByAlias(self::ALIAS)); } public static function indexAliasMappingDataProvider(): array { return [ - 'no indices' => [ + 'no_indices' => [ 'es_response' => [], 'expected_result' => [], ], - 'one index without alias' => [ + 'one_index_without_alias' => [ 'es_response' => [self::INDEX => ['aliases' => []]], 'expected_result' => [self::INDEX => []], ], - 'one index with one alias' => [ + 'one_index_with_one_alias' => [ 'es_response' => [self::INDEX => ['aliases' => [self::ALIAS => ['foo' => 'bar']]]], 'expected_result' => [self::INDEX => [self::ALIAS]], ], - 'one index with multiple aliases' => [ + 'one_index_with_multiple_aliases' => [ 'es_response' => [ self::INDEX => [ 'aliases' => [ @@ -562,18 +563,18 @@ public static function indexAliasMappingDataProvider(): array ], 'expected_result' => [self::INDEX => [self::ALIAS, 'other_alias']], ], - 'multiple indices without alias' => [ + 'multiple_indices_without_alias' => [ 'es_response' => [self::INDEX => ['aliases' => []], 'another_index' => ['aliases' => []]], 'expected_result' => [self::INDEX => [], 'another_index' => []], ], - 'multiple indices with one alias' => [ + 'multiple_indices_with_one_alias' => [ 'es_response' => [ self::INDEX => ['aliases' => [self::ALIAS => ['foo' => 'bar']]], 'another_index' => ['aliases' => ['fancy_index_alias' => []]], ], 'expected_result' => [self::INDEX => [self::ALIAS], 'another_index' => ['fancy_index_alias']], ], - 'multiple indices with multiple aliases' => [ + 'multiple_indices_with_multiple_aliases' => [ 'es_response' => [ self::INDEX => ['aliases' => [self::ALIAS => ['foo' => 'bar'], 'other_alias' => []]], 'other_index' => ['aliases' => ['my_special_alias' => []]], @@ -586,13 +587,13 @@ public static function indexAliasMappingDataProvider(): array ]; } - /** @dataProvider indexAliasMappingDataProvider */ + #[DataProvider('indexAliasMappingDataProvider')] public function testGetIndicesAliasesMapping(array $esResponse, array $expectedResult): void { $this->setUpIndexOperation(); $this->indicesMock - ->expects($this->once()) + ->expects(self::once()) ->method('get') ->with( ['index' => '_all'] @@ -600,10 +601,10 @@ public function testGetIndicesAliasesMapping(array $esResponse, array $expectedR ->willReturn($esResponse); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); - $this->assertEquals($expectedResult, $this->getManager()->getIndicesAliasesMapping()); + self::assertEquals($expectedResult, $this->getManager()->getIndicesAliasesMapping()); } public function testGetIndicesAliasesMappingCatches404(): void @@ -611,7 +612,7 @@ public function testGetIndicesAliasesMappingCatches404(): void $this->setUpIndexOperation(); $this->indicesMock - ->expects($this->once()) + ->expects(self::once()) ->method('get') ->with( ['index' => '_all'] @@ -619,10 +620,10 @@ public function testGetIndicesAliasesMappingCatches404(): void ->willThrowException(new Missing404Exception()); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); - $this->assertEquals([], $this->getManager()->getIndicesAliasesMapping()); + self::assertEquals([], $this->getManager()->getIndicesAliasesMapping()); } public function testGetIndicesAliasesMappingFails(): void @@ -630,7 +631,7 @@ public function testGetIndicesAliasesMappingFails(): void $this->setUpIndexOperation(); $this->indicesMock - ->expects($this->once()) + ->expects(self::once()) ->method('get') ->with( ['index' => '_all'] @@ -638,7 +639,7 @@ public function testGetIndicesAliasesMappingFails(): void ->willThrowException(new Exception('something happened')); $this->loggerMock - ->expects($this->once()) + ->expects(self::once()) ->method('error') ->with( 'Elasticsearch exception: Unable to get indices', @@ -656,7 +657,7 @@ public function testGetIndicesAliasesMappingFails(): void public function testReindex(): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('reindex') ->with( [ @@ -673,7 +674,7 @@ public function testReindex(): void ->willReturn(['acknowledged' => true]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $this->getManager()->reindex(self::INDEX, self::INDEX . '_v2'); @@ -682,12 +683,12 @@ public function testReindex(): void public function testReindexFails(): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('reindex') ->willThrowException(new Exception('something happened')); $this->loggerMock - ->expects($this->once()) + ->expects(self::once()) ->method('error') ->with( 'Elasticsearch exception: Unable to reindex', @@ -707,12 +708,12 @@ public function testReindexFails(): void public function testPutSettings(): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('indices') ->willReturn($this->indicesMock); $this->indicesMock - ->expects($this->once()) + ->expects(self::once()) ->method('putSettings') ->with([ 'index' => 'test', @@ -737,11 +738,11 @@ public function testPutSettings(): void public function testDisallowedPutSettings(): void { $this->clientMock - ->expects($this->never()) + ->expects(self::never()) ->method('indices'); $this->indicesMock - ->expects($this->never()) + ->expects(self::never()) ->method('putSettings'); $this->expectException(IndexManagementException::class); @@ -760,11 +761,11 @@ public function testDisallowedPutSettings(): void public function testAllowedAndDisallowedPutSettings(): void { $this->clientMock - ->expects($this->never()) + ->expects(self::never()) ->method('indices'); $this->indicesMock - ->expects($this->never()) + ->expects(self::never()) ->method('putSettings'); $this->expectException(IndexManagementException::class); @@ -781,13 +782,13 @@ public function testAllowedAndDisallowedPutSettings(): void ); } - /** @dataProvider getSingleIndexByAliasDataProvider */ + #[DataProvider('getSingleIndexByAliasDataProvider')] public function testGetSingleIndexByAlias(array $esResponse, ?string $expectedResult): void { $this->setUpIndexOperation(); $this->indicesMock - ->expects($this->once()) + ->expects(self::once()) ->method('getAlias') ->with(['name' => self::ALIAS]) ->willReturn($esResponse); @@ -801,26 +802,26 @@ public function testGetSingleIndexByAlias(array $esResponse, ?string $expectedRe } $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $result = $this->getManager()->getSingleIndexByAlias(self::ALIAS); - $this->assertEquals($expectedResult, $result); + self::assertEquals($expectedResult, $result); } public static function getSingleIndexByAliasDataProvider(): array { return [ - 'no indices mapped to alias' => [ + 'no_indices_mapped_to_alias' => [ 'es_response' => [], 'expected_result' => null, ], - 'one index mapped to alias' => [ + 'one_index_mapped_to_alias' => [ 'es_response' => [self::INDEX => ['foo' => 'bar']], 'expected_result' => self::INDEX, ], - 'multiple indices mapped to alias' => [ + 'multiple_indices_mapped_to_alias' => [ 'es_response' => [self::INDEX => ['foo' => 'bar'], 'another_index' => []], 'expected_result' => null, ], @@ -846,7 +847,7 @@ private function getManager(): IndexManagerInterface private function setUpIndexOperation(): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('indices') ->willReturn($this->indicesMock); } diff --git a/tests/Query/AbstractQueryTest.php b/tests/Query/AbstractQueryTest.php index 1ef95a5..9d471b4 100644 --- a/tests/Query/AbstractQueryTest.php +++ b/tests/Query/AbstractQueryTest.php @@ -7,6 +7,7 @@ use Kununu\Elasticsearch\Query\AbstractQuery; use Kununu\Elasticsearch\Query\QueryInterface; use Kununu\Elasticsearch\Query\SortOrder; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; final class AbstractQueryTest extends TestCase @@ -15,33 +16,33 @@ public function testBuildBaseBodyEmpty(): void { $query = $this->getQuery(); - $this->assertEquals([], $query->toArray()); + self::assertEquals([], $query->toArray()); } - /** @dataProvider selectDataProvider */ + #[DataProvider('selectDataProvider')] public function testSelect(mixed $input, mixed $expectedOutput): void { $query = $this->getQuery(); - $this->assertNull($query->getSelect()); - $this->assertEquals([], $query->toArray()); + self::assertNull($query->getSelect()); + self::assertEquals([], $query->toArray()); $query->select($input); - $this->assertEquals($input, $query->getSelect()); + self::assertEquals($input, $query->getSelect()); $serialized = $query->toArray(); - $this->assertArrayHasKey('_source', $serialized); - $this->assertEquals($expectedOutput, $serialized['_source']); + self::assertArrayHasKey('_source', $serialized); + self::assertEquals($expectedOutput, $serialized['_source']); } public static function selectDataProvider(): array { return [ - 'no field' => [ + 'no_field' => [ 'input' => [], 'expected_output' => false, ], - 'one field' => [ + 'one_field' => [ 'input' => [ 'foo', ], @@ -49,7 +50,7 @@ public static function selectDataProvider(): array 'foo', ], ], - 'two fields' => [ + 'two_fields' => [ 'input' => [ 'foo', 'bar', @@ -59,7 +60,7 @@ public static function selectDataProvider(): array 'bar', ], ], - 'same field twice' => [ + 'same_field_twice' => [ 'input' => [ 'foo', 'foo', @@ -75,66 +76,66 @@ public function testSkip(): void { $query = $this->getQuery(); - $this->assertNull($query->getOffset()); - $this->assertEquals([], $query->toArray()); + self::assertNull($query->getOffset()); + self::assertEquals([], $query->toArray()); $query->skip(10); - $this->assertEquals(10, $query->getOffset()); - $this->assertNull($query->getLimit()); - $this->assertEquals([], $query->getSort()); - $this->assertEquals(['from' => 10], $query->toArray()); + self::assertEquals(10, $query->getOffset()); + self::assertNull($query->getLimit()); + self::assertEquals([], $query->getSort()); + self::assertEquals(['from' => 10], $query->toArray()); // override offset $query->skip(20); - $this->assertEquals(20, $query->getOffset()); - $this->assertEquals(['from' => 20], $query->toArray()); + self::assertEquals(20, $query->getOffset()); + self::assertEquals(['from' => 20], $query->toArray()); } public function testLimit(): void { $query = $this->getQuery(); - $this->assertNull($query->getLimit()); - $this->assertEquals([], $query->toArray()); + self::assertNull($query->getLimit()); + self::assertEquals([], $query->toArray()); $query->limit(10); - $this->assertEquals(10, $query->getLimit()); - $this->assertNull($query->getOffset()); - $this->assertEquals([], $query->getSort()); - $this->assertEquals(['size' => 10], $query->toArray()); + self::assertEquals(10, $query->getLimit()); + self::assertNull($query->getOffset()); + self::assertEquals([], $query->getSort()); + self::assertEquals(['size' => 10], $query->toArray()); // override limit $query->limit(20); - $this->assertEquals(20, $query->getLimit()); - $this->assertEquals(['size' => 20], $query->toArray()); + self::assertEquals(20, $query->getLimit()); + self::assertEquals(['size' => 20], $query->toArray()); } - /** @dataProvider sortDataProvider */ + #[DataProvider('sortDataProvider')] public function testSort(array $input, array $expectedOutput): void { $query = $this->getQuery(); - $this->assertEquals([], $query->getSort()); - $this->assertEquals([], $query->toArray()); + self::assertEquals([], $query->getSort()); + self::assertEquals([], $query->toArray()); foreach ($input as $command) { $query->sort($command['key'], $command['order'], $command['options'] ?? []); } - $this->assertNull($query->getOffset()); - $this->assertNull($query->getLimit()); - $this->assertEquals($expectedOutput, $query->getSort()); - $this->assertEquals(['sort' => $expectedOutput], $query->toArray()); + self::assertNull($query->getOffset()); + self::assertNull($query->getLimit()); + self::assertEquals($expectedOutput, $query->getSort()); + self::assertEquals(['sort' => $expectedOutput], $query->toArray()); } public static function sortDataProvider(): array { return [ - 'one field' => [ + 'one_field' => [ 'input' => [ [ 'key' => 'foo', @@ -145,7 +146,7 @@ public static function sortDataProvider(): array 'foo' => ['order' => SortOrder::ASC], ], ], - 'two fields' => [ + 'two_fields' => [ 'input' => [ [ 'key' => 'foo', @@ -161,7 +162,7 @@ public static function sortDataProvider(): array 'bar' => ['order' => SortOrder::DESC], ], ], - 'override one field' => [ + 'override_one_field' => [ 'input' => [ [ 'key' => 'foo', @@ -176,7 +177,7 @@ public static function sortDataProvider(): array 'foo' => ['order' => SortOrder::DESC], ], ], - 'one field with options' => [ + 'one_field_with_options' => [ 'input' => [ [ 'key' => 'foo', @@ -188,7 +189,7 @@ public static function sortDataProvider(): array 'foo' => ['order' => SortOrder::ASC, 'missing' => '_last'], ], ], - 'two fields with options' => [ + 'two_fields_with_options' => [ 'input' => [ [ 'key' => 'foo', @@ -209,13 +210,13 @@ public static function sortDataProvider(): array ]; } - /** @dataProvider sortDataProvider */ + #[DataProvider('sortDataProvider')] public function testMultipleSort(array $input, array $expectedOutput): void { $query = $this->getQuery(); - $this->assertEquals([], $query->getSort()); - $this->assertEquals([], $query->toArray()); + self::assertEquals([], $query->getSort()); + self::assertEquals([], $query->toArray()); $combinedInput = array_reduce( $input, @@ -229,10 +230,10 @@ function(array $carry, array $command): array { $query->sort($combinedInput); - $this->assertNull($query->getOffset()); - $this->assertNull($query->getLimit()); - $this->assertEquals($expectedOutput, $query->getSort()); - $this->assertEquals(['sort' => $expectedOutput], $query->toArray()); + self::assertNull($query->getOffset()); + self::assertNull($query->getLimit()); + self::assertEquals($expectedOutput, $query->getSort()); + self::assertEquals(['sort' => $expectedOutput], $query->toArray()); } public function testSortInvalidOrder(): void diff --git a/tests/Query/AggregationTest.php b/tests/Query/AggregationTest.php index da3d2c5..9fdf379 100644 --- a/tests/Query/AggregationTest.php +++ b/tests/Query/AggregationTest.php @@ -7,18 +7,19 @@ use Kununu\Elasticsearch\Query\Aggregation; use Kununu\Elasticsearch\Query\Aggregation\Bucket; use Kununu\Elasticsearch\Query\Aggregation\Metric; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use stdClass; final class AggregationTest extends TestCase { - /** @dataProvider createDataProvider */ + #[DataProvider('createDataProvider')] public function testCreateWithoutOptions(string $field, string $type, string $name): void { $aggregation = Aggregation::create($field, $type, $name); - $this->assertEquals($name, $aggregation->getName()); - $this->assertEquals( + self::assertEquals($name, $aggregation->getName()); + self::assertEquals( [ $name => [ $type => [ @@ -34,7 +35,7 @@ public static function createDataProvider(): array { $ret = []; foreach (Metric::all() + Bucket::all() as $type) { - $ret['type ' . $type] = [ + $ret['type_' . $type] = [ 'field' => 'my_field', 'type' => $type, 'name' => 'my_agg', @@ -58,7 +59,7 @@ public function testCreateWithOptions(): void ] ); - $this->assertEquals( + self::assertEquals( [ 'my_agg' => [ 'sum' => [ @@ -75,7 +76,7 @@ public function testCreateWithoutName(): void { $aggregation = Aggregation::create('my_field', Metric::SUM); - $this->assertNotNull($aggregation->getName()); + self::assertNotNull($aggregation->getName()); } public function testCreateWithInvalidType(): void @@ -91,7 +92,7 @@ public function testNestOne(): void $aggregation = Aggregation::create('my_field', Bucket::TERMS, 'my_term_buckets') ->nest(Aggregation::create('my_field', Metric::CARDINALITY, 'term_cardinality')); - $this->assertEquals( + self::assertEquals( [ 'my_term_buckets' => [ 'terms' => [ @@ -116,7 +117,7 @@ public function testNestMultiple(): void ->nest(Aggregation::create('my_field', Metric::CARDINALITY, 'term_cardinality')) ->nest(Aggregation::create('my_field', Metric::VALUE_COUNT, 'term_value_count')); - $this->assertEquals( + self::assertEquals( [ 'my_term_buckets' => [ 'terms' => [ @@ -144,7 +145,7 @@ public function testCreateGlobalWithoutOptions(): void { $aggregation = Aggregation::createGlobal('my_global_agg'); - $this->assertEquals( + self::assertEquals( json_encode( [ 'my_global_agg' => [ @@ -160,7 +161,7 @@ public function testCreateGlobalWithOptions(): void { $aggregation = Aggregation::createGlobal('my_global_agg', ['my_option' => 'foobar']); - $this->assertEquals( + self::assertEquals( json_encode( [ 'my_global_agg' => [ @@ -178,7 +179,7 @@ public function testNestOneInGlobal(): void $aggregation = Aggregation::createGlobal('all_products') ->nest(Aggregation::create('price', Metric::AVG, 'avg_price')); - $this->assertEquals( + self::assertEquals( json_encode( [ 'all_products' => [ @@ -201,7 +202,7 @@ public function testCreateFieldlessWithoutOptions(): void { $aggregation = Aggregation::createFieldless(Bucket::FILTERS, 'my_fieldless_agg'); - $this->assertEquals( + self::assertEquals( [ 'my_fieldless_agg' => [ 'filters' => [], @@ -219,7 +220,7 @@ public function testCreateFieldlessWithOptions(): void ['other_bucket_key' => 'foobar', 'filters' => ['bucket_a' => ['term' => ['field' => 'field_a']]]] ); - $this->assertEquals( + self::assertEquals( [ 'my_fieldless_agg' => [ 'filters' => [ @@ -241,15 +242,18 @@ public function testCreateAggregationWithRange(): void ['ranges' => [['from' => 1, 'to' => 2]]] ); - $this->assertEquals([ - 'my_agg' => [ - 'range' => [ - 'field' => 'my_field', - 'ranges' => [ - ['from' => 1, 'to' => 2], + self::assertEquals( + [ + 'my_agg' => [ + 'range' => [ + 'field' => 'my_field', + 'ranges' => [ + ['from' => 1, 'to' => 2], + ], ], ], ], - ], $aggregation->toArray()); + $aggregation->toArray() + ); } } diff --git a/tests/Query/Criteria/Bool/BoolQueryTest.php b/tests/Query/Criteria/Bool/BoolQueryTest.php index a02a318..fcdf130 100644 --- a/tests/Query/Criteria/Bool/BoolQueryTest.php +++ b/tests/Query/Criteria/Bool/BoolQueryTest.php @@ -7,6 +7,7 @@ use Kununu\Elasticsearch\Query\Criteria\Bool\BoolQueryInterface; use Kununu\Elasticsearch\Query\Criteria\Filter; use LogicException; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; final class BoolQueryTest extends TestCase @@ -26,10 +27,10 @@ public function getOperator(): string $myBoolFilter->getOperator(); } - /** @dataProvider createDataProvider */ + #[DataProvider('createDataProvider')] public function testCreate(array $input): void { - $this->assertEquals($input, $this->getConcreteBoolQuery($input)->getChildren()); + self::assertEquals($input, $this->getConcreteBoolQuery($input)->getChildren()); } public static function createDataProvider(): array @@ -38,10 +39,10 @@ public static function createDataProvider(): array 'empty' => [ 'input' => [], ], - 'with a filter' => [ + 'with_a_filter' => [ 'input' => [Filter::create('field', 'value')], ], - 'with two filters search' => [ + 'with_two_filters_search' => [ 'input' => [ Filter::create('field', 'value'), Filter::create('another_field', 'another_value'), @@ -50,32 +51,32 @@ public static function createDataProvider(): array ]; } - /** @dataProvider createDataProvider */ + #[DataProvider('createDataProvider')] public function testAdd(array $input): void { $boolQuery = $this->getConcreteBoolQuery([]); - $this->assertEquals([], $boolQuery->getChildren()); + self::assertEquals([], $boolQuery->getChildren()); foreach ($input as $child) { $boolQuery->add($child); } - $this->assertEquals($input, $boolQuery->getChildren()); + self::assertEquals($input, $boolQuery->getChildren()); } - /** @dataProvider createDataProvider */ + #[DataProvider('createDataProvider')] public function testToArray(array $input): void { $boolQuery = $this->getConcreteBoolQuery($input); - $this->assertEquals($input, $boolQuery->getChildren()); + self::assertEquals($input, $boolQuery->getChildren()); $serialized = $boolQuery->toArray(); - $this->assertArrayHasKey('bool', $serialized); - $this->assertArrayHasKey('my_operator', $serialized['bool']); - $this->assertCount(count($input), $serialized['bool']['my_operator']); + self::assertArrayHasKey('bool', $serialized); + self::assertArrayHasKey('my_operator', $serialized['bool']); + self::assertCount(count($input), $serialized['bool']['my_operator']); } private function getConcreteBoolQuery(array $input): BoolQueryInterface diff --git a/tests/Query/Criteria/Bool/MustNotTest.php b/tests/Query/Criteria/Bool/MustNotTest.php index 128a1c7..79da33a 100644 --- a/tests/Query/Criteria/Bool/MustNotTest.php +++ b/tests/Query/Criteria/Bool/MustNotTest.php @@ -10,6 +10,6 @@ final class MustNotTest extends TestCase { public function testOperator(): void { - $this->assertEquals('must_not', MustNot::OPERATOR); + self::assertEquals('must_not', MustNot::OPERATOR); } } diff --git a/tests/Query/Criteria/Bool/MustTest.php b/tests/Query/Criteria/Bool/MustTest.php index 9f7a3d5..e4f5e2a 100644 --- a/tests/Query/Criteria/Bool/MustTest.php +++ b/tests/Query/Criteria/Bool/MustTest.php @@ -10,6 +10,6 @@ final class MustTest extends TestCase { public function testOperator(): void { - $this->assertEquals('must', Must::OPERATOR); + self::assertEquals('must', Must::OPERATOR); } } diff --git a/tests/Query/Criteria/Bool/ShouldTest.php b/tests/Query/Criteria/Bool/ShouldTest.php index 7a63cf5..1c35759 100644 --- a/tests/Query/Criteria/Bool/ShouldTest.php +++ b/tests/Query/Criteria/Bool/ShouldTest.php @@ -10,6 +10,6 @@ final class ShouldTest extends TestCase { public function testOperator(): void { - $this->assertEquals('should', Should::OPERATOR); + self::assertEquals('should', Should::OPERATOR); } } diff --git a/tests/Query/Criteria/Filter/ExistsTest.php b/tests/Query/Criteria/Filter/ExistsTest.php index 0a79821..4ef8caf 100644 --- a/tests/Query/Criteria/Filter/ExistsTest.php +++ b/tests/Query/Criteria/Filter/ExistsTest.php @@ -10,7 +10,7 @@ final class ExistsTest extends TestCase { public function testTrue(): void { - $this->assertEquals( + self::assertEquals( [ 'exists' => [ 'field' => 'field_a', @@ -22,7 +22,7 @@ public function testTrue(): void public function testFalse(): void { - $this->assertEquals( + self::assertEquals( [ 'bool' => [ 'must_not' => [ diff --git a/tests/Query/Criteria/Filter/GeoDistanceTest.php b/tests/Query/Criteria/Filter/GeoDistanceTest.php index 8eef3bb..1faa76d 100644 --- a/tests/Query/Criteria/Filter/GeoDistanceTest.php +++ b/tests/Query/Criteria/Filter/GeoDistanceTest.php @@ -10,11 +10,11 @@ final class GeoDistanceTest extends TestCase { - protected GeoDistanceInterface|MockObject $geoDistance; + protected MockObject&GeoDistanceInterface $geoDistance; public function testWithoutOptions(): void { - $this->assertEquals( + self::assertEquals( [ 'geo_distance' => [ 'distance' => '42km', @@ -27,7 +27,7 @@ public function testWithoutOptions(): void public function testWithOptions(): void { - $this->assertEquals( + self::assertEquals( [ 'geo_distance' => [ 'distance' => '42km', @@ -44,12 +44,12 @@ protected function setUp(): void $this->geoDistance = $this->createMock(GeoDistanceInterface::class); $this->geoDistance - ->expects($this->once()) + ->expects(self::once()) ->method('getDistance') ->willReturn('42km'); $this->geoDistance - ->expects($this->once()) + ->expects(self::once()) ->method('getLocation') ->willReturn([0, 0]); } diff --git a/tests/Query/Criteria/Filter/GeoShapeTest.php b/tests/Query/Criteria/Filter/GeoShapeTest.php index 354c62a..2cc5b27 100644 --- a/tests/Query/Criteria/Filter/GeoShapeTest.php +++ b/tests/Query/Criteria/Filter/GeoShapeTest.php @@ -10,11 +10,11 @@ final class GeoShapeTest extends TestCase { - protected GeoShapeInterface|MockObject $geoShape; + protected MockObject&GeoShapeInterface $geoShape; public function testWithoutOptions(): void { - $this->assertEquals( + self::assertEquals( [ 'geo_shape' => [ 'field_a' => [ @@ -28,7 +28,7 @@ public function testWithoutOptions(): void public function testWithOptions(): void { - $this->assertEquals( + self::assertEquals( [ 'geo_shape' => [ 'field_a' => [ @@ -46,7 +46,7 @@ protected function setUp(): void $this->geoShape = $this->createMock(GeoShapeInterface::class); $this->geoShape - ->expects($this->once()) + ->expects(self::once()) ->method('toArray') ->willReturn([]); } diff --git a/tests/Query/Criteria/Filter/PrefixTest.php b/tests/Query/Criteria/Filter/PrefixTest.php index 1dae8c3..4248927 100644 --- a/tests/Query/Criteria/Filter/PrefixTest.php +++ b/tests/Query/Criteria/Filter/PrefixTest.php @@ -10,7 +10,7 @@ final class PrefixTest extends TestCase { public function testWithoutOptions(): void { - $this->assertEquals( + self::assertEquals( [ 'prefix' => [ 'field_a' => 'foo', @@ -22,7 +22,7 @@ public function testWithoutOptions(): void public function testWithOptions(): void { - $this->assertEquals( + self::assertEquals( [ 'prefix' => [ 'field_a' => 'foo', diff --git a/tests/Query/Criteria/Filter/RangeTest.php b/tests/Query/Criteria/Filter/RangeTest.php index 1efa38f..3038a0c 100644 --- a/tests/Query/Criteria/Filter/RangeTest.php +++ b/tests/Query/Criteria/Filter/RangeTest.php @@ -11,7 +11,7 @@ final class RangeTest extends TestCase { public function testOneElementWithoutOptions(): void { - $this->assertEquals( + self::assertEquals( [ 'range' => [ 'field_a' => [ @@ -25,7 +25,7 @@ public function testOneElementWithoutOptions(): void public function testMultipleElementsWithoutOptions(): void { - $this->assertEquals( + self::assertEquals( [ 'range' => [ 'field_a' => [ @@ -40,7 +40,7 @@ public function testMultipleElementsWithoutOptions(): void public function testOneElementWithOptions(): void { - $this->assertEquals( + self::assertEquals( [ 'range' => [ 'field_a' => [ @@ -55,7 +55,7 @@ public function testOneElementWithOptions(): void public function testMultipleElementsWithOptions(): void { - $this->assertEquals( + self::assertEquals( [ 'range' => [ 'field_a' => [ diff --git a/tests/Query/Criteria/Filter/RegexpTest.php b/tests/Query/Criteria/Filter/RegexpTest.php index 8c50887..ca4465f 100644 --- a/tests/Query/Criteria/Filter/RegexpTest.php +++ b/tests/Query/Criteria/Filter/RegexpTest.php @@ -10,7 +10,7 @@ final class RegexpTest extends TestCase { public function testWithoutOptions(): void { - $this->assertEquals( + self::assertEquals( [ 'regexp' => [ 'field_a' => 'foo', @@ -22,7 +22,7 @@ public function testWithoutOptions(): void public function testWithOptions(): void { - $this->assertEquals( + self::assertEquals( [ 'regexp' => [ 'field_a' => [ diff --git a/tests/Query/Criteria/Filter/TermTest.php b/tests/Query/Criteria/Filter/TermTest.php index 346b6a0..24839de 100644 --- a/tests/Query/Criteria/Filter/TermTest.php +++ b/tests/Query/Criteria/Filter/TermTest.php @@ -10,7 +10,7 @@ final class TermTest extends TestCase { public function testWithoutOptions(): void { - $this->assertEquals( + self::assertEquals( [ 'term' => [ 'field_a' => 'foo', @@ -22,7 +22,7 @@ public function testWithoutOptions(): void public function testWithOptions(): void { - $this->assertEquals( + self::assertEquals( [ 'term' => [ 'field_a' => [ diff --git a/tests/Query/Criteria/Filter/TermsTest.php b/tests/Query/Criteria/Filter/TermsTest.php index 1c6b587..796af25 100644 --- a/tests/Query/Criteria/Filter/TermsTest.php +++ b/tests/Query/Criteria/Filter/TermsTest.php @@ -10,7 +10,7 @@ final class TermsTest extends TestCase { public function testWithoutOptions(): void { - $this->assertEquals( + self::assertEquals( [ 'terms' => [ 'field_a' => ['foo', 'bar'], @@ -22,7 +22,7 @@ public function testWithoutOptions(): void public function testWithOptions(): void { - $this->assertEquals( + self::assertEquals( [ 'terms' => [ 'field_a' => ['foo', 'bar'], diff --git a/tests/Query/Criteria/FilterTest.php b/tests/Query/Criteria/FilterTest.php index e2237d1..79bab71 100644 --- a/tests/Query/Criteria/FilterTest.php +++ b/tests/Query/Criteria/FilterTest.php @@ -8,6 +8,7 @@ use Kununu\Elasticsearch\Query\Criteria\GeoDistanceInterface; use Kununu\Elasticsearch\Query\Criteria\GeoShapeInterface; use Kununu\Elasticsearch\Query\Criteria\Operator; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; final class FilterTest extends TestCase @@ -20,15 +21,15 @@ public function testCreateWithInvalidOperator(): void Filter::create('my_field', 'foo', 'bar'); } - /** @dataProvider createDataProvider */ + #[DataProvider('createDataProvider')] public function testCreate(string $operator, mixed $value): void { $serialized = Filter::create('my_field', $value, $operator)->toArray(); - $this->assertNotEmpty($serialized); + self::assertNotEmpty($serialized); } - public function createDataProvider(): array + public static function createDataProvider(): array { $ret = []; @@ -54,8 +55,8 @@ public function testCreateWithoutOperatorCreatesTermFilter(): void { $serialized = Filter::create('my_field', 'value')->toArray(); - $this->assertNotEmpty($serialized); - $this->assertArrayHasKey('term', $serialized); + self::assertNotEmpty($serialized); + self::assertArrayHasKey('term', $serialized); } public function testCreateGeoShape(): void @@ -63,14 +64,14 @@ public function testCreateGeoShape(): void $geoShape = $this->createMock(GeoShapeInterface::class); $geoShape - ->expects($this->once()) + ->expects(self::once()) ->method('toArray') ->willReturn([]); $serialized = Filter::create('my_field', $geoShape, Operator::GEO_SHAPE)->toArray(); - $this->assertNotEmpty($serialized); - $this->assertArrayHasKey('geo_shape', $serialized); + self::assertNotEmpty($serialized); + self::assertArrayHasKey('geo_shape', $serialized); } public function testCreateGeoDistance(): void @@ -78,18 +79,18 @@ public function testCreateGeoDistance(): void $geoDistance = $this->createMock(GeoDistanceInterface::class); $geoDistance - ->expects($this->once()) + ->expects(self::once()) ->method('getDistance') ->willReturn('42km'); $geoDistance - ->expects($this->once()) + ->expects(self::once()) ->method('getLocation') ->willReturn([0, 0]); $serialized = Filter::create('my_field', $geoDistance, Operator::GEO_DISTANCE)->toArray(); - $this->assertNotEmpty($serialized); - $this->assertArrayHasKey('geo_distance', $serialized); + self::assertNotEmpty($serialized); + self::assertArrayHasKey('geo_distance', $serialized); } } diff --git a/tests/Query/Criteria/Search/MatchPhrasePrefixQueryTest.php b/tests/Query/Criteria/Search/MatchPhrasePrefixQueryTest.php index ac9283f..574ab67 100644 --- a/tests/Query/Criteria/Search/MatchPhrasePrefixQueryTest.php +++ b/tests/Query/Criteria/Search/MatchPhrasePrefixQueryTest.php @@ -8,11 +8,11 @@ final class MatchPhrasePrefixQueryTest extends TestCase { - protected const QUERY_STRING = 'what was i looking for?'; + private const QUERY_STRING = 'what was i looking for?'; public function testSingleField(): void { - $this->assertEquals( + self::assertEquals( [ 'match_phrase_prefix' => [ 'field_a' => [ @@ -26,7 +26,7 @@ public function testSingleField(): void public function testSingleFieldWithOptions(): void { - $this->assertEquals( + self::assertEquals( [ 'match_phrase_prefix' => [ 'field_a' => [ diff --git a/tests/Query/Criteria/Search/MatchPhraseQueryTest.php b/tests/Query/Criteria/Search/MatchPhraseQueryTest.php index 4ce51a8..2eaf921 100644 --- a/tests/Query/Criteria/Search/MatchPhraseQueryTest.php +++ b/tests/Query/Criteria/Search/MatchPhraseQueryTest.php @@ -8,11 +8,11 @@ final class MatchPhraseQueryTest extends TestCase { - protected const QUERY_STRING = 'what was i looking for?'; + private const QUERY_STRING = 'what was i looking for?'; public function testSingleField(): void { - $this->assertEquals( + self::assertEquals( [ 'match_phrase' => [ 'field_a' => [ @@ -26,7 +26,7 @@ public function testSingleField(): void public function testSingleFieldWithOptions(): void { - $this->assertEquals( + self::assertEquals( [ 'match_phrase' => [ 'field_a' => [ diff --git a/tests/Query/Criteria/Search/MatchQueryTest.php b/tests/Query/Criteria/Search/MatchQueryTest.php index 08633a9..3a8ecac 100644 --- a/tests/Query/Criteria/Search/MatchQueryTest.php +++ b/tests/Query/Criteria/Search/MatchQueryTest.php @@ -8,11 +8,11 @@ final class MatchQueryTest extends TestCase { - protected const QUERY_STRING = 'what was i looking for?'; + private const QUERY_STRING = 'what was i looking for?'; public function testSingleField(): void { - $this->assertEquals( + self::assertEquals( [ 'match' => [ 'field_a' => [ @@ -26,7 +26,7 @@ public function testSingleField(): void public function testMultipleFields(): void { - $this->assertEquals( + self::assertEquals( [ 'multi_match' => [ 'fields' => ['field_a', 'field_b'], @@ -39,7 +39,7 @@ public function testMultipleFields(): void public function testSingleFieldWithOptions(): void { - $this->assertEquals( + self::assertEquals( [ 'match' => [ 'field_a' => [ @@ -54,7 +54,7 @@ public function testSingleFieldWithOptions(): void public function testMultipleFieldsWithOptions(): void { - $this->assertEquals( + self::assertEquals( [ 'multi_match' => [ 'fields' => ['field_a', 'field_b'], diff --git a/tests/Query/Criteria/Search/MultiFieldTraitTest.php b/tests/Query/Criteria/Search/MultiFieldTraitTest.php index eb175e8..52babe0 100644 --- a/tests/Query/Criteria/Search/MultiFieldTraitTest.php +++ b/tests/Query/Criteria/Search/MultiFieldTraitTest.php @@ -4,36 +4,37 @@ namespace Kununu\Elasticsearch\Tests\Query\Criteria\Search; use Kununu\Elasticsearch\Query\Criteria\Search\MultiFieldTrait; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; final class MultiFieldTraitTest extends TestCase { - /** @dataProvider boostPreparationDataProvider */ + #[DataProvider('boostPreparationDataProvider')] public function testBoostPreparation(array $input, array $expectedOutput): void { - $this->assertEquals($expectedOutput, $this->getMultiFieldCapableObject()->publiclyPrepare($input)); + self::assertEquals($expectedOutput, $this->getMultiFieldCapableObject()->prepareFields($input)); } public static function boostPreparationDataProvider(): array { return [ - 'single field, without boost' => [ + 'single_field_without_boost' => [ 'input' => ['my_field'], 'expectedOutput' => ['my_field'], ], - 'multiple fields, without boost' => [ + 'multiple_fields_without_boost' => [ 'input' => ['field_a', 'field_b'], 'expectedOutput' => ['field_a', 'field_b'], ], - 'single field, with boost' => [ + 'single_field_with_boost' => [ 'input' => ['my_field' => ['boost' => 7]], 'expectedOutput' => ['my_field^7'], ], - 'multiple fields, with boost' => [ + 'multiple_fields_with_boost' => [ 'input' => ['field_a' => ['boost' => 7], 'field_b' => ['boost' => 42]], 'expectedOutput' => ['field_a^7', 'field_b^42'], ], - 'multiple fields, with and without boost' => [ + 'multiple_fields_with_and_without_boost' => [ 'input' => ['field_a' => ['boost' => 7], 'field_b'], 'expectedOutput' => ['field_a^7', 'field_b'], ], @@ -43,11 +44,13 @@ public static function boostPreparationDataProvider(): array private function getMultiFieldCapableObject(): object { return new class() { - use MultiFieldTrait; + use MultiFieldTrait { + prepareFields as traitPrepareFields; + } - public function publiclyPrepare(array $fields): array + public function prepareFields(array $fields): array { - return $this->prepareFields($fields); + return self::traitPrepareFields($fields); } }; } diff --git a/tests/Query/Criteria/Search/PrefixQueryTest.php b/tests/Query/Criteria/Search/PrefixQueryTest.php index 635b3e0..d5e4ec8 100644 --- a/tests/Query/Criteria/Search/PrefixQueryTest.php +++ b/tests/Query/Criteria/Search/PrefixQueryTest.php @@ -8,11 +8,11 @@ final class PrefixQueryTest extends TestCase { - protected const QUERY_STRING = 'what was i looking for?'; + private const QUERY_STRING = 'what was i looking for?'; public function testSingleField(): void { - $this->assertEquals( + self::assertEquals( [ 'prefix' => [ 'field_a' => [ @@ -26,7 +26,7 @@ public function testSingleField(): void public function testSingleFieldWithOptions(): void { - $this->assertEquals( + self::assertEquals( [ 'prefix' => [ 'field_a' => [ diff --git a/tests/Query/Criteria/Search/QueryStringQueryTest.php b/tests/Query/Criteria/Search/QueryStringQueryTest.php index 6de770f..f74a9a0 100644 --- a/tests/Query/Criteria/Search/QueryStringQueryTest.php +++ b/tests/Query/Criteria/Search/QueryStringQueryTest.php @@ -8,11 +8,11 @@ final class QueryStringQueryTest extends TestCase { - protected const QUERY_STRING = 'what was i looking for?'; + private const QUERY_STRING = 'what was i looking for?'; public function testSingleField(): void { - $this->assertEquals( + self::assertEquals( [ 'query_string' => [ 'fields' => ['field_a'], @@ -25,7 +25,7 @@ public function testSingleField(): void public function testMultipleFields(): void { - $this->assertEquals( + self::assertEquals( [ 'query_string' => [ 'fields' => ['field_a', 'field_b'], @@ -38,7 +38,7 @@ public function testMultipleFields(): void public function testWithOptions(): void { - $this->assertEquals( + self::assertEquals( [ 'query_string' => [ 'fields' => ['field_a', 'field_b'], diff --git a/tests/Query/Criteria/Search/TermQueryTest.php b/tests/Query/Criteria/Search/TermQueryTest.php index 3c51e89..3a81025 100644 --- a/tests/Query/Criteria/Search/TermQueryTest.php +++ b/tests/Query/Criteria/Search/TermQueryTest.php @@ -8,12 +8,12 @@ final class TermQueryTest extends TestCase { - protected const TERM = 'what was i looking for?'; - protected const FIELD = 'field_a'; + private const TERM = 'what was i looking for?'; + private const FIELD = 'field_a'; public function testSingleField(): void { - $this->assertEquals( + self::assertEquals( [ 'term' => [ self::FIELD => [ @@ -27,7 +27,7 @@ public function testSingleField(): void public function testSingleFieldWithOptions(): void { - $this->assertEquals( + self::assertEquals( [ 'term' => [ self::FIELD => [ diff --git a/tests/Query/Criteria/SearchTest.php b/tests/Query/Criteria/SearchTest.php index ec36f05..0c1a8f2 100644 --- a/tests/Query/Criteria/SearchTest.php +++ b/tests/Query/Criteria/SearchTest.php @@ -5,6 +5,7 @@ use InvalidArgumentException; use Kununu\Elasticsearch\Query\Criteria\Search; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; final class SearchTest extends TestCase @@ -25,13 +26,13 @@ public function testCreateWithInvalidType(): void Search::create(['my_field'], 'foo', 'bar'); } - /** @dataProvider createDataProvider */ + #[DataProvider('createDataProvider')] public function testCreate(string $type): void { $serialized = Search::create(['my_field'], 'i am looking for something', $type)->toArray(); - $this->assertNotEmpty($serialized); - $this->assertArrayHasKey($type, $serialized); + self::assertNotEmpty($serialized); + self::assertArrayHasKey($type, $serialized); } public static function createDataProvider(): array diff --git a/tests/Query/QueryTest.php b/tests/Query/QueryTest.php index cf55f2c..3037b6a 100644 --- a/tests/Query/QueryTest.php +++ b/tests/Query/QueryTest.php @@ -17,6 +17,7 @@ use Kununu\Elasticsearch\Query\Criteria\SearchInterface; use Kununu\Elasticsearch\Query\Query; use Kununu\Elasticsearch\Query\SortOrder; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use ReflectionProperty; @@ -26,7 +27,7 @@ final class QueryTest extends TestCase private const FIELD_NAME_FILTERS = 'filters'; private const FIELD_NAME_AGGREGATIONS = 'aggregations'; - /** @dataProvider createDataProvider */ + #[DataProvider('createDataProvider')] public function testCreate(array $input): void { $children = [ @@ -41,9 +42,9 @@ public function testCreate(array $input): void $query = Query::create(...$input); - $this->assertChildren($query, self::FIELD_NAME_SEARCHES, $children[Search::class]); - $this->assertChildren($query, self::FIELD_NAME_FILTERS, $children[Filter::class]); - $this->assertChildren($query, self::FIELD_NAME_AGGREGATIONS, $children[Aggregation::class]); + self::assertChildren($query, self::FIELD_NAME_SEARCHES, $children[Search::class]); + self::assertChildren($query, self::FIELD_NAME_FILTERS, $children[Filter::class]); + self::assertChildren($query, self::FIELD_NAME_AGGREGATIONS, $children[Aggregation::class]); } public static function createDataProvider(): array @@ -52,16 +53,16 @@ public static function createDataProvider(): array 'empty' => [ 'input' => [], ], - 'with a filter' => [ + 'with_a_filter' => [ 'input' => [Filter::create('field', 'value')], ], - 'with a search' => [ + 'with_a_search' => [ 'input' => [Search::create(['field'], 'value')], ], - 'with an aggregation' => [ + 'with_an_aggregation' => [ 'input' => [Aggregation::create('field', Metric::SUM)], ], - 'with a little bit of everything' => [ + 'with_a_little_bit_of_everything' => [ 'input' => [ Filter::create('field', 'value'), Search::create(['field'], 'value'), @@ -71,7 +72,7 @@ public static function createDataProvider(): array ]; } - /** @dataProvider createDataProvider */ + #[DataProvider('createDataProvider')] public function testCreateNested(array $input): void { $children = [ @@ -88,15 +89,15 @@ public function testCreateNested(array $input): void $query = Query::createNested($path, ...$input); - $this->assertChildren($query, self::FIELD_NAME_SEARCHES, $children[Search::class]); - $this->assertChildren($query, self::FIELD_NAME_FILTERS, $children[Filter::class]); - $this->assertChildren($query, self::FIELD_NAME_AGGREGATIONS, $children[Aggregation::class]); - $this->assertEquals($path, $query->getOption(NestableQueryInterface::OPTION_PATH)); - $this->assertNull($query->getOption(NestableQueryInterface::OPTION_IGNORE_UNMAPPED)); - $this->assertNull($query->getOption(NestableQueryInterface::OPTION_SCORE_MODE)); + self::assertChildren($query, self::FIELD_NAME_SEARCHES, $children[Search::class]); + self::assertChildren($query, self::FIELD_NAME_FILTERS, $children[Filter::class]); + self::assertChildren($query, self::FIELD_NAME_AGGREGATIONS, $children[Aggregation::class]); + self::assertEquals($path, $query->getOption(NestableQueryInterface::OPTION_PATH)); + self::assertNull($query->getOption(NestableQueryInterface::OPTION_IGNORE_UNMAPPED)); + self::assertNull($query->getOption(NestableQueryInterface::OPTION_SCORE_MODE)); } - /** @dataProvider createDataProvider */ + #[DataProvider('createDataProvider')] public function testAdd(array $input): void { $children = [ @@ -107,18 +108,18 @@ public function testAdd(array $input): void $query = Query::create(); - $this->assertChildren($query, self::FIELD_NAME_SEARCHES, $children[Search::class]); - $this->assertChildren($query, self::FIELD_NAME_FILTERS, $children[Filter::class]); - $this->assertChildren($query, self::FIELD_NAME_AGGREGATIONS, $children[Aggregation::class]); + self::assertChildren($query, self::FIELD_NAME_SEARCHES, $children[Search::class]); + self::assertChildren($query, self::FIELD_NAME_FILTERS, $children[Filter::class]); + self::assertChildren($query, self::FIELD_NAME_AGGREGATIONS, $children[Aggregation::class]); foreach ($input as $child) { $children[$child::class][] = $child; $query->add($child); } - $this->assertChildren($query, self::FIELD_NAME_SEARCHES, $children[Search::class]); - $this->assertChildren($query, self::FIELD_NAME_FILTERS, $children[Filter::class]); - $this->assertChildren($query, self::FIELD_NAME_AGGREGATIONS, $children[Aggregation::class]); + self::assertChildren($query, self::FIELD_NAME_SEARCHES, $children[Search::class]); + self::assertChildren($query, self::FIELD_NAME_FILTERS, $children[Filter::class]); + self::assertChildren($query, self::FIELD_NAME_AGGREGATIONS, $children[Aggregation::class]); } public function testCreateWithOnlyInvalid(): void @@ -156,16 +157,16 @@ public function testSearch(): void { $query = Query::create(); - $this->assertChildren($query, self::FIELD_NAME_SEARCHES, []); - $this->assertChildren($query, self::FIELD_NAME_FILTERS, []); - $this->assertChildren($query, self::FIELD_NAME_AGGREGATIONS, []); + self::assertChildren($query, self::FIELD_NAME_SEARCHES, []); + self::assertChildren($query, self::FIELD_NAME_FILTERS, []); + self::assertChildren($query, self::FIELD_NAME_AGGREGATIONS, []); $search = Search::create(['field'], 'value'); $query->search($search); - $this->assertChildren($query, self::FIELD_NAME_SEARCHES, [$search]); - $this->assertChildren($query, self::FIELD_NAME_FILTERS, []); - $this->assertChildren($query, self::FIELD_NAME_AGGREGATIONS, []); + self::assertChildren($query, self::FIELD_NAME_SEARCHES, [$search]); + self::assertChildren($query, self::FIELD_NAME_FILTERS, []); + self::assertChildren($query, self::FIELD_NAME_AGGREGATIONS, []); } public function testSearchWithInvalidCriteria(): void @@ -187,54 +188,54 @@ public function testWhere(): void { $query = Query::create(); - $this->assertChildren($query, self::FIELD_NAME_SEARCHES, []); - $this->assertChildren($query, self::FIELD_NAME_FILTERS, []); - $this->assertChildren($query, self::FIELD_NAME_AGGREGATIONS, []); + self::assertChildren($query, self::FIELD_NAME_SEARCHES, []); + self::assertChildren($query, self::FIELD_NAME_FILTERS, []); + self::assertChildren($query, self::FIELD_NAME_AGGREGATIONS, []); $filter = Filter::create('field', 'value'); $query->where($filter); - $this->assertChildren($query, self::FIELD_NAME_SEARCHES, []); - $this->assertChildren($query, self::FIELD_NAME_FILTERS, [$filter]); - $this->assertChildren($query, self::FIELD_NAME_AGGREGATIONS, []); + self::assertChildren($query, self::FIELD_NAME_SEARCHES, []); + self::assertChildren($query, self::FIELD_NAME_FILTERS, [$filter]); + self::assertChildren($query, self::FIELD_NAME_AGGREGATIONS, []); } public function testAggregate(): void { $query = Query::create(); - $this->assertChildren($query, self::FIELD_NAME_SEARCHES, []); - $this->assertChildren($query, self::FIELD_NAME_FILTERS, []); - $this->assertChildren($query, self::FIELD_NAME_AGGREGATIONS, []); + self::assertChildren($query, self::FIELD_NAME_SEARCHES, []); + self::assertChildren($query, self::FIELD_NAME_FILTERS, []); + self::assertChildren($query, self::FIELD_NAME_AGGREGATIONS, []); $aggregation = Aggregation::create('field', Metric::SUM); $query->aggregate($aggregation); - $this->assertChildren($query, self::FIELD_NAME_SEARCHES, []); - $this->assertChildren($query, self::FIELD_NAME_FILTERS, []); - $this->assertChildren($query, self::FIELD_NAME_AGGREGATIONS, [$aggregation]); + self::assertChildren($query, self::FIELD_NAME_SEARCHES, []); + self::assertChildren($query, self::FIELD_NAME_FILTERS, []); + self::assertChildren($query, self::FIELD_NAME_AGGREGATIONS, [$aggregation]); } public function testMinScore(): void { $query = Query::create(); - $this->assertNull($query->getOption(Query::OPTION_MIN_SCORE)); + self::assertNull($query->getOption(Query::OPTION_MIN_SCORE)); $query->setMinScore(42); - $this->assertEquals(42, $query->getOption(Query::OPTION_MIN_SCORE)); + self::assertEquals(42, $query->getOption(Query::OPTION_MIN_SCORE)); } public function testSearchOperator(): void { $query = Query::create(); - $this->assertEquals(Should::OPERATOR, $query->getSearchOperator()); + self::assertEquals(Should::OPERATOR, $query->getSearchOperator()); $query->setSearchOperator(Must::OPERATOR); - $this->assertEquals(Must::OPERATOR, $query->getSearchOperator()); + self::assertEquals(Must::OPERATOR, $query->getSearchOperator()); } public function testSetInvalidSearchOperator(): void @@ -254,7 +255,7 @@ public function testCommonFunctionalityIsPreservedOnToArray(): void ->skip(1) ->limit(10); - $this->assertEquals( + self::assertEquals( [ 'query' => ['bool' => ['filter' => ['bool' => ['must' => [['term' => ['field' => 'value']]]]]]], '_source' => ['field_a'], @@ -268,10 +269,10 @@ public function testCommonFunctionalityIsPreservedOnToArray(): void ); } - /** @dataProvider toArrayDataProvider */ + #[DataProvider('toArrayDataProvider')] public function testToArray(Query $query, array $expected): void { - $this->assertEquals($expected, $query->toArray()); + self::assertEquals($expected, $query->toArray()); } public static function toArrayDataProvider(): array @@ -286,23 +287,23 @@ public static function toArrayDataProvider(): array private static function toArrayBatch1(): array { return [ - 'empty' => [ + 'empty' => [ 'query' => Query::create(), 'expected' => [], ], - 'with a min_score' => [ + 'with_a_min_score' => [ 'query' => Query::create()->setMinScore(42), 'expected' => [ 'min_score' => 42, ], ], - 'with a filter' => [ + 'with_a_filter' => [ 'query' => Query::create(Filter::create('field', 'value')), 'expected' => [ 'query' => ['bool' => ['filter' => ['bool' => ['must' => [['term' => ['field' => 'value']]]]]]], ], ], - 'with a search, default search operator' => [ + 'with_a_search_and_default_search_operator' => [ 'query' => Query::create(Search::create(['field_a'], 'foo')), 'expected' => [ 'query' => [ @@ -319,7 +320,7 @@ private static function toArrayBatch1(): array private static function toArrayBatch2(): array { return [ - 'with two searches, AND connected' => [ + 'with_two_searches_connected_by_and' => [ 'query' => Query::create( Search::create(['field_a'], 'foo'), Search::create(['field_b'], 'bar') @@ -335,7 +336,7 @@ private static function toArrayBatch2(): array ], ], ], - 'with an aggregation' => [ + 'with_an_aggregation' => [ 'query' => Query::create(Aggregation::create('field_a', Metric::SUM, 'my_agg')), 'expected' => [ 'aggs' => [ @@ -345,7 +346,7 @@ private static function toArrayBatch2(): array ], ], ], - 'with a little bit of everything' => [ + 'with_a_little_bit_of_everything' => [ 'query' => Query::create( Filter::create('field', 'value'), Search::create(['field_a'], 'foo'), @@ -369,7 +370,7 @@ private static function toArrayBatch2(): array 'min_score' => 42, ], ], - 'advanced full text queries combined with bool queries' => [ + 'advanced_full_text_queries_combined_with_bool_queries' => [ 'query' => Query::create( Filter::create('field', 'value') ) @@ -440,7 +441,7 @@ private static function toArrayBatch2(): array private static function toArrayBatch3(): array { return [ - 'basic nested query as filter' => [ + 'basic_nested_query_as_filter' => [ 'query' => Query::create( Query::createNested('my_field', Filter::create('my_field.subfield', 'foobar')) ), @@ -477,7 +478,7 @@ private static function toArrayBatch3(): array ], ], ], - 'basic nested query as search' => [ + 'basic_nested_query_as_search' => [ 'query' => Query::create() ->search(Query::createNested('my_field', Filter::create('my_field.subfield', 'foobar'))), 'expected' => [ @@ -510,7 +511,7 @@ private static function toArrayBatch3(): array ], ], ], - 'nested query with options' => [ + 'nested_query_with_options' => [ 'query' => Query::create( Query::createNested('my_field', Filter::create('my_field.subfield', 'foobar')) ->setOption(NestableQueryInterface::OPTION_SCORE_MODE, 'max') @@ -554,16 +555,13 @@ private static function toArrayBatch3(): array ]; } - private function getPublicReflectionProperty(Query $query, string $fieldName): ReflectionProperty + private static function getPublicReflectionProperty(Query $query, string $fieldName): ReflectionProperty { - $property = new ReflectionProperty($query, $fieldName); - $property->setAccessible(true); - - return $property; + return new ReflectionProperty($query, $fieldName); } - private function assertChildren(Query $query, string $fieldName, array $expected): void + private static function assertChildren(Query $query, string $fieldName, array $expected): void { - $this->assertEquals($expected, $this->getPublicReflectionProperty($query, $fieldName)->getValue($query)); + self::assertEquals($expected, self::getPublicReflectionProperty($query, $fieldName)->getValue($query)); } } diff --git a/tests/Query/RawQueryTest.php b/tests/Query/RawQueryTest.php index 88cc74e..5527ccd 100644 --- a/tests/Query/RawQueryTest.php +++ b/tests/Query/RawQueryTest.php @@ -5,16 +5,17 @@ use Kununu\Elasticsearch\Query\RawQuery; use Kununu\Elasticsearch\Query\SortOrder; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; final class RawQueryTest extends TestCase { - /** @dataProvider createDataProvider */ + #[DataProvider('createDataProvider')] public function testCreate(array $rawQuery): void { $query = RawQuery::create($rawQuery); - $this->assertEquals($rawQuery, $query->toArray()); + self::assertEquals($rawQuery, $query->toArray()); } public static function createDataProvider(): array @@ -23,7 +24,7 @@ public static function createDataProvider(): array 'empty' => [ 'rawQuery' => [], ], - 'non-empty' => [ + 'non_empty' => [ 'rawQuery' => ['query' => ['term' => ['field' => 'value']]], ], ]; @@ -37,7 +38,7 @@ public function testCommonFunctionalityIsPreservedOnToArray(): void ->skip(1) ->limit(10); - $this->assertEquals( + self::assertEquals( [ 'query' => ['term' => ['field' => 'value']], '_source' => ['field_a'], diff --git a/tests/Repository/AbstractRepositoryTestCase.php b/tests/Repository/AbstractRepositoryTestCase.php index b50b0b6..f4d5947 100644 --- a/tests/Repository/AbstractRepositoryTestCase.php +++ b/tests/Repository/AbstractRepositoryTestCase.php @@ -7,8 +7,6 @@ use Kununu\Elasticsearch\Query\Criteria\Filter; use Kununu\Elasticsearch\Query\Query; use Kununu\Elasticsearch\Query\RawQuery; -use Kununu\Elasticsearch\Repository\EntityFactoryInterface; -use Kununu\Elasticsearch\Repository\PersistableEntityInterface; use Kununu\Elasticsearch\Repository\Repository; use Kununu\Elasticsearch\Repository\RepositoryInterface; use PHPUnit\Framework\MockObject\MockObject; @@ -29,19 +27,19 @@ abstract class AbstractRepositoryTestCase extends TestCase protected const DOCUMENT_COUNT = 42; protected const SCROLL_ID = 'DnF1ZXJ5VGhlbkZldGNoBQAAAAAAAAFbFkJVNEdjZWVjU'; - protected Client|MockObject $clientMock; - protected LoggerInterface|MockObject $loggerMock; + protected MockObject&Client $clientMock; + protected MockObject&LoggerInterface $loggerMock; public static function invalidDataTypesForSaveAndUpsertDataProvider(): array { return [ - [7], - [7.7], - [''], - ['string'], - [true], - [false], - [null], + 'integer' => [7], + 'float' => [7.7], + 'empty_string' => [''], + 'string' => ['string'], + 'true' => [true], + 'false' => [false], + 'null' => [null], ]; } @@ -76,18 +74,18 @@ public static function searchResultWithEntitiesDataProvider(): array public static function queriesDataProvider(): array { return [ - 'empty kununu query' => [ + 'empty_kununu_query' => [ 'query' => Query::create(), ], - 'some kununu term query' => [ + 'some_kununu_term_query' => [ 'query' => Query::create( Filter::create('foo', 'bar') ), ], - 'empty raw query' => [ + 'empty_raw_query' => [ 'query' => RawQuery::create(), ], - 'some raw term query' => [ + 'some_raw_term_query' => [ 'query' => RawQuery::create(['query' => ['bool' => ['must' => [['term' => ['foo' => 'bar']]]]]]), ], ]; @@ -96,7 +94,7 @@ public static function queriesDataProvider(): array public static function searchResultDataProvider(): array { return [ - 'no results' => [ + 'no_results' => [ 'es_result' => [ 'hits' => [ 'total' => [ @@ -108,7 +106,7 @@ public static function searchResultDataProvider(): array ], 'end_result' => [], ], - 'one result' => [ + 'one_result' => [ 'es_result' => [ 'hits' => [ 'total' => [ @@ -135,7 +133,7 @@ public static function searchResultDataProvider(): array ], ], ], - 'two results' => [ + 'two_results' => [ 'es_result' => [ 'hits' => [ 'total' => [ @@ -153,8 +151,8 @@ public static function searchResultDataProvider(): array '_index' => self::INDEX['read'], '_score' => 77, '_source' => [ - 'second' => 'result', - 'with_more_than' => 'one field', + 'second' => 'result', + 'withMoreThan' => 'one field', ], ], ], @@ -172,8 +170,8 @@ public static function searchResultDataProvider(): array '_index' => self::INDEX['read'], '_score' => 77, '_source' => [ - 'second' => 'result', - 'with_more_than' => 'one field', + 'second' => 'result', + 'withMoreThan' => 'one field', ], ], ], @@ -187,7 +185,7 @@ public static function searchResultVariationsDataProvider(): array foreach (self::searchResultDataProvider() as $caseName => $case) { foreach ([true, false] as $scroll) { $newCase = $case; - $fullCaseName = $caseName . '; scroll: ' . ($scroll ? 'true' : 'false'); + $fullCaseName = sprintf('%s_with_scroll_%s', $caseName, json_encode($scroll)); if ($scroll) { $newCase['es_result']['_scroll_id'] = self::SCROLL_ID; } @@ -206,21 +204,6 @@ protected function setUp(): void $this->loggerMock = $this->createMock(LoggerInterface::class); } - protected function getEntityClassInstance(): PersistableEntityInterface - { - return new PersistableEntityStub(); - } - - protected function getEntityClass(): string - { - return PersistableEntityStub::class; - } - - protected function getEntityFactory(): EntityFactoryInterface - { - return new EntityFactoryStub(); - } - protected function getRepository(array $additionalConfig = []): RepositoryInterface { $repo = new Repository( @@ -244,7 +227,10 @@ protected static function mergeQueryAndResultsVariations(array $queryVariations, $allVariations = []; foreach ($queryVariations as $queryName => $queryVariation) { foreach ($resultsVariations as $resultsName => $resultsVariation) { - $allVariations[$queryName . ', ' . $resultsName] = array_merge($queryVariation, $resultsVariation); + $allVariations[sprintf('%s_%s', $queryName, $resultsName)] = array_merge( + $queryVariation, + $resultsVariation + ); } } diff --git a/tests/Repository/EntitySerializerStub.php b/tests/Repository/EntitySerializerStub.php index 9b9e3ef..b6c843b 100644 --- a/tests/Repository/EntitySerializerStub.php +++ b/tests/Repository/EntitySerializerStub.php @@ -5,7 +5,7 @@ use Kununu\Elasticsearch\Repository\EntitySerializerInterface; -class EntitySerializerStub implements EntitySerializerInterface +final class EntitySerializerStub implements EntitySerializerInterface { public function toElastic(mixed $entity): array { diff --git a/tests/Repository/PersistableEntityStub.php b/tests/Repository/PersistableEntityStub.php index 1846744..72b40c5 100644 --- a/tests/Repository/PersistableEntityStub.php +++ b/tests/Repository/PersistableEntityStub.php @@ -7,6 +7,13 @@ final class PersistableEntityStub implements PersistableEntityInterface { + public array $_meta; + public mixed $property_a; + public mixed $property_b; + public mixed $foo; + public mixed $second; + public mixed $withMoreThan; + public function toElastic(): array { return (array) $this; diff --git a/tests/Repository/RepositoryAggregateByQueryTest.php b/tests/Repository/RepositoryAggregateByQueryTest.php index 8a23615..8582359 100644 --- a/tests/Repository/RepositoryAggregateByQueryTest.php +++ b/tests/Repository/RepositoryAggregateByQueryTest.php @@ -9,14 +9,15 @@ use Kununu\Elasticsearch\Query\Criteria\Filter; use Kununu\Elasticsearch\Query\Query; use Kununu\Elasticsearch\Query\QueryInterface; +use PHPUnit\Framework\Attributes\DataProvider; final class RepositoryAggregateByQueryTest extends AbstractRepositoryTestCase { - /** @dataProvider queryAndSearchResultDataProvider */ + #[DataProvider('queryAndSearchResultDataProvider')] public function testAggregateByQuery(QueryInterface $query, array $esResult): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('search') ->with([ 'index' => self::INDEX['read'], @@ -26,25 +27,25 @@ public function testAggregateByQuery(QueryInterface $query, array $esResult): vo $aggregationResult = $this->getRepository()->aggregateByQuery($query); - $this->assertEquals(count($esResult['hits']['hits']), $aggregationResult->getDocuments()->getCount()); - $this->assertEquals(self::DOCUMENT_COUNT, $aggregationResult->getDocuments()->getTotal()); - $this->assertCount(count($esResult['hits']['hits']), $aggregationResult->getDocuments()); - $this->assertNull($aggregationResult->getDocuments()->getScrollId()); - $this->assertEquals($esResult['hits']['hits'], $aggregationResult->getDocuments()->asArray()); + self::assertEquals(count($esResult['hits']['hits']), $aggregationResult->getDocuments()->getCount()); + self::assertEquals(self::DOCUMENT_COUNT, $aggregationResult->getDocuments()->getTotal()); + self::assertCount(count($esResult['hits']['hits']), $aggregationResult->getDocuments()); + self::assertNull($aggregationResult->getDocuments()->getScrollId()); + self::assertEquals($esResult['hits']['hits'], $aggregationResult->getDocuments()->asArray()); - $this->assertCount(1, $aggregationResult->getResults()); - $this->assertEquals('my_aggregation', $aggregationResult->getResultByName('my_aggregation')->getName()); - $this->assertEquals(0.1, $aggregationResult->getResultByName('my_aggregation')->getValue()); + self::assertCount(1, $aggregationResult->getResults()); + self::assertEquals('my_aggregation', $aggregationResult->getResultByName('my_aggregation')->getName()); + self::assertEquals(0.1, $aggregationResult->getResultByName('my_aggregation')->getValue()); } - /** @dataProvider queryAndSearchResultWithEntitiesDataProvider */ + #[DataProvider('queryAndSearchResultWithEntitiesDataProvider')] public function testAggregateByQueryWithEntityFactory( QueryInterface $query, array $esResult, mixed $endResult ): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('search') ->with([ 'index' => self::INDEX['read'], @@ -53,34 +54,34 @@ public function testAggregateByQueryWithEntityFactory( ->willReturn(array_merge($esResult, ['aggregations' => ['my_aggregation' => ['value' => 0.1]]])); $aggregationResult = $this - ->getRepository(['entity_factory' => $this->getEntityFactory()]) + ->getRepository(['entity_factory' => new EntityFactoryStub()]) ->aggregateByQuery($query); - $this->assertEquals(count($esResult['hits']['hits']), $aggregationResult->getDocuments()->getCount()); - $this->assertEquals(self::DOCUMENT_COUNT, $aggregationResult->getDocuments()->getTotal()); - $this->assertCount(count($esResult['hits']['hits']), $aggregationResult->getDocuments()); - $this->assertNull($aggregationResult->getDocuments()->getScrollId()); - $this->assertEquals($endResult, $aggregationResult->getDocuments()->asArray()); + self::assertEquals(count($esResult['hits']['hits']), $aggregationResult->getDocuments()->getCount()); + self::assertEquals(self::DOCUMENT_COUNT, $aggregationResult->getDocuments()->getTotal()); + self::assertCount(count($esResult['hits']['hits']), $aggregationResult->getDocuments()); + self::assertNull($aggregationResult->getDocuments()->getScrollId()); + self::assertEquals($endResult, $aggregationResult->getDocuments()->asArray()); if (!empty($aggregationResult->getDocuments())) { foreach ($aggregationResult->getDocuments() as $entity) { - $this->assertEquals(['_index' => self::INDEX['read'], '_score' => 77], $entity->_meta); + self::assertEquals(['_index' => self::INDEX['read'], '_score' => 77], $entity->_meta); } } - $this->assertCount(1, $aggregationResult->getResults()); - $this->assertEquals('my_aggregation', $aggregationResult->getResultByName('my_aggregation')->getName()); - $this->assertEquals(0.1, $aggregationResult->getResultByName('my_aggregation')->getValue()); + self::assertCount(1, $aggregationResult->getResults()); + self::assertEquals('my_aggregation', $aggregationResult->getResultByName('my_aggregation')->getName()); + self::assertEquals(0.1, $aggregationResult->getResultByName('my_aggregation')->getValue()); } - /** @dataProvider queryAndSearchResultWithEntitiesDataProvider */ + #[DataProvider('queryAndSearchResultWithEntitiesDataProvider')] public function testAggregateByQueryWithEntityClass( QueryInterface $query, array $esResult, mixed $endResult ): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('search') ->with([ 'index' => self::INDEX['read'], @@ -89,24 +90,24 @@ public function testAggregateByQueryWithEntityClass( ->willReturn(array_merge($esResult, ['aggregations' => ['my_aggregation' => ['value' => 0.1]]])); $aggregationResult = $this - ->getRepository(['entity_class' => $this->getEntityClass()]) + ->getRepository(['entity_class' => PersistableEntityStub::class]) ->aggregateByQuery($query); - $this->assertEquals(count($esResult['hits']['hits']), $aggregationResult->getDocuments()->getCount()); - $this->assertEquals(self::DOCUMENT_COUNT, $aggregationResult->getDocuments()->getTotal()); - $this->assertCount(count($esResult['hits']['hits']), $aggregationResult->getDocuments()); - $this->assertNull($aggregationResult->getDocuments()->getScrollId()); - $this->assertEquals($endResult, $aggregationResult->getDocuments()->asArray()); + self::assertEquals(count($esResult['hits']['hits']), $aggregationResult->getDocuments()->getCount()); + self::assertEquals(self::DOCUMENT_COUNT, $aggregationResult->getDocuments()->getTotal()); + self::assertCount(count($esResult['hits']['hits']), $aggregationResult->getDocuments()); + self::assertNull($aggregationResult->getDocuments()->getScrollId()); + self::assertEquals($endResult, $aggregationResult->getDocuments()->asArray()); if (!empty($aggregationResult->getDocuments())) { foreach ($aggregationResult->getDocuments() as $entity) { - $this->assertEquals(['_index' => self::INDEX['read'], '_score' => 77], $entity->_meta); + self::assertEquals(['_index' => self::INDEX['read'], '_score' => 77], $entity->_meta); } } - $this->assertCount(1, $aggregationResult->getResults()); - $this->assertEquals('my_aggregation', $aggregationResult->getResultByName('my_aggregation')->getName()); - $this->assertEquals(0.1, $aggregationResult->getResultByName('my_aggregation')->getValue()); + self::assertCount(1, $aggregationResult->getResults()); + self::assertEquals('my_aggregation', $aggregationResult->getResultByName('my_aggregation')->getName()); + self::assertEquals(0.1, $aggregationResult->getResultByName('my_aggregation')->getValue()); } public function testAggregateByQueryFails(): void @@ -117,7 +118,7 @@ public function testAggregateByQueryFails(): void ); $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('search') ->willThrowException(new Exception(self::ERROR_MESSAGE)); @@ -128,9 +129,9 @@ public function testAggregateByQueryFails(): void try { $this->getRepository()->aggregateByQuery($query); } catch (ReadOperationException $e) { - $this->assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); - $this->assertEquals(0, $e->getCode()); - $this->assertNull($e->getQuery()); + self::assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); + self::assertEquals(0, $e->getCode()); + self::assertNull($e->getQuery()); } } } diff --git a/tests/Repository/RepositoryClearScrollIdTest.php b/tests/Repository/RepositoryClearScrollIdTest.php index 49a1085..46d678a 100644 --- a/tests/Repository/RepositoryClearScrollIdTest.php +++ b/tests/Repository/RepositoryClearScrollIdTest.php @@ -13,7 +13,7 @@ public function testClearScrollId(): void $scrollId = 'foobar'; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('clearScroll') ->with([ 'body' => [ @@ -22,7 +22,7 @@ public function testClearScrollId(): void ]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $this->getRepository()->clearScrollId($scrollId); @@ -33,7 +33,7 @@ public function testClearScrollIdFails(): void $scrollId = 'foobar'; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('clearScroll') ->with([ 'body' => [ @@ -43,7 +43,7 @@ public function testClearScrollIdFails(): void ->willThrowException(new Exception(self::ERROR_MESSAGE)); $this->loggerMock - ->expects($this->once()) + ->expects(self::once()) ->method('error') ->with( self::ERROR_PREFIX . self::ERROR_MESSAGE @@ -52,8 +52,8 @@ public function testClearScrollIdFails(): void try { $this->getRepository()->clearScrollId($scrollId); } catch (RepositoryException $e) { - $this->assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); - $this->assertEquals(0, $e->getCode()); + self::assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); + self::assertEquals(0, $e->getCode()); } } } diff --git a/tests/Repository/RepositoryConfigurationTest.php b/tests/Repository/RepositoryConfigurationTest.php index b96764b..43a8599 100644 --- a/tests/Repository/RepositoryConfigurationTest.php +++ b/tests/Repository/RepositoryConfigurationTest.php @@ -9,45 +9,46 @@ use Kununu\Elasticsearch\Repository\OperationType; use Kununu\Elasticsearch\Repository\PersistableEntityInterface; use Kununu\Elasticsearch\Repository\RepositoryConfiguration; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use stdClass; use TypeError; final class RepositoryConfigurationTest extends TestCase { - /** @dataProvider inflateConfigDataProvider */ + #[DataProvider('inflateConfigDataProvider')] public function testInflateConfig(array $input, string $expectedReadAlias, string $expectedWriteAlias): void { $config = new RepositoryConfiguration($input); - $this->assertEquals($expectedReadAlias, $config->getIndex(OperationType::READ)); - $this->assertEquals($expectedWriteAlias, $config->getIndex(OperationType::WRITE)); + self::assertEquals($expectedReadAlias, $config->getIndex(OperationType::READ)); + self::assertEquals($expectedWriteAlias, $config->getIndex(OperationType::WRITE)); } public static function inflateConfigDataProvider(): array { return [ - 'only index name given' => [ + 'only_index_name_given' => [ 'input' => ['index' => 'my_index'], 'expected_read_alias' => 'my_index', 'expected_write_alias' => 'my_index', ], - 'index name and read alias given' => [ + 'index_name_and_read_alias_given' => [ 'input' => ['index' => 'my_index', 'index_read' => 'my_index_read'], 'expected_read_alias' => 'my_index_read', 'expected_write_alias' => 'my_index', ], - 'index name and write alias given' => [ + 'index_name_and_write_alias_given' => [ 'input' => ['index' => 'my_index', 'index_write' => 'my_index_write'], 'expected_read_alias' => 'my_index', 'expected_write_alias' => 'my_index_write', ], - 'read and write alias given' => [ + 'read_and_write_alias_given' => [ 'input' => ['index_read' => 'my_index_read', 'index_write' => 'my_index_write'], 'expected_read_alias' => 'my_index_read', 'expected_write_alias' => 'my_index_write', ], - 'index name as well as read and write alias given' => [ + 'index_name_as_well_as_read_and_write_alias_given' => [ 'input' => [ 'index' => 'this_will_be_ignored', 'index_read' => 'my_index_read', @@ -59,7 +60,7 @@ public static function inflateConfigDataProvider(): array ]; } - /** @dataProvider noValidIndexConfiguredDataProvider */ + #[DataProvider('noValidIndexConfiguredDataProvider')] public function testNoValidIndexConfigured(array $input, string $operationType, string $expectedExceptionMsg): void { $config = new RepositoryConfiguration($input); @@ -73,19 +74,19 @@ public function testNoValidIndexConfigured(array $input, string $operationType, public static function noValidIndexConfiguredDataProvider(): array { $cases = [ - 'nothing given' => [ + 'nothing_given' => [ 'input' => [], ], - 'empty index name given' => [ + 'empty_index_name_given' => [ 'input' => ['index' => ''], ], - 'empty aliases given' => [ + 'empty_aliases_given' => [ 'input' => ['index_read' => '', 'index_write' => ''], ], - 'null index name given' => [ + 'null_index_name_given' => [ 'input' => ['index' => null], ], - 'null aliases given' => [ + 'null_aliases_given' => [ 'input' => ['index_read' => null, 'index_write' => null], ], ]; @@ -94,9 +95,11 @@ public static function noValidIndexConfiguredDataProvider(): array foreach ($cases as $name => $data) { foreach ([OperationType::READ, OperationType::WRITE] as $operationType) { $data['operation_type'] = $operationType; - $data['expected_exception_msg'] = - 'No valid index name configured for operation "' . $operationType . '"'; - $variations['operation type ' . $operationType . ' ' . $name] = $data; + $data['expected_exception_msg'] = sprintf( + 'No valid index name configured for operation "%s"', + $operationType + ); + $variations[sprintf('operation_type_%s_%s', $operationType, $name)] = $data; } } @@ -114,7 +117,7 @@ public function toElastic($entity): array $config = new RepositoryConfiguration(['entity_serializer' => $mySerializer]); - $this->assertEquals($mySerializer, $config->getEntitySerializer()); + self::assertEquals($mySerializer, $config->getEntitySerializer()); } public function testInvalidEntitySerializer(): void @@ -123,7 +126,7 @@ public function testInvalidEntitySerializer(): void $this->expectException(TypeError::class); - $this->assertNull(new RepositoryConfiguration(['entity_serializer' => $mySerializer])); + self::assertNull(new RepositoryConfiguration(['entity_serializer' => $mySerializer])); } public function testValidEntityFactory(): void @@ -137,7 +140,7 @@ public function fromDocument(array $document, array $metaData): object $config = new RepositoryConfiguration(['entity_factory' => $myFactory]); - $this->assertEquals($myFactory, $config->getEntityFactory()); + self::assertEquals($myFactory, $config->getEntityFactory()); } public function testInvalidEntityFactory(): void @@ -146,7 +149,7 @@ public function testInvalidEntityFactory(): void $this->expectException(TypeError::class); - $this->assertNull(new RepositoryConfiguration(['entity_factory' => $myFactory])); + self::assertNull(new RepositoryConfiguration(['entity_factory' => $myFactory])); } public function testValidEntityClass(): void @@ -178,7 +181,7 @@ public function testInvalidEntityClass(): void ) ); - $this->assertNull(new RepositoryConfiguration(['entity_class' => stdClass::class])); + self::assertNull(new RepositoryConfiguration(['entity_class' => stdClass::class])); } public function testNonExistentEntityClass(): void @@ -188,62 +191,62 @@ public function testNonExistentEntityClass(): void 'Given entity class does not exist.' ); - $this->assertNull(new RepositoryConfiguration(['entity_class' => '\Foo\Bar'])); + self::assertNull(new RepositoryConfiguration(['entity_class' => '\Foo\Bar'])); } - /** @dataProvider forceRefreshOnWriteDataProvider */ + #[DataProvider('forceRefreshOnWriteDataProvider')] public function testForceRefreshOnWrite(array $input, bool $expected): void { $config = new RepositoryConfiguration($input); - $this->assertSame($expected, $config->getForceRefreshOnWrite()); + self::assertSame($expected, $config->getForceRefreshOnWrite()); } public static function forceRefreshOnWriteDataProvider(): array { return [ - 'param not given' => [ + 'param_not_given' => [ 'input' => [ 'index' => 'foobar', ], 'expected' => false, ], - 'false given' => [ + 'false_given' => [ 'input' => [ 'index' => 'foobar', 'force_refresh_on_write' => false, ], 'expected' => false, ], - 'falsy value given' => [ + 'falsy_value_given' => [ 'input' => [ 'index' => 'foobar', 'force_refresh_on_write' => 0, ], 'expected' => false, ], - 'true given' => [ + 'true_given' => [ 'input' => [ 'index' => 'foobar', 'force_refresh_on_write' => true, ], 'expected' => true, ], - 'true-ish integer given' => [ + 'true-ish_integer_given' => [ 'input' => [ 'index' => 'foobar', 'force_refresh_on_write' => 1, ], 'expected' => true, ], - 'true-ish string given' => [ + 'true-ish_string_given' => [ 'input' => [ 'index' => 'foobar', 'force_refresh_on_write' => 'yes', ], 'expected' => true, ], - 'not-so-clever-but-still-true-ish string given' => [ + 'not-so-clever-but-still-true-ish_string_given' => [ 'input' => [ 'index' => 'foobar', 'force_refresh_on_write' => 'no', @@ -253,59 +256,59 @@ public static function forceRefreshOnWriteDataProvider(): array ]; } - /** @dataProvider trackTotalHitsDataProvider */ + #[DataProvider('trackTotalHitsDataProvider')] public function testTrackTotalHits(array $input, ?bool $expected): void { $config = new RepositoryConfiguration($input); - $this->assertSame($expected, $config->getTrackTotalHits()); + self::assertSame($expected, $config->getTrackTotalHits()); } public static function trackTotalHitsDataProvider(): array { return [ - 'param not given' => [ + 'param_not_given' => [ 'input' => [ 'index' => 'foobar', ], 'expected' => null, ], - 'false given' => [ + 'false_given' => [ 'input' => [ 'index' => 'foobar', 'track_total_hits' => false, ], 'expected' => false, ], - 'falsy value given' => [ + 'falsy_value_given' => [ 'input' => [ 'index' => 'foobar', 'track_total_hits' => 0, ], 'expected' => false, ], - 'true given' => [ + 'true_given' => [ 'input' => [ 'index' => 'foobar', 'track_total_hits' => true, ], 'expected' => true, ], - 'true-ish integer given' => [ + 'true-ish_integer_given' => [ 'input' => [ 'index' => 'foobar', 'track_total_hits' => 1, ], 'expected' => true, ], - 'true-ish string given' => [ + 'true-ish_string_given' => [ 'input' => [ 'index' => 'foobar', 'track_total_hits' => 'yes', ], 'expected' => true, ], - 'not-so-clever-but-still-true-ish string given' => [ + 'not-so-clever-but-still-true-ish_string_given' => [ 'input' => [ 'index' => 'foobar', 'track_total_hits' => 'no', @@ -315,24 +318,24 @@ public static function trackTotalHitsDataProvider(): array ]; } - /** @dataProvider scrollContextKeepaliveDataProvider */ + #[DataProvider('scrollContextKeepaliveDataProvider')] public function testScrollContextKeepalive(array $input, string $expected): void { $config = new RepositoryConfiguration($input); - $this->assertSame($expected, $config->getScrollContextKeepalive()); + self::assertSame($expected, $config->getScrollContextKeepalive()); } public static function scrollContextKeepaliveDataProvider(): array { return [ - 'param not given' => [ + 'param_not_given' => [ 'input' => [ 'index' => 'foobar', ], 'expected' => '1m', // the default ], - 'valid time unit given' => [ + 'valid_time_unit_given' => [ 'input' => [ 'index' => 'foobar', 'scroll_context_keepalive' => '10m', @@ -349,14 +352,14 @@ public function testInvalidScrollContextKeepalive(): void 'Invalid value for scroll_context_keepalive given. Must be a valid time unit.' ); - $this->assertNull(new RepositoryConfiguration(['index' => 'foobar', 'scroll_context_keepalive' => 'xxx'])); + self::assertNull(new RepositoryConfiguration(['index' => 'foobar', 'scroll_context_keepalive' => 'xxx'])); } public function testGetDefaultScrollContextKeepalive(): void { $config = new RepositoryConfiguration([]); - $this->assertSame('1m', $config->getScrollContextKeepalive()); - $this->assertFalse($config->getForceRefreshOnWrite()); + self::assertSame('1m', $config->getScrollContextKeepalive()); + self::assertFalse($config->getForceRefreshOnWrite()); } } diff --git a/tests/Repository/RepositoryCountByQueryTest.php b/tests/Repository/RepositoryCountByQueryTest.php index 7b76431..999ed32 100644 --- a/tests/Repository/RepositoryCountByQueryTest.php +++ b/tests/Repository/RepositoryCountByQueryTest.php @@ -7,14 +7,15 @@ use Kununu\Elasticsearch\Exception\ReadOperationException; use Kununu\Elasticsearch\Query\Query; use Kununu\Elasticsearch\Query\QueryInterface; +use PHPUnit\Framework\Attributes\DataProvider; final class RepositoryCountByQueryTest extends AbstractRepositoryTestCase { - /** @dataProvider queriesDataProvider */ + #[DataProvider('queriesDataProvider')] public function testCountByQuery(QueryInterface $query): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('count') ->with([ 'index' => self::INDEX['read'], @@ -22,26 +23,27 @@ public function testCountByQuery(QueryInterface $query): void ]) ->willReturn(['count' => self::DOCUMENT_COUNT]); - $this->assertEquals(self::DOCUMENT_COUNT, $this->getRepository()->countByQuery($query)); + self::assertEquals(self::DOCUMENT_COUNT, $this->getRepository()->countByQuery($query)); } public function testCountByQueryFails(): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('count') ->willThrowException(new Exception(self::ERROR_MESSAGE)); $this->loggerMock + ->expects(self::once()) ->method('error') ->with(self::ERROR_PREFIX . self::ERROR_MESSAGE); try { $this->getRepository()->countByQuery(Query::create()); } catch (ReadOperationException $e) { - $this->assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); - $this->assertEquals(0, $e->getCode()); - $this->assertNull($e->getQuery()); + self::assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); + self::assertEquals(0, $e->getCode()); + self::assertNull($e->getQuery()); } } } diff --git a/tests/Repository/RepositoryCountTest.php b/tests/Repository/RepositoryCountTest.php index 0c25501..469572c 100644 --- a/tests/Repository/RepositoryCountTest.php +++ b/tests/Repository/RepositoryCountTest.php @@ -14,7 +14,7 @@ public function testCount(): void $query = Query::create(); $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('count') ->with([ 'index' => self::INDEX['read'], @@ -22,26 +22,27 @@ public function testCount(): void ]) ->willReturn(['count' => self::DOCUMENT_COUNT]); - $this->assertEquals(self::DOCUMENT_COUNT, $this->getRepository()->count()); + self::assertEquals(self::DOCUMENT_COUNT, $this->getRepository()->count()); } public function testCountFails(): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('count') ->willThrowException(new Exception(self::ERROR_MESSAGE)); $this->loggerMock + ->expects(self::once()) ->method('error') ->with(self::ERROR_PREFIX . self::ERROR_MESSAGE); try { $this->getRepository()->count(); } catch (ReadOperationException $e) { - $this->assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); - $this->assertEquals(0, $e->getCode()); - $this->assertNull($e->getQuery()); + self::assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); + self::assertEquals(0, $e->getCode()); + self::assertNull($e->getQuery()); } } } diff --git a/tests/Repository/RepositoryDeleteBulkTest.php b/tests/Repository/RepositoryDeleteBulkTest.php index a42d1ef..52ef72c 100644 --- a/tests/Repository/RepositoryDeleteBulkTest.php +++ b/tests/Repository/RepositoryDeleteBulkTest.php @@ -6,14 +6,13 @@ use Exception; use Kununu\Elasticsearch\Exception\BulkException; use Kununu\Elasticsearch\Repository\Repository; -use PHPUnit\Framework\TestCase; final class RepositoryDeleteBulkTest extends AbstractRepositoryTestCase { public function testDeleteBulk(): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('bulk') ->with([ 'index' => self::INDEX['write'], @@ -32,7 +31,7 @@ public function testDeleteBulk(): void ]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $this->getRepository()->deleteBulk(self::ID, self::ID_2); @@ -41,7 +40,7 @@ public function testDeleteBulk(): void public function testDeleteBulkWithForcedRefresh(): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('bulk') ->with([ 'index' => self::INDEX['write'], @@ -61,7 +60,7 @@ public function testDeleteBulkWithForcedRefresh(): void ]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $this->getRepository(['force_refresh_on_write' => true])->deleteBulk(self::ID, self::ID_2); @@ -70,7 +69,7 @@ public function testDeleteBulkWithForcedRefresh(): void public function testDeleteBulkFails(): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('bulk') ->with([ 'index' => self::INDEX['write'], @@ -90,22 +89,23 @@ public function testDeleteBulkFails(): void ->willThrowException(new Exception(self::ERROR_MESSAGE)); $this->loggerMock + ->expects(self::once()) ->method('error') ->with(self::ERROR_PREFIX . self::ERROR_MESSAGE); try { $this->getRepository()->deleteBulk(self::ID, self::ID_2); } catch (BulkException $e) { - $this->assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); - $this->assertEquals(0, $e->getCode()); - $this->assertEquals($operations, $e->getOperations()); + self::assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); + self::assertEquals(0, $e->getCode()); + self::assertEquals($operations, $e->getOperations()); } } public function testPostDeleteBulkIsCalled(): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('bulk') ->with([ 'index' => self::INDEX['write'], @@ -124,15 +124,15 @@ public function testPostDeleteBulkIsCalled(): void ]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $manager = new class($this->clientMock, ['index_write' => self::INDEX['write']]) extends Repository { protected function postDeleteBulk(string ...$ids): void { - TestCase::assertCount(2, $ids); - TestCase::assertEquals(AbstractRepositoryTestCase::ID, $ids[0]); - TestCase::assertEquals(AbstractRepositoryTestCase::ID_2, $ids[1]); + AbstractRepositoryTestCase::assertCount(2, $ids); + AbstractRepositoryTestCase::assertEquals(AbstractRepositoryTestCase::ID, $ids[0]); + AbstractRepositoryTestCase::assertEquals(AbstractRepositoryTestCase::ID_2, $ids[1]); } }; @@ -142,11 +142,11 @@ protected function postDeleteBulk(string ...$ids): void public function testDeleteBulkWithoutIds(): void { $this->clientMock - ->expects($this->never()) + ->expects(self::never()) ->method('bulk'); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $this->getRepository()->deleteBulk(); diff --git a/tests/Repository/RepositoryDeleteByQueryTest.php b/tests/Repository/RepositoryDeleteByQueryTest.php index e827636..b2f6fc4 100644 --- a/tests/Repository/RepositoryDeleteByQueryTest.php +++ b/tests/Repository/RepositoryDeleteByQueryTest.php @@ -15,7 +15,7 @@ public function testDeleteByQuery(): void $expectedResult = ['some_fake_es_response' => 'deletion was successful']; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('deleteByQuery') ->with([ 'index' => self::INDEX['write'], @@ -40,7 +40,7 @@ public function testDeleteByQuery(): void ->willReturn($expectedResult); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $result = $this->getRepository()->deleteByQuery( @@ -49,7 +49,7 @@ public function testDeleteByQuery(): void ) ); - $this->assertSame($expectedResult, $result); + self::assertSame($expectedResult, $result); } public function testDeleteByQueryWithForcedRefresh(): void @@ -57,7 +57,7 @@ public function testDeleteByQueryWithForcedRefresh(): void $expectedResult = ['some_fake_es_response' => 'deletion was successful']; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('deleteByQuery') ->with([ 'index' => self::INDEX['write'], @@ -83,7 +83,7 @@ public function testDeleteByQueryWithForcedRefresh(): void ->willReturn($expectedResult); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $result = $this->getRepository(['force_refresh_on_write' => true])->deleteByQuery( @@ -92,7 +92,7 @@ public function testDeleteByQueryWithForcedRefresh(): void ) ); - $this->assertSame($expectedResult, $result); + self::assertSame($expectedResult, $result); } public function testDeleteByQueryWithProceedOnConflicts(): void @@ -100,7 +100,7 @@ public function testDeleteByQueryWithProceedOnConflicts(): void $expectedResult = ['some_fake_es_response' => 'deletion was successful']; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('deleteByQuery') ->with([ 'index' => self::INDEX['write'], @@ -126,7 +126,7 @@ public function testDeleteByQueryWithProceedOnConflicts(): void ->willReturn($expectedResult); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $result = $this->getRepository()->deleteByQuery( @@ -136,13 +136,13 @@ public function testDeleteByQueryWithProceedOnConflicts(): void true ); - $this->assertSame($expectedResult, $result); + self::assertSame($expectedResult, $result); } public function testDeleteByQueryFails(): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('deleteByQuery') ->with([ 'index' => self::INDEX['write'], @@ -167,6 +167,7 @@ public function testDeleteByQueryFails(): void ->willThrowException(new Exception(self::ERROR_MESSAGE)); $this->loggerMock + ->expects(self::once()) ->method('error') ->with(self::ERROR_PREFIX . self::ERROR_MESSAGE); @@ -177,8 +178,8 @@ public function testDeleteByQueryFails(): void ) ); } catch (WriteOperationException $e) { - $this->assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); - $this->assertEquals(0, $e->getCode()); + self::assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); + self::assertEquals(0, $e->getCode()); } } } diff --git a/tests/Repository/RepositoryDeleteTest.php b/tests/Repository/RepositoryDeleteTest.php index e418f0e..523357d 100644 --- a/tests/Repository/RepositoryDeleteTest.php +++ b/tests/Repository/RepositoryDeleteTest.php @@ -8,14 +8,13 @@ use Kununu\Elasticsearch\Exception\DeleteException; use Kununu\Elasticsearch\Exception\DocumentNotFoundException; use Kununu\Elasticsearch\Repository\Repository; -use PHPUnit\Framework\TestCase; final class RepositoryDeleteTest extends AbstractRepositoryTestCase { public function testDelete(): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('delete') ->with([ 'index' => self::INDEX['write'], @@ -23,7 +22,7 @@ public function testDelete(): void ]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $this->getRepository()->delete(self::ID); @@ -32,7 +31,7 @@ public function testDelete(): void public function testDeleteWithForcedRefresh(): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('delete') ->with([ 'index' => self::INDEX['write'], @@ -41,7 +40,7 @@ public function testDeleteWithForcedRefresh(): void ]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $this->getRepository(['force_refresh_on_write' => true])->delete(self::ID); @@ -50,7 +49,7 @@ public function testDeleteWithForcedRefresh(): void public function testDeleteFails(): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('delete') ->with([ 'index' => self::INDEX['write'], @@ -65,16 +64,16 @@ public function testDeleteFails(): void try { $this->getRepository()->delete(self::ID); } catch (DeleteException $e) { - $this->assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); - $this->assertEquals(0, $e->getCode()); - $this->assertEquals(self::ID, $e->getDocumentId()); + self::assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); + self::assertEquals(0, $e->getCode()); + self::assertEquals(self::ID, $e->getDocumentId()); } } public function testDeleteFailsBecauseDocumentNotFound(): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('delete') ->with([ 'index' => self::INDEX['write'], @@ -83,22 +82,22 @@ public function testDeleteFailsBecauseDocumentNotFound(): void ->willThrowException(new Missing404Exception(self::ERROR_MESSAGE)); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); try { $this->getRepository()->delete(self::ID); } catch (DocumentNotFoundException $e) { - $this->assertEquals(self::ERROR_PREFIX . 'No document found with id ' . self::ID, $e->getMessage()); - $this->assertEquals(0, $e->getCode()); - $this->assertEquals(self::ID, $e->getDocumentId()); + self::assertEquals(self::ERROR_PREFIX . 'No document found with id ' . self::ID, $e->getMessage()); + self::assertEquals(0, $e->getCode()); + self::assertEquals(self::ID, $e->getDocumentId()); } } public function testPostDeleteIsCalled(): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('delete') ->with([ 'index' => self::INDEX['write'], @@ -106,13 +105,13 @@ public function testPostDeleteIsCalled(): void ]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $manager = new class($this->clientMock, ['index_write' => self::INDEX['write']]) extends Repository { protected function postDelete(string $id): void { - TestCase::assertEquals(AbstractRepositoryTestCase::ID, $id); + AbstractRepositoryTestCase::assertEquals(AbstractRepositoryTestCase::ID, $id); } }; diff --git a/tests/Repository/RepositoryFindByIdTest.php b/tests/Repository/RepositoryFindByIdTest.php index 6bc3edc..11a91e2 100644 --- a/tests/Repository/RepositoryFindByIdTest.php +++ b/tests/Repository/RepositoryFindByIdTest.php @@ -6,19 +6,20 @@ use Elasticsearch\Common\Exceptions\Missing404Exception; use Exception; use Kununu\Elasticsearch\Exception\ReadOperationException; +use PHPUnit\Framework\Attributes\DataProvider; final class RepositoryFindByIdTest extends AbstractRepositoryTestCase { public static function findByIdResultDataProvider(): array { return [ - 'no result' => [ + 'no_result' => [ 'es_result' => [ 'found' => false, ], 'end_result' => null, ], - 'document found' => [ + 'document_found' => [ 'es_result' => [ '_index' => self::INDEX['read'], '_version' => 1, @@ -63,11 +64,11 @@ function(array $variables) { ); } - /** @dataProvider findByIdResultDataProvider */ + #[DataProvider('findByIdResultDataProvider')] public function testFindById(array $esResult, mixed $endResult): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('get') ->with([ 'index' => self::INDEX['read'], @@ -76,17 +77,17 @@ public function testFindById(array $esResult, mixed $endResult): void ->willReturn($esResult); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); - $this->assertEquals($endResult, $this->getRepository()->findById(self::ID)); + self::assertEquals($endResult, $this->getRepository()->findById(self::ID)); } - /** @dataProvider findByIdResultDataProvider */ + #[DataProvider('findByIdResultDataProvider')] public function testFindByIdTrackingTotalHits(array $esResult, mixed $endResult): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('get') ->with([ 'index' => self::INDEX['read'], @@ -96,17 +97,17 @@ public function testFindByIdTrackingTotalHits(array $esResult, mixed $endResult) ->willReturn($esResult); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); - $this->assertEquals($endResult, $this->getRepository(['track_total_hits' => true])->findById(self::ID)); + self::assertEquals($endResult, $this->getRepository(['track_total_hits' => true])->findById(self::ID)); } - /** @dataProvider findByIdResultDataProvider */ + #[DataProvider('findByIdResultDataProvider')] public function testFindByIdWithSourceField(array $esResult, mixed $endResult): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('get') ->with([ 'index' => self::INDEX['read'], @@ -116,17 +117,17 @@ public function testFindByIdWithSourceField(array $esResult, mixed $endResult): ->willReturn($esResult); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); - $this->assertEquals($endResult, $this->getRepository()->findById(self::ID, ['foo', 'foo2'])); + self::assertEquals($endResult, $this->getRepository()->findById(self::ID, ['foo', 'foo2'])); } - /** @dataProvider findByIdResultWithEntitiesDataProvider */ + #[DataProvider('findByIdResultWithEntitiesDataProvider')] public function testFindByIdWithEntityClass(array $esResult, mixed $endResult): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('get') ->with([ 'index' => self::INDEX['read'], @@ -135,27 +136,27 @@ public function testFindByIdWithEntityClass(array $esResult, mixed $endResult): ->willReturn($esResult); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $result = $this - ->getRepository(['entity_class' => $this->getEntityClass()]) + ->getRepository(['entity_class' => PersistableEntityStub::class]) ->findById(self::ID); - $this->assertEquals($endResult, $result); + self::assertEquals($endResult, $result); if ($endResult) { - $this->assertEquals( + self::assertEquals( ['_index' => self::INDEX['read'], '_version' => 1, 'found' => true], $result->_meta ); } } - /** @dataProvider findByIdResultWithEntitiesDataProvider */ + #[DataProvider('findByIdResultWithEntitiesDataProvider')] public function testFindByIdWithEntityFactory(array $esResult, mixed $endResult): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('get') ->with([ 'index' => self::INDEX['read'], @@ -164,16 +165,16 @@ public function testFindByIdWithEntityFactory(array $esResult, mixed $endResult) ->willReturn($esResult); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $result = $this - ->getRepository(['entity_factory' => $this->getEntityFactory()]) + ->getRepository(['entity_factory' => new EntityFactoryStub()]) ->findById(self::ID); - $this->assertEquals($endResult, $result); + self::assertEquals($endResult, $result); if ($endResult) { - $this->assertEquals( + self::assertEquals( ['_index' => self::INDEX['read'], '_version' => 1, 'found' => true], $result->_meta ); @@ -183,7 +184,7 @@ public function testFindByIdWithEntityFactory(array $esResult, mixed $endResult) public function testFindByIdFails(): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('get') ->with([ 'index' => self::INDEX['read'], @@ -198,16 +199,16 @@ public function testFindByIdFails(): void try { $this->getRepository()->findById(self::ID); } catch (ReadOperationException $e) { - $this->assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); - $this->assertEquals(0, $e->getCode()); - $this->assertNull($e->getQuery()); + self::assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); + self::assertEquals(0, $e->getCode()); + self::assertNull($e->getQuery()); } } public function testFindByIdFailsWith404(): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('get') ->with([ 'index' => self::INDEX['read'], @@ -215,6 +216,6 @@ public function testFindByIdFailsWith404(): void ]) ->willThrowException(new Missing404Exception()); - $this->assertNull($this->getRepository()->findById(self::ID)); + self::assertNull($this->getRepository()->findById(self::ID)); } } diff --git a/tests/Repository/RepositoryFindByIdsTest.php b/tests/Repository/RepositoryFindByIdsTest.php index df9381c..d6ff3a1 100644 --- a/tests/Repository/RepositoryFindByIdsTest.php +++ b/tests/Repository/RepositoryFindByIdsTest.php @@ -5,6 +5,7 @@ use Exception; use Kununu\Elasticsearch\Exception\ReadOperationException; +use PHPUnit\Framework\Attributes\DataProvider; final class RepositoryFindByIdsTest extends AbstractRepositoryTestCase { @@ -134,11 +135,11 @@ function(array $variables): array { ); } - /** @dataProvider findByIdsResultDataProvider */ + #[DataProvider('findByIdsResultDataProvider')] public function testFindByIds(array $esResult, mixed $endResult): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('mget') ->with([ 'index' => self::INDEX['read'], @@ -156,21 +157,21 @@ public function testFindByIds(array $esResult, mixed $endResult): void ->willReturn($esResult); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('critical'); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); - $this->assertEquals($endResult, $this->getRepository()->findByIds([self::ID, self::ID_2])); + self::assertEquals($endResult, $this->getRepository()->findByIds([self::ID, self::ID_2])); } - /** @dataProvider findByIdsResultDataProvider */ + #[DataProvider('findByIdsResultDataProvider')] public function testFindByIdsWithSourceField(array $esResult, mixed $endResult): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('mget') ->with([ 'index' => self::INDEX['read'], @@ -190,21 +191,21 @@ public function testFindByIdsWithSourceField(array $esResult, mixed $endResult): ->willReturn($esResult); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('critical'); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); - $this->assertEquals($endResult, $this->getRepository()->findByIds([self::ID, self::ID_2], ['foo', 'foo2'])); + self::assertEquals($endResult, $this->getRepository()->findByIds([self::ID, self::ID_2], ['foo', 'foo2'])); } - /** @dataProvider findByIdsResultWithEntitiesDataProvider */ + #[DataProvider('findByIdsResultWithEntitiesDataProvider')] public function testFindByIdWithEntityClass(array $esResult, mixed $endResult): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('mget') ->with([ 'index' => self::INDEX['read'], @@ -222,21 +223,21 @@ public function testFindByIdWithEntityClass(array $esResult, mixed $endResult): ->willReturn($esResult); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('critical'); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $results = $this - ->getRepository(['entity_class' => $this->getEntityClass()]) + ->getRepository(['entity_class' => PersistableEntityStub::class]) ->findByIds([self::ID, self::ID_2]); - $this->assertEquals(array_values($endResult), $results); + self::assertEquals(array_values($endResult), $results); if (!empty($endResult)) { foreach ($endResult as $id => $result) { - $this->assertEquals( + self::assertEquals( ['_index' => self::INDEX['read'], '_id' => $id, '_version' => 1, 'found' => true], $result->_meta ); @@ -244,11 +245,11 @@ public function testFindByIdWithEntityClass(array $esResult, mixed $endResult): } } - /** @dataProvider findByIdsResultWithEntitiesDataProvider */ + #[DataProvider('findByIdsResultWithEntitiesDataProvider')] public function testFindByIdsWithEntityFactory(array $esResult, mixed $endResult): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('mget') ->with([ 'index' => self::INDEX['read'], @@ -266,21 +267,21 @@ public function testFindByIdsWithEntityFactory(array $esResult, mixed $endResult ->willReturn($esResult); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('critical'); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $results = $this - ->getRepository(['entity_factory' => $this->getEntityFactory()]) + ->getRepository(['entity_factory' => new EntityFactoryStub()]) ->findByIds([self::ID, self::ID_2]); - $this->assertEquals(array_values($endResult), $results); + self::assertEquals(array_values($endResult), $results); if (!empty($endResult)) { foreach ($endResult as $id => $result) { - $this->assertEquals( + self::assertEquals( ['_index' => self::INDEX['read'], '_id' => $id, '_version' => 1, 'found' => true], $result->_meta ); @@ -290,7 +291,7 @@ public function testFindByIdsWithEntityFactory(array $esResult, mixed $endResult public function testFindByIdsWithoutIds(): void { - $this->assertEmpty($this->getRepository()->findByIds([])); + self::assertEmpty($this->getRepository()->findByIds([])); } public function testFindByIdsFails(): void @@ -310,13 +311,13 @@ public function testFindByIdsFails(): void ]; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('mget') ->with($body) ->willThrowException(new Exception(self::ERROR_MESSAGE)); $this->loggerMock - ->expects($this->once()) + ->expects(self::once()) ->method('critical') ->with( 'Elasticsearch request error', @@ -324,7 +325,7 @@ public function testFindByIdsFails(): void ); $this->loggerMock - ->expects($this->once()) + ->expects(self::once()) ->method('error') ->with( self::ERROR_PREFIX . self::ERROR_MESSAGE @@ -333,9 +334,9 @@ public function testFindByIdsFails(): void try { $this->getRepository()->findByIds([self::ID, self::ID_2]); } catch (ReadOperationException $e) { - $this->assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); - $this->assertEquals(0, $e->getCode()); - $this->assertNull($e->getQuery()); + self::assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); + self::assertEquals(0, $e->getCode()); + self::assertNull($e->getQuery()); } } } diff --git a/tests/Repository/RepositoryFindByQueryTest.php b/tests/Repository/RepositoryFindByQueryTest.php index f92bfe9..ad30fc8 100644 --- a/tests/Repository/RepositoryFindByQueryTest.php +++ b/tests/Repository/RepositoryFindByQueryTest.php @@ -8,10 +8,11 @@ use Kununu\Elasticsearch\Query\Query; use Kununu\Elasticsearch\Query\QueryInterface; use Kununu\Elasticsearch\Repository\RepositoryConfiguration; +use PHPUnit\Framework\Attributes\DataProvider; final class RepositoryFindByQueryTest extends AbstractRepositoryTestCase { - /** @dataProvider queryAndSearchResultVariationsDataProvider */ + #[DataProvider('queryAndSearchResultVariationsDataProvider')] public function testFindByQuery(QueryInterface $query, array $esResult, mixed $endResult, bool $scroll): void { $rawParams = [ @@ -24,7 +25,7 @@ public function testFindByQuery(QueryInterface $query, array $esResult, mixed $e } $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('search') ->with($rawParams) ->willReturn($esResult); @@ -33,16 +34,16 @@ public function testFindByQuery(QueryInterface $query, array $esResult, mixed $e ? $this->getRepository()->findScrollableByQuery($query) : $this->getRepository()->findByQuery($query); - $this->assertEquals($endResult, $result->asArray()); - $this->assertEquals(self::DOCUMENT_COUNT, $result->getTotal()); + self::assertEquals($endResult, $result->asArray()); + self::assertEquals(self::DOCUMENT_COUNT, $result->getTotal()); if ($scroll) { - $this->assertEquals(self::SCROLL_ID, $result->getScrollId()); + self::assertEquals(self::SCROLL_ID, $result->getScrollId()); } else { - $this->assertNull($result->getScrollId()); + self::assertNull($result->getScrollId()); } } - /** @dataProvider queryAndSearchResultVariationsWithEntitiesDataProvider */ + #[DataProvider('queryAndSearchResultVariationsWithEntitiesDataProvider')] public function testFindByQueryWithEntityFactory( QueryInterface $query, array $esResult, @@ -59,37 +60,37 @@ public function testFindByQueryWithEntityFactory( } $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('search') ->with($rawParams) ->willReturn($esResult); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); - $repository = $this->getRepository(['entity_factory' => $this->getEntityFactory()]); + $repository = $this->getRepository(['entity_factory' => new EntityFactoryStub()]); $result = $scroll ? $repository->findScrollableByQuery($query) : $repository->findByQuery($query); - $this->assertEquals($endResult, $result->asArray()); - $this->assertEquals(self::DOCUMENT_COUNT, $result->getTotal()); + self::assertEquals($endResult, $result->asArray()); + self::assertEquals(self::DOCUMENT_COUNT, $result->getTotal()); if ($scroll) { - $this->assertEquals(self::SCROLL_ID, $result->getScrollId()); + self::assertEquals(self::SCROLL_ID, $result->getScrollId()); } else { - $this->assertNull($result->getScrollId()); + self::assertNull($result->getScrollId()); } if ($result->getCount() > 0) { foreach ($result as $entity) { - $this->assertEquals(['_index' => self::INDEX['read'], '_score' => 77], $entity->_meta); + self::assertEquals(['_index' => self::INDEX['read'], '_score' => 77], $entity->_meta); } } } - /** @dataProvider queryAndSearchResultVariationsWithEntitiesDataProvider */ + #[DataProvider('queryAndSearchResultVariationsWithEntitiesDataProvider')] public function testFindByQueryWithEntityClass( QueryInterface $query, array $esResult, @@ -106,32 +107,32 @@ public function testFindByQueryWithEntityClass( } $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('search') ->with($rawParams) ->willReturn($esResult); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); - $repository = $this->getRepository(['entity_class' => get_class($this->getEntityClassInstance())]); + $repository = $this->getRepository(['entity_class' => PersistableEntityStub::class]); $result = $scroll ? $repository->findScrollableByQuery($query) : $repository->findByQuery($query); - $this->assertEquals($endResult, $result->asArray()); - $this->assertEquals(self::DOCUMENT_COUNT, $result->getTotal()); + self::assertEquals($endResult, $result->asArray()); + self::assertEquals(self::DOCUMENT_COUNT, $result->getTotal()); if ($scroll) { - $this->assertEquals(self::SCROLL_ID, $result->getScrollId()); + self::assertEquals(self::SCROLL_ID, $result->getScrollId()); } else { - $this->assertNull($result->getScrollId()); + self::assertNull($result->getScrollId()); } if ($result->getCount() > 0) { foreach ($result as $entity) { - $this->assertEquals(['_index' => self::INDEX['read'], '_score' => 77], $entity->_meta); + self::assertEquals(['_index' => self::INDEX['read'], '_score' => 77], $entity->_meta); } } } @@ -139,21 +140,21 @@ public function testFindByQueryWithEntityClass( public function testFindByQueryFails(): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('search') ->willThrowException(new Exception(self::ERROR_MESSAGE)); $this->loggerMock - ->expects($this->once()) + ->expects(self::once()) ->method('error') ->with(self::ERROR_PREFIX . self::ERROR_MESSAGE); try { $this->getRepository()->findByQuery(Query::create()); } catch (ReadOperationException $e) { - $this->assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); - $this->assertEquals(0, $e->getCode()); - $this->assertNull($e->getQuery()); + self::assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); + self::assertEquals(0, $e->getCode()); + self::assertNull($e->getQuery()); } } } diff --git a/tests/Repository/RepositoryFindByScrollIdTest.php b/tests/Repository/RepositoryFindByScrollIdTest.php index 812b68b..715190a 100644 --- a/tests/Repository/RepositoryFindByScrollIdTest.php +++ b/tests/Repository/RepositoryFindByScrollIdTest.php @@ -6,16 +6,17 @@ use Exception; use Kununu\Elasticsearch\Exception\ReadOperationException; use Kununu\Elasticsearch\Repository\RepositoryConfiguration; +use PHPUnit\Framework\Attributes\DataProvider; final class RepositoryFindByScrollIdTest extends AbstractRepositoryTestCase { - /** @dataProvider searchResultDataProvider */ + #[DataProvider('searchResultDataProvider')] public function testFindByScrollId(array $esResult, mixed $endResult): void { $scrollId = 'foobar'; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('scroll') ->with([ 'body' => [ @@ -26,22 +27,22 @@ public function testFindByScrollId(array $esResult, mixed $endResult): void ->willReturn($esResult); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $result = $this->getRepository()->findByScrollId($scrollId); - $this->assertEquals($endResult, $result->asArray()); + self::assertEquals($endResult, $result->asArray()); } - /** @dataProvider searchResultDataProvider */ + #[DataProvider('searchResultDataProvider')] public function testFindByScrollIdCanOverrideScrollContextKeepalive(array $esResult, array $endResult): void { $scrollId = 'foobar'; $keepalive = '20m'; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('scroll') ->with([ 'body' => [ @@ -52,21 +53,21 @@ public function testFindByScrollIdCanOverrideScrollContextKeepalive(array $esRes ->willReturn($esResult); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $result = $this->getRepository()->findByScrollId($scrollId, $keepalive); - $this->assertEquals($endResult, $result->asArray()); + self::assertEquals($endResult, $result->asArray()); } - /** @dataProvider searchResultWithEntitiesDataProvider */ + #[DataProvider('searchResultWithEntitiesDataProvider')] public function testFindByScrollIdWithEntityFactory(array $esResult, array $endResult): void { $scrollId = 'foobar'; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('scroll') ->with([ 'body' => [ @@ -77,29 +78,29 @@ public function testFindByScrollIdWithEntityFactory(array $esResult, array $endR ->willReturn(array_merge($esResult, ['_scroll_id' => $scrollId])); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); - $result = $this->getRepository(['entity_factory' => $this->getEntityFactory()])->findByScrollId($scrollId); + $result = $this->getRepository(['entity_factory' => new EntityFactoryStub()])->findByScrollId($scrollId); - $this->assertEquals($endResult, $result->asArray()); - $this->assertEquals(self::DOCUMENT_COUNT, $result->getTotal()); - $this->assertEquals($scrollId, $result->getScrollId()); + self::assertEquals($endResult, $result->asArray()); + self::assertEquals(self::DOCUMENT_COUNT, $result->getTotal()); + self::assertEquals($scrollId, $result->getScrollId()); if ($result->getCount() > 0) { foreach ($result as $entity) { - $this->assertEquals(['_index' => self::INDEX['read'], '_score' => 77], $entity->_meta); + self::assertEquals(['_index' => self::INDEX['read'], '_score' => 77], $entity->_meta); } } } - /** @dataProvider searchResultWithEntitiesDataProvider */ + #[DataProvider('searchResultWithEntitiesDataProvider')] public function testFindByScrollIdWithEntityClass(array $esResult, array $endResult): void { $scrollId = 'foobar'; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('scroll') ->with([ 'body' => [ @@ -110,20 +111,20 @@ public function testFindByScrollIdWithEntityClass(array $esResult, array $endRes ->willReturn(array_merge($esResult, ['_scroll_id' => $scrollId])); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $result = $this - ->getRepository(['entity_class' => $this->getEntityClass()]) + ->getRepository(['entity_class' => PersistableEntityStub::class]) ->findByScrollId($scrollId); - $this->assertEquals($endResult, $result->asArray()); - $this->assertEquals(self::DOCUMENT_COUNT, $result->getTotal()); - $this->assertEquals($scrollId, $result->getScrollId()); + self::assertEquals($endResult, $result->asArray()); + self::assertEquals(self::DOCUMENT_COUNT, $result->getTotal()); + self::assertEquals($scrollId, $result->getScrollId()); if ($result->getCount() > 0) { foreach ($result as $entity) { - $this->assertEquals(['_index' => self::INDEX['read'], '_score' => 77], $entity->_meta); + self::assertEquals(['_index' => self::INDEX['read'], '_score' => 77], $entity->_meta); } } } @@ -133,7 +134,7 @@ public function testFindByScrollIdFails(): void $scrollId = 'foobar'; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('scroll') ->with([ 'body' => [ @@ -144,16 +145,16 @@ public function testFindByScrollIdFails(): void ->willThrowException(new Exception(self::ERROR_MESSAGE)); $this->loggerMock - ->expects($this->once()) + ->expects(self::once()) ->method('error') ->with(self::ERROR_PREFIX . self::ERROR_MESSAGE); try { $this->getRepository()->findByScrollId($scrollId); } catch (ReadOperationException $e) { - $this->assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); - $this->assertEquals(0, $e->getCode()); - $this->assertNull($e->getQuery()); + self::assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); + self::assertEquals(0, $e->getCode()); + self::assertNull($e->getQuery()); } } } diff --git a/tests/Repository/RepositoryFindScrollableByQueryTest.php b/tests/Repository/RepositoryFindScrollableByQueryTest.php index 25a3111..958bf9b 100644 --- a/tests/Repository/RepositoryFindScrollableByQueryTest.php +++ b/tests/Repository/RepositoryFindScrollableByQueryTest.php @@ -4,10 +4,11 @@ namespace Kununu\Elasticsearch\Tests\Repository; use Kununu\Elasticsearch\Query\Query; +use PHPUnit\Framework\Attributes\DataProvider; final class RepositoryFindScrollableByQueryTest extends AbstractRepositoryTestCase { - /** @dataProvider searchResultDataProvider */ + #[DataProvider('searchResultDataProvider')] public function testFindScrollableByQueryCanOverrideScrollContextKeepalive(array $esResult, mixed $endResult): void { $query = Query::create(); @@ -20,13 +21,13 @@ public function testFindScrollableByQueryCanOverrideScrollContextKeepalive(array ]; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('search') ->with($rawParams) ->willReturn($esResult); $result = $this->getRepository()->findScrollableByQuery($query, $keepalive); - $this->assertEquals($endResult, $result->asArray()); + self::assertEquals($endResult, $result->asArray()); } } diff --git a/tests/Repository/RepositorySaveBulkTest.php b/tests/Repository/RepositorySaveBulkTest.php index 4103d0a..2e57ce1 100644 --- a/tests/Repository/RepositorySaveBulkTest.php +++ b/tests/Repository/RepositorySaveBulkTest.php @@ -7,7 +7,7 @@ use Kununu\Elasticsearch\Exception\BulkException; use Kununu\Elasticsearch\Exception\RepositoryConfigurationException; use Kununu\Elasticsearch\Repository\Repository; -use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; use stdClass; use TypeError; @@ -23,7 +23,7 @@ public function testSaveBulkWithArrays(): void ]; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('bulk') ->with([ 'index' => self::INDEX['write'], @@ -40,7 +40,7 @@ public function testSaveBulkWithArrays(): void ]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $this->getRepository()->saveBulk($documents); @@ -56,7 +56,7 @@ public function testSaveBulkWithForcedRefresh(): void ]; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('bulk') ->with([ 'index' => self::INDEX['write'], @@ -74,7 +74,7 @@ public function testSaveBulkWithForcedRefresh(): void ]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $this->getRepository(['force_refresh_on_write' => true])->saveBulk($documents); @@ -83,7 +83,7 @@ public function testSaveBulkWithForcedRefresh(): void public function testSaveBulkObjectsWithEntitySerializer(): void { $documents = []; - for ($ii = 0; $ii < 3; $ii++) { + for ($ii = 0; $ii < 3; ++$ii) { $document = new stdClass(); $document->property_a = 'a' . $ii; $document->property_b = 'b' . $ii; @@ -91,7 +91,7 @@ public function testSaveBulkObjectsWithEntitySerializer(): void } $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('bulk') ->with([ 'index' => self::INDEX['write'], @@ -106,7 +106,7 @@ public function testSaveBulkObjectsWithEntitySerializer(): void ]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $this->getRepository(['entity_serializer' => new EntitySerializerStub()])->saveBulk($documents); @@ -115,15 +115,15 @@ public function testSaveBulkObjectsWithEntitySerializer(): void public function testSaveBulkObjectsWithEntityClass(): void { $documents = []; - for ($ii = 0; $ii < 3; $ii++) { - $document = $this->getEntityClassInstance(); + for ($ii = 0; $ii < 3; ++$ii) { + $document = new PersistableEntityStub(); $document->property_a = 'a' . $ii; $document->property_b = 'b' . $ii; $documents['doc_' . $ii] = $document; } $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('bulk') ->with([ 'index' => self::INDEX['write'], @@ -138,11 +138,11 @@ public function testSaveBulkObjectsWithEntityClass(): void ]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $this - ->getRepository(['entity_class' => $this->getEntityClass()]) + ->getRepository(['entity_class' => PersistableEntityStub::class]) ->saveBulk($documents); } @@ -154,7 +154,7 @@ public function testSaveBulkObjectsFailsWithoutEntitySerializerAndEntityClass(): $this->getRepository()->saveBulk([self::ID => new stdClass()]); } - /** @dataProvider invalidDataTypesForSaveAndUpsertDataProvider */ + #[DataProvider('invalidDataTypesForSaveAndUpsertDataProvider')] public function testSaveBulkFailsWithInvalidDataType(mixed $entity): void { $this->expectException(TypeError::class); @@ -176,7 +176,7 @@ public function testSaveBulkArrayFails(): void ]; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('bulk') ->with( [ @@ -187,16 +187,16 @@ public function testSaveBulkArrayFails(): void ->willThrowException(new Exception(self::ERROR_MESSAGE)); $this->loggerMock - ->expects($this->once()) + ->expects(self::once()) ->method('error') ->with(self::ERROR_PREFIX . self::ERROR_MESSAGE); try { $this->getRepository()->saveBulk($documents); } catch (BulkException $e) { - $this->assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); - $this->assertEquals(0, $e->getCode()); - $this->assertEquals($expectedOperations, $e->getOperations()); + self::assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); + self::assertEquals(0, $e->getCode()); + self::assertEquals($expectedOperations, $e->getOperations()); } } @@ -209,7 +209,7 @@ public function testPostSaveBulkIsCalled(): void ]; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('bulk') ->with([ 'index' => self::INDEX['write'], @@ -220,13 +220,13 @@ public function testPostSaveBulkIsCalled(): void ]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $manager = new class($this->clientMock, ['index_write' => self::INDEX['write']]) extends Repository { protected function postSaveBulk(array $entities): void { - TestCase::assertEquals( + AbstractRepositoryTestCase::assertEquals( [ AbstractRepositoryTestCase::ID => [ 'whatever' => 'just some data', diff --git a/tests/Repository/RepositorySaveObjectTest.php b/tests/Repository/RepositorySaveObjectTest.php index 43cc1d4..f12557e 100644 --- a/tests/Repository/RepositorySaveObjectTest.php +++ b/tests/Repository/RepositorySaveObjectTest.php @@ -4,6 +4,7 @@ namespace Kununu\Elasticsearch\Tests\Repository; use Kununu\Elasticsearch\Exception\RepositoryConfigurationException; +use PHPUnit\Framework\Attributes\DataProvider; use stdClass; use TypeError; @@ -16,7 +17,7 @@ public function testSaveObjectWithEntitySerializer(): void $document->property_b = 'b'; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('index') ->with([ 'index' => self::INDEX['write'], @@ -28,7 +29,7 @@ public function testSaveObjectWithEntitySerializer(): void ]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $this->getRepository(['entity_serializer' => new EntitySerializerStub()])->save(self::ID, $document); @@ -36,12 +37,12 @@ public function testSaveObjectWithEntitySerializer(): void public function testSaveObjectWithEntityClass(): void { - $document = $this->getEntityClassInstance(); + $document = new PersistableEntityStub(); $document->property_a = 'a'; $document->property_b = 'b'; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('index') ->with([ 'index' => self::INDEX['write'], @@ -53,10 +54,10 @@ public function testSaveObjectWithEntityClass(): void ]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); - $this->getRepository(['entity_class' => $this->getEntityClass()])->save(self::ID, $document); + $this->getRepository(['entity_class' => PersistableEntityStub::class])->save(self::ID, $document); } public function testSaveObjectFailsWithoutEntitySerializerAndEntityClass(): void @@ -67,7 +68,7 @@ public function testSaveObjectFailsWithoutEntitySerializerAndEntityClass(): void $this->getRepository()->save(self::ID, new stdClass()); } - /** @dataProvider invalidDataTypesForSaveAndUpsertDataProvider */ + #[DataProvider('invalidDataTypesForSaveAndUpsertDataProvider')] public function testSaveFailsWithInvalidDataType(mixed $entity): void { $this->expectException(TypeError::class); diff --git a/tests/Repository/RepositorySaveTest.php b/tests/Repository/RepositorySaveTest.php index 0261a3b..45d00eb 100644 --- a/tests/Repository/RepositorySaveTest.php +++ b/tests/Repository/RepositorySaveTest.php @@ -6,7 +6,6 @@ use Exception; use Kununu\Elasticsearch\Exception\UpsertException; use Kununu\Elasticsearch\Repository\Repository; -use PHPUnit\Framework\TestCase; final class RepositorySaveTest extends AbstractRepositoryTestCase { @@ -17,7 +16,7 @@ public function testSaveArray(): void ]; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('index') ->with([ 'index' => self::INDEX['write'], @@ -26,7 +25,7 @@ public function testSaveArray(): void ]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $this->getRepository()->save(self::ID, $document); @@ -39,7 +38,7 @@ public function testSaveWithForcedRefresh(): void ]; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('index') ->with([ 'index' => self::INDEX['write'], @@ -49,7 +48,7 @@ public function testSaveWithForcedRefresh(): void ]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $this->getRepository(['force_refresh_on_write' => true])->save(self::ID, $document); @@ -62,7 +61,7 @@ public function testSaveArrayFails(): void ]; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('index') ->with([ 'index' => self::INDEX['write'], @@ -72,16 +71,17 @@ public function testSaveArrayFails(): void ->willThrowException(new Exception(self::ERROR_MESSAGE)); $this->loggerMock + ->expects(self::once()) ->method('error') ->with(self::ERROR_PREFIX . self::ERROR_MESSAGE); try { $this->getRepository()->save(self::ID, $document); } catch (UpsertException $e) { - $this->assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); - $this->assertEquals(0, $e->getCode()); - $this->assertEquals(self::ID, $e->getDocumentId()); - $this->assertEquals($document, $e->getDocument()); + self::assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); + self::assertEquals(0, $e->getCode()); + self::assertEquals(self::ID, $e->getDocumentId()); + self::assertEquals($document, $e->getDocument()); } } @@ -92,7 +92,7 @@ public function testPostSaveIsCalled(): void ]; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('index') ->with([ 'index' => self::INDEX['write'], @@ -101,14 +101,14 @@ public function testPostSaveIsCalled(): void ]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $manager = new class($this->clientMock, ['index_write' => self::INDEX['write']]) extends Repository { protected function postSave(string $id, array $document): void { - TestCase::assertEquals(AbstractRepositoryTestCase::ID, $id); - TestCase::assertEquals(['whatever' => 'just some data'], $document); + AbstractRepositoryTestCase::assertEquals(AbstractRepositoryTestCase::ID, $id); + AbstractRepositoryTestCase::assertEquals(['whatever' => 'just some data'], $document); } }; diff --git a/tests/Repository/RepositoryUpdateByQueryTest.php b/tests/Repository/RepositoryUpdateByQueryTest.php index 599914c..fb4b688 100644 --- a/tests/Repository/RepositoryUpdateByQueryTest.php +++ b/tests/Repository/RepositoryUpdateByQueryTest.php @@ -41,7 +41,7 @@ public function testUpdateByQuery(): void ]; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('updateByQuery') ->with([ 'index' => self::INDEX['write'], @@ -59,10 +59,10 @@ public function testUpdateByQuery(): void ->willReturn($responseBody); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); - $this->assertEquals($responseBody, $this->getRepository()->updateByQuery($query, $updateScript)); + self::assertEquals($responseBody, $this->getRepository()->updateByQuery($query, $updateScript)); } public function testUpdateByQueryWithForcedRefresh(): void @@ -96,7 +96,7 @@ public function testUpdateByQueryWithForcedRefresh(): void ]; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('updateByQuery') ->with([ 'index' => self::INDEX['write'], @@ -115,30 +115,31 @@ public function testUpdateByQueryWithForcedRefresh(): void ->willReturn($responseBody); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $repository = $this->getRepository(['force_refresh_on_write' => true]); - $this->assertEquals($responseBody, $repository->updateByQuery($query, $updateScript)); + self::assertEquals($responseBody, $repository->updateByQuery($query, $updateScript)); } public function testUpdateByQueryFails(): void { $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('updateByQuery') ->willThrowException(new Exception(self::ERROR_MESSAGE)); $this->loggerMock + ->expects(self::once()) ->method('error') ->with(self::ERROR_PREFIX . self::ERROR_MESSAGE); try { $this->getRepository()->updateByQuery(Query::create(), ['script' => []]); } catch (WriteOperationException $e) { - $this->assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); - $this->assertEquals(0, $e->getCode()); + self::assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); + self::assertEquals(0, $e->getCode()); } } } diff --git a/tests/Repository/RepositoryUpdateTest.php b/tests/Repository/RepositoryUpdateTest.php index a9f7e73..d95a365 100644 --- a/tests/Repository/RepositoryUpdateTest.php +++ b/tests/Repository/RepositoryUpdateTest.php @@ -6,6 +6,7 @@ use Exception; use Kununu\Elasticsearch\Exception\RepositoryConfigurationException; use Kununu\Elasticsearch\Exception\UpdateException; +use PHPUnit\Framework\Attributes\DataProvider; use stdClass; use TypeError; @@ -18,7 +19,7 @@ public function testUpdateArray(): void ]; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('update') ->with([ 'index' => self::INDEX['write'], @@ -29,7 +30,7 @@ public function testUpdateArray(): void ]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $this->getRepository()->update(self::ID, $document); @@ -42,7 +43,7 @@ public function testUpdateWithForcedRefresh(): void ]; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('update') ->with([ 'index' => self::INDEX['write'], @@ -54,7 +55,7 @@ public function testUpdateWithForcedRefresh(): void ]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $this->getRepository(['force_refresh_on_write' => true])->update(self::ID, $document); @@ -67,7 +68,7 @@ public function testUpdateObjectWithEntitySerializer(): void $document->property_b = 'b'; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('update') ->with([ 'index' => self::INDEX['write'], @@ -81,7 +82,7 @@ public function testUpdateObjectWithEntitySerializer(): void ]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $this->getRepository(['entity_serializer' => new EntitySerializerStub()])->update(self::ID, $document); @@ -89,12 +90,12 @@ public function testUpdateObjectWithEntitySerializer(): void public function testUpdateObjectWithEntityClass(): void { - $document = $this->getEntityClassInstance(); + $document = new PersistableEntityStub(); $document->property_a = 'a'; $document->property_b = 'b'; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('update') ->with([ 'index' => self::INDEX['write'], @@ -108,10 +109,10 @@ public function testUpdateObjectWithEntityClass(): void ]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); - $this->getRepository(['entity_class' => $this->getEntityClass()])->update(self::ID, $document); + $this->getRepository(['entity_class' => PersistableEntityStub::class])->update(self::ID, $document); } public function testUpdateObjectFailsWithoutEntitySerializerAndEntityClass(): void @@ -122,7 +123,7 @@ public function testUpdateObjectFailsWithoutEntitySerializerAndEntityClass(): vo $this->getRepository()->update(self::ID, new stdClass()); } - /** @dataProvider invalidDataTypesForSaveAndUpsertDataProvider */ + #[DataProvider('invalidDataTypesForSaveAndUpsertDataProvider')] public function testUpdateFailsWithInvalidDataType(mixed $entity): void { $this->expectException(TypeError::class); @@ -137,7 +138,7 @@ public function testUpdateArrayFails(): void ]; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('update') ->with([ 'index' => self::INDEX['write'], @@ -149,16 +150,17 @@ public function testUpdateArrayFails(): void ->willThrowException(new Exception(self::ERROR_MESSAGE)); $this->loggerMock + ->expects(self::once()) ->method('error') ->with(self::ERROR_PREFIX . self::ERROR_MESSAGE); try { $this->getRepository()->update(self::ID, $document); } catch (UpdateException $e) { - $this->assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); - $this->assertEquals(0, $e->getCode()); - $this->assertEquals(self::ID, $e->getDocumentId()); - $this->assertEquals($document, $e->getDocument()); + self::assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); + self::assertEquals(0, $e->getCode()); + self::assertEquals(self::ID, $e->getDocumentId()); + self::assertEquals($document, $e->getDocument()); } } } diff --git a/tests/Repository/RepositoryUpsertTest.php b/tests/Repository/RepositoryUpsertTest.php index 8e1fdcc..34b19be 100644 --- a/tests/Repository/RepositoryUpsertTest.php +++ b/tests/Repository/RepositoryUpsertTest.php @@ -6,6 +6,7 @@ use Exception; use Kununu\Elasticsearch\Exception\RepositoryConfigurationException; use Kununu\Elasticsearch\Exception\UpsertException; +use PHPUnit\Framework\Attributes\DataProvider; use stdClass; use TypeError; @@ -18,7 +19,7 @@ public function testUpsertArray(): void ]; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('update') ->with([ 'index' => self::INDEX['write'], @@ -30,7 +31,7 @@ public function testUpsertArray(): void ]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $this->getRepository()->upsert(self::ID, $document); @@ -43,7 +44,7 @@ public function testUpsertWithForcedRefresh(): void ]; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('update') ->with([ 'index' => self::INDEX['write'], @@ -56,7 +57,7 @@ public function testUpsertWithForcedRefresh(): void ]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $this->getRepository(['force_refresh_on_write' => true])->upsert(self::ID, $document); @@ -69,7 +70,7 @@ public function testUpsertObjectWithEntitySerializer(): void $document->property_b = 'b'; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('update') ->with([ 'index' => self::INDEX['write'], @@ -84,7 +85,7 @@ public function testUpsertObjectWithEntitySerializer(): void ]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); $this->getRepository(['entity_serializer' => new EntitySerializerStub()])->upsert(self::ID, $document); @@ -92,12 +93,12 @@ public function testUpsertObjectWithEntitySerializer(): void public function testUpsertObjectWithEntityClass(): void { - $document = $this->getEntityClassInstance(); + $document = new PersistableEntityStub(); $document->property_a = 'a'; $document->property_b = 'b'; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('update') ->with([ 'index' => self::INDEX['write'], @@ -112,10 +113,10 @@ public function testUpsertObjectWithEntityClass(): void ]); $this->loggerMock - ->expects($this->never()) + ->expects(self::never()) ->method('error'); - $this->getRepository(['entity_class' => $this->getEntityClass()])->upsert(self::ID, $document); + $this->getRepository(['entity_class' => PersistableEntityStub::class])->upsert(self::ID, $document); } public function testUpsertObjectFailsWithoutEntitySerializerAndEntityClass(): void @@ -126,7 +127,7 @@ public function testUpsertObjectFailsWithoutEntitySerializerAndEntityClass(): vo $this->getRepository()->upsert(self::ID, new stdClass()); } - /** @dataProvider invalidDataTypesForSaveAndUpsertDataProvider */ + #[DataProvider('invalidDataTypesForSaveAndUpsertDataProvider')] public function testUpsertFailsWithInvalidDataType(mixed $entity): void { $this->expectException(TypeError::class); @@ -141,7 +142,7 @@ public function testUpsertArrayFails(): void ]; $this->clientMock - ->expects($this->once()) + ->expects(self::once()) ->method('update') ->with([ 'index' => self::INDEX['write'], @@ -154,16 +155,17 @@ public function testUpsertArrayFails(): void ->willThrowException(new Exception(self::ERROR_MESSAGE)); $this->loggerMock + ->expects(self::once()) ->method('error') ->with(self::ERROR_PREFIX . self::ERROR_MESSAGE); try { $this->getRepository()->upsert(self::ID, $document); } catch (UpsertException $e) { - $this->assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); - $this->assertEquals(0, $e->getCode()); - $this->assertEquals(self::ID, $e->getDocumentId()); - $this->assertEquals($document, $e->getDocument()); + self::assertEquals(self::ERROR_PREFIX . self::ERROR_MESSAGE, $e->getMessage()); + self::assertEquals(0, $e->getCode()); + self::assertEquals(self::ID, $e->getDocumentId()); + self::assertEquals($document, $e->getDocument()); } } } diff --git a/tests/Result/AggregationResultSetTest.php b/tests/Result/AggregationResultSetTest.php index 6d7c732..351c260 100644 --- a/tests/Result/AggregationResultSetTest.php +++ b/tests/Result/AggregationResultSetTest.php @@ -6,6 +6,7 @@ use Kununu\Elasticsearch\Result\AggregationResult; use Kununu\Elasticsearch\Result\AggregationResultSet; use Kununu\Elasticsearch\Result\ResultIterator; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; final class AggregationResultSetTest extends TestCase @@ -13,12 +14,12 @@ final class AggregationResultSetTest extends TestCase public static function createDataProvider(): array { return [ - 'empty result' => [ + 'empty_result' => [ 'rawResult' => [], ], - 'non-empty result' => [ + 'non_empty_result' => [ 'rawResult' => [ - 'my_first_agg' => [ + 'my_first_agg' => [ 'value' => 0.1, ], 'my_second_agg' => [ @@ -30,15 +31,16 @@ public static function createDataProvider(): array ]; } - /** @dataProvider createDataProvider */ + #[DataProvider('createDataProvider')] public function testCreate(array $rawResult): void { $result = AggregationResultSet::create($rawResult); - $this->assertCount(count($rawResult), $result->getResults()); + + self::assertCount(count($rawResult), $result->getResults()); foreach ($result->getResults() as $singleResultName => $singleResult) { - $this->assertInstanceOf(AggregationResult::class, $singleResult); - $this->assertEquals($singleResultName, $singleResult->getName()); - $this->assertEquals($rawResult[$singleResultName], $singleResult->getFields()); + self::assertInstanceOf(AggregationResult::class, $singleResult); + self::assertEquals($singleResultName, $singleResult->getName()); + self::assertEquals($rawResult[$singleResultName], $singleResult->getFields()); } } @@ -46,24 +48,24 @@ public function testGetAndSetDocuments(): void { $result = AggregationResultSet::create(); - $this->assertNull($result->getDocuments()); + self::assertNull($result->getDocuments()); $documentIterator = ResultIterator::create(); $result->setDocuments($documentIterator); - $this->assertEquals($documentIterator, $result->getDocuments()); + self::assertEquals($documentIterator, $result->getDocuments()); } - /** @dataProvider createDataProvider */ + #[DataProvider('createDataProvider')] public function testGetResultByName(array $rawResult): void { $result = AggregationResultSet::create($rawResult); foreach ($result->getResults() as $singleResultName => $singleResult) { - $this->assertEquals($singleResult, $result->getResultByName($singleResultName)); + self::assertEquals($singleResult, $result->getResultByName($singleResultName)); } - $this->assertNull($result->getResultByName('this_aggregation_does_not_exist')); + self::assertNull($result->getResultByName('this_aggregation_does_not_exist')); } } diff --git a/tests/Result/AggregationResultTest.php b/tests/Result/AggregationResultTest.php index d4c2dfd..e25372a 100644 --- a/tests/Result/AggregationResultTest.php +++ b/tests/Result/AggregationResultTest.php @@ -4,6 +4,7 @@ namespace Kununu\Elasticsearch\Tests\Result; use Kununu\Elasticsearch\Result\AggregationResult; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; final class AggregationResultTest extends TestCase @@ -11,52 +12,56 @@ final class AggregationResultTest extends TestCase public static function createDataProvider(): array { return [ - 'empty name, empty fields' => [ + 'empty_name_empty_fields' => [ 'name' => '', 'fields' => [], ], - 'non-empty name, empty fields' => [ + 'non_empty_name_empty_fields' => [ 'name' => 'my_agg', 'fields' => [], ], - 'empty name, non-empty fields' => [ + 'empty_name_non_empty_fields' => [ 'name' => '', 'fields' => ['some' => 'thing', 'foo' => 'bar'], ], - 'non-empty name, non-empty fields' => [ + 'non_empty_name_non_empty_fields' => [ 'name' => 'my_agg', 'fields' => ['some' => 'thing', 'foo' => 'bar'], ], ]; } - /** @dataProvider createDataProvider */ + #[DataProvider('createDataProvider')] public function testCreate(string $name, array $fields): void { $result = AggregationResult::create($name, $fields); - $this->assertEquals([$name => $fields], $result->toArray()); + + self::assertEquals([$name => $fields], $result->toArray()); } - /** @dataProvider createDataProvider */ + #[DataProvider('createDataProvider')] public function testGetName(string $name, array $fields): void { $result = AggregationResult::create($name, $fields); - $this->assertEquals($name, $result->getName()); + + self::assertEquals($name, $result->getName()); } - /** @dataProvider createDataProvider */ + #[DataProvider('createDataProvider')] public function testGetFields(string $name, array $fields): void { $result = AggregationResult::create($name, $fields); - $this->assertEquals($fields, $result->getFields()); + + self::assertEquals($fields, $result->getFields()); } public function testGetField(): void { $result = AggregationResult::create('my_agg', ['some' => 'thing', 'foo' => 'bar']); - $this->assertEquals('thing', $result->get('some')); - $this->assertEquals('bar', $result->get('foo')); - $this->assertNull($result->get('this_field_does_not_exist')); + + self::assertEquals('thing', $result->get('some')); + self::assertEquals('bar', $result->get('foo')); + self::assertNull($result->get('this_field_does_not_exist')); } public function testGetBuckets(): void @@ -65,13 +70,15 @@ public function testGetBuckets(): void 'my_agg', ['buckets' => ['a' => ['first_bucket'], 'b' => ['second_bucket']]] ); - $this->assertEquals(['a' => ['first_bucket'], 'b' => ['second_bucket']], $result->getBuckets()); + + self::assertEquals(['a' => ['first_bucket'], 'b' => ['second_bucket']], $result->getBuckets()); $result = AggregationResult::create( 'my_agg', [] ); - $this->assertNull($result->getBuckets()); + + self::assertNull($result->getBuckets()); } public function getValue(): void @@ -80,12 +87,14 @@ public function getValue(): void 'my_agg', ['value' => 0.1] ); - $this->assertEquals(0.1, $result->getValue()); + + self::assertEquals(0.1, $result->getValue()); $result = AggregationResult::create( 'my_agg', [] ); - $this->assertNull($result->getValue()); + + self::assertNull($result->getValue()); } } diff --git a/tests/Result/ResultIteratorTest.php b/tests/Result/ResultIteratorTest.php index 923aa55..febc650 100644 --- a/tests/Result/ResultIteratorTest.php +++ b/tests/Result/ResultIteratorTest.php @@ -4,6 +4,7 @@ namespace Kununu\Elasticsearch\Tests\Result; use Kununu\Elasticsearch\Result\ResultIterator; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use stdClass; @@ -12,20 +13,21 @@ final class ResultIteratorTest extends TestCase public static function createDataProvider(): array { return [ - 'empty array' => [ + 'empty_array' => [ 'input' => [], ], - 'non-empty array' => [ + 'non_empty_array' => [ 'input' => ['some' => 'thing', 'foo' => 'bar'], ], ]; } - /** @dataProvider createDataProvider */ + #[DataProvider('createDataProvider')] public function testCreate(array $input): void { $iterator = ResultIterator::create($input); - $this->assertEquals($input, $iterator->asArray()); + + self::assertEquals($input, $iterator->asArray()); } public function testIterate(): void @@ -38,9 +40,9 @@ public function testIterate(): void ['four' => 4], ]); - $this->assertEquals(['zero' => 0], $iterator->current()); - $this->assertEquals(0, $iterator->key()); - $this->assertTrue($iterator->valid()); + self::assertEquals(['zero' => 0], $iterator->current()); + self::assertEquals(0, $iterator->key()); + self::assertTrue($iterator->valid()); } public function testArrayAccess(): void @@ -55,89 +57,103 @@ public function testArrayAccess(): void $initialCount = count($input); $iterator = new ResultIterator($input); - $this->assertCount($initialCount, $iterator); - $this->assertEquals($initialCount, $iterator->getCount()); - $this->assertEquals($initialCount, $iterator->count()); + self::assertCount($initialCount, $iterator); + self::assertEquals($initialCount, $iterator->getCount()); + self::assertEquals($initialCount, $iterator->count()); - for ($ii = 0; $ii < count($input); $ii++) { - $this->assertTrue($iterator->offsetExists($ii)); - $this->assertEquals($input[$ii], $iterator->offsetGet($ii)); - $this->assertEquals($input[$ii], $iterator[$ii]); + for ($ii = 0; $ii < count($input); ++$ii) { + self::assertTrue($iterator->offsetExists($ii)); + self::assertEquals($input[$ii], $iterator->offsetGet($ii)); + self::assertEquals($input[$ii], $iterator[$ii]); } foreach ($iterator as $ii => $element) { - $this->assertEquals($input[$ii], $element); + self::assertEquals($input[$ii], $element); } $iterator->offsetUnset(0); - $this->assertCount($initialCount - 1, $iterator); - $this->assertNull($iterator[0]); + self::assertCount($initialCount - 1, $iterator); + self::assertNull($iterator[0]); unset($iterator[1]); - $this->assertCount($initialCount - 2, $iterator); - $this->assertNull($iterator[1]); + self::assertCount($initialCount - 2, $iterator); + self::assertNull($iterator[1]); $iterator->offsetSet(2, ['two' => 2.2]); - $this->assertEquals(['two' => 2.2], $iterator[2]); + self::assertEquals(['two' => 2.2], $iterator[2]); $iterator[2] = ['two' => 2.3]; - $this->assertEquals(['two' => 2.3], $iterator[2]); + self::assertEquals(['two' => 2.3], $iterator[2]); $iterator[6] = ['five' => 5]; - $this->assertEquals(['five' => 5], $iterator[6]); + self::assertEquals(['five' => 5], $iterator[6]); $iterator[] = ['six' => 6]; - $this->assertEquals(['six' => 6], $iterator[7]); + self::assertEquals(['six' => 6], $iterator[7]); } public function testTotal(): void { $iterator = ResultIterator::create(); - $this->assertEquals(0, $iterator->getTotal()); + + self::assertEquals(0, $iterator->getTotal()); + $iterator->setTotal(100); - $this->assertEquals(100, $iterator->getTotal()); + + self::assertEquals(100, $iterator->getTotal()); $iterator = ResultIterator::create(['some', 'thing']); - $this->assertEquals(0, $iterator->getTotal()); + + self::assertEquals(0, $iterator->getTotal()); + $iterator->setTotal(200); - $this->assertEquals(200, $iterator->getTotal()); + + self::assertEquals(200, $iterator->getTotal()); } public function testScrollId(): void { $iterator = ResultIterator::create(); - $this->assertNull($iterator->getScrollId()); + + self::assertNull($iterator->getScrollId()); + $iterator->setScrollId('my_scroll_id'); - $this->assertEquals('my_scroll_id', $iterator->getScrollId()); + + self::assertEquals('my_scroll_id', $iterator->getScrollId()); $iterator = ResultIterator::create(['some', 'thing']); - $this->assertEquals(0, $iterator->getTotal()); + + self::assertEquals(0, $iterator->getTotal()); + $iterator->setTotal(200); - $this->assertEquals(200, $iterator->getTotal()); + + self::assertEquals(200, $iterator->getTotal()); } public function testPushArray(): void { $iterator = ResultIterator::create(); - $this->assertEmpty($iterator->asArray()); - $this->assertEquals(0, $iterator->getCount()); + + self::assertEmpty($iterator->asArray()); + self::assertEquals(0, $iterator->getCount()); $iterator->push(['some' => 'thing']); - $this->assertEquals([['some' => 'thing']], $iterator->asArray()); - $this->assertEquals(1, $iterator->getCount()); + self::assertEquals([['some' => 'thing']], $iterator->asArray()); + self::assertEquals(1, $iterator->getCount()); } public function testPushObject(): void { $iterator = ResultIterator::create(); - $this->assertEmpty($iterator->asArray()); - $this->assertEquals(0, $iterator->getCount()); + + self::assertEmpty($iterator->asArray()); + self::assertEquals(0, $iterator->getCount()); $iterator->push(new stdClass()); - $this->assertEquals([new stdClass()], $iterator->asArray()); - $this->assertEquals(1, $iterator->getCount()); + self::assertEquals([new stdClass()], $iterator->asArray()); + self::assertEquals(1, $iterator->getCount()); } public function testFirstMatch(): void @@ -151,7 +167,7 @@ public function testFirstMatch(): void fn($element): bool => $element['foo'] === 'bar' ); - $this->assertEquals(['foo' => 'bar', 'num' => 0], $firstFooBar); + self::assertEquals(['foo' => 'bar', 'num' => 0], $firstFooBar); } public function testFirstNoMatch(): void @@ -165,7 +181,7 @@ public function testFirstNoMatch(): void fn($element): bool => isset($element['bar']) && $element['bar'] === 'foo' ); - $this->assertNull($firstBarFoo); + self::assertNull($firstBarFoo); } public function testFilter(): void @@ -180,7 +196,7 @@ public function testFilter(): void fn($element) => isset($element['foo']) && $element['foo'] === 'bar' ); - $this->assertEquals([['foo' => 'bar'], ['foo' => 'bar']], $allFooBars); + self::assertEquals([['foo' => 'bar'], ['foo' => 'bar']], $allFooBars); } public function testSome(): void @@ -194,13 +210,13 @@ public function testSome(): void fn($element) => isset($element['foo']) && $element['foo'] === 'bar' ); - $this->assertTrue($thereAreFooBars); + self::assertTrue($thereAreFooBars); $thereAreBarFoos = $iterator->some( fn($element): bool => isset($element['bar']) && $element['bar'] === 'foo' ); - $this->assertFalse($thereAreBarFoos); + self::assertFalse($thereAreBarFoos); } public function testEveryTrue(): void @@ -214,7 +230,7 @@ public function testEveryTrue(): void fn($element): bool => isset($element['foo']) && $element['foo'] === 'bar' ); - $this->assertTrue($thereAreOnlyFooBars); + self::assertTrue($thereAreOnlyFooBars); } public function testEveryFalse(): void @@ -228,37 +244,38 @@ public function testEveryFalse(): void fn($element): bool => isset($element['bar']) && $element['bar'] === 'foo' ); - $this->assertFalse($thereAreOnlyBarFoos); + self::assertFalse($thereAreOnlyBarFoos); } public function testEach(): void { - $mockBuilder = $this->getMockBuilder(stdClass::class)->addMethods(['someMethod']); - $mocks = [ - $mockBuilder->getMock(), - $mockBuilder->getMock(), - $mockBuilder->getMock(), - ]; - - foreach ($mocks as $mock) { - $mock - ->expects($this->once()) - ->method('someMethod'); - } - $calls = 0; + $stubs = []; + for ($i = 0; $i < 3; ++$i) { + $stubs[$i] = new class() { + public int $call = 0; + + public function someMethod(int $call): void + { + $this->call = $call; + } + }; + } - $iterator = ResultIterator::create($mocks); + $iterator = ResultIterator::create($stubs); $iterator->each( function($element) use (&$calls): void { - $calls++; + ++$calls; - $element->someMethod(); + $element->someMethod($calls); } ); - $this->assertEquals(count($mocks), $calls); + self::assertEquals(count($stubs), $calls); + foreach ($stubs as $i => $stub) { + self::assertEquals($i + 1, $stub->call); + } } public function testMap(): void @@ -273,7 +290,7 @@ public function testMap(): void fn($element): array => array_flip($element) ); - $this->assertEquals([['bar' => 'foo'], ['bar' => 'foo'], ['foo' => 'bar']], $flipped); + self::assertEquals([['bar' => 'foo'], ['bar' => 'foo'], ['foo' => 'bar']], $flipped); } public function testReduce(): void @@ -289,6 +306,6 @@ public function testReduce(): void 0 ); - $this->assertEquals(2, $numberOfFooBars); + self::assertEquals(2, $numberOfFooBars); } } diff --git a/tests/Util/ConstantContainerTraitTest.php b/tests/Util/ConstantContainerTraitTest.php index 5d4da75..c8683a0 100644 --- a/tests/Util/ConstantContainerTraitTest.php +++ b/tests/Util/ConstantContainerTraitTest.php @@ -10,12 +10,12 @@ final class ConstantContainerTraitTest extends TestCase { public function testAll(): void { - $this->assertEquals(['first', 'second', 'third'], $this->getConstantContainer()->all()); + self::assertEquals(['first', 'second', 'third'], $this->getConstantContainer()->all()); } public function testAllPreserveKeys(): void { - $this->assertEquals( + self::assertEquals( ['FIRST' => 'first', 'SECOND' => 'second', 'THIRD' => 'third'], $this->getConstantContainer()->all(true) ); @@ -24,16 +24,18 @@ public function testAllPreserveKeys(): void public function testHasConstantTrue(): void { $container = $this->getConstantContainer(); + foreach ($container->all() as $constant) { - $this->assertTrue($container->hasConstant($constant)); + self::assertTrue($container->hasConstant($constant)); } } public function testHasConstantFalse(): void { $container = $this->getConstantContainer(); - $this->assertFalse($container->hasConstant('')); - $this->assertFalse($container->hasConstant('foo')); + + self::assertFalse($container->hasConstant('')); + self::assertFalse($container->hasConstant('foo')); } private function getConstantContainer(): object diff --git a/tests/Util/LoggerAwareTraitTest.php b/tests/Util/LoggerAwareTraitTest.php index 00d581e..8e6e05f 100644 --- a/tests/Util/LoggerAwareTraitTest.php +++ b/tests/Util/LoggerAwareTraitTest.php @@ -19,24 +19,26 @@ public function testSetLogger(): void $loggerAwareObject->setLogger($logger); - $this->assertEquals($logger, $loggerAwareObject->publiclyGetLogger()); + self::assertEquals($logger, $loggerAwareObject->getLogger()); } public function testGetNullLoggerAsDefault(): void { $loggerAwareObject = $this->getLoggerAwareObject(); - $this->assertInstanceOf(NullLogger::class, $loggerAwareObject->publiclyGetLogger()); + self::assertInstanceOf(NullLogger::class, $loggerAwareObject->getLogger()); } public function getLoggerAwareObject(): LoggerAwareInterface { return new class() implements LoggerAwareInterface { - use LoggerAwareTrait; + use LoggerAwareTrait { + getLogger as traitGetLogger; + } - public function publiclyGetLogger() + public function getLogger(): LoggerInterface { - return $this->getLogger(); + return $this->traitGetLogger(); } }; } diff --git a/tests/Util/OptionableTraitTest.php b/tests/Util/OptionableTraitTest.php index dca1ad0..3a047c6 100644 --- a/tests/Util/OptionableTraitTest.php +++ b/tests/Util/OptionableTraitTest.php @@ -11,15 +11,16 @@ final class OptionableTraitTest extends TestCase { public const OPTION_A = 'option_a'; public const OPTION_B = 'option_b'; + protected const NOT_AN_OPTION = 'foobar'; public function testGetNotSetOption(): void { $optionable = $this->getOptionableObject(); - $this->assertNull($optionable->getOption(self::OPTION_A)); - $this->assertNull($optionable->getOption(self::OPTION_B)); - $this->assertEmpty($optionable->getOptions()); + self::assertNull($optionable->getOption(self::OPTION_A)); + self::assertNull($optionable->getOption(self::OPTION_B)); + self::assertEmpty($optionable->getOptions()); } public function testSetAndGet(): void @@ -30,9 +31,9 @@ public function testSetAndGet(): void $optionable->setOption(self::OPTION_A, $myOption); - $this->assertEquals($myOption, $optionable->getOption(self::OPTION_A)); - $this->assertNull($optionable->getOption(self::OPTION_B)); - $this->assertEquals([self::OPTION_A => $myOption], $optionable->getOptions()); + self::assertEquals($myOption, $optionable->getOption(self::OPTION_A)); + self::assertNull($optionable->getOption(self::OPTION_B)); + self::assertEquals([self::OPTION_A => $myOption], $optionable->getOptions()); } public function testSetNull(): void @@ -41,9 +42,9 @@ public function testSetNull(): void $optionable->setOption(self::OPTION_A, null); - $this->assertNull($optionable->getOption(self::OPTION_A)); - $this->assertNull($optionable->getOption(self::OPTION_B)); - $this->assertEmpty($optionable->getOptions()); + self::assertNull($optionable->getOption(self::OPTION_A)); + self::assertNull($optionable->getOption(self::OPTION_B)); + self::assertEmpty($optionable->getOptions()); } public function testGetUnknownOption(): void