From cd069421bdb8058009203f6379f44f5aee1dc15f Mon Sep 17 00:00:00 2001 From: Haralan Dobrev Date: Fri, 26 Sep 2014 00:21:24 +0300 Subject: [PATCH 1/3] Use https:// protocol for Postmark API This introduces the new methods: - `Api::get_is_secure()` - `Api::set_is_secure()` - `Api::get_send_uri()` They could be used to get/set the secure flag and get the send URI based on that flag. The new default is to use the `https://` protocol for the Postmark API. You could use the previous default `http://` with: $transport = Swift_PostmarkTransport::newInstance('your api key'); $transport->api()->set_is_secure(false); Resolves https://github.com/OpenBuildings/postmark/issues/7. --- src/Openbuildings/Postmark/Api.php | 29 +++++++++++++++++++- tests/src/ApiTest.php | 43 ++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/src/Openbuildings/Postmark/Api.php b/src/Openbuildings/Postmark/Api.php index 6ee73ed..ef090a6 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 $_is_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), @@ -101,4 +104,28 @@ public function send(array $data) return $response; } + + public function get_is_secure() + { + return $this->_is_secure; + } + + public function set_is_secure($is_secure) + { + $this->_is_secure = $is_secure; + + return $this; + } + + public function get_send_uri() + { + if ($this->get_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..10d0148 100644 --- a/tests/src/ApiTest.php +++ b/tests/src/ApiTest.php @@ -147,4 +147,47 @@ public function test_send() ) ); } + + /** + * @covers Openbuildings\Postmark\Api::get_is_secure + */ + public function test_get_is_secure() + { + $api = new Api(); + $this->assertTrue($api->get_is_secure()); + + $api->set_is_secure(false); + $this->assertFalse($api->get_is_secure()); + + $api->set_is_secure(true); + $this->assertTrue($api->get_is_secure()); + } + + /** + * @covers Openbuildings\Postmark\Api::set_is_secure + */ + public function test_set_is_secure() + { + $api = new Api(); + $this->assertSame($api, $api->set_is_secure(false)); + $this->assertFalse($api->get_is_secure()); + + $api->set_is_secure(true); + $this->assertTrue($api->get_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_is_secure(false); + $this->assertEquals(Api::SEND_URI, $api->get_send_uri()); + + $api->set_is_secure(true); + $this->assertEquals(Api::SEND_URI_SECURE, $api->get_send_uri()); + } } From 9a0ab01c9f1b695df5e25cd30a7985afdcb220f0 Mon Sep 17 00:00:00 2001 From: Haralan Dobrev Date: Fri, 26 Sep 2014 00:35:45 +0300 Subject: [PATCH 2/3] Improve tests based on `get_send_uri` We are now able to mock the `get_send_uri()` method during the tests and since it is not in a constant we could set to whatever runtime value we need. This way we can test the wrong JSON case better. --- src/Openbuildings/Postmark/Api.php | 3 ++- tests/src/ApiTest.php | 40 +++++++++++++++++++++++++++++- tests/test_data/wrong-json.json | 1 + 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 tests/test_data/wrong-json.json diff --git a/src/Openbuildings/Postmark/Api.php b/src/Openbuildings/Postmark/Api.php index ef090a6..15c7642 100644 --- a/src/Openbuildings/Postmark/Api.php +++ b/src/Openbuildings/Postmark/Api.php @@ -84,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); diff --git a/tests/src/ApiTest.php b/tests/src/ApiTest.php index 10d0148..d9bf6c2 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; /** @@ -190,4 +189,43 @@ public function test_get_send_uri() $api->set_is_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 @@ +{'["} From afaa69b2e5e77270b7ea6bc6b3ded82deedaf83a Mon Sep 17 00:00:00 2001 From: Haralan Dobrev Date: Fri, 26 Sep 2014 15:18:00 +0300 Subject: [PATCH 3/3] Improve secure methods naming The interface is now more consistent with the Symfony naming conventions. --- src/Openbuildings/Postmark/Api.php | 12 ++++++------ tests/src/ApiTest.php | 30 +++++++++++++++--------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/Openbuildings/Postmark/Api.php b/src/Openbuildings/Postmark/Api.php index 15c7642..89863eb 100644 --- a/src/Openbuildings/Postmark/Api.php +++ b/src/Openbuildings/Postmark/Api.php @@ -18,7 +18,7 @@ class Api protected $_token; - protected $_is_secure = true; + protected $secure = true; public function __construct($token = null) { @@ -106,21 +106,21 @@ public function send(array $data) return $response; } - public function get_is_secure() + public function is_secure() { - return $this->_is_secure; + return $this->secure; } - public function set_is_secure($is_secure) + public function set_secure($secure) { - $this->_is_secure = $is_secure; + $this->secure = $secure; return $this; } public function get_send_uri() { - if ($this->get_is_secure()) + if ($this->is_secure()) { return static::SEND_URI_SECURE; } diff --git a/tests/src/ApiTest.php b/tests/src/ApiTest.php index d9bf6c2..5a25a7f 100644 --- a/tests/src/ApiTest.php +++ b/tests/src/ApiTest.php @@ -148,31 +148,31 @@ public function test_send() } /** - * @covers Openbuildings\Postmark\Api::get_is_secure + * @covers Openbuildings\Postmark\Api::is_secure */ - public function test_get_is_secure() + public function test_is_secure() { $api = new Api(); - $this->assertTrue($api->get_is_secure()); + $this->assertTrue($api->is_secure()); - $api->set_is_secure(false); - $this->assertFalse($api->get_is_secure()); + $api->set_secure(false); + $this->assertFalse($api->is_secure()); - $api->set_is_secure(true); - $this->assertTrue($api->get_is_secure()); + $api->set_secure(true); + $this->assertTrue($api->is_secure()); } /** - * @covers Openbuildings\Postmark\Api::set_is_secure + * @covers Openbuildings\Postmark\Api::set_secure */ - public function test_set_is_secure() + public function test_set_secure() { $api = new Api(); - $this->assertSame($api, $api->set_is_secure(false)); - $this->assertFalse($api->get_is_secure()); + $this->assertSame($api, $api->set_secure(false)); + $this->assertFalse($api->is_secure()); - $api->set_is_secure(true); - $this->assertTrue($api->get_is_secure()); + $api->set_secure(true); + $this->assertTrue($api->is_secure()); } /** @@ -183,10 +183,10 @@ public function test_get_send_uri() $api = new Api(); $this->assertEquals(Api::SEND_URI_SECURE, $api->get_send_uri()); - $api->set_is_secure(false); + $api->set_secure(false); $this->assertEquals(Api::SEND_URI, $api->get_send_uri()); - $api->set_is_secure(true); + $api->set_secure(true); $this->assertEquals(Api::SEND_URI_SECURE, $api->get_send_uri()); }