Skip to content

Commit

Permalink
Merge 42601c0 into 8db60e7
Browse files Browse the repository at this point in the history
  • Loading branch information
rajumsys committed Nov 4, 2016
2 parents 8db60e7 + 42601c0 commit 58e020d
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ env:
- WP_VERSION=4.6 WP_MULTISITE=0

before_script:
- bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
- cd tests
- bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
- composer install --no-interaction

script: composer test
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ git clone git@github.com:SparkPost/wordpress-sparkpost.git ~/src/wordpress/wp-co

## Running Tests
* Make sure you're using PHP 5.6 or above.
* Go to `./tests` directory.
* Install test files by running `bash bin/install-wp-tests.sh wordpress_test root '' localhost latest`. Details on [wp-cli.org](http://wp-cli.org/docs/plugin-unit-tests/).
* [Install composer](https://getcomposer.org/doc/00-intro.md)
* Go to `./tests` directory.
* Run `composer install` to install required packages.
* To run tests, run `composer test`.
* Add your tests in `tests/specs` directory. Upon pushing the branch, Travis will automatically run it and generate reports (tests and coverage).
Expand Down
17 changes: 9 additions & 8 deletions mailer.http.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,20 +232,21 @@ protected function get_recipients()
$recipients = array();
$recipients_header_to = array();

//prepare header_to
foreach ($this->to as $to) {
$recipients[] = $this->build_recipient($to[0], $to[1]);

// prepare for header_to
if(!empty($to[1])) {
$recipients_header_to[] = sprintf('%s <%s>', $to[1], $to[0]);
} else {
if(empty($to[1])) { // if name is empty use only address
$recipients_header_to[] = $to[0];
} else { // otherwise, use name and email
$recipients_header_to[] = sprintf('%s <%s>', $to[1], $to[0]);
}
}
$recipients_header_to = implode(',', $recipients_header_to);
$recipients_header_to = implode(', ', $recipients_header_to);

foreach ($this->to as $to) {
$recipients[] = $this->build_recipient($to[0], $to[1], $recipients_header_to);
}

// include bcc to recipients
// sparkposts recipients list acts as bcc by default
$recipients = array_merge($recipients, $this->get_bcc($recipients_header_to));

// include cc to recipients, they need to included in recipients and in headers (refer to get_headers method)
Expand Down
File renamed without changes.
113 changes: 83 additions & 30 deletions tests/specs/test-mailer.http.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,18 @@ function setUp() {
$this->mailer = new SparkPostHTTPMailer();
}

function test_mailer_is_a_mailer_instance() {
$this->assertTrue( $this->mailer instanceof \PHPMailer );
function test_mailSend_calls_sparkpost_send() {
$stub = Mockery::mock($this->mailer);
$stub->shouldReceive('sparkpost_send')->andReturn('woowoo');

$this->assertTrue(NSA::invokeMethod($stub, 'mailSend', null, null) == 'woowoo');
}

function test_recipients_list() {

$this->mailer->addAddress('abc@xyz.com', 'abc');
$this->mailer->addAddress('def@xyz.com', 'def');
$this->mailer->addAddress('noname@xyz.com');
$prepared_list = array(
array(
'address' => array(
'email' => 'abc@xyz.com',
'name' => 'abc',
)
),
array(
'address' => array(
'name' => 'def',
'email' => 'def@xyz.com'
)
),
array(
'address' => array(
'email' => 'noname@xyz.com',
'name' => ''
)
)
);
$this->assertTrue(NSA::invokeMethod($this->mailer, 'get_recipients') == $prepared_list);
function test_mailer_is_a_mailer_instance() {
$this->assertTrue( $this->mailer instanceof \PHPMailer );
}

function test_sender_with_name() {
function test_get_sender_with_name() {
$this->mailer->setFrom( 'me@hello.com', 'me' );
$sender = array(
'name' => 'me',
Expand All @@ -55,7 +34,7 @@ function test_sender_with_name() {
$this->assertTrue(NSA::invokeMethod($this->mailer, 'get_sender') == $sender);
}

function test_sender_without_name() {
function test_get_sender_without_name() {
$this->mailer->setFrom( 'me@hello.com', '' );
$sender = array(
'email' => 'me@hello.com'
Expand Down Expand Up @@ -131,4 +110,78 @@ function test_get_headers_should_include_cc_if_exists() {

$this->assertTrue($formatted_headers == $expected);
}

function test_get_recipients() {
$this->mailer->addAddress('to@abc.com');
$this->mailer->addAddress('to1@abc.com', 'to1');
$this->mailer->addCc('cc@abc.com');
$this->mailer->addCc('cc1@abc.com', 'cc1');
$this->mailer->addBcc('bcc@abc.com');
$this->mailer->addBcc('bcc1@abc.com', 'bcc1');

$header_to = implode(', ', [
'to@abc.com',
'to1 <to1@abc.com>',
]);

$expected = [
[
'address' => [
'email' => 'to@abc.com',
'header_to' => $header_to
]
],
[
'address' => [
'email' => 'to1@abc.com',
'header_to' => $header_to
]
],
[
'address' => [
'email' => 'bcc@abc.com',
'header_to' => $header_to
]
],
[
'address' => [
'email' => 'bcc1@abc.com',
'header_to' => $header_to
]
],
[
'address' => [
'email' => 'cc@abc.com',
'header_to' => $header_to
]
],
[
'address' => [
'email' => 'cc1@abc.com',
'header_to' => $header_to
]
]
];

$recipients = NSA::invokeMethod($this->mailer, 'get_recipients');
$this->assertTrue($recipients == $expected);
}

function test_get_attachments() {
$temp = tempnam('/tmp', 'php-wordpress-sparkpost');
file_put_contents($temp, 'TEST');
$this->mailer->addAttachment($temp);
$attachments = NSA::invokeMethod($this->mailer, 'get_attachments');
$this->assertTrue($attachments[0]['type'] === 'application/octet-stream');
$this->assertTrue($attachments[0]['name'] === basename($temp));
$this->assertTrue($attachments[0]['data'] === base64_encode('TEST'));
}

function test_isMail() {
// test if isMail sets correct mailer
$this->mailer->Mailer = 'abc';
$this->assertTrue($this->mailer->Mailer === 'abc');
$this->mailer->isMail();
$this->assertTrue($this->mailer->Mailer === 'sparkpost');
}
}

0 comments on commit 58e020d

Please sign in to comment.