-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtilityMethodsTest.php
More file actions
122 lines (101 loc) · 3.56 KB
/
Copy pathUtilityMethodsTest.php
File metadata and controls
122 lines (101 loc) · 3.56 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
<?php
declare(strict_types=1);
namespace CloudLayer\Tests\Unit;
use CloudLayer\Errors\ApiException;
use CloudLayer\Errors\TimeoutException;
use CloudLayer\Errors\ValidationException;
use CloudLayer\Types\Job;
use CloudLayer\Types\JobStatus;
use PHPUnit\Framework\Attributes\Test;
final class UtilityMethodsTest extends TestCase
{
#[Test]
public function waitForJobPollsUntilSuccess(): void
{
$client = $this->createMockClient([
$this->mockJsonResponse(200, $this->loadFixture('job-pending.json')),
$this->mockJsonResponse(200, $this->loadFixture('job.json')),
]);
$job = $client->waitForJob('job-123', [
'interval' => 2000,
'sleepFn' => static function (int $us): void {
},
]);
self::assertSame(JobStatus::Success, $job->status);
}
#[Test]
public function waitForJobReturnsImmediatelyOnSuccess(): void
{
$client = $this->createMockClient([
$this->mockJsonResponse(200, $this->loadFixture('job.json')),
]);
$job = $client->waitForJob('job-123', [
'sleepFn' => static function (int $us): void {
},
]);
self::assertSame(JobStatus::Success, $job->status);
}
#[Test]
public function waitForJobThrowsOnError(): void
{
$client = $this->createMockClient([
$this->mockJsonResponse(200, $this->loadFixture('job-error.json')),
]);
$this->expectException(ApiException::class);
$this->expectExceptionMessage('Conversion failed');
$client->waitForJob('job-123', [
'sleepFn' => static function (int $us): void {
},
]);
}
#[Test]
public function waitForJobThrowsTimeoutException(): void
{
$pending = $this->loadFixture('job-pending.json');
$client = $this->createMockClient([
$this->mockJsonResponse(200, $pending),
$this->mockJsonResponse(200, $pending),
$this->mockJsonResponse(200, $pending),
]);
$this->expectException(TimeoutException::class);
$client->waitForJob('job-123', [
'interval' => 2000,
'maxWait' => 3000,
'sleepFn' => static function (int $us): void {
},
]);
}
#[Test]
public function waitForJobIntervalTooLowThrows(): void
{
$client = $this->createMockClient([]);
$this->expectException(ValidationException::class);
$this->expectExceptionMessage('interval');
$client->waitForJob('job-123', ['interval' => 500]);
}
#[Test]
public function waitForJobDefaultIntervalIs5000(): void
{
$sleepCalls = [];
$client = $this->createMockClient([
$this->mockJsonResponse(200, $this->loadFixture('job-pending.json')),
$this->mockJsonResponse(200, $this->loadFixture('job.json')),
]);
$client->waitForJob('job-123', [
'sleepFn' => static function (int $us) use (&$sleepCalls): void {
$sleepCalls[] = $us;
},
]);
self::assertCount(1, $sleepCalls);
self::assertSame(5_000_000, $sleepCalls[0]); // 5000ms in microseconds
}
#[Test]
public function downloadJobResultThrowsOnNullAssetUrl(): void
{
$client = $this->createMockClient([]);
$job = new Job(id: 'job-1', uid: 'u', status: JobStatus::Pending, timestamp: 0);
$this->expectException(ValidationException::class);
$this->expectExceptionMessage('assetUrl');
$client->downloadJobResult($job);
}
}