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