From 7d8941fee6cece66ae3f872beb8f71ddcf4bcb1c Mon Sep 17 00:00:00 2001 From: Mateusz Jaworski Date: Fri, 31 Jul 2015 09:09:38 +0200 Subject: curl error code in exception --- .gitignore | 3 +- src/PurpleCode/PCurl/PCurl.php | 5 +-- src/PurpleCode/PCurl/PCurlException.php | 56 ++++++++++++++++++++------------- test/PCurlTest.php | 10 +++--- 4 files changed, 43 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index 14bc68c..acb65f2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/nbproject/private/ \ No newline at end of file +/nbproject/private/ +.idea \ No newline at end of file diff --git a/src/PurpleCode/PCurl/PCurl.php b/src/PurpleCode/PCurl/PCurl.php index cd442d0..090f8a7 100644 --- a/src/PurpleCode/PCurl/PCurl.php +++ b/src/PurpleCode/PCurl/PCurl.php @@ -26,7 +26,7 @@ class PCurl { public function __construct($host) { if (!function_exists('curl_init')) { - throw new PCurlException('CURL module not available! See http://php.net/manual/en/book.curl.php'); + throw new PCurlException('CURL module not available! See http://php.net/manual/en/book.curl.php', 500, 2 /*CURLE_FAILED_INIT */); } $this->host = $host; @@ -100,8 +100,9 @@ class PCurl { $response = curl_exec($curl); if (!$response) { $error = curl_error($curl); + $errorCode = curl_errno($curl); curl_close($curl); - throw new PCurlException($error); + throw new PCurlException($error, 400, $errorCode); } $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); diff --git a/src/PurpleCode/PCurl/PCurlException.php b/src/PurpleCode/PCurl/PCurlException.php index 0e06e6d..ccd9269 100644 --- a/src/PurpleCode/PCurl/PCurlException.php +++ b/src/PurpleCode/PCurl/PCurlException.php @@ -11,31 +11,43 @@ namespace PurpleCode\PCurl; -class PCurlException extends \Exception { - - public static function getClassName() { - return get_called_class(); - } - - public static function assert($condition, $message, $arguments = array(), $code = 400) { - if (!$condition) { - $arguments = array_map('json_encode', self::ensureArray($arguments)); - $arguments = array_merge(array($message), $arguments); - $message = call_user_func_array('sprintf', $arguments); - $class = self::getClassName(); - throw new $class($message, $code); +class PCurlException extends \Exception +{ + + private $httpCode; + + public static function getClassName() + { + return get_called_class(); + } + + public static function assert($condition, $message, $arguments = array(), $code = 400) + { + if (!$condition) { + $arguments = array_map('json_encode', self::ensureArray($arguments)); + $arguments = array_merge(array($message), $arguments); + $message = call_user_func_array('sprintf', $arguments); + $class = self::getClassName(); + throw new $class($message, $code); + } } - } - public function __construct($message = "Exception", $code = 400, $previous = null) { - parent::__construct($message, $code, $previous); - } + private static function ensureArray($item) + { + if (is_null($item)) { + return array(); + } + return is_array($item) ? $item : array($item); + } + + public function __construct($message = "Exception", $httpCode = 400, $curlErrorCode = CURLE_OK, $previous = null) + { + parent::__construct($message, $curlErrorCode, $previous); + $this->httpCode = $httpCode; + } - private static function ensureArray($item) { - if (is_null($item)) { - return array(); + public function getHttpCode(){ + return $this->httpCode; } - return is_array($item) ? $item : array($item); - } } diff --git a/test/PCurlTest.php b/test/PCurlTest.php index a3c0724..0fbc610 100644 --- a/test/PCurlTest.php +++ b/test/PCurlTest.php @@ -18,7 +18,7 @@ class PCurlTest extends PHPUnit_Framework_TestCase { $response = $cut->get('/'); // then - $this->assertSelectRegExp('title', '/Google/', 1, $response->getBody()); + $this->assertSelectEquals('title', '/Google/', 1, $response->getBody()); $this->assertEquals(200, $response->getHttpCode()); } @@ -31,7 +31,7 @@ class PCurlTest extends PHPUnit_Framework_TestCase { $response = $cut->get('/'); // then - $this->assertSelectRegExp('title', '/Google/', 1, $response->getBody()); + $this->assertSelectEquals('title', '/Google/', 1, $response->getBody()); } public function testShouldGetGooglePageAndThrowExceptionOnMissingCertificate() { @@ -42,15 +42,13 @@ class PCurlTest extends PHPUnit_Framework_TestCase { $this->setExpectedException('PurpleCode\PCurl\PCurlException'); // when - $response = $cut->get('/'); + $cut->get('/'); } public function testShouldSetProperResponseCodeOnBadRequest() { // given $cut = new PCurl('http://www.google.com/pcurlnotfound'); - // then - //$this->setExpectedException('PurpleCode\PCurl\PCurlException'); // when $response = $cut->get('/'); @@ -66,7 +64,7 @@ class PCurlTest extends PHPUnit_Framework_TestCase { $this->setExpectedException('PurpleCode\PCurl\PCurlException'); // when - $response = $cut->get('/')->assertSuccess(); + $cut->get('/')->assertSuccess(); } } -- cgit v1.1