class EmailTest in Message Notify 8
Unit tests for the Email notifier.
@coversDefaultClass \Drupal\message_notify\Plugin\Notifier\Email
@group message_notify
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\message_notify\Unit\Plugin\Notifier\EmailTest
Expanded class hierarchy of EmailTest
File
- tests/
src/ Unit/ Plugin/ Notifier/ EmailTest.php, line 25
Namespace
Drupal\Tests\message_notify\Unit\Plugin\NotifierView source
class EmailTest extends UnitTestCase {
/**
* Digest configuration.
*
* @var array
*/
protected $configuration = [];
/**
* Mocked entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The mocked mail manager service.
*
* @var \Drupal\Core\Mail\MailManagerInterface
*/
protected $mailManager;
/**
* The rendering service.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* Plugin definition.
*
* @var array
*/
protected $pluginDefinition = [
'viewModes' => [
'mail_subject',
'mail_body',
],
];
/**
* Plugin ID.
*
* @var string
*/
protected $pluginId;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->entityTypeManager = $this
->prophesize(EntityTypeManagerInterface::class)
->reveal();
$this->mailManager = $this
->prophesize(MailManagerInterface::class)
->reveal();
$this->renderer = $this
->prophesize(RendererInterface::class)
->reveal();
$this->pluginId = $this
->randomMachineName();
$this->pluginDefinition['title'] = $this
->randomMachineName();
}
/**
* Test the send method.
*
* @covers ::send
* @covers ::setMessage
*/
public function testSend() {
// Mock a message object.
$message = $this
->prophesize(MessageInterface::class);
$account = $this
->prophesize(UserInterface::class);
$account
->id()
->willReturn(42);
$account
->getEmail()
->willReturn('foo@foo.com');
$account
->getPreferredLangcode()
->willReturn(Language::LANGCODE_DEFAULT);
$message
->getOwner()
->willReturn($account
->reveal());
$message
->getOwnerId()
->willReturn(42);
$template = $this
->prophesize(MessageTemplateInterface::class)
->reveal();
$message
->getTemplate()
->willReturn($template);
$message
->save()
->willReturn(1);
// Mock view builder.
$view_builder = $this
->prophesize(EntityViewBuilderInterface::class)
->reveal();
$entity_type_manager = $this
->prophesize(EntityTypeManagerInterface::class);
$entity_type_manager
->getViewBuilder('message')
->willReturn($view_builder);
$this->entityTypeManager = $entity_type_manager
->reveal();
$notifier = $this
->getNotifier();
$notifier
->setMessage($message
->reveal());
$this
->assertNull($notifier
->send());
}
/**
* Test sending without a message.
*
* @covers ::send
*/
public function testSendNoMessage() {
$this
->expectException(\AssertionError::class);
$this
->expectExceptionMessage('No message is set for this notifier.');
$notifier = $this
->getNotifier();
$notifier
->send();
}
/**
* Test sending without an email.
*
* @covers ::deliver
*/
public function testSendNoEmail() {
$this
->expectException(MessageNotifyException::class);
$this
->expectExceptionMessage('It is not possible to send a Message to an anonymous owner. You may set an owner using ::setOwner() or pass a "mail" to the $options array.');
$message = $this
->prophesize(MessageInterface::class);
$account = $this
->prophesize(UserInterface::class)
->reveal();
$message
->getOwner()
->willReturn($account);
$notifier = $this
->getNotifier($message
->reveal());
$notifier
->deliver([]);
}
/**
* Constructs an email notifier.
*
* @param \Drupal\message\MessageInterface $message
* (optional) The message to construct the notifier with.
*
* @return \Drupal\message_notify\Plugin\Notifier\Email
* The email notifier plugin.
*/
protected function getNotifier(MessageInterface $message = NULL) {
$logger = $this
->prophesize(LoggerChannelInterface::class)
->reveal();
return new Email($this->configuration, $this->pluginId, $this->pluginDefinition, $logger, $this->entityTypeManager, $this->renderer, $message, $this->mailManager);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EmailTest:: |
protected | property | Digest configuration. | |
EmailTest:: |
protected | property | Mocked entity type manager. | |
EmailTest:: |
protected | property | The mocked mail manager service. | |
EmailTest:: |
protected | property | Plugin definition. | |
EmailTest:: |
protected | property | Plugin ID. | |
EmailTest:: |
protected | property | The rendering service. | |
EmailTest:: |
protected | function | Constructs an email notifier. | |
EmailTest:: |
public | function |
Overrides UnitTestCase:: |
|
EmailTest:: |
public | function | Test the send method. | |
EmailTest:: |
public | function | Test sending without an email. | |
EmailTest:: |
public | function | Test sending without a message. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |