Skip to content

Commit 34355b1

Browse files
authored
Merge pull request php-curl-class#529 from zachborboa/mock
Add methods for mocking
2 parents 668acf1 + c5db809 commit 34355b1

File tree

5 files changed

+201
-16
lines changed

5 files changed

+201
-16
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,42 @@ Curl::error($callback)
207207
Curl::exec($ch = null)
208208
Curl::execDone()
209209
Curl::get($url, $data = array())
210+
Curl::getAttempts()
211+
Curl::getBeforeSendCallback()
212+
Curl::getCompleteCallback()
210213
Curl::getCookie($key)
214+
Curl::getCurl()
215+
Curl::getCurlErrorCode()
216+
Curl::getCurlErrorMessage()
217+
Curl::getDownloadCompleteCallback()
218+
Curl::getErrorCallback()
219+
Curl::getErrorCode()
220+
Curl::getErrorMessage()
221+
Curl::getFileHandle()
222+
Curl::getHttpErrorMessage()
223+
Curl::getHttpStatusCode()
224+
Curl::getId()
211225
Curl::getInfo($opt = null)
226+
Curl::getJsonDecoder()
212227
Curl::getOpt($option)
228+
Curl::getRawResponse()
229+
Curl::getRawResponseHeaders()
230+
Curl::getRemainingRetries()
231+
Curl::getRequestHeaders()
232+
Curl::getResponse()
213233
Curl::getResponseCookie($key)
214234
Curl::getResponseCookies()
235+
Curl::getResponseHeaders()
236+
Curl::getRetries()
237+
Curl::getRetryDecider()
238+
Curl::getSuccessCallback()
239+
Curl::getUrl()
240+
Curl::getXmlDecoder()
215241
Curl::head($url, $data = array())
242+
Curl::isChildOfMultiCurl()
243+
Curl::isCurlError()
244+
Curl::isError()
245+
Curl::isHttpError()
216246
Curl::options($url, $data = array())
217247
Curl::patch($url, $data = array())
218248
Curl::post($url, $data = '', $follow_303_with_post = false)

src/Curl/Curl.php

Lines changed: 157 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Curl
4242

4343
public $attempts = 0;
4444
public $retries = 0;
45-
public $isChildOfMultiCurl = false;
45+
public $childOfMultiCurl = false;
4646
public $remainingRetries = 0;
4747
public $retryDecider = null;
4848

@@ -396,7 +396,7 @@ public function exec($ch = null)
396396
$this->setOpt(CURLOPT_NOBODY, false);
397397

398398
// Allow multicurl to attempt retry as needed.
399-
if ($this->isChildOfMultiCurl) {
399+
if ($this->isChildOfMultiCurl()) {
400400
return;
401401
}
402402

@@ -741,18 +741,6 @@ public function getResponseCookie($key)
741741
return isset($this->responseCookies[$key]) ? $this->responseCookies[$key] : null;
742742
}
743743

744-
/**
745-
* Get Response Cookies
746-
*
747-
* @access public
748-
*
749-
* @return array
750-
*/
751-
public function getResponseCookies()
752-
{
753-
return $this->responseCookies;
754-
}
755-
756744
/**
757745
* Set Max Filesize
758746
*
@@ -1211,6 +1199,161 @@ public function reset()
12111199
$this->initialize();
12121200
}
12131201

1202+
public function getCurl()
1203+
{
1204+
return $this->curl;
1205+
}
1206+
1207+
public function getId()
1208+
{
1209+
return $this->id;
1210+
}
1211+
1212+
public function isError()
1213+
{
1214+
return $this->error;
1215+
}
1216+
1217+
public function getErrorCode()
1218+
{
1219+
return $this->errorCode;
1220+
}
1221+
1222+
public function getErrorMessage()
1223+
{
1224+
return $this->errorMessage;
1225+
}
1226+
1227+
public function isCurlError()
1228+
{
1229+
return $this->curlError;
1230+
}
1231+
1232+
public function getCurlErrorCode()
1233+
{
1234+
return $this->curlErrorCode;
1235+
}
1236+
1237+
public function getCurlErrorMessage()
1238+
{
1239+
return $this->curlErrorMessage;
1240+
}
1241+
1242+
public function isHttpError()
1243+
{
1244+
return $this->httpError;
1245+
}
1246+
1247+
public function getHttpStatusCode()
1248+
{
1249+
return $this->httpStatusCode;
1250+
}
1251+
1252+
public function getHttpErrorMessage()
1253+
{
1254+
return $this->httpErrorMessage;
1255+
}
1256+
1257+
public function getUrl()
1258+
{
1259+
return $this->url;
1260+
}
1261+
1262+
public function getRequestHeaders()
1263+
{
1264+
return $this->requestHeaders;
1265+
}
1266+
1267+
public function getResponseHeaders()
1268+
{
1269+
return $this->responseHeaders;
1270+
}
1271+
1272+
public function getRawResponseHeaders()
1273+
{
1274+
return $this->rawResponseHeaders;
1275+
}
1276+
1277+
public function getResponseCookies()
1278+
{
1279+
return $this->responseCookies;
1280+
}
1281+
1282+
public function getResponse()
1283+
{
1284+
return $this->response;
1285+
}
1286+
1287+
public function getRawResponse()
1288+
{
1289+
return $this->rawResponse;
1290+
}
1291+
1292+
public function getBeforeSendCallback()
1293+
{
1294+
return $this->beforeSendCallback;
1295+
}
1296+
1297+
public function getDownloadCompleteCallback()
1298+
{
1299+
return $this->downloadCompleteCallback;
1300+
}
1301+
1302+
public function getSuccessCallback()
1303+
{
1304+
return $this->successCallback;
1305+
}
1306+
1307+
public function getErrorCallback()
1308+
{
1309+
return $this->errorCallback;
1310+
}
1311+
1312+
public function getCompleteCallback()
1313+
{
1314+
return $this->completeCallback;
1315+
}
1316+
1317+
public function getFileHandle()
1318+
{
1319+
return $this->fileHandle;
1320+
}
1321+
1322+
public function getAttempts()
1323+
{
1324+
return $this->attempts;
1325+
}
1326+
1327+
public function getRetries()
1328+
{
1329+
return $this->retries;
1330+
}
1331+
1332+
public function isChildOfMultiCurl()
1333+
{
1334+
return $this->childOfMultiCurl;
1335+
}
1336+
1337+
public function getRemainingRetries()
1338+
{
1339+
return $this->remainingRetries;
1340+
}
1341+
1342+
public function getRetryDecider()
1343+
{
1344+
return $this->retryDecider;
1345+
}
1346+
1347+
public function getJsonDecoder()
1348+
{
1349+
return $this->jsonDecoder;
1350+
}
1351+
1352+
public function getXmlDecoder()
1353+
{
1354+
return $this->xmlDecoder;
1355+
}
1356+
12141357
/**
12151358
* Destruct
12161359
*

src/Curl/MultiCurl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ private function queueHandle($curl)
812812
{
813813
// Use sequential ids to allow for ordered post processing.
814814
$curl->id = $this->nextCurlId++;
815-
$curl->isChildOfMultiCurl = true;
815+
$curl->childOfMultiCurl = true;
816816
$this->curls[$curl->id] = $curl;
817817

818818
$curl->setHeaders($this->headers);

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3676,4 +3676,16 @@ public function testReset()
36763676
$user_agent = $test->server('server', 'GET', array('key' => 'HTTP_USER_AGENT'));
36773677
$this->assertEquals($original_user_agent, $user_agent);
36783678
}
3679+
3680+
public function testMock()
3681+
{
3682+
$curl = $this->getMockBuilder('Curl\Curl')
3683+
->getMock();
3684+
3685+
$curl->expects($this->once())
3686+
->method('getRawResponse')
3687+
->will($this->returnValue('[]'));
3688+
3689+
$this->assertEquals('[]', $curl->getRawResponse());
3690+
}
36793691
}

tests/script.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ EOF
6363
# Skip hhvm "Notice: File could not be loaded: ..."
6464
if [[ "${TRAVIS_PHP_VERSION}" != "hhvm" ]] && [[ "${TRAVIS_PHP_VERSION}" != "hhvm-nightly" ]]; then
6565
export -f "find_invalid_indentation"
66-
invalid_indentation=$(find . -type "f" -iname "*.php" ! -path "*/vendor/*" -exec bash -c 'find_invalid_indentation "{}"' \;)
66+
invalid_indentation=$(find . -type "f" -iname "*.php" ! -path "*/tests/*" ! -path "*/vendor/*" -exec bash -c 'find_invalid_indentation "{}"' \;)
6767
if [[ ! -z "${invalid_indentation}" ]]; then
6868
echo "${invalid_indentation}"
6969
((errors++))

0 commit comments

Comments
 (0)