summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README.md5
-rw-r--r--examples/transmission/simple_send.php (renamed from examples/transmission/configuration_based.php)0
-rw-r--r--lib/SparkPost/Transmission.php48
-rw-r--r--test/unit/SendGridCompatibiility/EmailTest.php8
-rw-r--r--test/unit/TransmissionTest.php27
5 files changed, 60 insertions, 28 deletions
diff --git a/README.md b/README.md
index b19a6e2..74bcb64 100644
--- a/README.md
+++ b/README.md
@@ -87,11 +87,12 @@ try {
## Development
### Setup
-
Run `composer install` inside the directory to install dependecies and development tools.
### Testing
-Once all the dependencies are installed, you can execute the unit tests using `vendor/bin/phpunit --bootstrap test/unit/bootstrap.php ./test/unit`
+Once all the dependencies are installed, you can execute the unit tests using `vendor/bin/phpunit --bootstrap test/unit/bootstrap.php ./test/unit`.
+
+If you're interested in code coverage, you can add the `--coverage` flag for phpunit like so: ```phpunit --coverage-html test/output/report --bootstrap test/unit/bootstrap.php ./test/unit```
### Contributing
Guidelines for adding issues
diff --git a/examples/transmission/configuration_based.php b/examples/transmission/simple_send.php
index 8149238..8149238 100644
--- a/examples/transmission/configuration_based.php
+++ b/examples/transmission/simple_send.php
diff --git a/lib/SparkPost/Transmission.php b/lib/SparkPost/Transmission.php
index 0149777..c083825 100644
--- a/lib/SparkPost/Transmission.php
+++ b/lib/SparkPost/Transmission.php
@@ -1,7 +1,7 @@
<?php
namespace SparkPost;
use Guzzle\Http\Client;
-use Guzzle\Http\Exception\RequestException;
+use Guzzle\Http\Exception\ClientErrorResponseException;
/**
* @desc SDK interface for managing transmissions
@@ -99,18 +99,6 @@ class Transmission {
}
/**
- * Helper function for extracting the response status code out of request thrown exceptions
- * @param string $exceptionMessage
- * @return number
- */
- private static function getStatusCode ($exceptionMessage) {
- $messageParts = explode("\n", $exceptionMessage);
- $codeLine = explode('] ', $messageParts[1]);
- $code = trim($codeLine[1]);
- return (int)$code;
- }
-
- /**
* @desc Method for issuing POST request to the Transmissions API
*
* This method assumes that all the appropriate fields have
@@ -150,11 +138,19 @@ class Transmission {
try {
$response = $request->post(self::getBaseUrl($hostConfig), array('authorization' => $hostConfig['key']), json_encode($model), array("verify"=>$hostConfig['strictSSL']))->send();
return $response->json();
- } catch (RequestException $exception) {
+ }
+ /*
+ * Handles 4XX responses
+ */
+ catch (ClientErrorResponseException $exception) {
$response = $exception->getResponse();
$responseArray = $response->json();
- throw new \Exception(json_encode($responseArray['errors']));
- } catch (\Exception $exception) {
+ throw new \Exception(json_encode($responseArray['errors']));
+ }
+ /*
+ * Handles 5XX Errors, Configuration Errors, and a catch all for other errors
+ */
+ catch (\Exception $exception) {
throw new \Exception('Unable to contact Transmissions API: '. $exception->getMessage());
}
}
@@ -184,15 +180,23 @@ class Transmission {
try {
$response = $request->get($url, array('authorization' => $hostConfig['key']), array("verify"=>$hostConfig['strictSSL']))->send();
return $response->json();
- } catch (\Exception $exception) {
- $statusCode = self::getStatusCode($exception->getMessage());
+ }
+ /*
+ * Handles 4XX responses
+ */
+ catch (ClientErrorResponseException $exception) {
+ $response = $exception->getResponse();
+ $statusCode = $response->getStatusCode();
if($statusCode === 404) {
throw new \Exception("The specified Transmission ID does not exist", 404);
- }else if ($statusCode !== null){
- throw new \Exception("Received bad response from Transmission API: ". $statusCode);
- } else {
- throw new \Exception('Unable to contact Transmissions API: '. $exception->getMessage());
}
+ throw new \Exception("Received bad response from Transmission API: ". $statusCode);
+ }
+ /*
+ * Handles 5XX Errors, Configuration Errors, and a catch all for other errors
+ */
+ catch (\Exception $exception) {
+ throw new \Exception('Unable to contact Transmissions API: '. $exception->getMessage());
}
}
diff --git a/test/unit/SendGridCompatibiility/EmailTest.php b/test/unit/SendGridCompatibiility/EmailTest.php
index 26e5108..6679745 100644
--- a/test/unit/SendGridCompatibiility/EmailTest.php
+++ b/test/unit/SendGridCompatibiility/EmailTest.php
@@ -96,11 +96,13 @@ class SendGridCompatibilityEmailTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals($value, $this->email->model['html']);
}
+ /**
+ * @desc test that adding a category throws an exception since we don't support tags at transmission level yet
+ * @expectedException Exception
+ * @expectedExceptionMessage Adding categories is not yet supported
+ */
public function testAddCategory() {
- $value = 'Category A';
$this->email->addCategory($value);
-
- $this->assertEquals(array($value), $this->email->model['tags']);
}
/**
diff --git a/test/unit/TransmissionTest.php b/test/unit/TransmissionTest.php
index cf4eda5..de72f2a 100644
--- a/test/unit/TransmissionTest.php
+++ b/test/unit/TransmissionTest.php
@@ -90,6 +90,18 @@ class TransmissionTest extends \PHPUnit_Framework_TestCase {
}
/**
+ * @desc tests bad response
+ * @expectedException Exception
+ * @expectedExceptionMessageRegExp /Unable to contact Transmissions API:.* /
+ */
+ public function testFindForCatchAllException() {
+ $mock = new MockPlugin();
+ $mock->addResponse(new Response(500));
+ $this->client->addSubscriber($mock);
+ Transmission::find('someId');
+ }
+
+ /**
* @desc tests happy path
*/
public function testSuccessfulSend() {
@@ -107,7 +119,7 @@ class TransmissionTest extends \PHPUnit_Framework_TestCase {
* @expectedException Exception
* @expectedExceptionMessage ["This is a fake error"]
*/
- public function testSendForRequestException() {
+ public function testSendFor400Exception() {
$body = array('errors'=>array('This is a fake error'));
$mock = new MockPlugin();
$mock->addResponse(new Response(400, array(), json_encode($body)));
@@ -115,5 +127,18 @@ class TransmissionTest extends \PHPUnit_Framework_TestCase {
Transmission::send(array('text'=>'awesome email'));
}
+
+ /**
+ * @desc tests bad response
+ * @expectedException Exception
+ * @expectedExceptionMessageRegExp /Unable to contact Transmissions API:.* /
+ */
+ public function testSendForCatchAllException() {
+ $mock = new MockPlugin();
+ $mock->addResponse(new Response(500));
+ $this->client->addSubscriber($mock);
+ Transmission::send(array('text'=>'awesome email'));
+ }
+
}
?> \ No newline at end of file