-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from ukfast/add-tests
Add tests
- Loading branch information
Showing
20 changed files
with
761 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
namespace Tests; | ||
|
||
use PHPUnit\Util\Filter; | ||
use Tests\Mocks\NoOpFilter; | ||
use Tests\Mocks\PenceWrapper; | ||
use UKFast\Sieve\FilterBuilder; | ||
use UKFast\Sieve\Filters\BooleanFilter; | ||
use UKFast\Sieve\Filters\DateFilter; | ||
use UKFast\Sieve\Filters\EnumFilter; | ||
use UKFast\Sieve\Filters\NumericFilter; | ||
use UKFast\Sieve\Filters\StringFilter; | ||
use UKFast\Sieve\MapFilter; | ||
|
||
class FilterBuilderTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function can_build_strings() | ||
{ | ||
$builder = new FilterBuilder; | ||
|
||
$this->assertInstanceOf(StringFilter::class, $builder->string()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function can_build_enum() | ||
{ | ||
$builder = new FilterBuilder; | ||
|
||
$this->assertInstanceOf(EnumFilter::class, $builder->enum(['a', 'b'])); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function can_build_numeric() | ||
{ | ||
$builder = new FilterBuilder; | ||
|
||
$this->assertInstanceOf(NumericFilter::class, $builder->numeric()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function can_build_date() | ||
{ | ||
$builder = new FilterBuilder; | ||
|
||
$this->assertInstanceOf(DateFilter::class, $builder->date()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function can_build_boolean() | ||
{ | ||
$builder = new FilterBuilder; | ||
|
||
$this->assertInstanceOf(BooleanFilter::class, $builder->boolean()); | ||
} | ||
|
||
|
||
/** | ||
* @test | ||
*/ | ||
public function can_build_custom_filters() | ||
{ | ||
$builder = new FilterBuilder; | ||
|
||
$this->assertInstanceOf(NoOpFilter::class, $builder->custom(new NoOpFilter)); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function can_wrap_filters() | ||
{ | ||
$builder = new FilterBuilder; | ||
$wrapped = $builder->wrap(new PenceWrapper)->string(); | ||
|
||
$this->assertInstanceOf(PenceWrapper::class, $wrapped); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function can_wrap_multiple_filters() | ||
{ | ||
$builder = new FilterBuilder; | ||
$wrapped = $builder->wrap(new PenceWrapper)->wrap(new MapFilter('target'))->string(); | ||
|
||
$this->assertInstanceOf(MapFilter::class, $wrapped); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Test\Filters; | ||
|
||
use Tests\Mocks\Pet; | ||
use Tests\TestCase; | ||
use UKFast\Sieve\Filters\BooleanFilter; | ||
use UKFast\Sieve\SearchTerm; | ||
|
||
class BooleanFilterTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function can_filter_eq() | ||
{ | ||
$filter = new BooleanFilter; | ||
$builder = Pet::query()->getQuery(); | ||
$filter->modifyQuery($builder, $this->searchTerm('eq', true)); | ||
|
||
$this->assertEquals( | ||
"select * from `pets` where `is_active` = ?", | ||
$builder->toSql() | ||
); | ||
$this->assertEquals([1], $builder->getBindings()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function can_filter_neq() | ||
{ | ||
$filter = new BooleanFilter; | ||
$builder = Pet::query()->getQuery(); | ||
$filter->modifyQuery($builder, $this->searchTerm('neq', true)); | ||
|
||
$this->assertEquals( | ||
"select * from `pets` where `is_active` != ?", | ||
$builder->toSql() | ||
); | ||
$this->assertEquals([1], $builder->getBindings()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function can_override_true_and_false_val() | ||
{ | ||
|
||
$filter = new BooleanFilter('yes', 'no'); | ||
$builder = Pet::query()->getQuery(); | ||
$filter->modifyQuery($builder, $this->searchTerm('eq', true)); | ||
|
||
$this->assertEquals( | ||
"select * from `pets` where `is_active` = ?", | ||
$builder->toSql() | ||
); | ||
$this->assertEquals(['yes'], $builder->getBindings()); | ||
} | ||
|
||
private function searchTerm($operator, $term) | ||
{ | ||
return new SearchTerm('is_active', $operator, 'is_active', $term); | ||
} | ||
} |
Oops, something went wrong.