Skip to content

Commit

Permalink
Improves tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone committed Nov 3, 2019
1 parent d437599 commit 5511989
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Searchable
public function __construct(?string $keyword, array $columns = [])
{
$this->keyword = $keyword ?? '';
$this->columns = $columns;
$this->columns = \array_filter($columns);
}

/**
Expand Down Expand Up @@ -101,6 +101,8 @@ protected function queryOnColumnUsing(
) {
if ($column->isJsonPathSelector()) {
return $this->queryOnJsonColumnUsing($query, $column, $likeOperator, $whereOperator);
} elseif (! $column->validate()) {
return $query;
}

$keywords = Str::searchable($this->keyword);
Expand Down
67 changes: 67 additions & 0 deletions tests/Unit/SearchableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,73 @@ public function it_can_build_search_query()
$this->assertEquals($query, $stub->apply($query));
}

/** @test */
public function it_ignores_build_search_query_when_columns_is_not_provided()
{
$query = m::mock('Illuminate\Database\Query\Builder');

$query->shouldReceive('getConnection->getDriverName')->andReturn('mysql');
$query->shouldReceive('orWhere')->never()->with(m::type('Closure'))
->andReturnUsing(static function ($c) use ($query) {
$c($query);
})
->shouldReceive('orWhere')->never()->with('name', 'like', 'hello')
->shouldReceive('orWhere')->never()->with('name', 'like', 'hello%')
->shouldReceive('orWhere')->never()->with('name', 'like', '%hello')
->shouldReceive('orWhere')->never()->with('name', 'like', '%hello%');

$stub = new Searchable(
'hello', []
);

$this->assertEquals($query, $stub->apply($query));
}

/** @test */
public function it_ignores_build_search_query_when_columns_is_invalid()
{
$query = m::mock('Illuminate\Database\Query\Builder');

$query->shouldReceive('getConnection->getDriverName')->andReturn('mysql');
$query->shouldReceive('orWhere')->never()->with(m::type('Closure'))
->andReturnUsing(static function ($c) use ($query) {
$c($query);
})
->shouldReceive('orWhere')->never()->with('name', 'like', 'hello')
->shouldReceive('orWhere')->never()->with('name', 'like', 'hello%')
->shouldReceive('orWhere')->never()->with('name', 'like', '%hello')
->shouldReceive('orWhere')->never()->with('name', 'like', '%hello%');

$stub = new Searchable(
'hello', ['']
);

$this->assertEquals($query, $stub->apply($query));
}

/** @test */
public function it_ignores_build_search_query_when_keyword_is_empty()
{
$query = m::mock('Illuminate\Database\Query\Builder');

$query->shouldReceive('getConnection->getDriverName')->andReturn('mysql');
$query->shouldReceive('orWhere')->never()->with(m::type('Closure'))
->andReturnUsing(static function ($c) use ($query) {
$c($query);
})
->shouldReceive('orWhere')->never()->with('name', 'like', 'hello')
->shouldReceive('orWhere')->never()->with('name', 'like', 'hello%')
->shouldReceive('orWhere')->never()->with('name', 'like', '%hello')
->shouldReceive('orWhere')->never()->with('name', 'like', '%hello%');

$stub = new Searchable(
'', ['name']
);

$this->assertEquals($query, $stub->apply($query));
}


/** @test */
public function it_can_build_search_query_with_expression_value()
{
Expand Down

0 comments on commit 5511989

Please sign in to comment.