Skip to content

Commit a0e7a97

Browse files
committedApr 19, 2024·
unit tests
1 parent c8c1cc2 commit a0e7a97

File tree

1 file changed

+225
-0
lines changed

1 file changed

+225
-0
lines changed
 

‎tests/ClientTest.php

+225
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests\VercelBlobPhp;
44

5+
use DateTime;
56
use Generator;
67
use GuzzleHttp\Exception\ClientException;
78
use PHPUnit\Framework\Attributes\DataProvider;
@@ -10,6 +11,8 @@
1011
use Psr\Http\Message\ResponseInterface;
1112
use Psr\Http\Message\StreamInterface;
1213
use VercelBlobPhp\Client;
14+
use VercelBlobPhp\CommonCreateBlobOptions;
15+
use VercelBlobPhp\CopyBlobResult;
1316
use VercelBlobPhp\Exception\BlobAccessException;
1417
use VercelBlobPhp\Exception\BlobException;
1518
use VercelBlobPhp\Exception\BlobNotFoundException;
@@ -18,9 +21,16 @@
1821
use VercelBlobPhp\Exception\BlobStoreNotFoundException;
1922
use VercelBlobPhp\Exception\BlobStoreSuspendedException;
2023
use VercelBlobPhp\Exception\BlobUnknownException;
24+
use VercelBlobPhp\HeadBlobResult;
25+
use VercelBlobPhp\PutBlobResult;
2126

2227
class ClientTest extends TestCase
2328
{
29+
protected function setUp(): void
30+
{
31+
putenv("VERCEL_BLOB_API_URL=blob");
32+
}
33+
2434
public static function requestThrowsCorrectExceptionBasedOnErrorCode(): Generator
2535
{
2636
yield [
@@ -124,4 +134,219 @@ public function testRequest(): void
124134

125135
$this->assertEquals($responseMock, $response);
126136
}
137+
138+
public static function putDataProvider(): Generator
139+
{
140+
yield [
141+
null,
142+
[]
143+
];
144+
145+
yield [
146+
new CommonCreateBlobOptions(addRandomSuffix: true),
147+
[
148+
'x-random-suffix' => true
149+
]
150+
];
151+
152+
yield [
153+
new CommonCreateBlobOptions(contentType: 'application/json'),
154+
[
155+
'x-content-type' => 'application/json'
156+
]
157+
];
158+
159+
yield [
160+
new CommonCreateBlobOptions(cacheControlMaxAge: 123),
161+
[
162+
'x-cache-control-max-age' => 123
163+
]
164+
];
165+
}
166+
167+
#[DataProvider('putDataProvider')]
168+
public function testPut(?CommonCreateBlobOptions $options, array $expectedHeaders): void
169+
{
170+
$sut = new Client('my-token');
171+
172+
$sut->setClient(
173+
$this->mockClient(
174+
'blob/hello-world.txt',
175+
'PUT',
176+
[
177+
'body' => 'hello world',
178+
'headers' => $expectedHeaders,
179+
],
180+
[
181+
'url' => 'url',
182+
'downloadUrl' => 'downloadUrl',
183+
'pathname' => 'pathname',
184+
'contentType' => 'contentType',
185+
'contentDisposition' => 'contentDisposition',
186+
]
187+
)
188+
);
189+
190+
$this->assertEquals(
191+
new PutBlobResult(
192+
'url',
193+
'downloadUrl',
194+
'pathname',
195+
'contentType',
196+
'contentDisposition'
197+
),
198+
$sut->put('hello-world.txt', 'hello world', $options)
199+
);
200+
}
201+
202+
public static function copyDataProvider(): Generator
203+
{
204+
yield [
205+
null,
206+
[]
207+
];
208+
209+
yield [
210+
new CommonCreateBlobOptions(addRandomSuffix: true),
211+
[
212+
'x-random-suffix' => true
213+
]
214+
];
215+
216+
yield [
217+
new CommonCreateBlobOptions(contentType: 'application/json'),
218+
[
219+
'x-content-type' => 'application/json'
220+
]
221+
];
222+
223+
yield [
224+
new CommonCreateBlobOptions(cacheControlMaxAge: 123),
225+
[
226+
'x-cache-control-max-age' => 123
227+
]
228+
];
229+
}
230+
231+
#[DataProvider('copyDataProvider')]
232+
public function testCopy(?CommonCreateBlobOptions $options, array $expectedHeaders): void
233+
{
234+
$sut = new Client('my-token');
235+
236+
$sut->setClient(
237+
$this->mockClient(
238+
'blob/hello-world.txt?fromUrl=test-url',
239+
'PUT',
240+
[
241+
'headers' => $expectedHeaders,
242+
],
243+
[
244+
'url' => 'url',
245+
'downloadUrl' => 'downloadUrl',
246+
'pathname' => 'pathname',
247+
'contentType' => 'contentType',
248+
'contentDisposition' => 'contentDisposition',
249+
]
250+
)
251+
);
252+
253+
$this->assertEquals(
254+
new CopyBlobResult(
255+
'url',
256+
'downloadUrl',
257+
'pathname',
258+
'contentType',
259+
'contentDisposition'
260+
),
261+
$sut->copy('test-url', 'hello-world.txt', $options)
262+
);
263+
}
264+
265+
public function testDel(): void
266+
{
267+
$sut = new Client('my-token');
268+
269+
$sut->setClient(
270+
$this->mockClient(
271+
'blob/delete',
272+
'POST',
273+
[
274+
'json' => [
275+
'urls' => [
276+
'url1',
277+
'url2',
278+
]
279+
]
280+
],
281+
[]
282+
)
283+
);
284+
285+
$sut->del(['url1', 'url2']);
286+
}
287+
288+
public function testHead(): void
289+
{
290+
$sut = new Client('my-token');
291+
292+
$sut->setClient(
293+
$this->mockClient(
294+
'blob?url=test-url',
295+
'GET',
296+
[],
297+
[
298+
'url' => 'url',
299+
'downloadUrl' => 'downloadUrl',
300+
'size' => 1,
301+
'uploadedAt' => '2024-01-01 10:00:00',
302+
'pathname' => 'pathname',
303+
'contentType' => 'contentType',
304+
'contentDisposition' => 'contentDisposition',
305+
'cacheControl' => 'cacheControl'
306+
]
307+
)
308+
);
309+
310+
$this->assertEquals(
311+
new HeadBlobResult(
312+
'url',
313+
'downloadUrl',
314+
1,
315+
new DateTime('2024-01-01 10:00:00'),
316+
'pathname',
317+
'contentType',
318+
'contentDisposition',
319+
'cacheControl'
320+
),
321+
$sut->head('test-url')
322+
);
323+
}
324+
325+
private function mockClient(
326+
string $url,
327+
string $method,
328+
array $options,
329+
array $response
330+
) {
331+
$clientMock = $this->createMock(\GuzzleHttp\Client::class);
332+
333+
$responseMock = $this->createMock(ResponseInterface::class);
334+
335+
$bodyMock = $this->createMock(StreamInterface::class);
336+
$bodyMock
337+
->method('getContents')
338+
->willReturn(json_encode($response));
339+
340+
$responseMock
341+
->method('getBody')
342+
->willReturn($bodyMock);
343+
344+
$clientMock
345+
->expects(self::once())
346+
->method('request')
347+
->with($method, $url, $options)
348+
->willReturn($responseMock);
349+
350+
return $clientMock;
351+
}
127352
}

0 commit comments

Comments
 (0)
Please sign in to comment.