laravel-shop/tests/Unit/Product/ProductPricingTest.php

86 lines
2.2 KiB
PHP
Raw Normal View History

2025-11-21 10:49:41 +00:00
<?php
namespace Blax\Shop\Tests\Unit;
use Blax\Shop\Models\Product;
use Blax\Shop\Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
2025-12-24 18:40:10 +00:00
use PHPUnit\Framework\Attributes\Test;
2025-11-21 10:49:41 +00:00
class ProductPricingTest extends TestCase
{
use RefreshDatabase;
2025-12-24 18:40:10 +00:00
#[Test]
2025-11-21 10:49:41 +00:00
public function it_returns_regular_price_when_not_on_sale()
{
2025-11-24 13:32:11 +00:00
$product = Product::factory()->withPrices(2, 100)->create();
2025-11-21 10:49:41 +00:00
2025-11-24 13:32:11 +00:00
$this->assertEquals(2, $product->prices()->count());
$this->assertFalse($product->isOnSale());
$this->assertNotNull($product->defaultPrice()->first());
2025-11-21 10:49:41 +00:00
$this->assertEquals(100, $product->getCurrentPrice());
}
2025-12-24 18:40:10 +00:00
#[Test]
2025-11-21 10:49:41 +00:00
public function it_returns_sale_price_when_on_sale()
{
2025-11-24 13:32:11 +00:00
$product = Product::factory()
2025-11-25 16:02:39 +00:00
->withPrices(1, 100)
->create([
'sale_start' => now()->subDay(),
'sale_end' => now()->addDay(),
]);
$price = $product->prices()->first();
$price->sale_unit_amount = 80;
$price->save();
2025-11-21 10:49:41 +00:00
$this->assertEquals(80, $product->getCurrentPrice());
}
2025-12-24 18:40:10 +00:00
#[Test]
2025-11-21 10:49:41 +00:00
public function it_returns_regular_price_when_sale_has_ended()
{
2025-11-25 16:02:39 +00:00
$product = Product::factory()->withPrices(1, 100)->create([
'sale_start' => now()->subWeek(),
'sale_end' => now()->addHour(),
]);
$price = $product->prices()->first();
2025-12-24 18:40:10 +00:00
$price->sale_unit_amount = 80;
2025-11-25 16:02:39 +00:00
$price->save();
$this->assertEquals(80, $product->getCurrentPrice());
$product->update([
'sale_end' => now()->subHour(),
2025-11-21 10:49:41 +00:00
]);
$this->assertEquals(100, $product->getCurrentPrice());
}
2025-12-24 18:40:10 +00:00
#[Test]
2025-11-21 10:49:41 +00:00
public function it_returns_regular_price_when_sale_hasnt_started()
{
2025-11-25 16:02:39 +00:00
$product = Product::factory()->withPrices(1, 100)->create([
2025-11-21 10:49:41 +00:00
'sale_start' => now()->addDay(),
'sale_end' => now()->addWeek(),
]);
2025-11-25 16:02:39 +00:00
$price = $product->prices()->first();
2025-12-24 18:40:10 +00:00
$price->sale_unit_amount = 80;
2025-11-25 16:02:39 +00:00
$price->save();
2025-11-21 10:49:41 +00:00
$this->assertEquals(100, $product->getCurrentPrice());
2025-11-25 16:02:39 +00:00
$product->update([
'sale_start' => now()->subHour(),
2025-11-21 10:49:41 +00:00
]);
2025-11-25 16:02:39 +00:00
$this->assertEquals(80, $product->getCurrentPrice());
2025-11-21 10:49:41 +00:00
}
}