protected function seedDatabase() { TestSimpleModel::create(['unique_field' => '11', 'second_field' => null, 'name' => 'simple name', 'test_related_model_id' => 1, 'position' => 0, 'active' => true]); TestSimpleModel::create(['unique_field' => '123', 'second_field' => 'random string', 'name' => 'random name', 'test_related_model_id' => 2, 'position' => 1, 'active' => true]); TestSimpleModel::create(['unique_field' => '1337', 'second_field' => 'some more', 'name' => 'special name', 'test_related_model_id' => 3, 'position' => 14, 'active' => true]); TestSimpleModel::create(['unique_field' => '1980', 'second_field' => 'yet more fun', 'name' => 'another name', 'test_related_model_id' => 1, 'position' => 14, 'active' => true]); TestRelatedModel::create(['name' => 'related A', 'some_property' => 'super', 'test_simple_model_id' => 3, 'active' => true]); TestRelatedModel::create(['name' => 'related B', 'some_property' => 'generic', 'test_simple_model_id' => 3, 'active' => true]); TestRelatedModel::create(['name' => 'related C', 'some_property' => 'mild', 'test_simple_model_id' => 2, 'active' => true]); }
/** * @test */ function simple_integer_parameter_filter() { $filter = new TestFilter([]); // simple single integer $pfilter = new SimpleInteger(); $query = $pfilter->apply('testcol', 13, TestSimpleModel::query(), $filter); $this->assertRegExp('#where ["`]testcol["`] =#i', $query->toSql(), "Query SQL wrong for default single integer"); $this->assertEquals(13, $query->getBindings()[0], "Binding not correct for default single integer"); // custom operator $pfilter = new SimpleInteger(null, null, '>'); $query = $pfilter->apply('testcol', 13, TestSimpleModel::query(), $filter); $this->assertRegExp('#where ["`]testcol["`] >#i', $query->toSql(), "Query SQL wrong for custom operator match"); $this->assertEquals(13, $query->getBindings()[0], "Binding not correct for custom operator match"); // custom table and column name $pfilter = new SimpleInteger('custom_table', 'custom_column'); $query = $pfilter->apply('testcol', 13, TestSimpleModel::query(), $filter); $this->assertRegExp('#where ["`]custom_table["`]\\.["`]custom_column["`] =#i', $query->toSql(), "Query SQL wrong for integer custom names match"); $this->assertEquals(13, $query->getBindings()[0], "Binding not correct for integer custom names match"); // whereIn match (array argument) $pfilter = new SimpleInteger(); $query = $pfilter->apply('testcol', [13, 14], TestSimpleModel::query(), $filter); $this->assertRegExp('#where ["`]testcol["`] in\\s*\\(\\s*\\?\\s*,\\s*\\?\\s*\\)#i', $query->toSql(), "Query SQL wrong for wherein match"); $this->assertEquals([13, 14], $query->getBindings(), "Bindings not correct for wherein match"); }
/** * @test */ function it_adds_joins_and_applies_them_after_all_filters() { // add joins using addJoin method $query = (new TestFilter(['adding_joins' => 'okay']))->apply(TestSimpleModel::query())->toSql(); $this->assertRegExp('#adding_joins#i', $query, "Query SQL did not have parameter check in where clause"); $this->assertRegExp('#(inner )?join [`"]test_related_models[`"] on [`"]test_related_models[`"].[`"]id[`"] ' . '= [`"]test_simple_models[`"].[`"]test_related_model_id[`"]#i', $query, "Query SQL does not feature expected join clause"); // check if joins are not duplicated $query = (new TestFilter(['adding_joins' => 'okay', 'no_duplicate_joins' => 'please']))->apply(TestSimpleModel::query())->toSql(); $this->assertRegExp('#no_duplicate_joins#i', $query, "Query SQL did not have parameter check in where clause (second param)"); $this->assertRegExp('#(inner )?join [`"]test_related_models[`"] on [`"]test_related_models[`"].[`"]id[`"] ' . '= [`"]test_simple_models[`"].[`"]test_related_model_id[`"]#i', $query, "Query SQL does not feature expected join clause (with second param)"); $this->assertEquals(1, substr_count(strtolower($query), ' join '), "Query SQL should have only one join clause"); }
protected function getCountableBaseQuery($parameter = null) { return TestSimpleModel::query(); }