- Adds MailGun mailer class + tests
- Updates tests for mailer classes
This commit is contained in:
@@ -4,7 +4,8 @@ namespace MailPoet\Mailer;
|
|||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
class ElasticEmail {
|
class ElasticEmail {
|
||||||
function __construct($api_key, $from_email, $from_name, $newsletter, $subscribers) {
|
function __construct($api_key, $from_email, $from_name, $newsletter,
|
||||||
|
$subscribers) {
|
||||||
$this->url = 'https://api.elasticemail.com/mailer/send';
|
$this->url = 'https://api.elasticemail.com/mailer/send';
|
||||||
$this->api_key = $api_key;
|
$this->api_key = $api_key;
|
||||||
$this->newsletter = $newsletter;
|
$this->newsletter = $newsletter;
|
||||||
@@ -16,21 +17,25 @@ class ElasticEmail {
|
|||||||
function send() {
|
function send() {
|
||||||
$result = wp_remote_post(
|
$result = wp_remote_post(
|
||||||
$this->url,
|
$this->url,
|
||||||
$this->request()
|
$this->request());
|
||||||
);
|
if(is_object($result) && get_class($result) === 'WP_Error') return false;
|
||||||
return preg_match('/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/', $result['body']);
|
return (preg_match('/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/', $result['body']) == true);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSubscribers() {
|
function getSubscribers() {
|
||||||
$subscribers = array_map(function ($subscriber) {
|
$subscribers = array_map(function ($subscriber) {
|
||||||
if(!isset($subscriber['email'])) return;
|
if(!isset($subscriber['email'])) return;
|
||||||
$first_name = (isset($subscriber['first_name'])) ? $subscriber['first_name'] : '';
|
$first_name = (isset($subscriber['first_name']))
|
||||||
$last_name = (isset($subscriber['last_name'])) ? $subscriber['last_name'] : '';
|
? $subscriber['first_name'] : '';
|
||||||
$subscriber = sprintf('%s %s <%s>', $first_name, $last_name, $subscriber['email']);
|
$last_name = (isset($subscriber['last_name']))
|
||||||
|
? $subscriber['last_name'] : '';
|
||||||
|
$subscriber = sprintf(
|
||||||
|
'%s %s <%s>', $first_name, $last_name, $subscriber['email']
|
||||||
|
);
|
||||||
$subscriber = trim(preg_replace('!\s\s+!', ' ', $subscriber));
|
$subscriber = trim(preg_replace('!\s\s+!', ' ', $subscriber));
|
||||||
return $subscriber;
|
return $subscriber;
|
||||||
}, $this->subscribers);
|
}, $this->subscribers);
|
||||||
return implode(';', array_filter($subscribers));
|
return array_filter($subscribers);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getBody() {
|
function getBody() {
|
||||||
@@ -38,15 +43,11 @@ class ElasticEmail {
|
|||||||
'api_key' => $this->api_key,
|
'api_key' => $this->api_key,
|
||||||
'from' => $this->from_email,
|
'from' => $this->from_email,
|
||||||
'from_name' => $this->from_name,
|
'from_name' => $this->from_name,
|
||||||
'to' => $this->getSubscribers(),
|
'to' => implode(';', $this->getSubscribers()),
|
||||||
'subject' => $this->newsletter['subject'],
|
'subject' => $this->newsletter['subject'],
|
||||||
'body_html' => $this->newsletter['body']
|
'body_html' => $this->newsletter['body']
|
||||||
);
|
);
|
||||||
|
return urldecode(http_build_query($parameters));
|
||||||
$body = array_map(function ($parameter, $value) {
|
|
||||||
return $parameter . '=' . urlencode($value);
|
|
||||||
}, array_keys($parameters), $parameters);
|
|
||||||
return implode('&', $body);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function request() {
|
function request() {
|
||||||
@@ -54,9 +55,6 @@ class ElasticEmail {
|
|||||||
'timeout' => 10,
|
'timeout' => 10,
|
||||||
'httpversion' => '1.0',
|
'httpversion' => '1.0',
|
||||||
'method' => 'POST',
|
'method' => 'POST',
|
||||||
'headers' => array(
|
|
||||||
'Content-Type' => 'application/x-www-form-urlencoded'
|
|
||||||
),
|
|
||||||
'body' => $this->getBody()
|
'body' => $this->getBody()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
82
lib/Mailer/MailGun.php
Normal file
82
lib/Mailer/MailGun.php
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
<?php
|
||||||
|
namespace MailPoet\Mailer;
|
||||||
|
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
|
class MailGun {
|
||||||
|
function __construct($domain, $api_key, $from_email, $from_name,
|
||||||
|
$newsletter, $subscribers) {
|
||||||
|
$this->url = 'https://api.mailgun.net/v3';
|
||||||
|
$this->domain = $domain;
|
||||||
|
$this->api_key = $api_key;
|
||||||
|
$this->newsletter = $newsletter;
|
||||||
|
$this->subscribers = $subscribers;
|
||||||
|
$this->from = sprintf('%s <%s>', $from_name, $from_email);
|
||||||
|
}
|
||||||
|
|
||||||
|
function send() {
|
||||||
|
$result = wp_remote_post(
|
||||||
|
$this->url . '/' . $this->domain . '/messages',
|
||||||
|
$this->request()
|
||||||
|
);
|
||||||
|
if(is_object($result) && get_class($result) === 'WP_Error') return false;
|
||||||
|
return ($result['response']['code'] === 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSubscribers() {
|
||||||
|
$subscribers = array_map(function ($subscriber) {
|
||||||
|
if(!isset($subscriber['email'])) return;
|
||||||
|
$first_name = (isset($subscriber['first_name']))
|
||||||
|
? $subscriber['first_name'] : '';
|
||||||
|
$last_name = (isset($subscriber['last_name']))
|
||||||
|
? $subscriber['last_name'] : '';
|
||||||
|
$subscriber = sprintf(
|
||||||
|
'%s %s <%s>', $first_name, $last_name, $subscriber['email']
|
||||||
|
);
|
||||||
|
$subscriber = trim(preg_replace('!\s\s+!', ' ', $subscriber));
|
||||||
|
return $subscriber;
|
||||||
|
}, $this->subscribers);
|
||||||
|
$subscribers = array_filter($subscribers);
|
||||||
|
|
||||||
|
$subscribersData = array_map(function ($subscriber) {
|
||||||
|
return array($subscriber => array());
|
||||||
|
}, $subscribers);
|
||||||
|
$subscribersData = array_map('array_merge', $subscribersData);
|
||||||
|
|
||||||
|
return array(
|
||||||
|
'emails' => $subscribers,
|
||||||
|
'data' => $subscribersData
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getBody() {
|
||||||
|
$subscribers = $this->getSubscribers();
|
||||||
|
$parameters = array(
|
||||||
|
'from' => $this->from,
|
||||||
|
'to' => $subscribers['emails'],
|
||||||
|
'recipient-variables' => json_encode($subscribers['data']),
|
||||||
|
'subject' => $this->newsletter['subject'],
|
||||||
|
'text' => $this->newsletter['body']
|
||||||
|
);
|
||||||
|
$parameters = http_build_query($parameters);
|
||||||
|
$parameters = preg_replace('!\[\d+\]!', '', urldecode($parameters));
|
||||||
|
return $parameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
function auth() {
|
||||||
|
return 'Basic ' . base64_encode('api:' . $this->api_key);
|
||||||
|
}
|
||||||
|
|
||||||
|
function request() {
|
||||||
|
return array(
|
||||||
|
'timeout' => 10,
|
||||||
|
'httpversion' => '1.0',
|
||||||
|
'method' => 'POST',
|
||||||
|
'headers' => array(
|
||||||
|
'Content-Type' => 'application/x-www-form-urlencoded',
|
||||||
|
'Authorization' => $this->auth()
|
||||||
|
),
|
||||||
|
'body' => $this->getBody()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@@ -4,21 +4,21 @@ namespace MailPoet\Mailer;
|
|||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
class SendGrid {
|
class SendGrid {
|
||||||
function __construct($api_key, $from_email, $from_name, $newsletter, $subscribers) {
|
function __construct($api_key, $from_email, $from_name, $newsletter,
|
||||||
|
$subscribers) {
|
||||||
$this->url = 'https://api.sendgrid.com/api/mail.send.json';
|
$this->url = 'https://api.sendgrid.com/api/mail.send.json';
|
||||||
$this->api_key = $api_key;
|
$this->api_key = $api_key;
|
||||||
$this->newsletter = $newsletter;
|
$this->newsletter = $newsletter;
|
||||||
$this->subscribers = $subscribers;
|
$this->subscribers = $subscribers;
|
||||||
$this->from_name = $from_name;
|
$this->from = sprintf('%s <%s>', $from_name, $from_email);
|
||||||
$this->from_email = $from_email;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function send() {
|
function send() {
|
||||||
if (!count($this->getSubscribers())) return false;
|
|
||||||
$result = wp_remote_post(
|
$result = wp_remote_post(
|
||||||
$this->url,
|
$this->url,
|
||||||
$this->request()
|
$this->request()
|
||||||
);
|
);
|
||||||
|
if(is_object($result) && get_class($result) === 'WP_Error') return false;
|
||||||
$result = json_decode($result['body'], true);
|
$result = json_decode($result['body'], true);
|
||||||
return (isset($result['errors']) === false);
|
return (isset($result['errors']) === false);
|
||||||
}
|
}
|
||||||
@@ -26,9 +26,13 @@ class SendGrid {
|
|||||||
function getSubscribers() {
|
function getSubscribers() {
|
||||||
$subscribers = array_map(function ($subscriber) {
|
$subscribers = array_map(function ($subscriber) {
|
||||||
if(!isset($subscriber['email'])) return;
|
if(!isset($subscriber['email'])) return;
|
||||||
$first_name = (isset($subscriber['first_name'])) ? $subscriber['first_name'] : '';
|
$first_name = (isset($subscriber['first_name']))
|
||||||
$last_name = (isset($subscriber['last_name'])) ? $subscriber['last_name'] : '';
|
? $subscriber['first_name'] : '';
|
||||||
$subscriber = sprintf('%s %s <%s>', $first_name, $last_name, $subscriber['email']);
|
$last_name = (isset($subscriber['last_name']))
|
||||||
|
? $subscriber['last_name'] : '';
|
||||||
|
$subscriber = sprintf(
|
||||||
|
'%s %s <%s>', $first_name, $last_name, $subscriber['email']
|
||||||
|
);
|
||||||
$subscriber = trim(preg_replace('!\s\s+!', ' ', $subscriber));
|
$subscriber = trim(preg_replace('!\s\s+!', ' ', $subscriber));
|
||||||
return $subscriber;
|
return $subscriber;
|
||||||
}, $this->subscribers);
|
}, $this->subscribers);
|
||||||
@@ -37,16 +41,17 @@ class SendGrid {
|
|||||||
|
|
||||||
function getBody() {
|
function getBody() {
|
||||||
$parameters = array(
|
$parameters = array(
|
||||||
'from' => sprintf('%s <%s>', $this->from_name, $this->from_email),
|
'to' => $this->from,
|
||||||
|
'from' => $this->from,
|
||||||
'x-smtpapi' => json_encode(array('to' => $this->getSubscribers())),
|
'x-smtpapi' => json_encode(array('to' => $this->getSubscribers())),
|
||||||
'to' => $this->from_email,
|
|
||||||
'subject' => $this->newsletter['subject'],
|
'subject' => $this->newsletter['subject'],
|
||||||
'html' => $this->newsletter['body']
|
'html' => $this->newsletter['body']
|
||||||
);
|
);
|
||||||
$body = array_map(function ($parameter, $value) {
|
return urldecode(http_build_query($parameters));
|
||||||
return $parameter . '=' . urlencode($value);
|
}
|
||||||
}, array_keys($parameters), $parameters);
|
|
||||||
return implode('&', array_filter($body));
|
function auth() {
|
||||||
|
return 'Bearer ' . $this->api_key;
|
||||||
}
|
}
|
||||||
|
|
||||||
function request() {
|
function request() {
|
||||||
@@ -55,8 +60,7 @@ class SendGrid {
|
|||||||
'httpversion' => '1.1',
|
'httpversion' => '1.1',
|
||||||
'method' => 'POST',
|
'method' => 'POST',
|
||||||
'headers' => array(
|
'headers' => array(
|
||||||
'Authorization' => 'Bearer ' . $this->api_key,
|
'Authorization' => $this->auth()
|
||||||
'Content-Type' => 'application/x-www-form-urlencoded'
|
|
||||||
),
|
),
|
||||||
'body' => $this->getBody()
|
'body' => $this->getBody()
|
||||||
);
|
);
|
||||||
|
@@ -6,24 +6,24 @@ class ElasticEmailCest {
|
|||||||
function __construct() {
|
function __construct() {
|
||||||
$this->data = array(
|
$this->data = array(
|
||||||
'api_key' => '997f1f7f-41de-4d7f-a8cb-86c8481370fa',
|
'api_key' => '997f1f7f-41de-4d7f-a8cb-86c8481370fa',
|
||||||
'from_email' => 'vlad@mailpoet.com',
|
'from_email' => 'do-not-reply@mailpoet.com',
|
||||||
'from_name' => 'Vlad',
|
'from_name' => 'Phoenix',
|
||||||
'newsletter' => array(
|
'newsletter' => array(
|
||||||
'subject' => 'hi there!',
|
'subject' => 'Testing ElasticEmail',
|
||||||
'body' => 'this is a test message....'
|
'body' => 'this is a test message....'
|
||||||
),
|
),
|
||||||
'subscribers' => array(
|
'subscribers' => array(
|
||||||
array(
|
array(
|
||||||
'email' => 'johndoe@mailpoet.com',
|
'email' => 'mailpoet-test1@mailinator.com',
|
||||||
'last_name' => 'Smith'
|
'last_name' => 'Smith'
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
'email' => 'janesmith@mailpoet.com',
|
'email' => 'mailpoet-test2@mailinator.com',
|
||||||
'first_name' => 'Jane',
|
'first_name' => 'Jane',
|
||||||
'last_name' => 'Smith'
|
'last_name' => 'Smith'
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
'email' => 'someone@mailpoet.com',
|
'email' => 'mailpoet-test3@mailinator.com',
|
||||||
),
|
),
|
||||||
array()
|
array()
|
||||||
)
|
)
|
||||||
@@ -38,7 +38,7 @@ class ElasticEmailCest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function itCanGenerateSubscribers() {
|
function itCanGenerateSubscribers() {
|
||||||
$subscribers = explode(';', $this->mailer->getSubscribers());
|
$subscribers = $this->mailer->getSubscribers();
|
||||||
expect(count($subscribers))->equals(3);
|
expect(count($subscribers))->equals(3);
|
||||||
// test proper handling of spaces between first/last name
|
// test proper handling of spaces between first/last name
|
||||||
expect($subscribers[0])->equals(
|
expect($subscribers[0])->equals(
|
||||||
@@ -57,11 +57,7 @@ class ElasticEmailCest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function itCanGenerateBody() {
|
function itCanGenerateBody() {
|
||||||
$urlEncodedBody = $this->mailer->getBody();
|
$body = explode('&', $this->mailer->getBody());
|
||||||
expect($urlEncodedBody)
|
|
||||||
->contains(urlencode($this->data['newsletter']['subject']));
|
|
||||||
|
|
||||||
$body = explode('&', urldecode($urlEncodedBody));
|
|
||||||
expect($body[0])
|
expect($body[0])
|
||||||
->equals("api_key=" . $this->data['api_key']);
|
->equals("api_key=" . $this->data['api_key']);
|
||||||
expect($body[1])
|
expect($body[1])
|
||||||
@@ -84,23 +80,10 @@ class ElasticEmailCest {
|
|||||||
->equals('1.0');
|
->equals('1.0');
|
||||||
expect($request['method'])
|
expect($request['method'])
|
||||||
->equals('POST');
|
->equals('POST');
|
||||||
expect($request['headers']['Content-Type'])
|
|
||||||
->equals('application/x-www-form-urlencoded');
|
|
||||||
expect($request['body'])
|
expect($request['body'])
|
||||||
->equals($this->mailer->getBody());
|
->equals($this->mailer->getBody());
|
||||||
}
|
}
|
||||||
|
|
||||||
function itCannotSendWithoutSubscribers() {
|
|
||||||
$mailer = new ElasticEmail(
|
|
||||||
$this->data['api_key'],
|
|
||||||
$this->data['from_email'],
|
|
||||||
$this->data['from_name'],
|
|
||||||
$this->data['newsletter'],
|
|
||||||
array()
|
|
||||||
);
|
|
||||||
expect($mailer->send())->equals(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
function itCannotSendWithoutProperAPIKey() {
|
function itCannotSendWithoutProperAPIKey() {
|
||||||
$mailer = new ElasticEmail(
|
$mailer = new ElasticEmail(
|
||||||
'someapikey',
|
'someapikey',
|
||||||
|
144
tests/unit/Mailer/MailGunCest.php
Normal file
144
tests/unit/Mailer/MailGunCest.php
Normal file
@@ -0,0 +1,144 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use MailPoet\Mailer\MailGun;
|
||||||
|
|
||||||
|
class MailGunCest {
|
||||||
|
function __construct() {
|
||||||
|
$this->data = array(
|
||||||
|
'domain' => 'mrcasual.com',
|
||||||
|
'api_key' => 'key-6cf5g5qjzenk-7nodj44gdt8phe6vam2',
|
||||||
|
'from_email' => 'do-not-reply@mailpoet.com',
|
||||||
|
'from_name' => 'Phoenix',
|
||||||
|
'newsletter' => array(
|
||||||
|
'subject' => 'Testing MailGun',
|
||||||
|
'body' => 'this is a test message....'
|
||||||
|
),
|
||||||
|
'subscribers' => array(
|
||||||
|
array(
|
||||||
|
'email' => 'mailpoet-test1@mailinator.com',
|
||||||
|
'last_name' => 'Smith'
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'email' => 'mailpoet-test2@mailinator.com',
|
||||||
|
'first_name' => 'Jane',
|
||||||
|
'last_name' => 'Smith'
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'email' => 'mailpoet-test3@mailinator.com',
|
||||||
|
),
|
||||||
|
array()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->mailer = new MailGun(
|
||||||
|
$this->data['domain'],
|
||||||
|
$this->data['api_key'],
|
||||||
|
$this->data['from_email'],
|
||||||
|
$this->data['from_name'],
|
||||||
|
$this->data['newsletter'],
|
||||||
|
$this->data['subscribers']);
|
||||||
|
}
|
||||||
|
|
||||||
|
function itCanGenerateSubscribers() {
|
||||||
|
$subscribers = $this->mailer->getSubscribers();
|
||||||
|
expect(count($subscribers))->equals(2);
|
||||||
|
expect(count($subscribers['emails']))->equals(3);
|
||||||
|
expect(count($subscribers['data']))->equals(3);
|
||||||
|
// test proper handling of spaces between first/last name
|
||||||
|
expect($subscribers['emails'][0])->equals(
|
||||||
|
sprintf(
|
||||||
|
'%s <%s>',
|
||||||
|
$this->data['subscribers'][0]['last_name'],
|
||||||
|
$this->data['subscribers'][0]['email'])
|
||||||
|
);
|
||||||
|
expect($subscribers['emails'][1])->equals(
|
||||||
|
sprintf(
|
||||||
|
'%s %s <%s>', $this->data['subscribers'][1]['first_name'],
|
||||||
|
$this->data['subscribers'][1]['last_name'],
|
||||||
|
$this->data['subscribers'][1]['email']
|
||||||
|
)
|
||||||
|
);
|
||||||
|
expect($subscribers['data'][0])->equals(
|
||||||
|
array(
|
||||||
|
sprintf(
|
||||||
|
'%s <%s>',
|
||||||
|
$this->data['subscribers'][0]['last_name'],
|
||||||
|
$this->data['subscribers'][0]['email']
|
||||||
|
) => array()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function itCanGenerateBody() {
|
||||||
|
$body = $this->mailer->getBody();
|
||||||
|
expect(substr_count($body, 'to='))->equals(3);
|
||||||
|
$body = explode('&', $body);
|
||||||
|
expect($body[0])
|
||||||
|
->equals('from=' .
|
||||||
|
sprintf(
|
||||||
|
'%s <%s>', $this->data['from_name'], $this->data['from_email']
|
||||||
|
)
|
||||||
|
);
|
||||||
|
expect($body[1])
|
||||||
|
->equals('to=' . $this->mailer->getSubscribers()['emails'][0]
|
||||||
|
);
|
||||||
|
expect($body[2])
|
||||||
|
->equals('to=' . $this->mailer->getSubscribers()['emails'][1]
|
||||||
|
);
|
||||||
|
expect($body[3])
|
||||||
|
->equals('to=' . $this->mailer->getSubscribers()['emails'][2]
|
||||||
|
);
|
||||||
|
expect($body[4])
|
||||||
|
->equals('recipient-variables=' .
|
||||||
|
json_encode($this->mailer->getSubscribers()['data'])
|
||||||
|
);
|
||||||
|
expect($body[5])->equals('subject=' . $this->data['newsletter']['subject']);
|
||||||
|
expect($body[6])->equals('text=' . $this->data['newsletter']['body']);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function itCanCreateRequest() {
|
||||||
|
$request = $this->mailer->request();
|
||||||
|
expect($request['timeout'])
|
||||||
|
->equals(10);
|
||||||
|
expect($request['httpversion'])
|
||||||
|
->equals('1.0');
|
||||||
|
expect($request['method'])
|
||||||
|
->equals('POST');
|
||||||
|
expect($request['headers']['Content-Type'])
|
||||||
|
->equals('application/x-www-form-urlencoded');
|
||||||
|
expect($request['headers']['Authorization'])
|
||||||
|
->equals('Basic ' . base64_encode('api:' . $this->data['api_key']));
|
||||||
|
expect($request['body'])
|
||||||
|
->equals($this->mailer->getBody());
|
||||||
|
}
|
||||||
|
|
||||||
|
function itCannotSendWithoutProperAPIKey() {
|
||||||
|
$mailer = new MailGun(
|
||||||
|
$this->data['domain'],
|
||||||
|
'someapikey',
|
||||||
|
$this->data['from_email'],
|
||||||
|
$this->data['from_name'],
|
||||||
|
$this->data['newsletter'],
|
||||||
|
$this->data['subscribers']
|
||||||
|
);
|
||||||
|
expect($mailer->send())->equals(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
function itCannotSendWithoutProperDomain() {
|
||||||
|
$mailer = new MailGun(
|
||||||
|
'mailpoet.com',
|
||||||
|
$this->data['api_key'],
|
||||||
|
$this->data['from_email'],
|
||||||
|
$this->data['from_name'],
|
||||||
|
$this->data['newsletter'],
|
||||||
|
$this->data['subscribers']
|
||||||
|
);
|
||||||
|
expect($mailer->send())->equals(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
function itCanSend() {
|
||||||
|
$result = $this->mailer->send();
|
||||||
|
expect($result)->equals(true);
|
||||||
|
}
|
||||||
|
}
|
@@ -6,24 +6,24 @@ class SendGridCest {
|
|||||||
function __construct() {
|
function __construct() {
|
||||||
$this->data = array(
|
$this->data = array(
|
||||||
'api_key' => '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',
|
'api_key' => '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',
|
||||||
'from_email' => 'vlad@mailpoet.com',
|
'from_email' => 'do-not-reply@mailpoet.com',
|
||||||
'from_name' => 'Vlad',
|
'from_name' => 'Phoenix',
|
||||||
'newsletter' => array(
|
'newsletter' => array(
|
||||||
'subject' => 'hi there!',
|
'subject' => 'Testing SendGrid',
|
||||||
'body' => 'this is a test message....'
|
'body' => 'this is a test message....'
|
||||||
),
|
),
|
||||||
'subscribers' => array(
|
'subscribers' => array(
|
||||||
array(
|
array(
|
||||||
'email' => 'johndoe@mailpoet.com',
|
'email' => 'mailpoet-test1@mailinator.com',
|
||||||
'last_name' => 'Smith'
|
'last_name' => 'Smith'
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
'email' => 'janesmith@mailpoet.com',
|
'email' => 'mailpoet-test2@mailinator.com',
|
||||||
'first_name' => 'Jane',
|
'first_name' => 'Jane',
|
||||||
'last_name' => 'Smith'
|
'last_name' => 'Smith'
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
'email' => 'someone@mailpoet.com',
|
'email' => 'mailpoet-test3@mailinator.com',
|
||||||
),
|
),
|
||||||
array()
|
array()
|
||||||
)
|
)
|
||||||
@@ -57,61 +57,54 @@ class SendGridCest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function itCanGenerateBody() {
|
function itCanGenerateBody() {
|
||||||
$urlEncodedBody = $this->mailer->getBody();
|
$body = explode('&', $this->mailer->getBody());
|
||||||
expect($urlEncodedBody)
|
|
||||||
->contains(urlencode($this->data['newsletter']['subject']));
|
|
||||||
|
|
||||||
$body = explode('&', urldecode($urlEncodedBody));
|
|
||||||
expect($body[0])
|
expect($body[0])
|
||||||
->equals("from=" . sprintf('%s <%s>', $this->data['from_name'], $this->data['from_email']));
|
->equals("to=" .
|
||||||
|
sprintf(
|
||||||
|
'%s <%s>', $this->data['from_name'], $this->data['from_email']
|
||||||
|
)
|
||||||
|
);
|
||||||
expect($body[1])
|
expect($body[1])
|
||||||
->contains($this->data['subscribers'][0]['email']);
|
->equals("from=" .
|
||||||
|
sprintf(
|
||||||
|
'%s <%s>', $this->data['from_name'], $this->data['from_email']
|
||||||
|
)
|
||||||
|
);
|
||||||
expect($body[2])
|
expect($body[2])
|
||||||
->equals("to=" . $this->data['from_email']);
|
->contains($this->data['subscribers'][0]['email']);
|
||||||
expect($body[3])
|
expect($body[3])
|
||||||
->equals("subject=" . $this->data['newsletter']['subject']);
|
->equals("subject=" . $this->data['newsletter']['subject']);
|
||||||
expect($body[4])
|
expect($body[4])
|
||||||
->equals("html=" . $this->data['newsletter']['body']);
|
->equals("html=" . $this->data['newsletter']['body']);
|
||||||
}
|
}
|
||||||
|
|
||||||
function itCanCreateRequest() {
|
function itCanCreateRequest() {
|
||||||
$request = $this->mailer->request();
|
$request = $this->mailer->request();
|
||||||
expect($request['timeout'])
|
expect($request['timeout'])
|
||||||
->equals(10);
|
->equals(10);
|
||||||
expect($request['httpversion'])
|
expect($request['httpversion'])
|
||||||
->equals('1.1');
|
->equals('1.1');
|
||||||
expect($request['method'])
|
expect($request['method'])
|
||||||
->equals('POST');
|
->equals('POST');
|
||||||
expect($request['headers']['Content-Type'])
|
expect($request['headers']['Authorization'])
|
||||||
->equals('application/x-www-form-urlencoded');
|
->equals('Bearer ' . $this->data['api_key']);
|
||||||
expect($request['body'])
|
expect($request['body'])
|
||||||
->equals($this->mailer->getBody());
|
->equals($this->mailer->getBody());
|
||||||
}
|
}
|
||||||
|
|
||||||
function itCannotSendWithoutSubscribers() {
|
function itCannotSendWithoutProperAPIKey() {
|
||||||
$mailer = new SendGrid(
|
$mailer = new SendGrid(
|
||||||
$this->data['api_key'],
|
'someapikey',
|
||||||
$this->data['from_email'],
|
$this->data['from_email'],
|
||||||
$this->data['from_name'],
|
$this->data['from_name'],
|
||||||
$this->data['newsletter'],
|
$this->data['newsletter'],
|
||||||
array()
|
$this->data['subscribers']
|
||||||
);
|
);
|
||||||
expect($mailer->send())->equals(false);
|
expect($mailer->send())->equals(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
function itCannotSendWithoutProperAPIKey() {
|
function itCanSend() {
|
||||||
$mailer = new SendGrid(
|
$result = $this->mailer->send();
|
||||||
'someapikey',
|
expect($result)->equals(true);
|
||||||
$this->data['from_email'],
|
}
|
||||||
$this->data['from_name'],
|
|
||||||
$this->data['newsletter'],
|
|
||||||
$this->data['subscribers']
|
|
||||||
);
|
|
||||||
expect($mailer->send())->equals(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
function itCanSend() {
|
|
||||||
$result = $this->mailer->send();
|
|
||||||
expect($result)->equals(true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user