1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Palmtree\Curl\Tests; |
4
|
|
|
|
5
|
|
|
use Palmtree\Curl\Curl; |
6
|
|
|
use Palmtree\Curl\Exception\BadMethodCallException; |
7
|
|
|
use Palmtree\Curl\Exception\InvalidArgumentException; |
8
|
|
|
use Palmtree\Curl\Tests\Fixtures\WebServer; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
class CurlTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
/** @var WebServer */ |
14
|
|
|
private $server; |
15
|
|
|
|
16
|
|
|
public function __construct() |
17
|
|
|
{ |
18
|
|
|
$this->server = new WebServer('localhost', __DIR__ . '/fixtures/server'); |
19
|
|
|
$this->server->start(); |
20
|
|
|
|
21
|
|
|
parent::__construct(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function __destruct() |
25
|
|
|
{ |
26
|
|
|
$this->server->stop(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testGetUrl() |
30
|
|
|
{ |
31
|
|
|
$curl = new Curl('https://example.org'); |
32
|
|
|
|
33
|
|
|
$this->assertSame('https://example.org', $curl->getUrl()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testCantSetCurlOptHeader() |
37
|
|
|
{ |
38
|
|
|
$this->expectException(InvalidArgumentException::class); |
39
|
|
|
|
40
|
|
|
$curl = new Curl('https://example.org'); |
41
|
|
|
|
42
|
|
|
$curl->setOpt(CURLOPT_HEADER, false); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testDefaultCurlOpts() |
46
|
|
|
{ |
47
|
|
|
$curl = new Curl('https://example.org'); |
48
|
|
|
|
49
|
|
|
$opts = $curl->getOpts(); |
50
|
|
|
|
51
|
|
|
$this->assertArrayHasKey(CURLOPT_USERAGENT, $opts); |
52
|
|
|
|
53
|
|
|
$this->assertSame($opts[CURLOPT_USERAGENT], 'Palmtree\Curl'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testCurlOptOverride() |
57
|
|
|
{ |
58
|
|
|
$curl = new Curl('https://example.org', [ |
59
|
|
|
CURLOPT_USERAGENT => 'Palmtree\Curl\Tests', |
60
|
|
|
]); |
61
|
|
|
|
62
|
|
|
$opts = $curl->getOpts(); |
63
|
|
|
|
64
|
|
|
$this->assertArrayHasKey(CURLOPT_USERAGENT, $opts); |
65
|
|
|
|
66
|
|
|
$this->assertSame($opts[CURLOPT_USERAGENT], 'Palmtree\Curl\Tests'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testCurlSetOpt() |
70
|
|
|
{ |
71
|
|
|
$curl = new Curl('https://example.org'); |
72
|
|
|
|
73
|
|
|
$curl->setOpt(CURLOPT_USERAGENT, 'Palmtree\Curl\Tests'); |
74
|
|
|
|
75
|
|
|
$this->assertArrayHasKey(CURLOPT_USERAGENT, $curl->getOpts()); |
76
|
|
|
$this->assertSame($curl->getOpts()[CURLOPT_USERAGENT], 'Palmtree\Curl\Tests'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testPost() |
80
|
|
|
{ |
81
|
|
|
$curl = new Curl($this->server->getUrl('post.php')); |
82
|
|
|
|
83
|
|
|
$response = $curl->post(['foo' => 'bar']); |
84
|
|
|
|
85
|
|
|
$this->assertSame('true', $response->getBody()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testPostJson() |
89
|
|
|
{ |
90
|
|
|
$curl = new Curl($this->server->getUrl('json.php')); |
91
|
|
|
|
92
|
|
|
$response = $curl->postJson(\json_encode(['foo' => true])); |
93
|
|
|
|
94
|
|
|
$this->assertSame('true', $response->getBody()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testGet() |
98
|
|
|
{ |
99
|
|
|
$curl = new Curl($this->server->getUrl('get.php')); |
100
|
|
|
|
101
|
|
|
$response = $curl->getResponse(); |
102
|
|
|
|
103
|
|
|
$this->assertSame('foo', $response->getBody()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testCannotExecuteMoreThanOnce() |
107
|
|
|
{ |
108
|
|
|
$this->expectException(BadMethodCallException::class); |
109
|
|
|
|
110
|
|
|
$curl = new Curl($this->server->getUrl()); |
111
|
|
|
|
112
|
|
|
$curl->execute(); |
113
|
|
|
$curl->execute(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testCurlGetContents() |
117
|
|
|
{ |
118
|
|
|
$contents = Curl::getContents($this->server->getUrl('get.php')); |
119
|
|
|
|
120
|
|
|
$this->assertSame('foo', $contents); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testToString() |
124
|
|
|
{ |
125
|
|
|
$curl = new Curl($this->server->getUrl('get.php')); |
126
|
|
|
|
127
|
|
|
$this->assertSame('foo', (string)$curl); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|