Skip to content

Commit

Permalink
Finished off tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnrich85 committed Aug 22, 2016
1 parent 0536d73 commit 602b76b
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 7 deletions.
4 changes: 1 addition & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,5 @@ $modifier = Johnrich85\EloquentQueryModifier\EloquentQueryModifier($config, $fac

##Todo

1. Test for EloquentQueryModifier.
2. Test for custom modifiers
3. Composer possible conflicts with laravel...
1. Composer possible conflicts with laravel...

5 changes: 5 additions & 0 deletions tests/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ protected function createCategory()
]);
$cat2->save();

$cat2 = new Models\Category([
'name' => 'B. Another cat'
]);
$cat2->save();

$book = new Models\Book([
'name' => 'Book 3'
]);
Expand Down
68 changes: 68 additions & 0 deletions tests/EloquentQueryModifierTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php namespace Johnrich85\Tests;

use Illuminate\Support\Facades\Input;
use Johnrich85\EloquentQueryModifier\EloquentQueryModifier;
use Johnrich85\EloquentQueryModifier\Factory\ModifierFactory;
use Johnrich85\EloquentQueryModifier\FilterQuery;
use Johnrich85\EloquentQueryModifier\InputConfig;
use Johnrich85\EloquentQueryModifier\Tests\Mock\Models\Category;

class EloquentQueryModifierTest extends \Johnrich85\Tests\BaseTest{

public function test_no_parameters_returns_expected()
{
$this->populateDatabase();
$modifier = $this->getEqmInstance();
$builder = $this->getCatQueryBuilder();

$modifier->modify($builder,[]);

$results = $builder->get();

$this->assertEquals(3, count($results));
}

public function test_with_multiple_default_modifiers_returns_expected()
{
$this->populateDatabase();

$modifier = $this->getEqmInstance();
$builder = $this->getCatQueryBuilder();

$query = new FilterQuery();
$query->operator = '!=';
$query->value = 'Cat 1';

$modifier->modify($builder,[
'sort' => 'name',
'name' => $query,
'fields' => 'name'
]);

$results = $builder->get();

$this->assertEquals(2, count($results));
$this->assertEquals('Another Cat', $results[0]->name);
$this->assertEquals(1, count($results[0]));
$this->assertEquals(true, array_key_exists('name', (array) $results[0]->getAttributes()));
}

protected function getEqmInstance()
{
$config = new InputConfig();
$factory = new ModifierFactory();

$modifier = new EloquentQueryModifier($config, $factory);

return $modifier;
}

protected function getCatQueryBuilder()
{
$cat = new Category();

$builder = $cat->newQuery();

return $builder;
}
}
18 changes: 18 additions & 0 deletions tests/InputConfigTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Johnrich85\Tests;

use Johnrich85\EloquentQueryModifier\InputConfig;
use Johnrich85\EloquentQueryModifier\Tests\Mock\Models\Category;

class InputConfigTest extends \Johnrich85\Tests\BaseTest{

Expand Down Expand Up @@ -28,4 +29,21 @@ public function testDeleteModifier()
$this->assertEquals(4, count($modifiers));
}

public function test_all_columns_returned()
{
$model = new Category();

$config = new InputConfig();

$config->setFilterableFields($model->query());

$columns = $config->getFilterableFields();

$this->assertEquals(4, count($columns));
$this->assertContains('id', $columns);
$this->assertContains('name', $columns);
$this->assertContains('created_at', $columns);
$this->assertContains('updated_at', $columns);
}

}
2 changes: 1 addition & 1 deletion tests/Modifiers/FilterModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ public function test_exclude() {
$this->assertEquals(3, $wheres[0]['values'][2]);
$this->assertEquals('and', $wheres[0]['boolean']);

$this->assertEquals(1, count($query->get()));
$this->assertEquals(2, count($query->get()));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Modifiers/SearchModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function test_search_wildcard() {

$modifier->modify($query);

$this->assertEquals(2, count($query->get()));
$this->assertEquals(3, count($query->get()));
}

public function test_search_literal() {
Expand Down
4 changes: 2 additions & 2 deletions tests/Modifiers/SortModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function test_single() {

$result = $query->get();

$this->assertEquals(2, count($result));
$this->assertEquals(3, count($result));
$this->assertEquals('Cat 1', $result[0]->name);
}

Expand All @@ -178,7 +178,7 @@ public function test_single_asc() {

$result = $query->get();

$this->assertEquals(2, count($result));
$this->assertEquals(3, count($result));
$this->assertEquals('Another Cat', $result[0]->name);
}

Expand Down

0 comments on commit 602b76b

Please sign in to comment.