Files
pricebuddy/tests/Feature/Models/StoreScrapeStrategyCastTest.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

54 lines
1.8 KiB
PHP

<?php
namespace Tests\Feature\Models;
use App\Dto\StoreScraperStrategySetDto;
use App\Models\Store;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class StoreScrapeStrategyCastTest extends TestCase
{
use RefreshDatabase;
public function test_returns_a_strategy_set_dto_from_the_scrape_strategy_attribute(): void
{
$store = Store::factory()->create([
'scrape_strategy' => [
'title' => ['type' => 'selector', 'value' => 'h1'],
'price' => ['type' => 'selector', 'value' => '.price'],
],
]);
$store->refresh();
$this->assertInstanceOf(StoreScraperStrategySetDto::class, $store->scrape_strategy);
$this->assertSame('h1', $store->scrape_strategy->title->value);
$this->assertSame('.price', $store->scrape_strategy->price->value);
}
public function test_serializes_back_to_the_original_nested_array_shape(): void
{
$strategy = [
'title' => ['type' => 'selector', 'value' => 'title'],
'price' => ['type' => 'selector', 'value' => '.a-price > .a-offscreen'],
'image' => ['type' => 'regex', 'value' => '~"hiRes":"(.+?)"~'],
];
$store = Store::factory()->create(['scrape_strategy' => $strategy]);
$this->assertEquals($strategy, $store->toArray()['scrape_strategy']);
}
public function test_stores_the_canonical_json_in_the_database_column(): void
{
$store = Store::factory()->create([
'scrape_strategy' => ['title' => ['type' => 'selector', 'value' => 'h1']],
]);
$raw = json_decode($store->getRawOriginal('scrape_strategy'), true);
$this->assertEquals(['title' => ['type' => 'selector', 'value' => 'h1']], $raw);
}
}