forked from phpro/http-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSizeExtractorTest.php
More file actions
81 lines (70 loc) · 2.49 KB
/
SizeExtractorTest.php
File metadata and controls
81 lines (70 loc) · 2.49 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
<?php
declare(strict_types=1);
namespace Phpro\HttpTools\Tests\Unit\Encoding\Binary\Extractor;
use Phpro\HttpTools\Encoding\Binary\Extractor\SizeExtractor;
use Phpro\HttpTools\Test\UseHttpFactories;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
final class SizeExtractorTest extends TestCase
{
use UseHttpFactories;
/**
* @param callable<ResponseInterface> $response
*/
#[DataProvider('provideCases')]
#[Test]
public function it_can_extract_size(callable $response, ?int $expected): void
{
$extractor = new SizeExtractor();
$actual = $extractor($response($this));
self::assertSame($actual, $expected);
}
public static function provideCases()
{
yield 'from-empty-stream-size' => [
static fn (self $testCase) => self::createResponse(),
0,
];
yield 'from-stream-size' => [
static fn (self $testCase) => self::createResponse()
->withBody(self::createStream('12345')),
5,
];
yield 'from-single-content-length' => [
static fn (self $testCase) => self::createResponse()
->withBody($testCase->notSizeableStreamMock())
->withHeader('Content-Length', '500'),
500,
];
yield 'from-multiple-content-length' => [
static fn (self $testCase) => self::createResponse()
->withBody($testCase->notSizeableStreamMock())
->withHeader('Content-Length', [
'500',
'600',
]),
500,
];
yield 'from-invalid-content-length' => [
static fn (self $testCase) => self::createResponse()
->withBody($testCase->notSizeableStreamMock())
->withHeader('Content-Length', 'thisisnotanint'),
null,
];
yield 'from-no-info-whatsoever' => [
static fn (self $testCase) => self::createResponse()
->withBody($testCase->notSizeableStreamMock()),
null,
];
}
private function notSizeableStreamMock(): MockObject
{
$notSizeableStream = $this->createMock(StreamInterface::class);
$notSizeableStream->method('getSize')->willReturn(null);
return $notSizeableStream;
}
}