From ec22dfbded8d5801cf6ec356dbd9e6567a8e4218 Mon Sep 17 00:00:00 2001 From: Leon Weemen Date: Mon, 2 Dec 2013 22:02:09 +0100 Subject: [PATCH 1/4] Added composer.json and unittest --- composer.json | 12 +++++ .../Clockwork/Clockwork.php | 5 +- .../Clockwork/ClockworkException.php | 4 +- tests/Clockwork/ClockworkTest.php | 54 +++++++++++++++++++ tests/bootstrap.php | 2 + 5 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 composer.json rename class-Clockwork.php => src/Clockwork/Clockwork.php (99%) rename class-ClockworkException.php => src/Clockwork/ClockworkException.php (95%) create mode 100644 tests/Clockwork/ClockworkTest.php create mode 100644 tests/bootstrap.php diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..50c9208 --- /dev/null +++ b/composer.json @@ -0,0 +1,12 @@ +{ + "name": "weemen/clockwork-php", + "minimum-stability":"dev", + "require-dev": { + "phpunit/phpunit": "3.7.*" + }, + "autoload": { + "psr-0": { + "Clockwork\\": "src/" + } + } +} \ No newline at end of file diff --git a/class-Clockwork.php b/src/Clockwork/Clockwork.php similarity index 99% rename from class-Clockwork.php rename to src/Clockwork/Clockwork.php index b38b886..61190e0 100755 --- a/class-Clockwork.php +++ b/src/Clockwork/Clockwork.php @@ -9,9 +9,8 @@ * @version 1.3.0 */ -if ( !class_exists('ClockworkException') ) { - require_once('class-ClockworkException.php'); -} +namespace Clockwork; +use ClockworkException; /** * Main Clockwork API Class diff --git a/class-ClockworkException.php b/src/Clockwork/ClockworkException.php similarity index 95% rename from class-ClockworkException.php rename to src/Clockwork/ClockworkException.php index bf8d63e..51680ff 100755 --- a/class-ClockworkException.php +++ b/src/Clockwork/ClockworkException.php @@ -6,7 +6,9 @@ * @copyright Mediaburst Ltd 2012 * @license ISC * @link http://www.clockworksms.com - */ + */ + +namespace Clockwork; /* * ClockworkException diff --git a/tests/Clockwork/ClockworkTest.php b/tests/Clockwork/ClockworkTest.php new file mode 100644 index 0000000..a1cbde2 --- /dev/null +++ b/tests/Clockwork/ClockworkTest.php @@ -0,0 +1,54 @@ +markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + public function testCheckCredit() + { + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + public function testCheckBalance() + { + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + public function testCheckKey() + { + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + public function msisdProvider() + { + return array( + array('01234567', 0), //starts with 0 + array('11234567', 1), + array('1123456', 0), //not enough numbers + array('1123456789000', 1), + array('11234567890000', 0) //too many numbers + ); + } + + /** + * @dataProvider msisdProvider + */ + public function testIsValidMsisd($number, $expectedResult) + { + $this->assertEquals($expectedResult,Clockwork::is_valid_msisdn($number)); + } +} \ No newline at end of file diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..5bfc905 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,2 @@ + Date: Mon, 2 Dec 2013 22:09:47 +0100 Subject: [PATCH 2/4] removed skipped unittest, refactoring required to create unittests --- tests/Clockwork/ClockworkTest.php | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/tests/Clockwork/ClockworkTest.php b/tests/Clockwork/ClockworkTest.php index a1cbde2..b1e4e90 100644 --- a/tests/Clockwork/ClockworkTest.php +++ b/tests/Clockwork/ClockworkTest.php @@ -5,34 +5,6 @@ class ClockworkTest extends PHPUnit_Framework_TestCase { - public function testSend() - { - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); - } - - public function testCheckCredit() - { - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); - } - - public function testCheckBalance() - { - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); - } - - public function testCheckKey() - { - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); - } - public function msisdProvider() { return array( From 8e70ad7f4646659351fa27a10e3b5cf39534bd5f Mon Sep 17 00:00:00 2001 From: Leon Weemen Date: Mon, 2 Dec 2013 22:36:45 +0100 Subject: [PATCH 3/4] updated readme.md for the use of namespaces --- README.md | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 0f46cfc..d07e4ed 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,7 @@ Require the Clockwork library: require 'class-Clockwork.php'; ### Sending a message - - $clockwork = new Clockwork( $API_KEY ); + $clockwork = new \Clockwork\Clockwork( $API_KEY ); $message = array( 'to' => '441234567891', 'message' => 'This is a test!' ); $result = $clockwork->send( $message ); @@ -26,7 +25,7 @@ Require the Clockwork library: We recommend you use batch sizes of 500 messages or fewer. By limiting the batch size it prevents any timeouts when sending. - $clockwork = new Clockwork( $API_KEY ); + $clockwork = new \Clockwork\Clockwork( $API_KEY ); $messages = array( array( 'to' => '441234567891', 'message' => 'This is a test!' ), array( 'to' => '441234567892', 'message' => 'This is a test 2!' ) @@ -103,7 +102,7 @@ For example, if you send to invalid phone number "abc": Check your available SMS balance: - $clockwork = new Clockwork( $API_KEY ); + $clockwork = new \Clockwork\Clockwork( $API_KEY ); $clockwork->checkBalance(); This will return: @@ -121,11 +120,11 @@ The Clockwork wrapper will throw a `ClockworkException` if the entire call faile try { - $clockwork = new Clockwork( 'invalid_key' ); + $clockwork = new \Clockwork\Clockwork( 'invalid_key' ); $message = array( 'to' => 'abc', 'message' => 'This is a test!' ); $result = $clockwork->send( $message ); } - catch( ClockworkException $e ) + catch( \Clockwork\ClockworkException $e ) { print $e->getMessage(); // Invalid API Key @@ -183,7 +182,7 @@ Set option values individually on each message. In this example, one message will be from Clockwork and the other from 84433: - $clockwork = new Clockwork( $API_KEY, $options ); + $clockwork = new \Clockwork\Clockwork( $API_KEY, $options ); $messages = array( array( 'to' => '441234567891', 'message' => 'This is a test!', 'from' => 'Clockwork' ), array( 'to' => '441234567892', 'message' => 'This is a test 2!', 'from' => '84433' ) @@ -207,7 +206,7 @@ If you're seeing this error there are two fixes available, the first is easy, si #### Disable SSL on Clockwork calls $options = array( 'ssl' => false ); - $clockwork = new Clockwork( $API_KEY, $options ); + $clockwork = new \Clockwork\Clockwork( $API_KEY, $options ); #### Setup SSL root certificates on your server From 32e070cf8bad9ec0da3c6ef9b5c802f149ff4da4 Mon Sep 17 00:00:00 2001 From: Leon Weemen Date: Fri, 29 May 2015 07:40:58 +0200 Subject: [PATCH 4/4] fixed bugs --- src/Clockwork/Clockwork.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Clockwork/Clockwork.php b/src/Clockwork/Clockwork.php index 61190e0..2bfcfa3 100755 --- a/src/Clockwork/Clockwork.php +++ b/src/Clockwork/Clockwork.php @@ -172,7 +172,7 @@ public function send(array $sms) { $sms = array($sms); } - $req_doc = new DOMDocument('1.0', 'UTF-8'); + $req_doc = new \DOMDocument('1.0', 'UTF-8'); $root = $req_doc->createElement('Message'); $req_doc->appendChild($root); @@ -250,7 +250,7 @@ public function send(array $sms) { $req_xml = $req_doc->saveXML(); $resp_xml = $this->postToClockwork(self::API_SMS_METHOD, $req_xml); - $resp_doc = new DOMDocument(); + $resp_doc = new \DOMDocument(); $resp_doc->loadXML($resp_xml); $response = array(); @@ -316,7 +316,7 @@ public function send(array $sms) { */ public function checkCredit() { // Create XML doc for request - $req_doc = new DOMDocument('1.0', 'UTF-8'); + $req_doc = new \DOMDocument('1.0', 'UTF-8'); $root = $req_doc->createElement('Credit'); $req_doc->appendChild($root); $root->appendChild($req_doc->createElement('Key', $this->key)); @@ -326,7 +326,7 @@ public function checkCredit() { $resp_xml = $this->postToClockwork(self::API_CREDIT_METHOD, $req_xml); // Create XML doc for response - $resp_doc = new DOMDocument(); + $resp_doc = new \DOMDocument(); $resp_doc->loadXML($resp_xml); // Parse the response to find credit value @@ -364,7 +364,7 @@ public function checkCredit() { */ public function checkBalance() { // Create XML doc for request - $req_doc = new DOMDocument('1.0', 'UTF-8'); + $req_doc = new \DOMDocument('1.0', 'UTF-8'); $root = $req_doc->createElement('Balance'); $req_doc->appendChild($root); $root->appendChild($req_doc->createElement('Key', $this->key)); @@ -374,7 +374,7 @@ public function checkBalance() { $resp_xml = $this->postToClockwork(self::API_BALANCE_METHOD, $req_xml); // Create XML doc for response - $resp_doc = new DOMDocument(); + $resp_doc = new \DOMDocument(); $resp_doc->loadXML($resp_xml); // Parse the response to find balance value @@ -425,7 +425,7 @@ public function checkBalance() { */ public function checkKey() { // Create XML doc for request - $req_doc = new DOMDocument('1.0', 'UTF-8'); + $req_doc = new \DOMDocument('1.0', 'UTF-8'); $root = $req_doc->createElement('Authenticate'); $req_doc->appendChild($root); $root->appendChild($req_doc->createElement('Key', $this->key)); @@ -435,7 +435,7 @@ public function checkKey() { $resp_xml = $this->postToClockwork(self::API_AUTH_METHOD, $req_xml); // Create XML doc for response - $resp_doc = new DOMDocument(); + $resp_doc = new \DOMDocument(); $resp_doc->loadXML($resp_xml); // Parse the response to see if authenticated @@ -524,9 +524,9 @@ protected function xmlPost($url, $data) { $info = curl_getinfo($ch); if ($response === false || $info['http_code'] != 200) { - throw new Exception('HTTP Error calling Clockwork API - HTTP Status: ' . $info['http_code'] . ' - cURL Erorr: ' . curl_error($ch)); + throw new \Exception('HTTP Error calling Clockwork API - HTTP Status: ' . $info['http_code'] . ' - cURL Erorr: ' . curl_error($ch)); } elseif (curl_errno($ch) > 0) { - throw new Exception('HTTP Error calling Clockwork API - cURL Error: ' . curl_error($ch)); + throw new \Exception('HTTP Error calling Clockwork API - cURL Error: ' . curl_error($ch)); } curl_close($ch); @@ -552,17 +552,17 @@ protected function xmlPost($url, $data) { $fp = @fopen($url, 'rb', false, $ctx); if (!$fp) { ini_set('track_errors',$track); - throw new Exception("HTTP Error calling Clockwork API - fopen Error: $php_errormsg"); + throw new \Exception("HTTP Error calling Clockwork API - fopen Error: $php_errormsg"); } $response = @stream_get_contents($fp); if ($response === false) { ini_set('track_errors',$track); - throw new Exception("HTTP Error calling Clockwork API - stream Error: $php_errormsg"); + throw new \Exception("HTTP Error calling Clockwork API - stream Error: $php_errormsg"); } ini_set('track_errors',$track); return $response; } else { - throw new Exception("Clockwork requires PHP5 with cURL or HTTP stream support"); + throw new \Exception("Clockwork requires PHP5 with cURL or HTTP stream support"); } }