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_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_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_invalid_relation_does_not_add_eager_load() { $model = new Models\Category(); $query = $model->query(); $data = ['with' => ['xx' => []]]; $modifier = $this->getFilterModifierInstance($query, $data); $this->setExpectedException(Exception::class); $modifier->modify($query); }
public function test_invalid_relation_throws_exception() { $model = new Models\Category(); $query = $model->query(); $data = ['has' => ['xx' => []]]; $modifier = $this->getFilterModifierInstance($query, $data); $this->setExpectedException(Exception::class); $modifier->modify($query); }
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')); }
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())); }