AdapterTest.php in Mail System 8.4
File
tests/src/Unit/AdapterTest.php
View source
<?php
namespace Drupal\Tests\mailsystem\Unit;
use Drupal\Core\Mail\MailInterface;
use Drupal\mailsystem\Adapter;
use Drupal\Tests\UnitTestCase;
class AdapterTest extends UnitTestCase {
protected $adapter;
protected $sender;
protected $formatter;
protected function setUp() {
$this->formatter = new Test();
$this->sender = new Test();
$this->adapter = new Adapter($this->formatter, $this->sender);
}
protected function getEmptyMessage() {
return [
'subject' => 'test',
'message' => 'message',
'headers' => [],
];
}
public function testFormatting() {
$message = $this->adapter
->format($this
->getEmptyMessage());
$this
->assertEquals(Test::TEST_SUBJECT, $message['subject'], 'Subject match');
$this
->assertEquals(Test::TEST_BODY, $message['body'], 'Body match');
$this
->assertEquals([
Test::TEST_HEADER_NAME => Test::TEST_HEADER_VALUE,
], $message['headers'], 'Header match');
}
public function testSending() {
$message = $this
->getEmptyMessage();
$this
->assertFalse($this->adapter
->mail($message), 'Sending message failed as expected');
$message['subject'] = Test::SEND_SUCCESS_SUBJECT;
$this
->assertTrue($this->adapter
->mail($message), 'Sending message successful as expected');
}
}
class Test implements MailInterface {
const TEST_SUBJECT = 'Subject';
const TEST_BODY = 'Vivamus varius commodo leo at eleifend. Nunc vestibulum dolor eget turpis pulvinar volutpat.';
const TEST_HEADER_NAME = 'X-System';
const TEST_HEADER_VALUE = 'D8 PHP Unit test';
const SEND_SUCCESS_SUBJECT = 'Failed';
public function format(array $message) {
return [
'subject' => self::TEST_SUBJECT,
'body' => self::TEST_BODY,
'headers' => [
self::TEST_HEADER_NAME => self::TEST_HEADER_VALUE,
],
];
}
public function mail(array $message) {
return $message['subject'] == self::SEND_SUCCESS_SUBJECT;
}
}
Classes
Name |
Description |
AdapterTest |
Test the adapter class from mailsystem which is used as the mail plugin. |
Test |
Provides a test plugin to send emails. |