Files
pricebuddy/tests/Feature/Jobs/RetryUrlPriceJobTest.php
Jeremy dc88f7b037 test: fix settings save isolation and updatePrices return assertions
- Forget the AppSettings singleton in TestCase::setUp so each test loads
  settings fresh from the migrated DB; a singleton loaded before the new
  settings rows exist marks them default-loaded, making AppSettings::save()
  throw MissingSettings (exposed by the new required scrape_retry settings).
- Align the retry-settings page test with the existing convention (assert no
  form errors on the fields; add a min-value validation test) instead of a
  full-form persistence assertion the page's unrelated required fields block.
- Update UrlTest for the new Collection return type of Product::updatePrices().

harden: RetryUrlPriceJob tries/missing-models/notification throttle

Address final code review findings:
- Set $tries=1 so queue-level retries don't duplicate the explicit retry chain.
- Set $deleteWhenMissingModels=true so a URL/product deleted before the delayed
  job runs is discarded silently rather than failing the job.
- Throttle the exhaustion notification to one per product per retry window, so a
  multi-URL product doesn't send duplicate product-level scrape-fail notices.
- Assert the retry delay is applied in the job tests.
2026-06-10 21:58:32 +10:00

130 lines
4.0 KiB
PHP

<?php
namespace Tests\Feature\Jobs;
use App\Jobs\RetryUrlPriceJob;
use App\Models\Price;
use App\Models\Product;
use App\Models\Url;
use App\Models\User;
use App\Notifications\ScrapeFailNotification;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Queue;
use Mockery;
use Tests\TestCase;
class RetryUrlPriceJobTest extends TestCase
{
use RefreshDatabase;
/**
* Build a partial-mocked Url whose product relation is a partial-mocked
* Product, so we can drive updatePrice() outcomes without real scraping.
*
* @return array{0: Url, 1: Product}
*/
private function makeUrlAndProduct(bool $paused = false): array
{
$product = Mockery::mock(Product::class)->makePartial();
$product->paused = $paused;
$product->shouldReceive('updatePriceCache')->andReturnNull();
$url = Mockery::mock(Url::class)->makePartial();
$url->setRelation('product', $product);
return [$url, $product];
}
public function test_reschedules_next_attempt_when_still_failing(): void
{
Queue::fake();
Notification::fake();
[$url] = $this->makeUrlAndProduct();
$url->shouldReceive('updatePrice')->once()->andReturnNull();
(new RetryUrlPriceJob($url, 2))->handle();
Queue::assertPushed(
RetryUrlPriceJob::class,
fn (RetryUrlPriceJob $job) => $job->attempt === 3 && $job->url === $url && $job->delay !== null
);
Notification::assertNothingSent();
}
public function test_notifies_and_stops_when_attempts_exhausted(): void
{
Queue::fake();
Notification::fake();
$user = User::factory()->create();
[$url, $product] = $this->makeUrlAndProduct();
$product->setRelation('user', $user);
$url->shouldReceive('updatePrice')->once()->andReturnNull();
// Default max attempts is 3, so attempt 3 is the final one.
(new RetryUrlPriceJob($url, 3))->handle();
Notification::assertSentTo($user, ScrapeFailNotification::class);
Queue::assertNotPushed(RetryUrlPriceJob::class);
}
public function test_stops_silently_on_success(): void
{
Queue::fake();
Notification::fake();
[$url] = $this->makeUrlAndProduct();
$url->shouldReceive('updatePrice')->once()->andReturn(new Price);
(new RetryUrlPriceJob($url, 2))->handle();
Queue::assertNotPushed(RetryUrlPriceJob::class);
Notification::assertNothingSent();
}
public function test_exhaustion_notifies_once_per_product_across_urls(): void
{
Queue::fake();
Notification::fake();
$user = User::factory()->create();
$product = Mockery::mock(Product::class)->makePartial();
$product->id = 1;
$product->paused = false;
$product->shouldReceive('updatePriceCache')->andReturnNull();
$product->setRelation('user', $user);
$urlA = Mockery::mock(Url::class)->makePartial();
$urlA->setRelation('product', $product);
$urlA->shouldReceive('updatePrice')->once()->andReturnNull();
$urlB = Mockery::mock(Url::class)->makePartial();
$urlB->setRelation('product', $product);
$urlB->shouldReceive('updatePrice')->once()->andReturnNull();
// Both URLs of the same product exhaust their retries (attempt 3 of 3).
(new RetryUrlPriceJob($urlA, 3))->handle();
(new RetryUrlPriceJob($urlB, 3))->handle();
// Only one product-level notification should be sent.
Notification::assertSentToTimes($user, ScrapeFailNotification::class, 1);
}
public function test_aborts_when_product_is_paused(): void
{
Queue::fake();
Notification::fake();
[$url] = $this->makeUrlAndProduct(paused: true);
$url->shouldReceive('updatePrice')->never();
(new RetryUrlPriceJob($url, 2))->handle();
Queue::assertNotPushed(RetryUrlPriceJob::class);
Notification::assertNothingSent();
}
}