public function test_search_literal()
 {
     $this->populateDatabase();
     $model = new Models\Category();
     $query = $model->query();
     $data = ['q' => '"Another Cat"'];
     $modifier = $this->getSearchModifierInstance($query, $data, null, 'not');
     $modifier->modify($query);
     $this->assertEquals(1, count($query->get()));
 }
 public function test_per_page_invalid()
 {
     $model = new Models\Category();
     $query = $model->query();
     $data = ['page' => 'sdf'];
     $modifier = $this->getFilterModifierInstance($query, $data);
     $result = $modifier->modify($query);
     $this->assertInstanceOf(\Illuminate\Database\Eloquent\Builder::class, $result);
     $this->assertEquals(0, $result->getQuery()->offset);
 }
 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);
 }
 public function test_integration_returns_expected()
 {
     $this->populateDatabase();
     $model1 = Models\Category::find(1);
     $query1 = $model1->query();
     $model2 = Models\Category::find(1);
     $query2 = $model2->query();
     $data = ['with' => ['themes' => ['column' => 'name', 'operator' => '==', 'value' => 'Not here']]];
     $modifier = $this->getFilterModifierInstance($query1, $data);
     $data['with']['themes']['value'] = 'Theme 1';
     $modifier2 = $this->getFilterModifierInstance($query2, $data);
     $result1 = $modifier->modify($query1);
     $result2 = $modifier2->modify($query2);
     $result1 = $result1->first();
     $result2 = $result2->first();
     $this->assertEquals(0, count($result1->themes));
     $this->assertEquals(1, count($result2->themes));
 }
 /**
  * @return Models\Category
  */
 protected function createCategory()
 {
     $cat = new Models\Category(['name' => 'Cat 1']);
     $cat->save();
     $cat2 = new Models\Category(['name' => 'Another Cat']);
     $cat2->save();
     $cat2 = new Models\Category(['name' => 'B. Another cat']);
     $cat2->save();
     $book = new Models\Book(['name' => 'Book 3']);
     $book->save();
     $cat->book()->save($book);
     $theme = new Models\Theme(['name' => 'Theme 1']);
     $theme->save();
     $cat->themes()->save($theme);
     return $cat;
 }
 public function test_multiple()
 {
     $this->populateDatabase();
     $model = new Models\Category();
     $query = $model->query();
     $data = ['sort' => '+name, id'];
     $modifier = $this->getFilterModifierInstance($query, $data);
     $result = $modifier->modify($query);
     $this->assertInstanceOf(\Illuminate\Database\Eloquent\Builder::class, $result);
     $orders = $result->getQuery()->orders;
     $result = $query->get();
     $this->assertEquals(2, count($orders));
     $this->assertEquals('Another Cat', $result[0]->name);
 }
 public function test_only_selected_fields_are_returned()
 {
     $this->populateDatabase();
     $model = new Models\Category();
     $query = $model->query();
     $modifier = $this->_getInstance($query, ['fields' => 'name']);
     $result = $modifier->modify($query);
     $model = $result->find(1);
     $atts = $model->getAttributes();
     $this->assertEquals(1, count($atts));
     $this->assertEquals(true, array_has($atts, 'name'));
 }
 protected function getCatQueryBuilder()
 {
     $cat = new Category();
     $builder = $cat->newQuery();
     return $builder;
 }
 public function test_exclude()
 {
     $this->populateDatabase();
     $model = new Models\Category();
     $query = $model->query();
     $queryObject = new \Johnrich85\EloquentQueryModifier\FilterQuery();
     $queryObject->operator = 'exclude';
     $queryObject->value = ['Cat 1', 2, 3];
     $data = ['name' => $queryObject];
     $modifier = $this->getFilterModifierInstance($query, $data, null);
     $result = $modifier->modify($query);
     $wheres = $result->getQuery()->wheres;
     $this->assertEquals(1, count($wheres));
     $this->assertEquals('name', $wheres[0]['column']);
     $this->assertEquals('NotIn', $wheres[0]['type']);
     $this->assertEquals('Cat 1', $wheres[0]['values'][0]);
     $this->assertEquals(2, $wheres[0]['values'][1]);
     $this->assertEquals(3, $wheres[0]['values'][2]);
     $this->assertEquals('and', $wheres[0]['boolean']);
     $this->assertEquals(2, count($query->get()));
 }