1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Buzz\Test\Unit\Client; |
6
|
|
|
|
7
|
|
|
use Buzz\Client\FileGetContents; |
8
|
|
|
use Buzz\Configuration\ParameterBag; |
9
|
|
|
use Nyholm\Psr7\Request; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
use Psr\Http\Message\RequestInterface; |
12
|
|
|
|
13
|
|
|
class StreamClient extends FileGetContents |
14
|
|
|
{ |
15
|
|
|
public function getStreamContextArray(RequestInterface $request, ParameterBag $options): array |
16
|
|
|
{ |
17
|
|
|
return parent::getStreamContextArray($request, $options); |
18
|
|
|
} |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
class FileGetContentsTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
public function testConvertsARequestToAContextArray() |
24
|
|
|
{ |
25
|
|
|
$request = new Request('POST', 'http://example.com/resource/123', [ |
26
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded', |
27
|
|
|
'Content-Length' => '15', |
28
|
|
|
], 'foo=bar&bar=baz'); |
29
|
|
|
|
30
|
|
|
$client = new StreamClient(); |
31
|
|
|
$expected = [ |
32
|
|
|
'http' => [ |
33
|
|
|
'method' => 'POST', |
34
|
|
|
'header' => "Content-Type: application/x-www-form-urlencoded\r\nContent-Length: 15", |
35
|
|
|
'content' => 'foo=bar&bar=baz', |
36
|
|
|
'protocol_version' => '1.1', |
37
|
|
|
'ignore_errors' => true, |
38
|
|
|
'follow_location' => true, |
39
|
|
|
'max_redirects' => 6, |
40
|
|
|
'timeout' => 10, |
41
|
|
|
], |
42
|
|
|
'ssl' => [ |
43
|
|
|
'verify_peer' => true, |
44
|
|
|
'verify_host' => 2, |
45
|
|
|
], |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
$options = new ParameterBag([ |
49
|
|
|
'max_redirects' => 5, |
50
|
|
|
'timeout' => 10, |
51
|
|
|
'allow_redirects' => true, |
52
|
|
|
'verify' => true, |
53
|
|
|
]); |
54
|
|
|
$this->assertEquals($expected, $client->getStreamContextArray($request, $options)); |
55
|
|
|
|
56
|
|
|
$options = $options->add(['verify' => false]); |
57
|
|
|
$expected['ssl']['verify_peer'] = false; |
58
|
|
|
$expected['ssl']['verify_host'] = false; |
59
|
|
|
$this->assertEquals($expected, $client->getStreamContextArray($request, $options)); |
60
|
|
|
|
61
|
|
|
$options = $options->add(['max_redirects' => 0]); |
62
|
|
|
$expected['http']['follow_location'] = false; |
63
|
|
|
$expected['http']['max_redirects'] = 1; |
64
|
|
|
$this->assertEquals($expected, $client->getStreamContextArray($request, $options)); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|