Skip to content

Commit

Permalink
refactor test
Browse files Browse the repository at this point in the history
  • Loading branch information
rajumsys committed Mar 27, 2017
1 parent d0eb0d2 commit c902061
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
3 changes: 2 additions & 1 deletion mailer.http.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
45 changes: 27 additions & 18 deletions tests/specs/test-mailer.http.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -357,25 +353,38 @@ function test_get_request_body_with_template_and_attachments() {
'headers' => array(),
'html' => '<h1>Hello there<h1>'
);
$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 <abc@xyz.com>';
NSA::setProperty($mock, 'settings', [
NSA::setProperty($mailer, 'settings', [
'enable_tracking' => true,
'transactional' => false,
'template' => 'hello'
Expand All @@ -402,11 +411,11 @@ function test_get_request_body_with_template_and_attachments() {
],
'subject' => 'test subject',
'html' => '<h1>Hello there<h1>',
'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);

Expand Down

0 comments on commit c902061

Please sign in to comment.