available_mailer_methods = array( array( 'method' => 'AmazonSES', 'region' => 'us-west-2', 'access_key' => '1234567890', 'secret_key' => 'abcdefghijk', ), array( 'method' => 'ElasticEmail', 'api_key' => 'abcdefghijk' ), array( 'method' => 'MailGun', 'domain' => 'example.com', 'api_key' => 'abcdefghijk' ), array( 'method' => 'MailPoet', 'mailpoet_api_key' => 'abcdefghijk' ), array( 'method' => 'SendGrid', 'api_key' => 'abcdefghijk' ), array( 'method' => 'PHPMail' ), array( 'method' => 'SMTP', 'host' => 'example.com', 'port' => 25, 'authentication' => true, 'login' => 'username', 'password' => 'password', 'encryption' => 'tls', ) ); $this->sender = array( 'name' => 'Sender', 'address' => 'staff@mailinator.com' ); $this->reply_to = array( 'name' => 'Reply To', 'address' => 'staff@mailinator.com' ); $this->mailer = array( 'method' => 'MailPoet', 'mailpoet_api_key' => getenv('WP_TEST_MAILER_MAILPOET_API') ? getenv('WP_TEST_MAILER_MAILPOET_API') : '1234567890' ); $this->subscriber = 'Recipient '; $this->newsletter = array( 'subject' => 'testing Mailer', 'body' => array( 'html' => 'HTML body', 'text' => 'TEXT body' ) ); } function testItRequiresMailerMethod() { try { $mailer = new Mailer(); $this->fail('Mailer did not throw an exception'); } catch(Exception $e) { expect($e->getMessage())->equals('Mailer is not configured.'); } } function testItRequiresSender() { try { $mailer = new Mailer($mailer = $this->mailer); $this->fail('Mailer did not throw an exception'); } catch(Exception $e) { expect($e->getMessage())->equals('Sender name and email are not configured.'); } } function testItCanConstruct() { $mailer = new Mailer($this->mailer, $this->sender, $this->reply_to); expect($mailer->sender['from_name'])->equals($this->sender['name']); expect($mailer->sender['from_email'])->equals($this->sender['address']); expect($mailer->reply_to['reply_to_name'])->equals($this->reply_to['name']); expect($mailer->reply_to['reply_to_email'])->equals($this->reply_to['address']); } function testItCanBuildKnownMailerInstances() { foreach($this->available_mailer_methods as $method) { $mailer = new Mailer($method, $this->sender); $mailer->buildMailer(); expect(get_class($mailer->mailer_instance)) ->equals('MailPoet\Mailer\Methods\\' . $method['method']); } } function testItThrowsUnknownMailerException() { try { $mailer = new Mailer(array('method' => 'Unknown'), $this->sender); $this->fail('Mailer did not throw an exception'); } catch(Exception $e) { expect($e->getMessage())->equals('Mailing method does not exist.'); } } function testItSetsReplyToAddressWhenOnlyNameIsAvailable() { $reply_to = array('name' => 'test'); $mailer = new Mailer($this->mailer, $this->sender, $reply_to); $reply_to = $mailer->getReplyTo(); expect($reply_to['reply_to_email'])->equals($this->sender['address']); } function testItCanTransformSubscriber() { $mailer = new Mailer($this->mailer, $this->sender, $this->reply_to); expect($mailer->transformSubscriber('test@email.com')) ->equals('test@email.com'); expect($mailer->transformSubscriber( array( 'email' => 'test@email.com' )) )->equals('test@email.com'); expect($mailer->transformSubscriber( array( 'first_name' => 'First', 'email' => 'test@email.com' )) )->equals('First '); expect($mailer->transformSubscriber( array( 'last_name' => 'Last', 'email' => 'test@email.com' )) )->equals('Last '); expect($mailer->transformSubscriber( array( 'first_name' => 'First', 'last_name' => 'Last', 'email' => 'test@email.com' )) )->equals('First Last '); } function testItCanSend() { if(getenv('WP_TEST_MAILER_ENABLE_SENDING') !== 'true') return; $mailer = new Mailer($this->mailer, $this->sender, $this->reply_to); expect($mailer->send($this->newsletter, $this->subscriber))->true(); } }