Skip to content

Commit

Permalink
Merge pull request #176 from mahdirezaei-dev/master
Browse files Browse the repository at this point in the history
Add price based search filter
  • Loading branch information
fulopattila122 committed Apr 30, 2024
2 parents 78c2ad7 + 31ea479 commit 84aee30
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Foundation/Search/ProductSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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%");
Expand Down
102 changes: 102 additions & 0 deletions src/Foundation/Tests/ProductSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit 84aee30

Please sign in to comment.