From 7c3a8733679eefb4f538f47e006a56cad574700b Mon Sep 17 00:00:00 2001 From: mhossain Date: Mon, 27 Mar 2017 13:17:55 -0400 Subject: [PATCH] add tests for template class --- templates.class.php | 1 - tests/phpunit.xml.dist | 1 + tests/specs/test-templates.class.php | 98 ++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 tests/specs/test-templates.class.php diff --git a/templates.class.php b/templates.class.php index ecff217e..e1f1db14 100644 --- a/templates.class.php +++ b/templates.class.php @@ -11,7 +11,6 @@ public function __construct($mailer){ $this->mailer = $mailer; } - public function preview($id, $substitution_data){ $url = "{$this->endpoint}/{$id}/preview?draft=false"; diff --git a/tests/phpunit.xml.dist b/tests/phpunit.xml.dist index becafe37..29f2213d 100644 --- a/tests/phpunit.xml.dist +++ b/tests/phpunit.xml.dist @@ -18,6 +18,7 @@ ../mailer.smtp.class.php ../mailer.http.class.php ../admin.widget.class.php + ../templates.class.php diff --git a/tests/specs/test-templates.class.php b/tests/specs/test-templates.class.php new file mode 100644 index 00000000..a371137c --- /dev/null +++ b/tests/specs/test-templates.class.php @@ -0,0 +1,98 @@ +assertTrue($templateObj->mailer instanceof SparkPostHTTPMailer); + } + + function test_preview_returns_template_data() { + $mailer = $this->getMockBuilder('WPSparkPost\SparkPostHTTPMailer') + ->setMethods(array('request', 'get_request_headers', 'error', 'debug')) + ->getMock(); + + $response = array( + 'response' => array( + 'code' => 200 + ), + 'headers' => array(), + 'body' => json_encode(array( + 'results' => array( + 'from' => array( + 'from' => 'me@hello.com', + 'from_name' => 'me' + ), + 'subject' => 'test subject', + 'headers' => array(), + 'html' => '

Hello there

', + 'text' => 'hello there', + 'reply_to' => 'me@hello.com' + ) + )) + ); + + $mailer->expects($this->once()) + ->method('request') + ->will($this->returnValue($response)); + + $templateObj = new SparkPostTemplates($mailer); + $result = $templateObj->preview('abcd', array()); + $this->assertEquals($result->subject, 'test subject'); + $this->assertEquals($result->html, '

Hello there

'); + $this->assertEquals($result->text, 'hello there'); + } + + function test_preview_returns_false_on_error() { + $mailer = $this->getMockBuilder('WPSparkPost\SparkPostHTTPMailer') + ->setMethods(array('request', 'get_request_headers', 'error', 'debug')) + ->getMock(); + + $response = array( + 'response' => array( + 'code' => 200 + ), + 'headers' => array(), + 'body' => json_encode(array( + 'errors' => array( + 'some interesting error' + ) + )) + ); + + $mailer->expects($this->once()) + ->method('request') + ->will($this->returnValue($response)); + + $templateObj = new SparkPostTemplates($mailer); + $result = $templateObj->preview('abcd', array()); + $this->assertEquals($result, false); + } + + function test_preview_returns_false_on_unknown_error() { + $mailer = $this->getMockBuilder('WPSparkPost\SparkPostHTTPMailer') + ->setMethods(array('request', 'get_request_headers', 'error', 'debug')) + ->getMock(); + + $response = array( + 'response' => array( + 'code' => 200 + ), + 'headers' => array(), + 'body' => json_encode(array('unknown_key' => 'unknown result')) + ); + + $mailer->expects($this->once()) + ->method('request') + ->will($this->returnValue($response)); + + $templateObj = new SparkPostTemplates($mailer); + $result = $templateObj->preview('abcd', array()); + $this->assertEquals($result, false); + } +}