-
-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathDownload.php
More file actions
224 lines (185 loc) · 5.09 KB
/
Download.php
File metadata and controls
224 lines (185 loc) · 5.09 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
declare(strict_types=1);
namespace Pest\Browser\Api;
use Generator;
use Pest\Browser\Exceptions\BrowserExpectationFailedException;
use Pest\Browser\Execution;
use Pest\Browser\Playwright\Client;
use Pest\Browser\Playwright\Page;
use PHPUnit\Framework\ExpectationFailedException;
final class Download
{
/**
* The download URL.
*/
private ?string $url = null;
/**
* The suggested filename for the download.
*/
private ?string $suggestedFilename = null;
/**
* The Playwright artifact GUID.
*/
private ?string $artifactGuid = null;
/**
* Creates a new download instance.
*/
public function __construct(
private readonly Page $page,
) {}
/**
* Resolves the download with the given details.
*
* @internal This method is called by the Client when a download event is received.
*/
public function resolve(string $url, string $suggestedFilename, string $artifactGuid): void
{
$this->url = $url;
$this->suggestedFilename = $suggestedFilename;
$this->artifactGuid = $artifactGuid;
}
/**
* Returns the download URL.
*/
public function url(): string
{
$this->ensureResolved();
return (string) $this->url;
}
/**
* Returns the suggested filename for the download.
*/
public function suggestedFilename(): string
{
$this->ensureResolved();
return (string) $this->suggestedFilename;
}
/**
* Saves the download to the given path.
*/
public function saveAs(string $path): self
{
$this->ensureResolved();
iterator_to_array($this->artifact('saveAs', ['path' => $path]));
return $this;
}
/**
* Returns the path where the download was saved.
*/
public function path(): string
{
return $this->artifactValue('pathAfterFinished') ?? '';
}
/**
* Returns the contents of the downloaded file.
*/
public function contents(): string
{
return (string) file_get_contents($this->path());
}
/**
* Returns the failure message, or null if successful.
*/
public function failure(): ?string
{
return $this->artifactValue('failure');
}
/**
* Checks if the download was successful.
*/
public function isSuccessful(): bool
{
return $this->failure() === null;
}
/**
* Assert the download has the expected filename.
*/
public function assertFilename(string $expected): self
{
expect($this->suggestedFilename())->toBe($expected);
return $this;
}
/**
* Assert the download filename contains the expected string.
*/
public function assertFilenameContains(string $expected): self
{
expect($this->suggestedFilename())->toContain($expected);
return $this;
}
/**
* Assert the download URL contains the expected string.
*/
public function assertUrlContains(string $expected): self
{
expect($this->url())->toContain($expected);
return $this;
}
/**
* Assert the download content contains the expected string.
*/
public function assertContentContains(string $expected): self
{
expect($this->contents())->toContain($expected);
return $this;
}
/**
* Assert the download was successful.
*/
public function assertSuccessful(): self
{
expect($this->isSuccessful())->toBeTrue();
return $this;
}
/**
* Assert the download failed.
*/
public function assertFailed(): self
{
expect($this->isSuccessful())->toBeFalse();
return $this;
}
/**
* Executes a method on the download artifact and extracts the result value.
*/
private function artifactValue(string $method): ?string
{
$this->ensureResolved();
foreach ($this->artifact($method) as $message) {
$result = $message['result'] ?? [];
$value = is_array($result) ? ($result['value'] ?? null) : null;
if (is_string($value) || $value === null) {
return $value;
}
}
return null;
}
/**
* Executes a method on the download artifact.
*
* @param array<string, mixed> $params
* @return Generator<array<string, mixed>>
*/
private function artifact(string $method, array $params = []): Generator
{
assert($this->artifactGuid !== null);
return Client::instance()->execute($this->artifactGuid, $method, $params);
}
/**
* Ensures the download has been resolved.
*/
private function ensureResolved(): void
{
if ($this->artifactGuid !== null) {
return;
}
Execution::instance()->waitForExpectation(function (): void {
if ($this->artifactGuid === null) {
throw BrowserExpectationFailedException::from(
$this->page,
new ExpectationFailedException('No download started'),
);
}
});
}
}