Skip to content

Commit

Permalink
Changed the ProductSearch price range methods that amends #176
Browse files Browse the repository at this point in the history
- priceGreaterThan uses the `>` operator instead of `>=`
- priceLessThan uses the `<` operator instead of `<=`
- added the priceGreaterThanOrEqualTo method (`>=`)
- added the priceLessThanOrEqualTo method (`<=`)
  • Loading branch information
fulopattila122 committed Apr 30, 2024
1 parent 1e0670e commit 2404ec2
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/Foundation/Search/ProductSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
50 changes: 47 additions & 3 deletions src/Foundation/Tests/ProductSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand All @@ -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();

}
}

0 comments on commit 2404ec2

Please sign in to comment.