diff --git a/mailer.http.class.php b/mailer.http.class.php
index 77f6e652..1d13e5ce 100644
--- a/mailer.http.class.php
+++ b/mailer.http.class.php
@@ -19,6 +19,7 @@ class SparkPostHTTPMailer extends \PHPMailer
function __construct($exceptions = false)
{
$this->settings = SparkPost::get_settings();
+ $this->template = new SparkPostTemplates($this);
parent::__construct($exceptions);
do_action('wpsp_init_mailer', $this);
@@ -121,7 +122,7 @@ protected function get_request_body()
// stored template
$substitution_data = $this->get_template_substitutes($sender, $replyTo);
if(sizeof($attachments) > 0){ //get template preview data and then send it as inline
- $preview_contents = $this->get_template_preview($template_id, $substitution_data);
+ $preview_contents = $this->template->preview($template_id, $substitution_data);
if($preview_contents === false) {
return false;
}
diff --git a/tests/specs/test-mailer.http.class.php b/tests/specs/test-mailer.http.class.php
index 822102f6..71c792b0 100644
--- a/tests/specs/test-mailer.http.class.php
+++ b/tests/specs/test-mailer.http.class.php
@@ -344,11 +344,7 @@ function test_get_request_body_with_template() {
}
function test_get_request_body_with_template_and_attachments() {
- $mock = $this->getMockBuilder('WPSparkPost\SparkPostHTTPMailer')
- ->setMethods(array('get_template_preview', 'get_attachments'))
- ->getMock();
-
- $template_preview = (object) array(
+ $template_data = (object) array(
'from' => array(
'from' => 'me@hello.com',
'from_name' => 'me'
@@ -357,25 +353,38 @@ function test_get_request_body_with_template_and_attachments() {
'headers' => array(),
'html' => '
Hello there'
);
- $mock->addAddress('abc@xyz.com', 'abc');
- $mock->setFrom( 'me@hello.com', 'me');
-
- $mock->expects($this->once())
- ->method('get_template_preview')
- ->will($this->returnValue($template_preview));
-
- $attachments = [
+ $attachments_data = [
'name' => 'php-wordpress-sparkpost.txt',
'type' => 'plain/text',
'data' => base64_encode('TEST')
];
- $mock->expects($this->once())
+
+ $mailer = $this->getMockBuilder('WPSparkPost\SparkPostHTTPMailer')
+ ->setMethods(array('get_attachments'))
+ ->getMock();
+
+ $template = $this->getMockBuilder('WPSparkPost\SparkPostTemplates')
+ ->setConstructorArgs(array($mailer))
+ ->setMethods(array('preview'))
+ ->getMock();
+
+ $template->expects($this->once())
+ ->method('preview')
+ ->will($this->returnValue($template_data));
+
+ $mailer->template = $template;
+
+ $mailer->addAddress('abc@xyz.com', 'abc');
+ $mailer->setFrom( 'me@hello.com', 'me');
+
+
+ $mailer->expects($this->once())
->method('get_attachments')
- ->will($this->returnValue($attachments));
+ ->will($this->returnValue($attachments_data));
$header_to = 'abc ';
- NSA::setProperty($mock, 'settings', [
+ NSA::setProperty($mailer, 'settings', [
'enable_tracking' => true,
'transactional' => false,
'template' => 'hello'
@@ -402,11 +411,11 @@ function test_get_request_body_with_template_and_attachments() {
],
'subject' => 'test subject',
'html' => 'Hello there',
- 'attachments' => $attachments
+ 'attachments' => $attachments_data
]
];
- $actual = NSA::invokeMethod($mock, 'get_request_body');
+ $actual = NSA::invokeMethod($mailer, 'get_request_body');
unset($actual['content']['headers']); //to simplify assertion
$this->assertTrue($expected_request_body == $actual);