From 4be420f1e4df51cd9bc6245e43875a0ff9b57b69 Mon Sep 17 00:00:00 2001 From: Damian Dziaduch Date: Thu, 11 Dec 2014 10:42:34 +0100 Subject: [PATCH] Added reset method. --- src/Curl/Curl.php | 33 ++++++++++++++++++++++++++++----- tests/CurlTest.php | 22 ++++++++++++++++++++++ 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index e9e2a78..3db0379 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -42,11 +42,7 @@ public function __construct() { throw new \ErrorException('cURL library is not loaded'); } - $this->curl = curl_init(); - $this->setUserAgent(self::USER_AGENT); - $this->setopt(CURLINFO_HEADER_OUT, TRUE); - $this->setopt(CURLOPT_HEADER, TRUE); - $this->setopt(CURLOPT_RETURNTRANSFER, TRUE); + $this->init(); } public function get($url, $data = array()) { @@ -125,6 +121,25 @@ public function close() { curl_close($this->curl); } } + + public function reset() { + $this->close(); + $this->_cookies = array(); + $this->_headers = array(); + $this->error = FALSE; + $this->error_code = 0; + $this->error_message = NULL; + $this->curl_error = FALSE; + $this->curl_error_code = 0; + $this->curl_error_message = NULL; + $this->http_error = FALSE; + $this->http_status_code = 0; + $this->http_error_message = NULL; + $this->request_headers = NULL; + $this->response_headers = NULL; + $this->response = NULL; + $this->init(); + } public function _exec() { $this->response = curl_exec($this->curl); @@ -155,4 +170,12 @@ public function _exec() { public function __destruct() { $this->close(); } + + private function init() { + $this->curl = curl_init(); + $this->setUserAgent(self::USER_AGENT); + $this->setopt(CURLINFO_HEADER_OUT, TRUE); + $this->setopt(CURLOPT_HEADER, TRUE); + $this->setopt(CURLOPT_RETURNTRANSFER, TRUE); + } } diff --git a/tests/CurlTest.php b/tests/CurlTest.php index 64a0f3e..182cc19 100644 --- a/tests/CurlTest.php +++ b/tests/CurlTest.php @@ -208,6 +208,28 @@ public function testHeaders() { 'key' => 'HTTP_ACCEPT', )) === 'application/json'); } + + public function testReset() + { + $curl = $this->getMockBuilder(get_class($this->curl))->getMock(); + $curl->expects($this->once())->method('reset')->with(); + // lets make small request + $curl->setOpt(CURLOPT_CONNECTTIMEOUT_MS, 2000); + $curl->get('http://1.2.3.4/'); + $curl->reset(); + $this->assertFalse($curl->error); + $this->assertSame(0, $curl->error_code); + $this->assertNull($curl->error_message); + $this->assertFalse($curl->curl_error); + $this->assertSame(0, $curl->curl_error_code); + $this->assertNull($curl->curl_error_message); + $this->assertFalse($curl->http_error); + $this->assertSame(0, $curl->http_status_code); + $this->assertNull($curl->http_error_message); + $this->assertNull($curl->request_headers); + $this->assertNull($curl->response_headers); + $this->assertNull($curl->response); + } function create_png() { // PNG image data, 1 x 1, 1-bit colormap, non-interlaced