|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace StatamicRadPack\Shopify\Tests\Unit; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\Event; |
| 6 | +use Illuminate\Support\Facades\Log; |
| 7 | +use PHPUnit\Framework\Attributes\Test; |
| 8 | +use RuntimeException; |
| 9 | +use StatamicRadPack\Shopify\Events\ProductImportFailed; |
| 10 | +use StatamicRadPack\Shopify\Jobs\ImportSingleProductJob; |
| 11 | +use StatamicRadPack\Shopify\Tests\TestCase; |
| 12 | + |
| 13 | +class ProductImportFailedTest extends TestCase |
| 14 | +{ |
| 15 | + #[Test] |
| 16 | + public function logs_error_on_failure() |
| 17 | + { |
| 18 | + Log::shouldReceive('error') |
| 19 | + ->once() |
| 20 | + ->withArgs(function (string $message, array $context) { |
| 21 | + return str_contains($message, '12345') |
| 22 | + && $context['product_id'] === 12345; |
| 23 | + }); |
| 24 | + |
| 25 | + $job = new ImportSingleProductJob(12345); |
| 26 | + $job->failed(new RuntimeException('Something went wrong')); |
| 27 | + } |
| 28 | + |
| 29 | + #[Test] |
| 30 | + public function includes_store_handle_in_log_context_when_present() |
| 31 | + { |
| 32 | + Log::shouldReceive('error') |
| 33 | + ->once() |
| 34 | + ->withArgs(function (string $message, array $context) { |
| 35 | + return $context['product_id'] === 12345 |
| 36 | + && $context['store'] === 'uk'; |
| 37 | + }); |
| 38 | + |
| 39 | + $job = new ImportSingleProductJob(12345, [], 'uk'); |
| 40 | + $job->failed(new RuntimeException('Something went wrong')); |
| 41 | + } |
| 42 | + |
| 43 | + #[Test] |
| 44 | + public function omits_store_from_log_context_in_single_store_mode() |
| 45 | + { |
| 46 | + Log::shouldReceive('error') |
| 47 | + ->once() |
| 48 | + ->withArgs(function (string $message, array $context) { |
| 49 | + return ! array_key_exists('store', $context); |
| 50 | + }); |
| 51 | + |
| 52 | + $job = new ImportSingleProductJob(12345); |
| 53 | + $job->failed(new RuntimeException('Something went wrong')); |
| 54 | + } |
| 55 | + |
| 56 | + #[Test] |
| 57 | + public function fires_product_import_failed_event() |
| 58 | + { |
| 59 | + Event::fake(); |
| 60 | + Log::shouldReceive('error')->once(); |
| 61 | + |
| 62 | + $exception = new RuntimeException('Something went wrong'); |
| 63 | + |
| 64 | + $job = new ImportSingleProductJob(12345); |
| 65 | + $job->failed($exception); |
| 66 | + |
| 67 | + Event::assertDispatched(ProductImportFailed::class, function ($event) use ($exception) { |
| 68 | + return $event->productId === 12345 |
| 69 | + && $event->storeHandle === null |
| 70 | + && $event->exception === $exception; |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + #[Test] |
| 75 | + public function event_carries_store_handle_in_multi_store_mode() |
| 76 | + { |
| 77 | + Event::fake(); |
| 78 | + Log::shouldReceive('error')->once(); |
| 79 | + |
| 80 | + $job = new ImportSingleProductJob(99, [], 'uk'); |
| 81 | + $job->failed(new RuntimeException('Timeout')); |
| 82 | + |
| 83 | + Event::assertDispatched(ProductImportFailed::class, function ($event) { |
| 84 | + return $event->productId === 99 |
| 85 | + && $event->storeHandle === 'uk'; |
| 86 | + }); |
| 87 | + } |
| 88 | +} |
0 commit comments