forked from phpro/http-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryFileDecoderTest.php
More file actions
70 lines (62 loc) · 2.34 KB
/
BinaryFileDecoderTest.php
File metadata and controls
70 lines (62 loc) · 2.34 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
<?php
declare(strict_types=1);
namespace Phpro\HttpTools\Tests\Unit\Encoding\Binary;
use Phpro\HttpTools\Encoding\Binary\BinaryFile;
use Phpro\HttpTools\Encoding\Binary\BinaryFileDecoder;
use Phpro\HttpTools\Test\UseHttpFactories;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
final class BinaryFileDecoderTest extends TestCase
{
use UseHttpFactories;
#[DataProvider('provideCases')]
#[Test]
public function it_can_decode_binary_files(
BinaryFileDecoder $decoder,
ResponseInterface $response,
BinaryFile $expected,
): void {
$actual = $decoder($response);
self::assertSame($actual->stream(), $expected->stream());
self::assertSame($actual->fileSizeInBytes(), $expected->fileSizeInBytes());
self::assertSame($actual->mimeType(), $expected->mimeType());
self::assertSame($actual->fileName(), $expected->fileName());
self::assertSame($actual->extension(), $expected->extension());
}
public static function provideCases(): iterable
{
$defaultDecoder = BinaryFileDecoder::createWithAutodiscoveredPsrFactories();
yield 'from-empty-response' => [
$defaultDecoder,
$response = self::createResponse(),
new BinaryFile(
$response->getBody(), 0, null, null, null, md5('')
),
];
yield 'from-full-response' => [
$defaultDecoder,
$response = self::createResponse()
->withBody(self::createStream('12345'))
->withHeader('Content-Type', 'image/jpeg')
->withHeader('Content-Disposition', 'inline; filename="hello.jpg"'),
new BinaryFile(
$response->getBody(), 5, 'image/jpeg', 'hello.jpg', 'jpg', md5('12345')
),
];
yield 'from-configurable-extractors' => [
new BinaryFileDecoder(
fn () => 5,
fn () => 'image/jpeg',
fn () => 'hello.jpg',
fn () => 'jpg',
fn () => 'md5',
),
$response = self::createResponse(),
new BinaryFile(
$response->getBody(), 5, 'image/jpeg', 'hello.jpg', 'jpg', 'md5'
),
];
}
}