Files
boursecu_server/tests/Unit/ProductDisplayPriceServiceTest.php
T
2026-06-27 11:29:09 +03:30

159 lines
5.6 KiB
PHP

<?php
namespace Tests\Unit;
use App\Models\Product;
use App\Services\ConfigService;
use App\Services\ProductDisplayPriceService;
use App\Services\ZaarboxPriceService;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Tests\TestCase;
class ProductDisplayPriceServiceTest extends TestCase
{
protected function tearDown(): void
{
Cache::flush();
parent::tearDown();
}
public function test_reference_product_gets_live_price_from_fetched_feed(): void
{
$this->mock(ConfigService::class, function ($mock) {
$mock->shouldReceive('isStore')->andReturn(false);
});
$row = [
'key' => 'tala18',
'name' => 'گرم ۱۸ tala.ir',
'price' => 165_000_000,
'profit' => 58_400,
];
$feed = [$row];
$this->mock(ZaarboxPriceService::class, function ($mock) use ($feed, $row) {
$mock->shouldReceive('cachedPriceList')->with(false)->once()->andReturn($feed);
$mock->shouldReceive('findFeedRow')->with($feed, 'tala18')->andReturn($row);
$mock->shouldReceive('resolveReferenceProviderTotal')->with($row)->andReturn(165_058_400);
$mock->shouldReceive('computeAdjustedProductPrice')->andReturn(165_058_400);
});
$product = new Product([
'id' => 13,
'reading_method' => 'tala18',
'use_last_price_if_zero' => false,
'price' => 0,
'active' => true,
]);
app(ProductDisplayPriceService::class)->enrichForApi(collect([$product]));
$this->assertSame(165_058_400, (int) $product->price);
$this->assertTrue($product->getAttribute('reference_provider_active'));
}
public function test_reference_product_with_zero_provider_and_toggle_off_stays_unpriced(): void
{
$this->mock(ConfigService::class, function ($mock) {
$mock->shouldReceive('isStore')->andReturn(false);
});
$row = [
'key' => 'tala18',
'name' => 'گرم ۱۸ tala.ir',
'price' => 0,
'profit' => 58_400,
];
$feed = [$row];
$this->mock(ZaarboxPriceService::class, function ($mock) use ($feed, $row) {
$mock->shouldReceive('cachedPriceList')->with(false)->once()->andReturn($feed);
$mock->shouldReceive('findFeedRow')->with($feed, 'tala18')->andReturn($row);
$mock->shouldReceive('resolveReferenceProviderTotal')->with($row)->andReturn(0);
});
$product = new Product([
'id' => 13,
'reading_method' => 'tala18',
'use_last_price_if_zero' => false,
'price' => 99_000_000,
'active' => true,
]);
$service = app(ProductDisplayPriceService::class);
$service->enrichForApi(collect([$product]));
$this->assertSame(0, (int) $product->price);
$this->assertFalse($product->getAttribute('reference_provider_active'));
$this->assertFalse($service->resolveCustomerShow($product));
}
public function test_reference_product_with_zero_provider_and_toggle_on_uses_stored_fallback(): void
{
if (! Product::hasUseLastPriceIfZeroColumn()) {
$this->markTestSkipped('use_last_price_if_zero column not present');
}
$this->mock(ConfigService::class, function ($mock) {
$mock->shouldReceive('isStore')->andReturn(false);
$mock->shouldReceive('isProductWithinTradingHours')->andReturn(true);
$mock->shouldReceive('getBool')->with('show_last_price', false)->andReturn(false);
});
$row = [
'key' => 'tala18',
'name' => 'گرم ۱۸ tala.ir',
'price' => 0,
'profit' => 0,
];
$feed = [$row];
$this->mock(ZaarboxPriceService::class, function ($mock) use ($feed, $row) {
$mock->shouldReceive('cachedPriceList')->with(false)->once()->andReturn($feed);
$mock->shouldReceive('findFeedRow')->with($feed, 'tala18')->andReturn($row);
$mock->shouldReceive('resolveReferenceProviderTotal')->with($row)->andReturn(0);
$mock->shouldReceive('computeAdjustedProductPrice')->andReturn(0);
});
$product = new Product([
'id' => 13,
'reading_method' => 'tala18',
'use_last_price_if_zero' => true,
'price' => 150_000_000,
'active' => true,
]);
$service = app(ProductDisplayPriceService::class);
$service->enrichForApi(collect([$product]));
$this->assertSame(150_000_000, (int) $product->price);
$this->assertTrue($product->getAttribute('reference_provider_active'));
$this->assertTrue($service->resolveCustomerShow($product));
}
public function test_missing_feed_with_toggle_off_zeros_reference_product(): void
{
$this->mock(ConfigService::class, function ($mock) {
$mock->shouldReceive('isStore')->andReturn(false);
});
$this->mock(ZaarboxPriceService::class, function ($mock) {
$mock->shouldReceive('cachedPriceList')->with(false)->once()->andReturn(null);
});
$product = new Product([
'id' => 13,
'reading_method' => 'tala18',
'use_last_price_if_zero' => false,
'price' => 120_000_000,
'active' => true,
]);
app(ProductDisplayPriceService::class)->enrichForApi(collect([$product]));
$this->assertSame(0, (int) $product->price);
$this->assertFalse($product->getAttribute('reference_provider_active'));
}
}