Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add and configure PHPStan static code checks #66

Merged
merged 4 commits into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: PHPUnit tests
name: Checks

on:
push:
Expand Down Expand Up @@ -37,5 +37,8 @@ jobs:
with:
elasticsearch version: 7.17.1

- name: Run PHPStan checks
run: ./vendor/bin/phpstan

- name: Run test suite
run: ./vendor/bin/phpunit
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# elasticsearcher

![Unit tests](https://github.com/madewithlove/elasticsearcher/actions/workflows/run-tests.yml/badge.svg)
![checks](https://github.com/madewithlove/elasticsearcher/actions/workflows/run-checks.yml/badge.svg)

This agnostic package is a lightweight wrapper on top of the [Elasticsearch PHP client](http://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html).
Its main goal is to allow for easier structuring of queries and indices in your application. It does not want to hide or replace
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"jdrieghe/array-helpers": "^0.2.0"
},
"require-dev": {
"phpstan/phpstan": "^1.5",
"phpunit/phpunit": "^9.5"
},
"extra": {
Expand Down
2 changes: 2 additions & 0 deletions phpstan-ignore.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
ignoreErrors: []
10 changes: 10 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
includes:
- phpstan-ignore.neon

parameters:
level: 1
paths:
- src
- tests
bootstrapFiles:
- tests/bootstrap.php
5 changes: 5 additions & 0 deletions src/Abstracts/AbstractQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ abstract class AbstractQuery
*/
protected $fragmentParser;

/**
* @var array
*/
protected $types;

/**
* Prepare the query. Add filters, sorting, ....
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Managers/IndicesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function registerIndices(array $indices)
}

/**
* @return AbstractIndex
* @return void
*
* @param string $indexName
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/ElasticSearcherTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ElasticSearcherTestCase extends TestCase
*/
protected function setUp(): void
{
$env = new Environment(
$env = new Environment(
['hosts' => [ELASTICSEARCH_HOST]]
);
$this->elasticSearcher = new ElasticSearcher($env);
Expand Down
100 changes: 47 additions & 53 deletions tests/Fragments/Traits/SortableTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,69 +7,68 @@ class SortableTraitTest extends ElasticSearcherTestCase
public function testSettingSortingFields()
{
$this->hasSort(
[
'name',
['age' => ['order' => 'asc', 'mode' => 'avg']],
['date' => 'asc'],
],
[
'sort_fields' => [
'name',
'age' => ['order' => 'asc', 'mode' => 'avg'],
'date' => 'asc',
]
],
null,
[
'name',
['age' => ['order' => 'asc', 'mode' => 'avg']],
['date' => 'asc'],
]
);
}

public function testSortingAFieldWithoutPredefinedFields()
{
$this->hasSort(
[
['date' => 'desc'],
],
null,
['sort' => 'date', 'sort_direction' => 'desc'],
[
['date' => 'desc'],
]
['sort' => 'date', 'sort_direction' => 'desc']
);
}

public function testSortingAFieldFromPredefinedFields()
{
$this->hasSort(
[
['date' => 'desc'],
['name' => 'asc'],
['age' => ['order' => 'asc', 'mode' => 'avg']],
],
[
'sort_fields' => [
'name' => 'asc',
'age' => ['order' => 'asc', 'mode' => 'avg'],
'date' => 'asc',
]
],
['sort' => 'date', 'sort_direction' => 'desc'],
[
['date' => 'desc'],
['name' => 'asc'],
['age' => ['order' => 'asc', 'mode' => 'avg']],
]
['sort' => 'date', 'sort_direction' => 'desc']
);
}

public function testSortingAnUndefinedFieldWithPredefinedFields()
{
$this->hasSort(
[
['email' => 'asc'],
['name' => 'asc'],
['age' => ['order' => 'asc', 'mode' => 'avg']],
['date' => 'asc'],
],
[
'sort_fields' => [
'name' => 'asc',
'age' => ['order' => 'asc', 'mode' => 'avg'],
'date' => 'asc',
]
],
['sort' => 'email', 'sort_direction' => 'asc'],
[
['email' => 'asc'],
['name' => 'asc'],
['age' => ['order' => 'asc', 'mode' => 'avg']],
['date' => 'asc'],
]
['sort' => 'email', 'sort_direction' => 'asc']
);
}

Expand All @@ -84,22 +83,22 @@ public function testSortingAFieldWithoutSortDirection()
];

$this->hasSort(
[
['date' => 'desc'],
'name',
['age' => ['order' => 'asc', 'mode' => 'avg']],
],
$fields,
['sort' => 'date', 'sort_direction' => null],
[
['date' => 'desc'],
'name',
['age' => ['order' => 'asc', 'mode' => 'avg']],
]
['sort' => 'date', 'sort_direction' => null]
);
$this->hasSort(
[
['age' => ['order' => 'asc', 'mode' => 'avg']],
'name',
['date' => 'desc'],
],
$fields,
['sort' => 'age', 'sort_direction' => null],
[
['age' => ['order' => 'asc', 'mode' => 'avg']],
'name',
['date' => 'desc'],
]
['sort' => 'age', 'sort_direction' => null]
);
}

Expand All @@ -114,37 +113,32 @@ public function testSortingAFieldWithoutPredefinedDirection()
];

$this->hasSort(
[
'name',
['age' => ['order' => 'asc', 'mode' => 'avg']],
['date' => 'desc'],
],
[
'sort_fields' => [
'name',
'age' => ['order' => 'asc', 'mode' => 'avg'],
'date' => 'desc',
]
],
['sort' => 'name'],
[
'name',
['age' => ['order' => 'asc', 'mode' => 'avg']],
['date' => 'desc'],
]
['sort' => 'name']
);
$this->hasSort(
[
['name' => 'desc'],
['age' => ['order' => 'asc', 'mode' => 'avg']],
['date' => 'desc'],
],
$fields,
['sort' => 'name', 'sort_direction' => 'desc'],
[
['name' => 'desc'],
['age' => ['order' => 'asc', 'mode' => 'avg']],
['date' => 'desc'],
]
['sort' => 'name', 'sort_direction' => 'desc']
);
}

/**
* @param null|array $predefinedFields
* @param null|array $sortBy
* @param array $expectedSort
*/
private function hasSort($predefinedFields = null, $sortBy = null, array $expectedSort)
private function hasSort(array $expectedSort, ?array $predefinedFields = null, ?array $sortBy = null)
{
$query = new SortedQuery($this->getElasticSearcher());
if ($predefinedFields) {
Expand Down
2 changes: 1 addition & 1 deletion tests/dummy/Queries/BooksFrom2014Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class BooksFrom2014Query extends AbstractQuery
{
public function setup()
{
$this->searchIn('books', 'books');
$this->searchIn('books');

$this->set('query.bool.filter', [new TermQuery('year', 2014)]);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/dummy/Queries/CountMoviesFrom2014Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class CountMoviesFrom2014Query extends AbstractQuery
{
public function setup()
{
$this->searchIn('movies', 'movies');
$this->searchIn('movies');

$this->set('query.bool.filter', [new TermQuery('year', 2014)]);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/dummy/Queries/MovieWithIDXQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class MovieWithIDXQuery extends AbstractQuery
{
public function setup()
{
$this->searchIn('movies', 'movies');
$this->searchIn('movies');

$this->set('query.bool.filter', [new IDFilter($this->getData('id'))]);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/dummy/Queries/MoviesFrom2014Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class MoviesFrom2014Query extends AbstractQuery
{
public function setup()
{
$this->searchIn('movies', 'movies');
$this->searchIn('movies');

$this->set('query.bool.filter', [new TermQuery('year', 2014)]);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/dummy/Queries/MoviesFromXYearQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class MoviesFromXYearQuery extends AbstractQuery
{
public function setup()
{
$this->searchIn('movies', 'movies');
$this->searchIn('movies');

$body = array(
'query' => array(
Expand Down
2 changes: 1 addition & 1 deletion tests/dummy/Queries/SortedQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function setup()
$this->sort($fields);
}

if ($fieldName = $this->getData('sort')) {
if ($this->getData('sort')) {
$this->sortBy($this->getData('sort'), $this->getData('sort_direction'));
}
}
Expand Down