Commit b75f8353 authored by Adam Shepherd's avatar Adam Shepherd
Browse files

Issue #3271802 by AdamPS: Automatic test framework

parent c595be3f
Loading
Loading
Loading
Loading
+22 −0
Changes for src/Address.php: 22 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ namespace Drupal\symfony_mailer;

use Drupal\Core\Session\AccountInterface;
use Symfony\Component\Mime\Address as SymfonyAddress;
use Drupal\user\Entity\User;

/**
 * Defines the class for an Email address.
@@ -139,4 +140,25 @@ class Address implements AddressInterface {
    return $result;
  }

  /**
   * {@inheritdoc}
   *
   * Serialization is intended only for testing.
   *
   * @internal
   */
  public function __serialize() {
    return [$this->email, $this->displayName, $this->langcode, $this->account ? $this->account->id() : NULL];
  }

  /**
   * {@inheritdoc}
   */
  public function __unserialize(array $data) {
    [$this->email, $this->displayName, $this->langcode, $account_id] = $data;
    if ($account_id) {
      $this->account = User::load($account_id);
    }
  }

}
+17 −4
Changes for src/Email.php: 17 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -4,12 +4,14 @@ namespace Drupal\symfony_mailer;

use Drupal\Component\Render\MarkupInterface;
use Drupal\Component\Render\PlainTextOutput;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\Entity\User;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Mime\Email as SymfonyEmail;

@@ -144,6 +146,8 @@ class Email implements InternalEmailInterface {
   *   The entity type manager.
   * @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
   *   The theme manager.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   * @param string $type
   *   Type. @see self::getType()
   * @param string $sub_type
@@ -151,11 +155,12 @@ class Email implements InternalEmailInterface {
   * @param \Drupal\Core\Config\Entity\ConfigEntityInterface|null $entity
   *   (optional) Entity. @see self::getEntity()
   */
  public function __construct(MailerInterface $mailer, RendererInterface $renderer, EntityTypeManagerInterface $entity_type_manager, ThemeManagerInterface $theme_manager, string $type, string $sub_type, ?ConfigEntityInterface $entity) {
  public function __construct(MailerInterface $mailer, RendererInterface $renderer, EntityTypeManagerInterface $entity_type_manager, ThemeManagerInterface $theme_manager, ConfigFactoryInterface $config_factory, string $type, string $sub_type, ?ConfigEntityInterface $entity) {
    $this->mailer = $mailer;
    $this->renderer = $renderer;
    $this->entityTypeManager = $entity_type_manager;
    $this->themeManager = $theme_manager;
    $this->configFactory = $config_factory;
    $this->type = $type;
    $this->subType = $sub_type;
    $this->entity = $entity;
@@ -185,6 +190,7 @@ class Email implements InternalEmailInterface {
      $container->get('renderer'),
      $container->get('entity_type.manager'),
      $container->get('theme.manager'),
      $container->get('config.factory'),
      $type,
      $sub_type,
      $entity
@@ -546,15 +552,22 @@ class Email implements InternalEmailInterface {
   * @internal
   */
  public function __serialize() {
    return [$this->subject, $this->addresses, $this->inner];
    // Exclude $this->params, $this->variables as they may not serialize.
    return [$this->type, $this->subType, $this->entity ? $this->entity->id() : '', $this->phase, $this->subject, $this->langcode, $this->account ? $this->account->id() : '', $this->theme, $this->libraries, $this->transportDsn, $this->inner, $this->addresses, $this->sender];
  }

  /**
   * {@inheritdoc}
   */
  public function __unserialize(array $data) {
    $this->phase = self::PHASE_POST_SEND;
    [$this->subject, $this->addresses, $this->inner] = $data;
    [$this->type, $this->subType, $entity_id, $this->phase, $this->subject, $this->langcode, $account_id, $this->theme, $this->libraries, $this->transportDsn, $this->inner, $this->addresses, $this->sender] = $data;

    if ($entity_id) {
      $this->entity = $this->configFactory->get($entity_id);
    }
    if ($account_id) {
      $this->account = User::load($account_id);
    }
  }

}
+0 −36
Changes for tests/modules/symfony_mailer_test/src/MailerTest.php: 0 added lines, 36 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\symfony_mailer_test;

use Drupal\symfony_mailer\EmailInterface;
use Drupal\symfony_mailer\Processor\EmailProcessorCustomBase;

/**
 * Tracks sent emails for testing.
 */
class MailerTest extends EmailProcessorCustomBase {

  /**
   * {@inheritdoc}
   */
  public function postRender(EmailInterface $email) {
    $email->setTransportDsn('null://default');
  }

  /**
   * {@inheritdoc}
   */
  public function postSend(EmailInterface $email) {
    $emails = \Drupal::state()->get('mailer_test.emails', []);
    $emails[] = $email;
    \Drupal::state()->set('mailer_test.emails', $emails);
  }

  /**
   * {@inheritdoc}
   */
  protected function getWeight(int $phase) {
    return 10000;
  }

}
+74 −0
Changes for tests/modules/symfony_mailer_test/src/MailerTestService.php: 74 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\symfony_mailer_test;

use Drupal\Core\DestructableInterface;
use Drupal\Core\State\StateInterface;
use Drupal\symfony_mailer\EmailInterface;
use Drupal\symfony_mailer\Processor\EmailProcessorInterface;

/**
 * Tracks sent emails for testing.
 */
class MailerTestService implements MailerTestServiceInterface, EmailProcessorInterface, DestructableInterface {

  /**
   * The state service.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  protected $emails = [];

  /**
   * Constructs the MailerTestService.
   *
   * @param \Drupal\Core\State\StateInterface $state
   *   The state service.
   */
  public function __construct(StateInterface $state) {
    $this->state = $state;
    if ($existing_emails = $this->state->get(self::STATE_KEY, [])) {
      throw new \Exception(count($existing_emails) . ' emails have not been checked.');
    }
  }

  /**
   * {@inheritdoc}
   */
  public function destruct() {
    if ($this->emails) {
      $this->state->set(self::STATE_KEY, $this->emails);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function init(EmailInterface $email) {
    $email->addProcessor([$this, 'postRender'], EmailInterface::PHASE_POST_RENDER, 10000, static::class);
    $email->addProcessor([$this, 'postSend'], EmailInterface::PHASE_POST_SEND, 10000, static::class);
  }

  /**
   * Post-render function.
   *
   * @param \Drupal\symfony_mailer\EmailInterface $email
   *   The email.
   */
  public function postRender(EmailInterface $email) {
    $email->setTransportDsn('null://default');
  }

  /**
   * Post-send function.
   *
   * @param \Drupal\symfony_mailer\EmailInterface $email
   *   The email.
   */
  public function postSend(EmailInterface $email) {
    $this->emails[] = $email;
  }

}
+17 −0
Changes for tests/modules/symfony_mailer_test/src/MailerTestServiceInterface.php: 17 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\symfony_mailer_test;

use Drupal\symfony_mailer\EmailInterface;

/**
 * Tracks sent emails for testing.
 */
interface MailerTestServiceInterface {

  /**
   * The name of the state key used for storing sent emails.
   */
  const STATE_KEY = 'mailer_test.emails';

}
Loading