mirror of
https://github.com/jez500/pricebuddy.git
synced 2026-11-24 19:58:13 +01:00
100 lines
3.0 KiB
PHP
100 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Jobs;
|
|
|
|
use App\Jobs\UpdateProductPricesJob;
|
|
use App\Models\Product;
|
|
use App\Models\User;
|
|
use App\Notifications\ScrapeFailNotification;
|
|
use App\Settings\AppSettings;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Notification;
|
|
use Illuminate\Support\Sleep;
|
|
use Tests\TestCase;
|
|
|
|
class UpdateProductPricesJobTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
Http::fake();
|
|
Sleep::fake();
|
|
}
|
|
|
|
public function test_job_updates_product_prices()
|
|
{
|
|
$user = User::factory()->create();
|
|
$dbProduct = Product::factory()->create(['user_id' => $user->id])->refresh();
|
|
$expectedSleep = AppSettings::new()->sleep_seconds_between_scrape;
|
|
|
|
// Mock the updatePrices method to return true
|
|
$product = $this->partialMock(Product::class, function ($mock) {
|
|
$mock->shouldReceive('updatePrices')->once()->andReturn(true);
|
|
});
|
|
$product->id = 1;
|
|
$product->title = 'Test Product';
|
|
$product->user_id = $user->id;
|
|
|
|
$job = new UpdateProductPricesJob($product, false);
|
|
$job->handle();
|
|
|
|
$this->assertTrue(true); // Job executed without error
|
|
|
|
// Test sleep between scrape.
|
|
$this->assertGreaterThan(0, $expectedSleep);
|
|
Sleep::assertSequence([
|
|
Sleep::for($expectedSleep)->seconds(),
|
|
]);
|
|
}
|
|
|
|
public function test_job_logs_when_logging_enabled()
|
|
{
|
|
$user = User::factory()->create();
|
|
$product = Product::factory()->create(['user_id' => $user->id, 'title' => 'Test Product']);
|
|
|
|
$job = new UpdateProductPricesJob($product, true);
|
|
$job->handle();
|
|
|
|
$this->assertTrue(true); // Job executed with logging
|
|
}
|
|
|
|
public function test_job_sends_notification_on_failure()
|
|
{
|
|
Notification::fake();
|
|
|
|
$user = User::factory()->create();
|
|
$product = Product::factory()->create(['user_id' => $user->id]);
|
|
|
|
// Mock updatePrices to return false (failure)
|
|
$product = $this->partialMock(Product::class, function ($mock) use ($user) {
|
|
$mock->shouldReceive('updatePrices')->once()->andReturn(false);
|
|
$mock->shouldReceive('getAttribute')->with('user')->andReturn($user);
|
|
$mock->shouldReceive('setAttribute')->andReturnSelf();
|
|
$mock->id = 1;
|
|
$mock->title = 'Test Product';
|
|
$mock->user_id = $user->id;
|
|
});
|
|
|
|
$job = new UpdateProductPricesJob($product, false);
|
|
$job->handle();
|
|
|
|
Notification::assertSentTo($user, ScrapeFailNotification::class);
|
|
}
|
|
|
|
public function test_job_does_not_send_notification_on_success()
|
|
{
|
|
Notification::fake();
|
|
|
|
$user = User::factory()->create();
|
|
$product = Product::factory()->create(['user_id' => $user->id]);
|
|
|
|
$job = new UpdateProductPricesJob($product, false);
|
|
$job->handle();
|
|
|
|
Notification::assertNothingSent();
|
|
}
|
|
}
|