<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

class EMailAddressTest extends \PHPUnit\Framework\TestCase
{
  /**
   * This method is called before each test.
   */
  protected function setUp() : void
  {
  }

  /**
   * This method is called after each test.
   */
  protected function tearDown() : void
  {
  }

  /**
   * @test
   * @covers \Kufer\Mail\Address\EMailAddress::getEMailName
   * @covers \Kufer\Mail\Address\EMailAddress::getEMailAddress
   */
  public function testGetter()
  {
    $address = new \Kufer\Mail\Address\EMailAddress('Vorname Name', 'test@example.org');
    $this->assertEquals('Vorname Name', $address->getEMailName());
    $this->assertEquals('test@example.org', $address->getEMailAddress());
  }

  /**
   * @test
   * @covers \Kufer\Mail\Address\EMailAddress::secureAddressString
   */
  public function secureAddressString_onlyemail_onlyemail()
  {
    $address = new \Kufer\Mail\Address\EMailAddress('', 'test@example.org');
    $this->assertEquals('test@example.org', \Kufer\Mail\Address\EMailAddress::secureAddressString($address));

    $addressTn = new \Teilnehmer();
    $addressTn->name = '';
    $addressTn->vorname = '';
    $addressTn->setValue2VarEx('email', 'test@example.org', 'kommunikation');
    $this->assertEquals('test@example.org', \Kufer\Mail\Address\EMailAddress::secureAddressString($addressTn));
  }

  /**
   * @test
   * @covers \Kufer\Mail\Address\EMailAddress::secureAddressString
   */
  public function secureAddressString_null_emptystring()
  {
    $address = new \Kufer\Mail\Address\EMailAddress('', null);
    $this->assertEquals('', \Kufer\Mail\Address\EMailAddress::secureAddressString($address));

    $addressTn = new \Teilnehmer();
    $addressTn->name = '';
    $addressTn->vorname = '';
    //$addressTn->setValue2VarEx('email', 'test@example.org', 'kommunikation');
    $this->assertEquals('', \Kufer\Mail\Address\EMailAddress::secureAddressString($addressTn));
  }

  /**
   * @test
   * @covers \Kufer\Mail\Address\EMailAddress::secureAddressString
   */
  public function secureAddressString_nameemail_nameenclosedemail()
  {
    $address = new \Kufer\Mail\Address\EMailAddress('Test', 'test@example.org');
    $this->assertEquals('test@example.org', \Kufer\Mail\Address\EMailAddress::secureAddressString($address));

    $addressTn = new \Teilnehmer();
    $addressTn->name = 'Name';
    $addressTn->vorname = 'Vorname';
    $addressTn->setValue2VarEx('email', 'test@example.org', 'kommunikation');
    $this->assertEquals('test@example.org', \Kufer\Mail\Address\EMailAddress::secureAddressString($addressTn));
  }

  /**
   * @test
   * @covers \Kufer\Mail\Address\EMailAddress::toCommaSeperatedEMails
   */
  public function toCommaSeperatedEMails_empty_emptystring()
  {
    $this->assertEquals('', \Kufer\Mail\Address\EMailAddress::toCommaSeperatedEMails(array()));
  }

  /**
   * @test
   * @covers \Kufer\Mail\Address\EMailAddress::toCommaSeperatedEMails
   */
  public function toCommaSeperatedEMails_onenoname_onenoenclosedmail()
  {
    $address = new \Kufer\Mail\Address\EMailAddress('', 'test@example.org');
    $this->assertEquals('test@example.org', \Kufer\Mail\Address\EMailAddress::toCommaSeperatedEMails(array($address)));
  }

  /**
   * @test
   * @covers \Kufer\Mail\Address\EMailAddress::toCommaSeperatedEMails
   */
  public function toCommaSeperatedEMails_twononame_twonoenclosedmail()
  {
    $addresses = array(
      new \Kufer\Mail\Address\EMailAddress('', 'test0@example.org'),
      new \Kufer\Mail\Address\EMailAddress('', 'test1@example.org')
    );
    $this->assertEquals('test0@example.org, test1@example.org', \Kufer\Mail\Address\EMailAddress::toCommaSeperatedEMails($addresses));
  }

  /**
   * @test
   * @covers \Kufer\Mail\Address\EMailAddress::toCommaSeperatedEMails
   */
  public function toCommaSeperatedEMails_on_oneenclosedmail()
  {
    $address = new \Kufer\Mail\Address\EMailAddress('Test', 'test@example.org');
    $this->assertEquals('test@example.org', \Kufer\Mail\Address\EMailAddress::toCommaSeperatedEMails(array($address)));
  }

  /**
   * @test
   * @covers \Kufer\Mail\Address\EMailAddress::toCommaSeperatedEMails
   */
  public function toCommaSeperatedEMails_two_twoenclosedmail()
  {
    $addresses = array(
      new \Kufer\Mail\Address\EMailAddress('Test0', 'test0@example.org'),
      new \Kufer\Mail\Address\EMailAddress('Test1', 'test1@example.org')
    );
    $this->assertEquals('test0@example.org, test1@example.org', \Kufer\Mail\Address\EMailAddress::toCommaSeperatedEMails($addresses));
  }

  /**
   * @test
   * @covers \Kufer\Mail\Address\EMailAddress::splitEMailsCommaSeperated
   */
  public function splitEMailsCommaSeperated_empty_empty()
  {
    $this->assertEquals(array(), \Kufer\Mail\Address\EMailAddress::splitEMailsCommaSeperated(''));
  }

  /**
   * @test
   * @covers \Kufer\Mail\Address\EMailAddress::splitEMailsCommaSeperated
   */
  public function splitEMailsCommaSeperated_onenoenclosedemail_onenoname()
  {
    $this->assertEquals(array(
      new \Kufer\Mail\Address\EMailAddress('', 'test@example.org')
    ), \Kufer\Mail\Address\EMailAddress::splitEMailsCommaSeperated('test@example.org'));
  }

  /**
   * @test
   * @covers \Kufer\Mail\Address\EMailAddress::splitEMailsCommaSeperated
   */
  public function splitEMailsCommaSeperated_twonoenclosedemail_twononame()
  {
    $this->assertEquals(array(
      new \Kufer\Mail\Address\EMailAddress('', 'test0@example.org'),
      new \Kufer\Mail\Address\EMailAddress('', 'test1@example.org')
    ), \Kufer\Mail\Address\EMailAddress::splitEMailsCommaSeperated('test0@example.org, test1@example.org'));

    $this->assertEquals(array(
      new \Kufer\Mail\Address\EMailAddress('', 'test0@example.org'),
      new \Kufer\Mail\Address\EMailAddress('', 'test1@example.org')
    ), \Kufer\Mail\Address\EMailAddress::splitEMailsCommaSeperated('test0@example.org,     test1@example.org'));

    $this->assertEquals(array(
      new \Kufer\Mail\Address\EMailAddress('', 'test0@example.org'),
      new \Kufer\Mail\Address\EMailAddress('', 'test1@example.org')
    ), \Kufer\Mail\Address\EMailAddress::splitEMailsCommaSeperated('test0@example.org,test1@example.org'));

    $this->assertEquals(array(
      new \Kufer\Mail\Address\EMailAddress('', 'test0@example.org'),
      new \Kufer\Mail\Address\EMailAddress('', 'test1@example.org')
    ), \Kufer\Mail\Address\EMailAddress::splitEMailsCommaSeperated('test0@example.org,test1@example.org    '));

    $this->assertEquals(array(
      new \Kufer\Mail\Address\EMailAddress('', 'test0@example.org'),
      new \Kufer\Mail\Address\EMailAddress('', 'test1@example.org')
    ), \Kufer\Mail\Address\EMailAddress::splitEMailsCommaSeperated('    test0@example.org,test1@example.org'));
  }

  /**
   * @test
   * @covers \Kufer\Mail\Address\EMailAddress::splitEMailsCommaSeperated
   */
  public function splitEMailsCommaSeperated_one_one()
  {
    $this->assertEquals(array(
      new \Kufer\Mail\Address\EMailAddress('Test', 'test@example.org')
    ), \Kufer\Mail\Address\EMailAddress::splitEMailsCommaSeperated('Test <test@example.org>'));

    $this->assertEquals(array(
      new \Kufer\Mail\Address\EMailAddress('Test', 'test@example.org')
    ), \Kufer\Mail\Address\EMailAddress::splitEMailsCommaSeperated('Test<test@example.org>'));

    $this->assertEquals(array(
      new \Kufer\Mail\Address\EMailAddress('Test', 'test@example.org')
    ), \Kufer\Mail\Address\EMailAddress::splitEMailsCommaSeperated('   Test<test@example.org>'));

    $this->assertEquals(array(
      new \Kufer\Mail\Address\EMailAddress('Test', 'test@example.org')
    ), \Kufer\Mail\Address\EMailAddress::splitEMailsCommaSeperated('   Test<   test@example.org>'));
  }

  /**
   * @test
   * @covers \Kufer\Mail\Address\EMailAddress::splitEMailsCommaSeperated
   */
  public function splitEMailsCommaSeperated_two_two()
  {
    $this->assertEquals(array(
      new \Kufer\Mail\Address\EMailAddress('Test0', 'test0@example.org'),
      new \Kufer\Mail\Address\EMailAddress('Test1', 'test1@example.org')
    ), \Kufer\Mail\Address\EMailAddress::splitEMailsCommaSeperated('Test0 <test0@example.org>, Test1 <test1@example.org>'));

    $this->assertEquals(array(
      new \Kufer\Mail\Address\EMailAddress('Test0', 'test0@example.org'),
      new \Kufer\Mail\Address\EMailAddress('Test1', 'test1@example.org')
    ), \Kufer\Mail\Address\EMailAddress::splitEMailsCommaSeperated('Test0 <test0@example.org>,Test1 <test1@example.org>'));

    $this->assertEquals(array(
      new \Kufer\Mail\Address\EMailAddress('Test0', 'test0@example.org'),
      new \Kufer\Mail\Address\EMailAddress('Test1', 'test1@example.org')
    ), \Kufer\Mail\Address\EMailAddress::splitEMailsCommaSeperated('        Test0 <test0@example.org>, Test1 <test1@example.org>'));

    $this->assertEquals(array(
      new \Kufer\Mail\Address\EMailAddress('Test0', 'test0@example.org'),
      new \Kufer\Mail\Address\EMailAddress('Test1', 'test1@example.org')
    ), \Kufer\Mail\Address\EMailAddress::splitEMailsCommaSeperated('Test0 <test0@example.org>, Test1 <test1@example.org>        '));

    $this->assertEquals(array(
      new \Kufer\Mail\Address\EMailAddress('Test0', 'test0@example.org'),
      new \Kufer\Mail\Address\EMailAddress('Test1', 'test1@example.org')
    ), \Kufer\Mail\Address\EMailAddress::splitEMailsCommaSeperated('Test0 <test0@example.org>,             Test1 <test1@example.org>'));

    $this->assertEquals(array(
      new \Kufer\Mail\Address\EMailAddress('Test0', 'test0@example.org'),
      new \Kufer\Mail\Address\EMailAddress('Test1', 'test1@example.org')
    ), \Kufer\Mail\Address\EMailAddress::splitEMailsCommaSeperated('Test0 <test0@example.org>, Test1 <          test1@example.org>'));
  }
}
