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

86 lines
2.3 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()
{
2026-01-05 09:30:21 +00:00
$product = Product::factory()->withPrices(2, 10000)->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());
2026-01-05 09:30:21 +00:00
$this->assertEquals(10000, $product->getCurrentPrice());
2025-11-21 10:49:41 +00:00
}
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()
2026-01-05 09:30:21 +00:00
->withPrices(1, 10000)
2025-11-25 16:02:39 +00:00
->create([
'sale_start' => now()->subDay(),
'sale_end' => now()->addDay(),
]);
$price = $product->prices()->first();
2026-01-05 09:30:21 +00:00
$price->sale_unit_amount = 8000;
2025-11-25 16:02:39 +00:00
$price->save();
2025-11-21 10:49:41 +00:00
2026-01-05 09:30:21 +00:00
$this->assertEquals(8000, $product->getCurrentPrice());
2025-11-21 10:49:41 +00:00
}
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()
{
2026-01-05 09:30:21 +00:00
$product = Product::factory()->withPrices(1, 10000)->create([
2025-11-25 16:02:39 +00:00
'sale_start' => now()->subWeek(),
'sale_end' => now()->addHour(),
]);
$price = $product->prices()->first();
2026-01-05 09:30:21 +00:00
$price->sale_unit_amount = 8000;
2025-11-25 16:02:39 +00:00
$price->save();
2026-01-05 09:30:21 +00:00
$this->assertEquals(8000, $product->getCurrentPrice());
2025-11-25 16:02:39 +00:00
$product->update([
'sale_end' => now()->subHour(),
2025-11-21 10:49:41 +00:00
]);
2026-01-05 09:30:21 +00:00
$this->assertEquals(10000, $product->getCurrentPrice());
2025-11-21 10:49:41 +00:00
}
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()
{
2026-01-05 09:30:21 +00:00
$product = Product::factory()->withPrices(1, 10000)->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();
2026-01-05 09:30:21 +00:00
$price->sale_unit_amount = 8000;
2025-11-25 16:02:39 +00:00
$price->save();
2026-01-05 09:30:21 +00:00
$this->assertEquals(10000, $product->getCurrentPrice());
2025-11-21 10:49:41 +00:00
2025-11-25 16:02:39 +00:00
$product->update([
'sale_start' => now()->subHour(),
2025-11-21 10:49:41 +00:00
]);
2026-01-05 09:30:21 +00:00
$this->assertEquals(8000, $product->getCurrentPrice());
2025-11-21 10:49:41 +00:00
}
}