diff --git a/src/Openbuildings/Postmark/Api.php b/src/Openbuildings/Postmark/Api.php index 6ee73ed..89863eb 100644 --- a/src/Openbuildings/Postmark/Api.php +++ b/src/Openbuildings/Postmark/Api.php @@ -14,8 +14,11 @@ class Api { const SEND_URI = 'http://api.postmarkapp.com/email'; + const SEND_URI_SECURE = 'https://api.postmarkapp.com/email'; + protected $_token; + protected $secure = true; public function __construct($token = null) { @@ -70,7 +73,7 @@ public function send(array $data) curl_setopt_array( $curl, array( - CURLOPT_URL => static::SEND_URI, + CURLOPT_URL => $this->get_send_uri(), CURLOPT_POST => true, CURLOPT_HTTPHEADER => $this->headers(), CURLOPT_POSTFIELDS => json_encode($data), @@ -81,7 +84,8 @@ public function send(array $data) $response = curl_exec($curl); if (!$response) { - throw new \Exception('Postmark delivery failed: ' . curl_error($curl)); + $curl_error = curl_error($curl) ?: 'unknown cURL error'; + throw new \Exception('Postmark delivery failed: ' . $curl_error); } $response = @json_decode($response, true); @@ -101,4 +105,28 @@ public function send(array $data) return $response; } + + public function is_secure() + { + return $this->secure; + } + + public function set_secure($secure) + { + $this->secure = $secure; + + return $this; + } + + public function get_send_uri() + { + if ($this->is_secure()) + { + return static::SEND_URI_SECURE; + } + else + { + return static::SEND_URI; + } + } } diff --git a/tests/src/ApiTest.php b/tests/src/ApiTest.php index 384b336..5a25a7f 100644 --- a/tests/src/ApiTest.php +++ b/tests/src/ApiTest.php @@ -3,7 +3,6 @@ namespace Openbuildings\Postmark\Test; use Openbuildings\Postmark\Api; -use Openbuildings\Postmark\Test\Mock; use PHPUnit_Framework_TestCase; /** @@ -147,4 +146,86 @@ public function test_send() ) ); } + + /** + * @covers Openbuildings\Postmark\Api::is_secure + */ + public function test_is_secure() + { + $api = new Api(); + $this->assertTrue($api->is_secure()); + + $api->set_secure(false); + $this->assertFalse($api->is_secure()); + + $api->set_secure(true); + $this->assertTrue($api->is_secure()); + } + + /** + * @covers Openbuildings\Postmark\Api::set_secure + */ + public function test_set_secure() + { + $api = new Api(); + $this->assertSame($api, $api->set_secure(false)); + $this->assertFalse($api->is_secure()); + + $api->set_secure(true); + $this->assertTrue($api->is_secure()); + } + + /** + * @covers Openbuildings\Postmark\Api::get_send_uri + */ + public function test_get_send_uri() + { + $api = new Api(); + $this->assertEquals(Api::SEND_URI_SECURE, $api->get_send_uri()); + + $api->set_secure(false); + $this->assertEquals(Api::SEND_URI, $api->get_send_uri()); + + $api->set_secure(true); + $this->assertEquals(Api::SEND_URI_SECURE, $api->get_send_uri()); + } + + /** + * @covers Openbuildings\Postmark\Api::send + */ + public function test_send_wrong_json() + { + $api_mock = $this->getMock( + 'Openbuildings\Postmark\Api', + array( + 'get_send_uri' + ), + array( + 'POSTMARK_API_TEST' + ) + ); + + $path_to_wrong_json = 'file://'.realpath(__DIR__.'/../test_data/wrong-json.json'); + + $api_mock + ->expects($this->once()) + ->method('get_send_uri') + ->will($this->returnValue($path_to_wrong_json)); + + $this->setExpectedException( + 'Exception', + 'Postmark delivery failed: wrong json response' + ); + + $response = $api_mock->send( + array( + 'From' => 'Mark Smith ', + 'To' => 'test_email@example.com,test_email2@example.com,test_email3@example.com', + 'Subject' => 'Test', + 'HtmlBody' => 'Hello', + 'TextBody' => 'Hello', + 'ReplyTo' => 'reply@example.com', + ) + ); + } } diff --git a/tests/test_data/wrong-json.json b/tests/test_data/wrong-json.json new file mode 100644 index 0000000..54f775c --- /dev/null +++ b/tests/test_data/wrong-json.json @@ -0,0 +1 @@ +{'["}