New Bridge class to send messages with our sending service.
This commit is contained in:
@ -1,21 +1,26 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace MailPoet\Mailer;
|
namespace MailPoet\Mailer;
|
||||||
|
|
||||||
|
use MailPoet\Models\Setting;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
// TODO: remove
|
class Bridge {
|
||||||
define('MAILPOET_BRIDGE_KEY', 'Xc_1zr7aOxqfD5s5xZqGvnvUJ7yc6HBvGBxk4PD3V2U');
|
function __construct($newsletter, $subscribers) {
|
||||||
|
|
||||||
class Mailer {
|
|
||||||
|
|
||||||
protected $newsletter;
|
|
||||||
protected $subscribers;
|
|
||||||
|
|
||||||
function __construct(array $newsletter, array $subscribers) {
|
|
||||||
$this->newsletter = $newsletter;
|
$this->newsletter = $newsletter;
|
||||||
$this->subscribers = $subscribers;
|
$this->subscribers = $subscribers;
|
||||||
$this->fromAddress = 'info@mailpoet.com';
|
|
||||||
$this->replyToAddress = 'info@mailpoet.com';
|
$this->from_name =
|
||||||
|
Setting::where('name', 'from_name')
|
||||||
|
->findOne()->value;
|
||||||
|
|
||||||
|
$this->from_address =
|
||||||
|
Setting::where('name', 'from_address')
|
||||||
|
->findOne()->value;
|
||||||
|
|
||||||
|
$this->api_key =
|
||||||
|
Setting::where('name', 'api_key')
|
||||||
|
->findOne()->value;
|
||||||
}
|
}
|
||||||
|
|
||||||
function messages() {
|
function messages() {
|
||||||
@ -28,26 +33,23 @@ class Mailer {
|
|||||||
|
|
||||||
function generateMessage($subscriber) {
|
function generateMessage($subscriber) {
|
||||||
return array(
|
return array(
|
||||||
'from' => (array(
|
'subject' => $this->newsletter['subject'],
|
||||||
'address' => $this->fromAddress,
|
|
||||||
'name' => ''
|
|
||||||
)),
|
|
||||||
'to' => (array(
|
'to' => (array(
|
||||||
'address' => $subscriber['email'],
|
'address' => $subscriber['email'],
|
||||||
'name' => $subscriber['first_name'].' '.$subscriber['last_name']
|
'name' => $subscriber['first_name'].' '.$subscriber['last_name']
|
||||||
)),
|
)),
|
||||||
'reply_to' => (array(
|
'from' => (array(
|
||||||
'address' => $this->replyToAddress,
|
'address' => $this->from_address,
|
||||||
'name' => ''
|
'name' => $this->from_name
|
||||||
)),
|
)),
|
||||||
'subject' => $this->newsletter['subject'],
|
'text' => "",
|
||||||
'html' => $this->newsletter['body'],
|
'html' => $this->newsletter['body']
|
||||||
'text' => ""
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function auth() {
|
function auth() {
|
||||||
$auth = 'Basic ' . base64_encode('api:' . MAILPOET_BRIDGE_KEY);
|
$auth = 'Basic '
|
||||||
|
. base64_encode('api:' . $this->api_key);
|
||||||
return $auth;
|
return $auth;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,7 +72,10 @@ class Mailer {
|
|||||||
'https://bridge.mailpoet.com/api/messages',
|
'https://bridge.mailpoet.com/api/messages',
|
||||||
$this->request()
|
$this->request()
|
||||||
);
|
);
|
||||||
$success = wp_remote_retrieve_response_code($result) === 201;
|
|
||||||
|
$success =
|
||||||
|
(wp_remote_retrieve_response_code($result)===201);
|
||||||
|
|
||||||
return $success;
|
return $success;
|
||||||
}
|
}
|
||||||
}
|
}
|
112
tests/unit/Mailer/BridgeCest.php
Normal file
112
tests/unit/Mailer/BridgeCest.php
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
<?php
|
||||||
|
use MailPoet\Mailer\Bridge;
|
||||||
|
use MailPoet\Models\Setting;
|
||||||
|
|
||||||
|
class BridgeCest {
|
||||||
|
|
||||||
|
function _before() {
|
||||||
|
$from_name = Setting::create();
|
||||||
|
$from_name->name = 'from_name';
|
||||||
|
$from_name->value = 'Marco';
|
||||||
|
$from_name->save();
|
||||||
|
|
||||||
|
$from_address = Setting::create();
|
||||||
|
$from_address->name = 'from_address';
|
||||||
|
$from_address->value = 'marco@mailpoet.com';
|
||||||
|
$from_address->save();
|
||||||
|
|
||||||
|
$api_key = Setting::create();
|
||||||
|
$api_key->name = 'api_key';
|
||||||
|
$api_key->value = 'xxxccc';
|
||||||
|
$api_key->save();
|
||||||
|
|
||||||
|
$this->newsletter = array(
|
||||||
|
'subject' => 'A test message from mp3',
|
||||||
|
'body' => 'Hey, I am mp3, chapter two.'
|
||||||
|
);
|
||||||
|
$this->subscribers = array(
|
||||||
|
array(
|
||||||
|
'first_name' => 'Marco',
|
||||||
|
'last_name' => 'Lisci',
|
||||||
|
'email' => 'marco@mailpoet.com'
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'first_name' => 'Jonathan',
|
||||||
|
'last_name' => 'Labreuille',
|
||||||
|
'email' => 'jonathan@mailpoet.com'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->mailer = new Bridge(
|
||||||
|
$this->newsletter,
|
||||||
|
$this->subscribers
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function itCanDoBasicAuth() {
|
||||||
|
$api_key = Setting::where('name', 'api_key')
|
||||||
|
->findOne()->value;
|
||||||
|
expect($this->mailer->auth())->equals(
|
||||||
|
'Basic '
|
||||||
|
. base64_encode('api:' . $api_key)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function itCanGenerateACorrectMessage() {
|
||||||
|
$subscriber = $this->subscribers[0];
|
||||||
|
$message =
|
||||||
|
$this->mailer->generateMessage($subscriber);
|
||||||
|
|
||||||
|
expect($message['to']['address'])
|
||||||
|
->equals($subscriber['email']);
|
||||||
|
|
||||||
|
expect($message['to']['name'])
|
||||||
|
->equals($subscriber['first_name'].' '.$subscriber['last_name']);
|
||||||
|
|
||||||
|
expect($message['subject'])
|
||||||
|
->equals($this->newsletter['subject']);
|
||||||
|
|
||||||
|
expect($message['html'])
|
||||||
|
->equals($this->newsletter['body']);
|
||||||
|
|
||||||
|
expect($message['text'])
|
||||||
|
->equals('');
|
||||||
|
}
|
||||||
|
|
||||||
|
function itCanGenerateCorrectMessages() {
|
||||||
|
$messages = $this->mailer->messages();
|
||||||
|
|
||||||
|
expect(count($messages))
|
||||||
|
->equals(count($this->subscribers));
|
||||||
|
}
|
||||||
|
|
||||||
|
function itCanCreateARequest() {
|
||||||
|
$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/json');
|
||||||
|
}
|
||||||
|
|
||||||
|
function itCanSend() {
|
||||||
|
/* $result = $this->mailer->send(); */
|
||||||
|
/* expect($result)->equals(true); */
|
||||||
|
}
|
||||||
|
|
||||||
|
function _after() {
|
||||||
|
Setting::where('name', 'from_name')
|
||||||
|
->findOne()->delete();
|
||||||
|
Setting::where('name', 'from_address')
|
||||||
|
->findOne()->delete();
|
||||||
|
Setting::where('name', 'api_key')
|
||||||
|
->findOne()->delete();
|
||||||
|
}
|
||||||
|
}
|
@ -1,104 +0,0 @@
|
|||||||
<?php
|
|
||||||
use MailPoet\Mailer\Mailer;
|
|
||||||
|
|
||||||
class MailerCest {
|
|
||||||
|
|
||||||
function _before() {
|
|
||||||
$this->newsletter = array(
|
|
||||||
'subject' => 'A test message',
|
|
||||||
'body' => 'Test, one, two, three.'
|
|
||||||
);
|
|
||||||
$this->subscribers = array(
|
|
||||||
array(
|
|
||||||
'first_name' => 'Marco',
|
|
||||||
'last_name' => 'Lisci',
|
|
||||||
'email' => 'marco@mailpoet.com'
|
|
||||||
),
|
|
||||||
array(
|
|
||||||
'first_name' => 'Test',
|
|
||||||
'last_name' => 'MailPoet',
|
|
||||||
'email' => 'testmailpoet@gmail.com'
|
|
||||||
),
|
|
||||||
array(
|
|
||||||
'first_name' => 'Vlad',
|
|
||||||
'last_name' => '',
|
|
||||||
'email' => 'vlad@mailpoet.com'
|
|
||||||
),
|
|
||||||
array(
|
|
||||||
'first_name' => 'Jonathan',
|
|
||||||
'last_name' => 'Labreuille',
|
|
||||||
'email' => 'jonathan@mailpoet.com'
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$this->mailer = new Mailer(
|
|
||||||
$this->newsletter,
|
|
||||||
$this->subscribers
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function itCanDoBasicAuth() {
|
|
||||||
expect($this->mailer->auth())->equals(
|
|
||||||
'Basic YXBpOlhjXzF6cjdhT3hxZkQ1czV4WnFHdm52VUo3eWM2SEJ2R0J4azRQRDNWMlU='
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function itCanGenerateACorrectMessage() {
|
|
||||||
$subscriber = $this->subscribers[0];
|
|
||||||
$message = $this->mailer->generateMessage($subscriber);
|
|
||||||
expect($message['to']['address'])
|
|
||||||
->equals($subscriber['email']);
|
|
||||||
expect($message['to']['name'])
|
|
||||||
->equals($subscriber['first_name'].' '.$subscriber['last_name']);
|
|
||||||
expect($message['reply_to']['address'])
|
|
||||||
->equals('info@mailpoet.com');
|
|
||||||
expect($message['reply_to']['name'])
|
|
||||||
->equals('');
|
|
||||||
expect($message['subject'])
|
|
||||||
->equals($this->newsletter['subject']);
|
|
||||||
expect($message['html'])
|
|
||||||
->equals($this->newsletter['body']);
|
|
||||||
expect($message['text'])
|
|
||||||
->equals('');
|
|
||||||
}
|
|
||||||
|
|
||||||
function itCanGenerateCorrectMessages() {
|
|
||||||
$messages = $this->mailer->messages();
|
|
||||||
expect(count($messages))
|
|
||||||
->equals(count($this->subscribers));
|
|
||||||
for ($i=0; $i<count($this->subscribers); $i++) {
|
|
||||||
expect($messages[$i]['to']['address'])
|
|
||||||
->equals($this->subscribers[$i]['email']);
|
|
||||||
expect($messages[$i]['to']['name'])
|
|
||||||
->equals(
|
|
||||||
$this->subscribers[$i]['first_name']
|
|
||||||
.' '
|
|
||||||
.$this->subscribers[$i]['last_name']
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function itCanCreateARequest() {
|
|
||||||
$request = $this->mailer->request();
|
|
||||||
expect($request['timeout'])
|
|
||||||
->equals(10);
|
|
||||||
expect($request['httpversion'])
|
|
||||||
->equals('1.0');
|
|
||||||
expect($request['method'])
|
|
||||||
->equals('POST');
|
|
||||||
expect($request['headers']['Authorization'])
|
|
||||||
->equals(
|
|
||||||
'Basic YXBpOlhjXzF6cjdhT3hxZkQ1czV4WnFHdm52VUo3eWM2SEJ2R0J4azRQRDNWMlU='
|
|
||||||
);
|
|
||||||
expect($request['headers']['Content-Type'])
|
|
||||||
->equals('application/json');
|
|
||||||
expect($request['body'])
|
|
||||||
->equals(
|
|
||||||
'[{"from":{"address":"info@mailpoet.com","name":""},"to":{"address":"marco@mailpoet.com","name":"Marco Lisci"},"reply_to":{"address":"info@mailpoet.com","name":""},"subject":"A test message","html":"Test, one, two, three.","text":""},{"from":{"address":"info@mailpoet.com","name":""},"to":{"address":"testmailpoet@gmail.com","name":"Test MailPoet"},"reply_to":{"address":"info@mailpoet.com","name":""},"subject":"A test message","html":"Test, one, two, three.","text":""},{"from":{"address":"info@mailpoet.com","name":""},"to":{"address":"vlad@mailpoet.com","name":"Vlad "},"reply_to":{"address":"info@mailpoet.com","name":""},"subject":"A test message","html":"Test, one, two, three.","text":""},{"from":{"address":"info@mailpoet.com","name":""},"to":{"address":"jonathan@mailpoet.com","name":"Jonathan Labreuille"},"reply_to":{"address":"info@mailpoet.com","name":""},"subject":"A test message","html":"Test, one, two, three.","text":""}]'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function itCanSend() {
|
|
||||||
/* $result = $this->mailer->send(); */
|
|
||||||
/* expect($result)->equals(true); */
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user