diff --git a/src/Foundation/Search/ProductSearch.php b/src/Foundation/Search/ProductSearch.php index be508ce7..36e49d02 100644 --- a/src/Foundation/Search/ProductSearch.php +++ b/src/Foundation/Search/ProductSearch.php @@ -153,6 +153,30 @@ public function nameContains(string $term): self return $this; } + public function priceBetween(float $min, float $max): self + { + $this->productQuery->whereBetween('price', [$min, $max]); + $this->masterProductQuery->whereBetween('price', [$min, $max]); + + return $this; + } + + public function priceGreaterThan(float $min): self + { + $this->productQuery->where('price', '>=', $min); + $this->masterProductQuery->where('price', '>=', $min); + + return $this; + } + + public function priceLessThan(float $max): self + { + $this->productQuery->where('price', '<=', $max); + $this->masterProductQuery->where('price', '<=', $max); + + return $this; + } + public function nameStartsWith(string $term): self { $this->productQuery->where('name', 'like', "$term%"); diff --git a/src/Foundation/Tests/ProductSearchTest.php b/src/Foundation/Tests/ProductSearchTest.php index 0ef77f7d..e0b3c907 100644 --- a/src/Foundation/Tests/ProductSearchTest.php +++ b/src/Foundation/Tests/ProductSearchTest.php @@ -546,4 +546,106 @@ public function it_can_order_and_limit_the_results() $this->assertEquals('Bitcoin', $resultset[1]->name); $this->assertEquals('Dogecoin', $resultset[2]->name); } + + /** @test */ + public function it_can_find_products_by_price_range() + { + factory(Product::class)->create([ + 'price' => 31 + ]); + + factory(Product::class)->create([ + 'price' => 35 + ]); + + factory(Product::class)->create([ + 'price' => 11 + ]); + + factory(Product::class)->create([ + 'price' => 99 + ]); + + $finder = new ProductSearch(); + $result = $finder->priceBetween(30, 40)->getResults(); + + $this->assertCount(2, $result); + + $prices = $result->pluck('price'); + + foreach ($prices as $price) { + $this->assertLessThanOrEqual(40, $price); + $this->assertGreaterThanOrEqual(20, $price); + } + } + + /** @test */ + public function it_can_find_products_below_a_certain_price() + { + factory(Product::class)->create([ + 'price' => 31 + ]); + + factory(Product::class)->create([ + 'price' => 35 + ]); + + factory(Product::class)->create([ + 'price' => 11 + ]); + + factory(Product::class)->create([ + 'price' => 10 + ]); + + factory(Product::class)->create([ + 'price' => 99 + ]); + + $finder = new ProductSearch(); + $result = $finder->priceLessThan(12)->getResults(); + + $this->assertCount(2, $result); + + $prices = $result->pluck('price'); + + foreach ($prices as $price) { + $this->assertLessThanOrEqual(12, $price); + } + } + + /** @test */ + public function it_can_find_products_above_a_certain_price() + { + factory(Product::class)->create([ + 'price' => 31 + ]); + + factory(Product::class)->create([ + 'price' => 35 + ]); + + factory(Product::class)->create([ + 'price' => 11 + ]); + + factory(Product::class)->create([ + 'price' => 10 + ]); + + factory(Product::class)->create([ + 'price' => 99 + ]); + + $finder = new ProductSearch(); + $result = $finder->priceGreaterThan(35)->getResults(); + + $this->assertCount(2, $result); + + $prices = $result->pluck('price'); + + foreach ($prices as $price) { + $this->assertGreaterThanOrEqual(35, $price); + } + } }