Skip to content

Commit 4be420f

Browse files
author
Damian Dziaduch
committed
Added reset method.
1 parent 6d9a380 commit 4be420f

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

src/Curl/Curl.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ public function __construct() {
4242
throw new \ErrorException('cURL library is not loaded');
4343
}
4444

45-
$this->curl = curl_init();
46-
$this->setUserAgent(self::USER_AGENT);
47-
$this->setopt(CURLINFO_HEADER_OUT, TRUE);
48-
$this->setopt(CURLOPT_HEADER, TRUE);
49-
$this->setopt(CURLOPT_RETURNTRANSFER, TRUE);
45+
$this->init();
5046
}
5147

5248
public function get($url, $data = array()) {
@@ -125,6 +121,25 @@ public function close() {
125121
curl_close($this->curl);
126122
}
127123
}
124+
125+
public function reset() {
126+
$this->close();
127+
$this->_cookies = array();
128+
$this->_headers = array();
129+
$this->error = FALSE;
130+
$this->error_code = 0;
131+
$this->error_message = NULL;
132+
$this->curl_error = FALSE;
133+
$this->curl_error_code = 0;
134+
$this->curl_error_message = NULL;
135+
$this->http_error = FALSE;
136+
$this->http_status_code = 0;
137+
$this->http_error_message = NULL;
138+
$this->request_headers = NULL;
139+
$this->response_headers = NULL;
140+
$this->response = NULL;
141+
$this->init();
142+
}
128143

129144
public function _exec() {
130145
$this->response = curl_exec($this->curl);
@@ -155,4 +170,12 @@ public function _exec() {
155170
public function __destruct() {
156171
$this->close();
157172
}
173+
174+
private function init() {
175+
$this->curl = curl_init();
176+
$this->setUserAgent(self::USER_AGENT);
177+
$this->setopt(CURLINFO_HEADER_OUT, TRUE);
178+
$this->setopt(CURLOPT_HEADER, TRUE);
179+
$this->setopt(CURLOPT_RETURNTRANSFER, TRUE);
180+
}
158181
}

tests/CurlTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,28 @@ public function testHeaders() {
208208
'key' => 'HTTP_ACCEPT',
209209
)) === 'application/json');
210210
}
211+
212+
public function testReset()
213+
{
214+
$curl = $this->getMockBuilder(get_class($this->curl))->getMock();
215+
$curl->expects($this->once())->method('reset')->with();
216+
// lets make small request
217+
$curl->setOpt(CURLOPT_CONNECTTIMEOUT_MS, 2000);
218+
$curl->get('http://1.2.3.4/');
219+
$curl->reset();
220+
$this->assertFalse($curl->error);
221+
$this->assertSame(0, $curl->error_code);
222+
$this->assertNull($curl->error_message);
223+
$this->assertFalse($curl->curl_error);
224+
$this->assertSame(0, $curl->curl_error_code);
225+
$this->assertNull($curl->curl_error_message);
226+
$this->assertFalse($curl->http_error);
227+
$this->assertSame(0, $curl->http_status_code);
228+
$this->assertNull($curl->http_error_message);
229+
$this->assertNull($curl->request_headers);
230+
$this->assertNull($curl->response_headers);
231+
$this->assertNull($curl->response);
232+
}
211233

212234
function create_png() {
213235
// PNG image data, 1 x 1, 1-bit colormap, non-interlaced

0 commit comments

Comments
 (0)