|
| 1 | +<?php |
| 2 | + |
| 3 | +use App\Actions\PingHostname; |
| 4 | +use App\Enums\ResultStatus; |
| 5 | +use App\Events\SpeedtestFailed; |
| 6 | +use App\Jobs\CheckForInternetConnectionJob; |
| 7 | +use App\Models\Result; |
| 8 | +use Illuminate\Support\Facades\Event; |
| 9 | +use Illuminate\Support\Facades\Http; |
| 10 | +use Spatie\Ping\PingResult; |
| 11 | + |
| 12 | +beforeEach(function () { |
| 13 | + Event::fake(); |
| 14 | +}); |
| 15 | + |
| 16 | +describe('CheckForInternetConnectionJob', function () { |
| 17 | + test('batch continues when ping succeeds', function () { |
| 18 | + $result = Result::factory()->create(['status' => ResultStatus::Started]); |
| 19 | + |
| 20 | + $successfulPing = PingResult::fromArray(['success' => true, 'host' => 'icanhazip.com']); |
| 21 | + app()->bind(PingHostname::class, fn () => new class($successfulPing) |
| 22 | + { |
| 23 | + public function __construct(private PingResult $ping) {} |
| 24 | + |
| 25 | + public function handle(?string $hostname = null, int $count = 1): ?PingResult |
| 26 | + { |
| 27 | + return $this->ping; |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + [$job, $batch] = (new CheckForInternetConnectionJob($result))->withFakeBatch(); |
| 32 | + $job->handle(); |
| 33 | + |
| 34 | + $this->assertFalse($batch->cancelled()); |
| 35 | + $result->refresh(); |
| 36 | + expect($result->status)->toBe(ResultStatus::Checking); |
| 37 | + Event::assertNotDispatched(SpeedtestFailed::class); |
| 38 | + }); |
| 39 | + |
| 40 | + test('batch continues when ping fails but HTTP fallback succeeds', function () { |
| 41 | + $result = Result::factory()->create(['status' => ResultStatus::Started]); |
| 42 | + |
| 43 | + $failedPing = PingResult::fromArray([ |
| 44 | + 'success' => false, |
| 45 | + 'error' => 'hostUnreachable', |
| 46 | + 'host' => 'icanhazip.com', |
| 47 | + ]); |
| 48 | + app()->bind(PingHostname::class, fn () => new class($failedPing) |
| 49 | + { |
| 50 | + public function __construct(private PingResult $ping) {} |
| 51 | + |
| 52 | + public function handle(?string $hostname = null, int $count = 1): ?PingResult |
| 53 | + { |
| 54 | + return $this->ping; |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + Http::fake([ |
| 59 | + '*' => Http::response('1.2.3.4', 200), |
| 60 | + ]); |
| 61 | + |
| 62 | + [$job, $batch] = (new CheckForInternetConnectionJob($result))->withFakeBatch(); |
| 63 | + $job->handle(); |
| 64 | + |
| 65 | + $this->assertFalse($batch->cancelled()); |
| 66 | + $result->refresh(); |
| 67 | + expect($result->status)->toBe(ResultStatus::Checking); |
| 68 | + Event::assertNotDispatched(SpeedtestFailed::class); |
| 69 | + }); |
| 70 | + |
| 71 | + test('batch continues when ping is unavailable but HTTP fallback succeeds', function () { |
| 72 | + $result = Result::factory()->create(['status' => ResultStatus::Started]); |
| 73 | + |
| 74 | + app()->bind(PingHostname::class, fn () => new class |
| 75 | + { |
| 76 | + public function handle(?string $hostname = null, int $count = 1): ?PingResult |
| 77 | + { |
| 78 | + return null; |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + Http::fake([ |
| 83 | + '*' => Http::response('1.2.3.4', 200), |
| 84 | + ]); |
| 85 | + |
| 86 | + [$job, $batch] = (new CheckForInternetConnectionJob($result))->withFakeBatch(); |
| 87 | + $job->handle(); |
| 88 | + |
| 89 | + $this->assertFalse($batch->cancelled()); |
| 90 | + $result->refresh(); |
| 91 | + expect($result->status)->toBe(ResultStatus::Checking); |
| 92 | + Event::assertNotDispatched(SpeedtestFailed::class); |
| 93 | + }); |
| 94 | + |
| 95 | + test('batch is cancelled when ping fails and HTTP fallback also fails', function () { |
| 96 | + $result = Result::factory()->create(['status' => ResultStatus::Started]); |
| 97 | + |
| 98 | + $failedPing = PingResult::fromArray([ |
| 99 | + 'success' => false, |
| 100 | + 'error' => 'hostUnreachable', |
| 101 | + 'host' => 'icanhazip.com', |
| 102 | + ]); |
| 103 | + app()->bind(PingHostname::class, fn () => new class($failedPing) |
| 104 | + { |
| 105 | + public function __construct(private PingResult $ping) {} |
| 106 | + |
| 107 | + public function handle(?string $hostname = null, int $count = 1): ?PingResult |
| 108 | + { |
| 109 | + return $this->ping; |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + Http::fake([ |
| 114 | + '*' => Http::response('Service Unavailable', 503), |
| 115 | + ]); |
| 116 | + |
| 117 | + [$job, $batch] = (new CheckForInternetConnectionJob($result))->withFakeBatch(); |
| 118 | + $job->handle(); |
| 119 | + |
| 120 | + $this->assertTrue($batch->cancelled()); |
| 121 | + $result->refresh(); |
| 122 | + expect($result->status)->toBe(ResultStatus::Failed); |
| 123 | + expect($result->data['level'])->toBe('error'); |
| 124 | + expect($result->data['message'])->toContain('HTTP fallback also failed'); |
| 125 | + Event::assertDispatched(SpeedtestFailed::class); |
| 126 | + }); |
| 127 | + |
| 128 | + test('batch is cancelled when ping is unavailable and HTTP fallback throws', function () { |
| 129 | + $result = Result::factory()->create(['status' => ResultStatus::Started]); |
| 130 | + |
| 131 | + app()->bind(PingHostname::class, fn () => new class |
| 132 | + { |
| 133 | + public function handle(?string $hostname = null, int $count = 1): ?PingResult |
| 134 | + { |
| 135 | + return null; |
| 136 | + } |
| 137 | + }); |
| 138 | + |
| 139 | + Http::fake([ |
| 140 | + '*' => Http::failedConnection(), |
| 141 | + ]); |
| 142 | + |
| 143 | + [$job, $batch] = (new CheckForInternetConnectionJob($result))->withFakeBatch(); |
| 144 | + $job->handle(); |
| 145 | + |
| 146 | + $this->assertTrue($batch->cancelled()); |
| 147 | + $result->refresh(); |
| 148 | + expect($result->status)->toBe(ResultStatus::Failed); |
| 149 | + expect($result->data['message'])->toBe('Ping command is unavailable and HTTP fallback also failed.'); |
| 150 | + Event::assertDispatched(SpeedtestFailed::class); |
| 151 | + }); |
| 152 | +}); |
0 commit comments