Files
pricebuddy/tests/Unit/Dto/PriceCacheDtoTest.php
Gordon HeydonandJeremy 837fc267e3 fix: don't flag unavailable URLs as failed scrapes due to stale price rows
The product admin page surfaces a "Scrape error" badge via
`Product::is_last_scrape_successful`, which under the hood checks
`PriceCacheDto::isLastScrapeSuccessful()` — i.e. "was a Price row
created in the last 24 hours". Because `Url::updatePrice()` doesn't
insert a Price row when the scrape returns no price (the correct
behaviour for an out-of-stock product), the `last_scrape` timestamp
for an OOS URL stays frozen at the last in-stock observation and
grows arbitrarily stale, leaving every OOS product permanently
flagged as a scrape failure.

Treats an unavailable URL as fresh in the freshness check. The URL
already shows its actual stock status badge separately, so the user
sees "Out of Stock" without the misleading "Scrape error" alongside.
In-stock URLs still need a price row within 24h to be considered
fresh; the only behavioural change is for URLs whose stock status
is itself unavailable.
2026-06-02 21:09:15 +10:00

251 lines
8.0 KiB
PHP

<?php
namespace Tests\Unit\Dto;
use App\Dto\PriceCacheDto;
use App\Enums\StockStatus;
use App\Enums\Trend;
use Tests\TestCase;
class PriceCacheDtoTest extends TestCase
{
public function test_unit_of_measure_getter()
{
$dto = new PriceCacheDto(
price: 10.00,
locale: 'en',
currency: 'USD',
unitPrice: 5.00,
priceFactor: 2,
unitOfMeasure: 'tablets',
);
$this->assertSame('tablets', $dto->getUnitOfMeasurePlural());
$this->assertSame('tablet', $dto->getUnitOfMeasure());
}
public function test_unit_of_measure_unit_by_default()
{
$dto = new PriceCacheDto(
price: 10.00,
locale: 'en',
currency: 'USD',
unitPrice: 10.00,
);
$this->assertSame(PriceCacheDto::DEFAULT_UNIT_OF_MEASURE, $dto->getUnitOfMeasurePlural());
}
public function test_from_array_includes_unit_of_measure()
{
$dto = PriceCacheDto::fromArray([
'price' => 10.00,
'history' => [],
'unit_of_measure' => 'items',
'locale' => 'en',
'currency' => 'USD',
]);
$this->assertSame('item', $dto->getUnitOfMeasure());
$this->assertSame('items', $dto->getUnitOfMeasurePlural());
}
public function test_to_array_includes_unit_of_measure()
{
$dto = new PriceCacheDto(
price: 10.00,
locale: 'en',
currency: 'USD',
unitOfMeasure: 'grams',
);
$array = $dto->toArray();
$this->assertSame('gram', $array['unit_of_measure']);
}
public function test_from_array_without_unit_of_measure_defaults_to_unit()
{
$dto = PriceCacheDto::fromArray([
'price' => 10.00,
'history' => [],
'locale' => 'en',
'currency' => 'USD',
]);
$this->assertSame('unit', $dto->getUnitOfMeasure());
}
public function test_availability_defaults_to_in_stock()
{
$dto = new PriceCacheDto(price: 10.0, locale: 'en', currency: 'USD');
$this->assertFalse($dto->isUnavailable());
$this->assertSame(StockStatus::InStock, $dto->getStockStatus());
}
public function test_availability_can_be_set_via_constructor()
{
$dto = new PriceCacheDto(price: 10.0, availability: StockStatus::OutOfStock, locale: 'en', currency: 'USD');
$this->assertTrue($dto->isUnavailable());
$this->assertSame(StockStatus::OutOfStock, $dto->getStockStatus());
}
public function test_from_array_includes_availability()
{
$dto = PriceCacheDto::fromArray([
'price' => 10.0,
'history' => [],
'availability' => StockStatus::OutOfStock,
'locale' => 'en',
'currency' => 'USD',
]);
$this->assertTrue($dto->isUnavailable());
$this->assertSame(StockStatus::OutOfStock, $dto->getStockStatus());
}
public function test_from_array_defaults_availability_to_in_stock()
{
$dto = PriceCacheDto::fromArray([
'price' => 10.0,
'history' => [],
'locale' => 'en',
'currency' => 'USD',
]);
$this->assertFalse($dto->isUnavailable());
$this->assertSame(StockStatus::InStock, $dto->getStockStatus());
}
public function test_to_array_includes_availability()
{
$dto = new PriceCacheDto(price: 10.0, availability: StockStatus::OutOfStock, locale: 'en', currency: 'USD');
$array = $dto->toArray();
$this->assertArrayHasKey('availability', $array);
$this->assertSame(StockStatus::OutOfStock->value, $array['availability']);
}
public function test_to_array_availability_null_by_default()
{
$dto = new PriceCacheDto(price: 10.0, locale: 'en', currency: 'USD');
$array = $dto->toArray();
$this->assertArrayHasKey('availability', $array);
$this->assertNull($array['availability']);
}
public function test_from_array_to_array_preserves_availability()
{
$data = [
'price' => 25.0,
'store_id' => 1,
'store_name' => 'Test Store',
'url_id' => 1,
'url' => 'https://example.com',
'trend' => Trend::None->value,
'history' => [10.0, 20.0, 25.0],
'availability' => StockStatus::OutOfStock,
'locale' => 'en',
'currency' => 'USD',
];
$dto = PriceCacheDto::fromArray($data);
$array = $dto->toArray();
$this->assertSame(StockStatus::OutOfStock->value, $array['availability']);
$this->assertSame(25.0, $array['price']);
$this->assertTrue($dto->isUnavailable());
}
public function test_stock_status_convenience_methods()
{
$dto = new PriceCacheDto(price: 10.0, availability: StockStatus::OutOfStock, locale: 'en', currency: 'USD');
$this->assertSame('Out of Stock', $dto->getStockStatusLabel());
$this->assertSame('danger', $dto->getStockStatusColor());
$this->assertSame('heroicon-m-x-circle', $dto->getStockStatusIcon());
}
public function test_stock_status_convenience_methods_for_in_stock()
{
$dto = new PriceCacheDto(price: 10.0, locale: 'en', currency: 'USD');
$this->assertSame('In Stock', $dto->getStockStatusLabel());
$this->assertSame('success', $dto->getStockStatusColor());
$this->assertSame('heroicon-m-check-circle', $dto->getStockStatusIcon());
}
public function test_pre_order_scraped_value_maps_correctly()
{
$dto = new PriceCacheDto(price: 10.0, availability: StockStatus::PreOrder, locale: 'en', currency: 'USD');
$this->assertTrue($dto->isUnavailable());
$this->assertSame(StockStatus::PreOrder, $dto->getStockStatus());
$this->assertSame('Pre-Order', $dto->getStockStatusLabel());
$this->assertSame('info', $dto->getStockStatusColor());
}
public function test_from_array_with_enum_value_string()
{
$dto = PriceCacheDto::fromArray([
'price' => 10.0,
'history' => [],
'availability' => StockStatus::OutOfStock->value,
'locale' => 'en',
'currency' => 'USD',
]);
$this->assertTrue($dto->isUnavailable());
$this->assertSame(StockStatus::OutOfStock, $dto->getStockStatus());
}
public function test_unavailable_url_is_considered_successfully_scraped_despite_stale_price_row()
{
// Regression: OOS URLs don't get new price rows inserted by
// Url::updatePrice, so last_scrape lags arbitrarily far behind.
// The "Scrape error" badge in the product UI is gated on
// isLastScrapeSuccessful(); without this guard an OOS product is
// permanently flagged as a scrape failure.
$dto = PriceCacheDto::fromArray([
'price' => 10.0,
'history' => [],
'availability' => StockStatus::OutOfStock->value,
'last_scrape' => now()->subMonths(3)->toDateTimeString(),
'locale' => 'en',
'currency' => 'USD',
]);
$this->assertTrue($dto->isUnavailable());
$this->assertTrue($dto->isLastScrapeSuccessful());
}
public function test_in_stock_url_with_stale_price_row_is_unsuccessful()
{
$dto = PriceCacheDto::fromArray([
'price' => 10.0,
'history' => [],
'last_scrape' => now()->subDays(2)->toDateTimeString(),
'locale' => 'en',
'currency' => 'USD',
]);
$this->assertFalse($dto->isUnavailable());
$this->assertFalse($dto->isLastScrapeSuccessful());
}
public function test_in_stock_url_with_recent_price_row_is_successful()
{
$dto = PriceCacheDto::fromArray([
'price' => 10.0,
'history' => [],
'last_scrape' => now()->subHours(2)->toDateTimeString(),
'locale' => 'en',
'currency' => 'USD',
]);
$this->assertTrue($dto->isLastScrapeSuccessful());
}
}