Files
pricebuddy/tests/Unit/Dto/AvailabilityStrategyDtoTest.php
Jeremy 1a857e1582 feat: meta extraction API and typed scrape-strategy DTOs
Add a meta-extraction API endpoint and refactor store scrape strategies onto
strongly-typed DTOs.

- POST /api/meta-extraction endpoint with SSRF protection on the target URL
- Typed scrape-strategy DTOs and enums (StandardStrategyDto, AvailabilityMatchDto,
  AvailabilityMatchType, AvailabilityStrategyDto, StoreScraperStrategySetDto)
  with a model cast for the stores.scrape_strategy column
- Convert ScrapeUrl, StrategyExtractor and availability resolution to consume the
  typed strategy slots; preserve legacy plain-string availability match entries
- Filament scrape_strategy form bridge plus DTO test coverage
2026-06-18 23:13:01 +10:00

99 lines
3.3 KiB
PHP

<?php
namespace Tests\Unit\Dto;
use App\Dto\AvailabilityMatchDto;
use App\Dto\AvailabilityStrategyDto;
use App\Enums\ScraperStrategyType;
use App\Enums\StockStatus;
use PHPUnit\Framework\TestCase;
class AvailabilityStrategyDtoTest extends TestCase
{
public function test_from_array_splits_match_and_default(): void
{
$dto = AvailabilityStrategyDto::fromArray([
'type' => 'selector',
'value' => '#avail',
'match' => [
'out_of_stock' => ['type' => 'match', 'value' => 'Sold out'],
'pre_order' => ['type' => 'regex', 'value' => 'pre.?order'],
'default' => 'in_stock',
],
]);
$this->assertSame(ScraperStrategyType::Selector, $dto->type);
$this->assertSame('#avail', $dto->value);
$this->assertSame(StockStatus::InStock, $dto->defaultStatus);
$this->assertInstanceOf(AvailabilityMatchDto::class, $dto->match['out_of_stock']);
$this->assertSame('Sold out', $dto->match['out_of_stock']->value);
$this->assertArrayNotHasKey('default', $dto->match);
}
public function test_from_array_without_match(): void
{
$dto = AvailabilityStrategyDto::fromArray(['type' => 'selector', 'value' => '#avail']);
$this->assertSame([], $dto->match);
$this->assertSame(StockStatus::InStock, $dto->defaultStatus);
$this->assertNull($dto->matchConfig());
}
public function test_match_config_rebuilds_legacy_array(): void
{
$dto = AvailabilityStrategyDto::fromArray([
'type' => 'selector',
'match' => [
'out_of_stock' => ['type' => 'match', 'value' => 'Sold out'],
'default' => 'in_stock',
],
]);
$this->assertEquals([
'out_of_stock' => ['type' => 'match', 'value' => 'Sold out'],
'default' => 'in_stock',
], $dto->matchConfig());
}
public function test_to_array_round_trips(): void
{
$input = [
'type' => 'selector',
'value' => '#avail',
'match' => [
'out_of_stock' => ['type' => 'match', 'value' => 'Sold out'],
'default' => 'in_stock',
],
];
$this->assertEquals($input, AvailabilityStrategyDto::fromArray($input)->toArray());
}
public function test_to_array_omits_empty_match(): void
{
$this->assertEquals(
['type' => 'selector', 'value' => '#avail'],
AvailabilityStrategyDto::fromArray(['type' => 'selector', 'value' => '#avail'])->toArray(),
);
}
public function test_from_array_normalizes_legacy_plain_string_match_entries(): void
{
$dto = AvailabilityStrategyDto::fromArray([
'type' => 'selector',
'value' => '#avail',
'match' => [
'out_of_stock' => 'Sold out',
'default' => 'in_stock',
],
]);
$this->assertSame(\App\Enums\AvailabilityMatchType::Match, $dto->match['out_of_stock']->type);
$this->assertSame('Sold out', $dto->match['out_of_stock']->value);
$this->assertEquals([
'out_of_stock' => ['type' => 'match', 'value' => 'Sold out'],
'default' => 'in_stock',
], $dto->matchConfig());
}
}