You are here

class PhpMailerSmtpUnitTest in PHPMailer SMTP 8

Same name and namespace in other branches
  1. 2.x tests/src/Unit/PhpMailerSmtpUnitTest.php \Drupal\Tests\phpmailer_smtp\Unit\PhpMailerSmtpUnitTest

Validates e-mail address extraction.

This needs more work, so that it actually works with PHPUnit. It maybe that due to it only testing a single function in the .module file and not a method in a class, it won't work at all.

@group phpmailer_smtp

Hierarchy

Expanded class hierarchy of PhpMailerSmtpUnitTest

File

tests/src/Unit/PhpMailerSmtpUnitTest.php, line 16

Namespace

Drupal\Tests\phpmailer_smtp\Unit
View source
class PhpMailerSmtpUnitTest extends UnitTestCase {

  /**
   * Tests e-mail address extraction using phpmailer_smtp_parse_address().
   */
  public function testAddressParser() {

    // Set up various test addresses according to RFC 5322.
    $this->addresses = [
      // addr-spec.
      [
        'mail' => 'user-1@domain.tld',
        'name' => '',
      ],
      // Invalid but supported angle-addr without preceding display-name.
      '<user-2@domain.tld>' => [
        'mail' => 'user-2@domain.tld',
        'name' => '',
      ],
      // Unquoted atom name-addr.
      'John Doe <user-3@domain.tld>' => [
        'mail' => 'user-3@domain.tld',
        'name' => 'John Doe',
      ],
      // Quoted atom name-addr.
      '"John Doe" <user-4@domain.tld>' => [
        'mail' => 'user-4@domain.tld',
        'name' => 'John Doe',
      ],
      // name-addr with a quoted-string in display-name.
      [
        'mail' => 'user-5@domain.tld',
        'name' => 'John "The Dude" Doe',
      ],
      // name-addr with a quoted-string and comma in display-name.
      [
        'mail' => 'user-6@domain.tld',
        'name' => 'John "The Dude" Doe (Foo, Bar)',
      ],
      // name-addr containing non-ASCII chars in display-name.
      [
        'mail' => 'user-7@domain.tld',
        'name' => 'Jöhn "The Düde" Döe',
      ],
    ];
    $all = [];

    // Validate each address format is correctly parsed.
    foreach ($this->addresses as $test => $address) {
      if (is_numeric($test)) {
        if ($address['name'] != '') {

          // Create a valid encoded email address.
          $test = '"' . addslashes(Unicode::mimeHeaderEncode($address['name'])) . '" <' . $address['mail'] . '>';
        }
        else {
          $test = $address['mail'];
        }
      }
      $result = phpmailer_smtp_parse_address($test);
      $this
        ->assertEqual($result[0], $address, t('Successfully extracted %email, %name from %address.', [
        '%email' => $result[0]['mail'],
        '%name' => $result[0]['name'] ? $result[0]['name'] : '(blank)',
        '%address' => $test,
      ]), 'PHPMailer SMTP');
      $all[] = $test;
    }

    // One final test with all addresses concatenated.
    $result = phpmailer_smtp_parse_address(implode(', ', $all));
    $expected_result = array_values($this->addresses);
    $this
      ->assertEqual($result, $expected_result, t('All concatenated e-mail addresses could be extracted.'), 'PHPMailer SMTP');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpMailerSmtpUnitTest::testAddressParser public function Tests e-mail address extraction using phpmailer_smtp_parse_address().
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340