Skip to content

Commit 5cb1492

Browse files
committed
Implement success, error, and complete callbacks
1 parent 344d1d2 commit 5cb1492

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

Curl.class.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function get($url_mixed, $data=array()) {
7676
else {
7777
$this->setopt(CURLOPT_URL, $this->_buildURL($url_mixed, $data));
7878
$this->setopt(CURLOPT_HTTPGET, TRUE);
79-
return $this->_exec($this);
79+
return $this->_exec();
8080
}
8181
}
8282

@@ -176,6 +176,18 @@ private function http_build_multi_query($data, $key=NULL) {
176176
return implode('&', $query);
177177
}
178178

179+
public function success($callback) {
180+
$this->_success = $callback;
181+
}
182+
183+
public function error($callback) {
184+
$this->_error = $callback;
185+
}
186+
187+
public function complete($callback) {
188+
$this->_complete = $callback;
189+
}
190+
179191
private function _buildURL($url, $data=array()) {
180192
return $url . (empty($data) ? '' : '?' . http_build_query($data));
181193
}
@@ -232,13 +244,13 @@ protected function _exec($_ch=NULL) {
232244
$ch->error_message = $ch->curl_error ? $ch->curl_error_message : $ch->http_error_message;
233245

234246
if (!$ch->error) {
235-
$ch->_call($ch->_success);
247+
$ch->_call($ch->_success, $ch);
236248
}
237249
else {
238-
$ch->_call($ch->_error);
250+
$ch->_call($ch->_error, $ch);
239251
}
240252

241-
$ch->_call($ch->_complete);
253+
$ch->_call($ch->_complete, $ch);
242254

243255
return $ch->error_code;
244256
}

tests/helper.inc.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
class Test {
33
const TEST_URL = 'https://127.0.0.1/php-curl-class/tests/server.php';
4+
const ERROR_URL = 'https://1.2.3.4/';
45

56
function __construct() {
67
$this->curl = new Curl();

tests/run.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public function testCookies() {
167167
public function testError() {
168168
$test = new Test();
169169
$test->curl->setOpt(CURLOPT_CONNECTTIMEOUT_MS, 2000);
170-
$test->curl->get('http://1.2.3.4/');
170+
$test->curl->get(Test::ERROR_URL);
171171
$this->assertTrue($test->curl->error === TRUE);
172172
$this->assertTrue($test->curl->curl_error === TRUE);
173173
$this->assertTrue($test->curl->curl_error_code === CURLE_OPERATION_TIMEOUTED);
@@ -297,4 +297,43 @@ public function testParallelRequests() {
297297
$this->assertTrue(substr($curl->curls['1']->response, - $len) === '/b/?foo=bar');
298298
$this->assertTrue(substr($curl->curls['2']->response, - $len) === '/c/?foo=bar');
299299
}
300+
301+
public function testSuccessCallback() {
302+
$success_called = FALSE;
303+
$error_called = FALSE;
304+
$complete_called = FALSE;
305+
306+
$curl = new Curl();
307+
$curl->setHeader('X-DEBUG-TEST', 'get');
308+
$curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE);
309+
$curl->setOpt(CURLOPT_SSL_VERIFYHOST, FALSE);
310+
311+
$curl->success(function($instance) use (&$success_called, &$error_called, &$complete_called) {
312+
PHPUnit_Framework_Assert::assertInstanceOf('Curl', $instance);
313+
PHPUnit_Framework_Assert::assertFalse($success_called);
314+
PHPUnit_Framework_Assert::assertFalse($error_called);
315+
PHPUnit_Framework_Assert::assertFalse($complete_called);
316+
$success_called = TRUE;
317+
});
318+
$curl->error(function($instance) use (&$success_called, &$error_called, &$complete_called, &$curl) {
319+
PHPUnit_Framework_Assert::assertInstanceOf('Curl', $instance);
320+
PHPUnit_Framework_Assert::assertFalse($success_called);
321+
PHPUnit_Framework_Assert::assertFalse($error_called);
322+
PHPUnit_Framework_Assert::assertFalse($complete_called);
323+
$error_called = TRUE;
324+
});
325+
$curl->complete(function($instance) use (&$success_called, &$error_called, &$complete_called) {
326+
PHPUnit_Framework_Assert::assertInstanceOf('Curl', $instance);
327+
PHPUnit_Framework_Assert::assertTrue($success_called);
328+
PHPUnit_Framework_Assert::assertFalse($error_called);
329+
PHPUnit_Framework_Assert::assertFalse($complete_called);
330+
$complete_called = TRUE;
331+
});
332+
333+
$curl->get(Test::TEST_URL);
334+
335+
$this->assertTrue($success_called);
336+
$this->assertFalse($error_called);
337+
$this->assertTrue($complete_called);
338+
}
300339
}

0 commit comments

Comments
 (0)