Skip to content
This repository has been archived by the owner on May 13, 2021. It is now read-only.

Commit

Permalink
Add linter (#39)
Browse files Browse the repository at this point in the history
* Add cs_fixer

* Fix linter errors

* Add linter check to CI
  • Loading branch information
curquiza committed Aug 21, 2020
1 parent 2c1067f commit 0ec2fff
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 38 deletions.
12 changes: 9 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: Tests

on: [pull_request]
on:
push:
branches:
- master
pull_request:

jobs:
tests:
Expand All @@ -11,5 +15,7 @@ jobs:
run: composer validate
- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest
- name: Run test suite
run: vendor/bin/phpunit --color tests/
- name: Run tests
run: composer test
- name: Run linter
run: composer lint
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
composer.lock
phpunit.xml
.phpunit.result.cache
.php_cs.cache
.DS_Store
16 changes: 16 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

$finder = \PhpCsFixer\Finder::create()
->in(__DIR__.DIRECTORY_SEPARATOR.'src')
->in(__DIR__.DIRECTORY_SEPARATOR.'tests')
->append(['.php_cs.dist']);

$rules = [
'@Symfony' => true,
];

return \PhpCsFixer\Config::create()
->setRules($rules)
->setFinder($finder);
9 changes: 7 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ $ composer install

### Tests and Linter

Each PR should pass the tests to be accepted.
Each PR should pass the tests and the linter to be accepted.

```bash
$ vendor/bin/phpunit --color tests/
# Tests
$ composer test
# Linter (with auto-fix)
$ composer lint:fix
# Linter (without auto-fix)
$ composer lint
```

### Release Process
Expand Down
14 changes: 13 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@
"email": "hello@jordan-massart.be"
}
],
"scripts": {
"lint": [
"./vendor/friendsofphp/php-cs-fixer/php-cs-fixer fix --verbose --config=./.php_cs.dist --show-progress=estimating --dry-run --using-cache=yes"
],
"lint:fix": [
"./vendor/friendsofphp/php-cs-fixer/php-cs-fixer fix --verbose --config=./.php_cs.dist --diff --show-progress=estimating --using-cache=no"
],
"test": [
"./vendor/bin/phpunit --color tests/"
]
},
"require": {
"php": "^7.2.5",
"laravel/scout": "^8.0",
Expand All @@ -18,7 +29,8 @@
"require-dev": {
"orchestra/testbench": "^5.0",
"mockery/mockery": "^1.0",
"phpunit/phpunit": "^9.1"
"phpunit/phpunit": "^9.1",
"friendsofphp/php-cs-fixer": "^2.16"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 2 additions & 2 deletions src/Console/IndexMeilisearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Meilisearch\Scout\Console;

use MeiliSearch\Client;
use Illuminate\Console\Command;
use MeiliSearch\Client;
use MeiliSearch\Exceptions\HTTPRequestException;

class IndexMeilisearch extends Command
Expand All @@ -25,7 +25,7 @@ class IndexMeilisearch extends Command
/**
* Execute the console command.
*
* @param \Illuminate\Contracts\Events\Dispatcher $events
* @param \Illuminate\Contracts\Events\Dispatcher $events
*
* @return void
*/
Expand Down
18 changes: 5 additions & 13 deletions src/Engines/MeilisearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ public function delete($models)
/**
* Perform the given search on the engine.
*
* @param Builder $builder
*
* @return mixed
*/
public function search(Builder $builder)
Expand All @@ -96,7 +94,6 @@ public function search(Builder $builder)
/**
* Perform the given search on the engine.
*
* @param Builder $builder
* @param int $perPage
* @param int $page
*
Expand All @@ -114,9 +111,6 @@ public function paginate(Builder $builder, $perPage, $page)
/**
* Perform the given search on the engine.
*
* @param Builder $builder
* @param array $options
*
* @return mixed
*/
protected function performSearch(Builder $builder, array $options = [])
Expand All @@ -138,13 +132,12 @@ protected function performSearch(Builder $builder, array $options = [])
/**
* Get the filter array for the query.
*
* @param \Laravel\Scout\Builder $builder
* @return array
*/
protected function filters(Builder $builder)
{
return collect($builder->wheres)->map(function ($value, $key) {
return $key . '=' . '"'.$value.'"';
return $key.'='.'"'.$value.'"';
})->values()->implode(' AND ');
}

Expand All @@ -157,7 +150,7 @@ protected function filters(Builder $builder)
*/
public function mapIds($results)
{
if (count($results['hits']) === 0) {
if (0 === count($results['hits'])) {
return collect();
}

Expand All @@ -170,15 +163,14 @@ public function mapIds($results)
/**
* Map the given results to instances of the given model.
*
* @param Builder $builder
* @param mixed $results
* @param mixed $results
* @param \Illuminate\Database\Eloquent\Model $model
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function map(Builder $builder, $results, $model)
{
if (is_null($results) || count($results['hits']) === 0) {
if (is_null($results) || 0 === count($results['hits'])) {
return $model->newCollection();
}

Expand Down Expand Up @@ -236,7 +228,7 @@ protected function usesSoftDelete($model)
* Dynamically call the MeiliSearch client instance.
*
* @param string $method
* @param array $parameters
* @param array $parameters
*
* @return mixed
*/
Expand Down
4 changes: 2 additions & 2 deletions src/MeilisearchServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Meilisearch\Scout;

use MeiliSearch\Client;
use Laravel\Scout\EngineManager;
use Illuminate\Support\ServiceProvider;
use Laravel\Scout\EngineManager;
use MeiliSearch\Client;
use Meilisearch\Scout\Console\IndexMeilisearch;
use Meilisearch\Scout\Engines\MeilisearchEngine;

Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/SearchableModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Meilisearch\Scout\Tests\Fixtures;

use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

class SearchableModel extends Model
{
Expand Down
25 changes: 12 additions & 13 deletions tests/MeilisearchEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Meilisearch\Scout\Tests;

use MeiliSearch\Endpoints\Indexes;
use stdClass;
use Mockery as m;
use MeiliSearch\Client;
use Laravel\Scout\Builder;
use Illuminate\Database\Eloquent\Collection;
use Laravel\Scout\Builder;
use MeiliSearch\Client;
use MeiliSearch\Endpoints\Indexes;
use Meilisearch\Scout\Engines\MeilisearchEngine;
use Meilisearch\Scout\Tests\Fixtures\SearchableModel;
use Mockery as m;
use stdClass;

class MeilisearchEngineTest extends TestCase
{
Expand All @@ -30,7 +30,7 @@ public function update_adds_objects_to_index()
]);

$engine = new MeilisearchEngine($client);
$engine->update(Collection::make([new SearchableModel]));
$engine->update(Collection::make([new SearchableModel()]));
}

/** @test */
Expand All @@ -54,7 +54,7 @@ public function search_sends_correct_parameters_to_meilisearch()
]);

$engine = new MeilisearchEngine($client);
$builder = new Builder(new SearchableModel, 'mustang', function ($meilisearch, $query, $options) {
$builder = new Builder(new SearchableModel(), 'mustang', function ($meilisearch, $query, $options) {
$options['filters'] = 'foo=1';

return $meilisearch->search($query, $options);
Expand Down Expand Up @@ -138,7 +138,7 @@ public function a_model_is_indexed_with_a_custom_meilisearch_key()
$index->shouldReceive('addDocuments')->with([['id' => 'my-meilisearch-key.1']]);

$engine = new MeilisearchEngine($client);
$engine->update(Collection::make([new CustomKeySearchableModel]));
$engine->update(Collection::make([new CustomKeySearchableModel()]));
}

/** @test */
Expand All @@ -149,7 +149,7 @@ public function flush_a_model_with_a_custom_meilisearch_key()
$index->shouldReceive('deleteAllDocuments');

$engine = new MeilisearchEngine($client);
$engine->flush(new CustomKeySearchableModel);
$engine->flush(new CustomKeySearchableModel());
}

/** @test */
Expand All @@ -160,7 +160,7 @@ public function update_empty_searchable_array_does_not_add_objects_to_index()
$index->shouldNotReceive('addObjects');

$engine = new MeilisearchEngine($client);
$engine->update(Collection::make([new EmptySearchableModel]));
$engine->update(Collection::make([new EmptySearchableModel()]));
}

/** @test */
Expand All @@ -178,7 +178,7 @@ public function pagination_correct_parameters()
]);

$engine = new MeilisearchEngine($client);
$builder = new Builder(new SearchableModel, 'mustang', function ($meilisearch, $query, $options) {
$builder = new Builder(new SearchableModel(), 'mustang', function ($meilisearch, $query, $options) {
$options['filters'] = 'foo=1';

return $meilisearch->search($query, $options);
Expand Down Expand Up @@ -210,7 +210,7 @@ public function update_empty_searchable_array_from_soft_deleted_model_does_not_a
$index->shouldNotReceive('addDocuments');

$engine = new MeilisearchEngine($client, true);
$engine->update(Collection::make([new SoftDeleteEmptySearchableModel]));
$engine->update(Collection::make([new SoftDeleteEmptySearchableModel()]));
}
}

Expand All @@ -223,7 +223,6 @@ public function toSearchableArray()

public function pushSoftDeleteMetadata()
{
//
}

public function scoutMetadata()
Expand Down
1 change: 0 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ protected function getPackageProviders($app)

protected function getEnvironmentSetUp($app)
{
//
}
}

0 comments on commit 0ec2fff

Please sign in to comment.