1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral: PSR7 aware cURL client (https://github.com/juliangut/spiral) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/juliangut/spiral for the canonical source repository |
6
|
|
|
* |
7
|
|
|
* @license https://raw.githubusercontent.com/juliangut/spiral/master/LICENSE |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Jgut\Spiral\Tests\Transport; |
11
|
|
|
|
12
|
|
|
use Jgut\Spiral\Option\OptionFactory; |
13
|
|
|
use Jgut\Spiral\Transport; |
14
|
|
|
use Jgut\Spiral\Transport\Curl; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Curl transport tests. |
18
|
|
|
*/ |
19
|
|
|
class CurlTest extends \PHPUnit_Framework_TestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @expectedException \Jgut\Spiral\Exception\OptionException |
23
|
|
|
*/ |
24
|
|
|
public function testGetterSetter() |
25
|
|
|
{ |
26
|
|
|
$transport = new Curl; |
27
|
|
|
|
28
|
|
|
static::assertFalse($transport->hasOption('fake_option')); |
29
|
|
|
|
30
|
|
|
$options = [CURLOPT_VERBOSE => false]; |
31
|
|
|
|
32
|
|
|
$transport->setOptions($options); |
33
|
|
|
static::assertEquals(1, count($transport->getOptions())); |
34
|
|
|
static::assertTrue($transport->hasOption(CURLOPT_VERBOSE)); |
35
|
|
|
static::assertTrue($transport->hasOption(OptionFactory::build(CURLOPT_VERBOSE, false))); |
36
|
|
|
static::assertEquals( |
37
|
|
|
$options, |
38
|
|
|
[$transport->getOptions()[0]->getOption() => $transport->getOptions()[0]->getValue()] |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
$transport->removeOption('fake_option'); |
42
|
|
|
$transport->removeOption(CURLOPT_VERBOSE); |
43
|
|
|
$transport->removeOption(OptionFactory::build(CURLOPT_VERBOSE, false)); |
44
|
|
|
static::assertFalse($transport->hasOption(CURLOPT_VERBOSE)); |
45
|
|
|
|
46
|
|
|
$transport->setOption('fake_option'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testDefaultCreation() |
50
|
|
|
{ |
51
|
|
|
$transport = Curl::createFromDefaults(); |
52
|
|
|
|
53
|
|
|
static::assertTrue($transport->hasOption(CURLOPT_VERBOSE)); |
54
|
|
|
static::assertTrue($transport->hasOption('timeout')); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @expectedException \Jgut\Spiral\Exception\TransportException |
59
|
|
|
* @expectedExceptionMessageRegExp /^Could( not|n't) resolve host/ |
60
|
|
|
*/ |
61
|
|
|
public function testErrorRequest() |
62
|
|
|
{ |
63
|
|
|
$transport = Curl::createFromDefaults(); |
64
|
|
|
|
65
|
|
|
$transport->request(Transport::METHOD_HEAD, 'http://fake_made_up_web.com'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testForgeAndInfo() |
69
|
|
|
{ |
70
|
|
|
$transport = Curl::createFromDefaults(); |
71
|
|
|
$transport->setOption('user_password', 'user:pass'); |
72
|
|
|
|
73
|
|
|
static::assertNull($transport->responseInfo()); |
74
|
|
|
|
75
|
|
|
$transport->request(Transport::METHOD_GET, 'http://www.php.net', ['Accept-Charset' => 'utf-8']); |
76
|
|
|
|
77
|
|
|
static::assertInternalType('array', $transport->responseInfo()); |
78
|
|
|
static::assertEquals(200, $transport->responseInfo(CURLINFO_HTTP_CODE)); |
79
|
|
|
|
80
|
|
|
$transport->close(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testRequestWithVars() |
84
|
|
|
{ |
85
|
|
|
$transport = Curl::createFromDefaults(); |
86
|
|
|
|
87
|
|
|
$transport->request(Transport::METHOD_GET, 'http://www.php.net', [], ['var' => 'value']); |
88
|
|
|
static::assertEquals(200, $transport->responseInfo(CURLINFO_HTTP_CODE)); |
89
|
|
|
|
90
|
|
|
$transport->request(Transport::METHOD_POST, 'http://www.php.net', [], ['var' => 'value']); |
91
|
|
|
static::assertEquals(200, $transport->responseInfo(CURLINFO_HTTP_CODE)); |
92
|
|
|
|
93
|
|
|
$transport->close(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $method |
98
|
|
|
* @param string $shorthand |
99
|
|
|
* @param int $expectedCode |
100
|
|
|
* |
101
|
|
|
* @dataProvider methodProvider |
102
|
|
|
*/ |
103
|
|
|
public function testRequestMethods($method, $shorthand, $expectedCode) |
104
|
|
|
{ |
105
|
|
|
$transport = Curl::createFromDefaults(); |
106
|
|
|
|
107
|
|
|
$transport->request($method, 'http://www.php.net'); |
108
|
|
|
static::assertEquals($expectedCode, $transport->responseInfo(CURLINFO_HTTP_CODE)); |
109
|
|
|
|
110
|
|
|
call_user_func([$transport, $shorthand], 'http://www.php.net'); |
111
|
|
|
static::assertEquals($expectedCode, $transport->responseInfo(CURLINFO_HTTP_CODE)); |
112
|
|
|
|
113
|
|
|
$transport->close(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Provide HTTP methods. |
118
|
|
|
* |
119
|
|
|
* @return array[] |
|
|
|
|
120
|
|
|
*/ |
121
|
|
|
public function methodProvider() |
122
|
|
|
{ |
123
|
|
|
return [ |
124
|
|
|
[Transport::METHOD_OPTIONS, 'options', 200], |
125
|
|
|
[Transport::METHOD_HEAD, 'head', 200], |
126
|
|
|
[Transport::METHOD_GET, 'get', 200], |
127
|
|
|
[Transport::METHOD_POST, 'post', 200], |
128
|
|
|
[Transport::METHOD_PUT, 'put', 200], |
129
|
|
|
[Transport::METHOD_DELETE, 'delete', 200], |
130
|
|
|
[Transport::METHOD_PATCH, 'patch', 200], |
131
|
|
|
]; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.