Skip to content

Commit

Permalink
add tests for template class
Browse files Browse the repository at this point in the history
  • Loading branch information
rajumsys committed Mar 27, 2017
1 parent 7eddf6c commit 7c3a873
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
1 change: 0 additions & 1 deletion templates.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public function __construct($mailer){
$this->mailer = $mailer;
}


public function preview($id, $substitution_data){
$url = "{$this->endpoint}/{$id}/preview?draft=false";

Expand Down
1 change: 1 addition & 0 deletions tests/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<file>../mailer.smtp.class.php</file>
<file>../mailer.http.class.php</file>
<file>../admin.widget.class.php</file>
<file>../templates.class.php</file>
</whitelist>
</filter>
<logging>
Expand Down
98 changes: 98 additions & 0 deletions tests/specs/test-templates.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* @package wp-sparkpost
*/
namespace WPSparkPost;

class TestTemplates extends \WP_UnitTestCase {
function test_it_should_set_mailer_obj(){
$mailer = new SparkPostHTTPMailer();
$templateObj = new SparkPostTemplates($mailer);

$this->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' => '<h1>Hello there<h1>',
'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, '<h1>Hello there<h1>');
$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);
}
}

0 comments on commit 7c3a873

Please sign in to comment.