From 2404ec2679e0ed834bcdf9ef343bc08eae506b1e Mon Sep 17 00:00:00 2001 From: Attila Fulop <1162360+fulopattila122@users.noreply.github.com> Date: Tue, 30 Apr 2024 18:33:48 +0300 Subject: [PATCH] Changed the ProductSearch price range methods that amends #176 - priceGreaterThan uses the `>` operator instead of `>=` - priceLessThan uses the `<` operator instead of `<=` - added the priceGreaterThanOrEqualTo method (`>=`) - added the priceLessThanOrEqualTo method (`<=`) --- src/Foundation/Search/ProductSearch.php | 16 +++++++ src/Foundation/Tests/ProductSearchTest.php | 50 ++++++++++++++++++++-- 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/Foundation/Search/ProductSearch.php b/src/Foundation/Search/ProductSearch.php index 69a45c76..05d28034 100644 --- a/src/Foundation/Search/ProductSearch.php +++ b/src/Foundation/Search/ProductSearch.php @@ -165,6 +165,14 @@ public function priceBetween(float $min, float $max): self } public function priceGreaterThan(float $min): self + { + $this->productQuery->where('price', '>', $min); + $this->masterProductQuery->where('price', '>', $min); + + return $this; + } + + public function priceGreaterThanOrEqualTo(float $min): self { $this->productQuery->where('price', '>=', $min); $this->masterProductQuery->where('price', '>=', $min); @@ -173,6 +181,14 @@ public function priceGreaterThan(float $min): self } public function priceLessThan(float $max): self + { + $this->productQuery->where('price', '<', $max); + $this->masterProductQuery->where('price', '<', $max); + + return $this; + } + + public function priceLessThanOrEqualTo(float $max): self { $this->productQuery->where('price', '<=', $max); $this->masterProductQuery->where('price', '<=', $max); diff --git a/src/Foundation/Tests/ProductSearchTest.php b/src/Foundation/Tests/ProductSearchTest.php index e0b3c907..6214c21f 100644 --- a/src/Foundation/Tests/ProductSearchTest.php +++ b/src/Foundation/Tests/ProductSearchTest.php @@ -610,12 +610,12 @@ public function it_can_find_products_below_a_certain_price() $prices = $result->pluck('price'); foreach ($prices as $price) { - $this->assertLessThanOrEqual(12, $price); + $this->assertLessThan(12, $price); } } /** @test */ - public function it_can_find_products_above_a_certain_price() + public function it_can_find_products_above_or_equal_to_a_certain_price() { factory(Product::class)->create([ 'price' => 31 @@ -638,7 +638,7 @@ public function it_can_find_products_above_a_certain_price() ]); $finder = new ProductSearch(); - $result = $finder->priceGreaterThan(35)->getResults(); + $result = $finder->priceGreaterThanOrEqualTo(35)->getResults(); $this->assertCount(2, $result); @@ -648,4 +648,48 @@ public function it_can_find_products_above_a_certain_price() $this->assertGreaterThanOrEqual(35, $price); } } + + /** @test */ + public function it_can_find_products_above_a_given_price() + { + factory(Product::class)->createMany([ + ['price' => 7], + ['price' => 8], + ['price' => 9], + ['price' => 10], + ['price' => 200], + ['price' => 1999884], + ]); + + $result = (new ProductSearch())->priceGreaterThan(8)->getResults(); + + $this->assertCount(4, $result); + $result->each(fn (Product $product) => $this->assertGreaterThan(8, $product->price)); + } + + /** @test */ + public function it_can_find_products_below_or_equal_to_a_given_price() + { + factory(Product::class)->createMany([ + ['price' => 300], + ['price' => 300.01], + ['price' => 301.01], + ['price' => 301.01], + ['price' => 301.02], + ['price' => 301.03], + ['price' => 301.011], + ]); + + $result = (new ProductSearch())->priceLessThanOrEqualTo(301.01)->getResults(); + + $this->assertCount(4, $result); + $result->each(fn (Product $product) => $this->assertLessThanOrEqual(301.01, $product->price)); + } + + /** @test */ + public function it_can_be_extended_using_macros() + { + $finder = new ProductSearch(); + + } }