From 0d55c3f19a13defc20e75e0156ada740f332f3eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20V=C3=A1radi?= Date: Fri, 16 Apr 2021 22:33:18 +0200 Subject: [PATCH 1/6] Add test for columns with reserved name --- tests/ModelSearchAspectTest.php | 14 ++++++++++++++ tests/TestCase.php | 1 + 2 files changed, 15 insertions(+) diff --git a/tests/ModelSearchAspectTest.php b/tests/ModelSearchAspectTest.php index 498d3a0..e80b65e 100644 --- a/tests/ModelSearchAspectTest.php +++ b/tests/ModelSearchAspectTest.php @@ -41,6 +41,20 @@ public function it_can_perform_a_search_on_multiple_columns() $this->assertInstanceOf(TestModel::class, $results[0]); } + /** @test */ + public function it_can_perform_a_search_on_columns_with_reserved_name() + { + TestModel::createWithNameAndLastName('jane', 'doe'); + TestModel::createWithNameAndLastName('Taylor', 'Otwell'); + + $searchAspect = ModelSearchAspect::forModel(TestModel::class, 'name', 'where'); + + $results = $searchAspect->getResults('Taylor Otwell'); + + $this->assertCount(1, $results); + $this->assertInstanceOf(TestModel::class, $results[0]); + } + /** @test */ public function it_can_add_searchable_attributes() { diff --git a/tests/TestCase.php b/tests/TestCase.php index f6ec3dd..f232d39 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -23,6 +23,7 @@ protected function setUpDatabase(Application $app) $table->timestamps(); $table->string('name'); $table->string('last_name')->nullable(); + $table->string('where')->nullable(); $table->boolean('active')->default(false); $table->string('gender')->nullable(); }); From b480c7bdb84c78c890efec272b7811b5f3a212eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20V=C3=A1radi?= Date: Fri, 16 Apr 2021 22:33:47 +0200 Subject: [PATCH 2/6] Wrap column name --- src/ModelSearchAspect.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ModelSearchAspect.php b/src/ModelSearchAspect.php index 600d070..a02c436 100644 --- a/src/ModelSearchAspect.php +++ b/src/ModelSearchAspect.php @@ -120,8 +120,9 @@ protected function addSearchConditions(Builder $query, string $term) $query->where(function (Builder $query) use ($attributes, $term, $searchTerms) { foreach (Arr::wrap($attributes) as $attribute) { + $sql = "LOWER({$query->getGrammar()->wrap($attribute->getAttribute())}) LIKE ?"; + foreach ($searchTerms as $searchTerm) { - $sql = "LOWER({$attribute->getAttribute()}) LIKE ?"; $searchTerm = mb_strtolower($searchTerm, 'UTF8'); $attribute->isPartial() From 384e342bf6c6e447a0e2ea5e64ca39117bd4ccb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20V=C3=A1radi?= Date: Fri, 16 Apr 2021 22:34:11 +0200 Subject: [PATCH 3/6] Update existing test to not fail --- tests/ModelSearchAspectTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/ModelSearchAspectTest.php b/tests/ModelSearchAspectTest.php index e80b65e..96c2730 100644 --- a/tests/ModelSearchAspectTest.php +++ b/tests/ModelSearchAspectTest.php @@ -85,7 +85,7 @@ public function it_can_build_an_eloquent_query() $searchAspect->getResults('john'); - $expectedQuery = 'select * from "test_models" where (LOWER(name) LIKE ? or "email" = ?)'; + $expectedQuery = 'select * from "test_models" where (LOWER("name") LIKE ? or "email" = ?)'; $executedQuery = Arr::get(DB::getQueryLog(), '0.query'); @@ -124,7 +124,7 @@ 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 "active" = ? and (LOWER("name") LIKE ?)'; $executedQuery = Arr::get(DB::getQueryLog(), '0.query'); $firstBinding = Arr::get(DB::getQueryLog(), '0.bindings.0'); @@ -187,7 +187,7 @@ 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 "gender" = ? and "status" = ? and (LOWER("name") LIKE ?)'; $executedQuery = Arr::get(DB::getQueryLog(), '0.query'); From d28fa6a0fbfb03a6ff19d2bd7b1bb4657a4e416d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20V=C3=A1radi?= Date: Mon, 3 May 2021 09:51:43 +0200 Subject: [PATCH 4/6] Remove Laravel 6 support --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 79f7933..33c1f2f 100644 --- a/composer.json +++ b/composer.json @@ -23,12 +23,12 @@ ], "require": { "php": "^7.2|^8.0", - "laravel/framework": "^6.0|^7.0|^8.0" + "laravel/framework": "^7.0|^8.0" }, "require-dev": { - "orchestra/testbench": "~3.8.0|^4.0|^5.0|^6.0", + "orchestra/testbench": "^5.0|^6.0", "larapack/dd": "^1.0", - "phpunit/phpunit": "^7.5|^8.0|^9.3" + "phpunit/phpunit": "^8.5|^9.3" }, "autoload": { "psr-4": { From 30b298e5d535de661e2d5e4a198f715d3e977bb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20V=C3=A1radi?= Date: Mon, 3 May 2021 09:52:45 +0200 Subject: [PATCH 5/6] Remove Laravel 6 test runner --- .github/workflows/run-tests.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 7175525..51e1098 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -10,15 +10,13 @@ jobs: matrix: os: [ubuntu-latest] php: [7.2, 7.3, 7.4, 8.0] - laravel: [8.*, 6.*, 7.*] + laravel: [8.*, 7.*] dependency-version: [prefer-lowest, prefer-stable] include: - laravel: 8.* testbench: 6.* - laravel: 7.* testbench: 5.* - - laravel: 6.* - testbench: 4.* exclude: - laravel: 8.* php: 7.2 From 0bf7417873c9381ce0af92aa5affe456c7befa37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20V=C3=A1radi?= Date: Mon, 3 May 2021 10:05:53 +0200 Subject: [PATCH 6/6] Remove Laravel 7 support --- .github/workflows/run-tests.yml | 9 ++------- composer.json | 8 ++++---- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 51e1098..3ea4b24 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -9,17 +9,12 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest] - php: [7.2, 7.3, 7.4, 8.0] - laravel: [8.*, 7.*] + php: [7.3, 7.4, 8.0] + laravel: [8.*] dependency-version: [prefer-lowest, prefer-stable] include: - laravel: 8.* testbench: 6.* - - laravel: 7.* - testbench: 5.* - exclude: - - laravel: 8.* - php: 7.2 name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} diff --git a/composer.json b/composer.json index 33c1f2f..eeba53b 100644 --- a/composer.json +++ b/composer.json @@ -22,13 +22,13 @@ } ], "require": { - "php": "^7.2|^8.0", - "laravel/framework": "^7.0|^8.0" + "php": "^7.3|^8.0", + "laravel/framework": "^8.0" }, "require-dev": { - "orchestra/testbench": "^5.0|^6.0", + "orchestra/testbench": "^6.0", "larapack/dd": "^1.0", - "phpunit/phpunit": "^8.5|^9.3" + "phpunit/phpunit": "^9.3" }, "autoload": { "psr-4": {