Skip to content

Commit

Permalink
fix: Changed query building order to play nice with forwarded calls
Browse files Browse the repository at this point in the history
  • Loading branch information
voydz committed May 4, 2021
1 parent ef8a82b commit f4919c9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .php_cs.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"php":"8.0.5","version":"2.18.6","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":true,"class_definition":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"ordered_imports":{"sortAlgorithm":"alpha"},"no_unused_imports":true,"not_operator_with_successor_space":true,"trailing_comma_in_multiline_array":true,"phpdoc_scalar":true,"unary_operator_spaces":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","continue","declare","return","throw","try"]},"phpdoc_single_line_var_spacing":true,"phpdoc_var_without_name":true},"hashes":{"src\/Searchable.php":1242980759,"src\/Search.php":1813740488,"src\/SearchResultCollection.php":2567122850,"src\/SearchAspect.php":4127695379,"src\/SearchResult.php":2693161934,"src\/SearchableAttribute.php":3752884723,"src\/Exceptions\/InvalidSearchableModel.php":3300456690,"src\/Exceptions\/InvalidModelSearchAspect.php":2689367197,"src\/ModelSearchAspect.php":4153366588,"tests\/stubs\/Account.php":470375312,"tests\/stubs\/CustomNameSearchAspect.php":2380198170,"tests\/SearchResultTest.php":4266329428,"tests\/Models\/TestComment.php":2445821388,"tests\/Models\/TestModel.php":2013473586,"tests\/TestCase.php":517075324,"tests\/SearchAspectTest.php":830066418,"tests\/ModelSearchAspectTest.php":2803194365,"tests\/SearchTest.php":2850208164}}
{"php":"8.0.5","version":"2.18.6","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":true,"class_definition":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"ordered_imports":{"sortAlgorithm":"alpha"},"no_unused_imports":true,"not_operator_with_successor_space":true,"trailing_comma_in_multiline_array":true,"phpdoc_scalar":true,"unary_operator_spaces":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","continue","declare","return","throw","try"]},"phpdoc_single_line_var_spacing":true,"phpdoc_var_without_name":true},"hashes":{"src\/Searchable.php":1242980759,"src\/Search.php":1813740488,"src\/SearchResultCollection.php":2567122850,"src\/SearchAspect.php":4127695379,"src\/SearchResult.php":2693161934,"src\/SearchableAttribute.php":3752884723,"src\/Exceptions\/InvalidSearchableModel.php":3300456690,"src\/Exceptions\/InvalidModelSearchAspect.php":2689367197,"src\/ModelSearchAspect.php":4153366588,"tests\/stubs\/Account.php":470375312,"tests\/stubs\/CustomNameSearchAspect.php":2380198170,"tests\/SearchResultTest.php":4266329428,"tests\/Models\/TestComment.php":2445821388,"tests\/Models\/TestModel.php":2013473586,"tests\/TestCase.php":517075324,"tests\/SearchAspectTest.php":830066418,"tests\/ModelSearchAspectTest.php":2803194365,"tests\/SearchTest.php":2850208164}}
4 changes: 2 additions & 2 deletions src/ModelSearchAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ public function getResults(string $term): Collection

$query = ($this->model)::query();

$this->addSearchConditions($query, $term);

foreach ($this->callsToForward as $callToForward) {
$this->forwardCallTo($query, $callToForward['method'], $callToForward['parameters']);
}

$this->addSearchConditions($query, $term);

if ($this->limit) {
$query->limit($this->limit);
}
Expand Down
52 changes: 48 additions & 4 deletions tests/ModelSearchAspectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ public function it_can_build_an_eloquent_query_applying_scopes()

$searchAspect->getResults('john');

$expectedQuery = 'select * from "test_models" where "active" = ? and (LOWER("name") LIKE ?)';
$expectedQuery = 'select * from "test_models" where (LOWER("name") LIKE ?) and "active" = ?';

$executedQuery = Arr::get(DB::getQueryLog(), '0.query');
$firstBinding = Arr::get(DB::getQueryLog(), '0.bindings.0');
$secondBinding = Arr::get(DB::getQueryLog(), '0.bindings.1');

$this->assertEquals($expectedQuery, $executedQuery);
$this->assertEquals(1, $firstBinding);
$this->assertEquals(1, $secondBinding);
}

/** @test */
Expand Down Expand Up @@ -187,7 +187,51 @@ public function it_can_build_an_eloquent_query_by_many_same_methods()

$searchAspect->getResults('taylor');

$expectedQuery = 'select * from "test_models" where "gender" = ? and "status" = ? and (LOWER("name") LIKE ?)';
$expectedQuery = 'select * from "test_models" where (LOWER("name") LIKE ?) and "gender" = ? and "status" = ?';

$executedQuery = Arr::get(DB::getQueryLog(), '0.query');

$this->assertEquals($expectedQuery, $executedQuery);
}

/** @test */
public function it_can_build_an_eloquent_query_with_or_clause()
{
TestModel::createWithNameAndLastNameAndGenderAndStatus('Taylor', 'Otwell', 'woman', true);

$searchAspect = ModelSearchAspect::forModel(TestModel::class)
->addSearchableAttribute('name', true)
->orWhere('gender', 'woman')
->orWhere('status', 'activated');

DB::enableQueryLog();

$searchAspect->getResults('woman');

$expectedQuery = 'select * from "test_models" where (LOWER(name) LIKE ?) or "gender" = ? or "status" = ?';

$executedQuery = Arr::get(DB::getQueryLog(), '0.query');

$this->assertEquals($expectedQuery, $executedQuery);
}

/** @test */
public function it_can_build_an_eloquent_query_with_mixed_andor_clause()
{
TestModel::createWithNameAndLastNameAndGenderAndStatus('Taylor', 'Otwell', 'woman', true);

$searchAspect = ModelSearchAspect::forModel(TestModel::class)
->addSearchableAttribute('name', true)
->orWhere(function ($query) {
$query->where('gender', 'woman')
->Where('status', 'activated');
});

DB::enableQueryLog();

$searchAspect->getResults('woman');

$expectedQuery = 'select * from "test_models" where (LOWER(name) LIKE ?) or ("gender" = ? and "status" = ?)';

$executedQuery = Arr::get(DB::getQueryLog(), '0.query');

Expand Down

0 comments on commit f4919c9

Please sign in to comment.