Skip to content
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
7 changes: 3 additions & 4 deletions src/Services/Search/SearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

class SearchService extends Searchable
{

/**
* @param RestifyRequest $request
* @param Model $model
Expand All @@ -25,6 +24,7 @@ public function search(RestifyRequest $request, Model $model)
])));

$query = $this->prepareMatchFields($request, $this->prepareSearchFields($request, $model->newQuery(), $this->fixedInput), $this->fixedInput);

return $this->prepareRelations($request, $this->prepareOrders($request, $query), $this->fixedInput);
}

Expand All @@ -41,7 +41,7 @@ public function prepareMatchFields(RestifyRequest $request, $query, $extra = [])
$model = $query->getModel();
if ($model instanceof RestifySearchable) {
foreach ($model::getMatchByFields() as $key => $type) {
if ( ! $request->has($key) && ! data_get($extra, "match.$key")) {
if (! $request->has($key) && ! data_get($extra, "match.$key")) {
continue;
}

Expand Down Expand Up @@ -166,15 +166,14 @@ public function prepareSearchFields(RestifyRequest $request, $query, $extra = []
$likeOperator = $connectionType == 'pgsql' ? 'ilike' : 'like';

foreach ($model::getSearchableFields() as $column) {
$query->orWhere($model->qualifyColumn($column), $likeOperator, '%' . $search . '%');
$query->orWhere($model->qualifyColumn($column), $likeOperator, '%'.$search.'%');
}
});
}

return $query;
}


/**
* @param $query
* @param $param
Expand Down
4 changes: 0 additions & 4 deletions src/Services/Search/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

namespace Binaryk\LaravelRestify\Services\Search;

use Binaryk\LaravelRestify\Contracts\RestifySearchable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

abstract class Searchable
{
/**
Expand Down
5 changes: 3 additions & 2 deletions tests/Fixtures/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ public function posts()
}

/**
* Set default test values
* Set default test values.
*/
public static function reset() {
public static function reset()
{
static::$search = ['id', 'email'];
static::$sort = ['id'];
static::$match = ['id' => 'int', 'email' => 'string'];
Expand Down
5 changes: 3 additions & 2 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected function setUp(): void
$this->repositoryMock();
$this->loadMigrations();
$this->loadRoutes();
$this->withFactories(__DIR__ . '/Factories');
$this->withFactories(__DIR__.'/Factories');
$this->injectTranslator();

Restify::repositories([
Expand Down Expand Up @@ -69,7 +69,7 @@ protected function loadMigrations()
{
$this->loadMigrationsFrom([
'--database' => 'sqlite',
'--realpath' => realpath(__DIR__ . '/Migrations'),
'--realpath' => realpath(__DIR__.'/Migrations'),
]);
}

Expand Down Expand Up @@ -149,6 +149,7 @@ public function loadRoutes()
public function lastQuery()
{
$queries = DB::getQueryLog();

return end($queries);
}
}
21 changes: 10 additions & 11 deletions tests/SearchServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
use Mockery as MockeryAlias;

/**
* @package Binaryk\LaravelRestify\Tests;
* @author Eduard Lupacescu <eduard.lupacescu@binarcode.com>
*/
class SearchServiceTest extends IntegrationTest
{
/**
* @var SearchService $service
* @var SearchService
*/
private $service;

Expand All @@ -39,7 +38,7 @@ public function test_should_attach_with_from_extra_in_eager_load()
$request->shouldReceive('get')
->andReturn();
/**
* @var Builder $query
* @var Builder
*/
$query = $this->service->prepareRelations($request, $builder, ['posts']);
$this->assertInstanceOf(Closure::class, $query->getEagerLoads()['posts']);
Expand All @@ -55,7 +54,7 @@ public function test_should_attach_with_in_eager_load()
$request->shouldReceive('get')
->andReturn('posts');
/**
* @var Builder $query
* @var Builder
*/
$query = $this->service->prepareRelations($request, $builder);
$this->assertInstanceOf(Closure::class, $query->getEagerLoads()['posts']);
Expand All @@ -70,7 +69,7 @@ public function test_should_order_desc_by_field()
$request->shouldReceive('get')
->andReturn('-id');
/**
* @var Builder $query
* @var Builder
*/
$query = $this->service->prepareOrders($request, $builder);
$this->assertEquals('id', $query->getQuery()->orders[0]['column']);
Expand All @@ -86,7 +85,7 @@ public function test_should_order_asc_by_field()
$request->shouldReceive('get')
->andReturn('id');
/**
* @var Builder $query
* @var Builder
*/
$query = $this->service->prepareOrders($request, $builder);
$this->assertEquals('id', $query->getQuery()->orders[0]['column']);
Expand All @@ -102,7 +101,7 @@ public function test_should_order_asc_by_extra_passed_field()
$request->shouldReceive('get')
->andReturn();
/**
* @var Builder $query
* @var Builder
*/
$query = $this->service->prepareOrders($request, $builder, [
'sort' => 'id',
Expand All @@ -123,7 +122,7 @@ public function test_match_fields_should_add_equal_where_clause()
$request->shouldReceive('has')
->andReturnTrue();
/**
* @var Builder $query
* @var Builder
*/
$query = $this->service->prepareMatchFields($request, $builder);
$this->assertCount(count(User::$match), $query->getQuery()->getRawBindings()['where']);
Expand All @@ -147,7 +146,7 @@ public function test_match_fields_from_extra_should_add_equal_where_clause()
$request->shouldReceive('has')
->andReturnFalse();
/**
* @var Builder $query
* @var Builder
*/
$query = $this->service->prepareMatchFields($request, $builder, [
'match' => [
Expand Down Expand Up @@ -175,7 +174,7 @@ public function test_match_fields_should_not_add_equal_where_if_value_passed_in_
$request->shouldReceive('has')
->andReturnTrue();
/**
* @var Builder $query
* @var Builder
*/
$query = $this->service->prepareMatchFields($request, $builder);
$this->assertCount(0, $query->getQuery()->getRawBindings()['where']);
Expand All @@ -200,7 +199,7 @@ public function test_prepare_search_should_add_where_clause()
$request->shouldReceive('get')
->andReturn('some search');
/**
* @var Builder $query
* @var Builder
*/
$query = $this->service->prepareSearchFields($request, $builder);
$this->assertArrayHasKey('where', $query->getQuery()->getRawBindings());
Expand Down