transparent-email/tests/Emails/EditableEmailTest.php

142 lines
4.8 KiB
PHP
Raw Permalink Normal View History

2016-09-24 13:09:20 +03:00
<?php
namespace bkrukowski\TransparentEmail\Tests\Emails;
2016-09-24 14:10:25 +03:00
use bkrukowski\TransparentEmail\Emails\EditableEmail;
2016-09-24 13:09:20 +03:00
use bkrukowski\TransparentEmail\Emails\Email;
use bkrukowski\TransparentEmail\Emails\EmailInterface;
2020-02-13 16:27:05 +03:00
use PHPUnit\Framework\TestCase;
2016-09-24 13:09:20 +03:00
2020-02-13 16:27:05 +03:00
class EditableEmailTest extends TestCase
2016-09-24 13:09:20 +03:00
{
/**
* @dataProvider providerRemoveFromLocalPart
*
* @param EmailInterface $email
* @param string $toRemove
*/
public function testRemoveFromLocalPart(EmailInterface $email, string $toRemove)
{
2016-09-24 14:10:25 +03:00
$editable = new EditableEmail($email);
$new = $editable->removeFromLocalPart($toRemove);
$this->assertNotSame($editable, $new);
$this->assertNotContains((string) $new, $toRemove);
2016-09-24 14:10:25 +03:00
$this->assertSame($email->getDomain(), $new->getDomain());
2016-09-24 13:09:20 +03:00
}
2016-10-14 21:25:52 +03:00
public function providerRemoveFromLocalPart() : array
2016-09-24 13:09:20 +03:00
{
return [
[new Email('jane.doe.1990@gmail.com'), '.'],
[new Email('janedoe1990@gmail.com'), '.'],
[new Email('jane.doe.1990@yandex.ru'), '.'],
[new Email('janedoe1990@yandex.ru'), '.'],
2016-09-24 13:09:20 +03:00
];
}
/**
* @dataProvider providerRemoveSuffixAlias
*
* @param EmailInterface $email
* @param string $delimiter
* @param string $expected
*/
public function testRemoveSuffixAlias(EmailInterface $email, string $delimiter, string $expected)
{
2016-09-24 14:10:25 +03:00
$editable = new EditableEmail($email);
$new = $editable->removeSuffixAlias($delimiter);
$this->assertNotSame($editable, $new);
$this->assertEquals($expected, $new);
2016-09-24 13:09:20 +03:00
$this->assertSame($email->getDomain(), $editable->getDomain());
}
2016-10-14 21:25:52 +03:00
public function providerRemoveSuffixAlias() : array
2016-09-24 13:09:20 +03:00
{
return [
[new Email('John.Doe+alias@gmail.com', true), '+', 'John.Doe@gmail.com'],
[new Email('JohnDoe-alias@gmail.com'), '-', 'johndoe@gmail.com'],
[new Email('JohnDoe@gmail.com'), '-', 'johndoe@gmail.com'],
[new Email('JohnDoe+alias@yandex.ru'), '+', 'johndoe@yandex.ru'],
[new Email('JohnDoe+alias@mail.ru'), '+', 'johndoe@mail.ru'],
2016-09-24 13:09:20 +03:00
];
}
/**
* @dataProvider providerSetDomain
*
* @param EmailInterface $email
* @param string $domain
*/
public function testSetDomain(EmailInterface $email, string $domain)
{
2016-09-24 14:10:25 +03:00
$editable = new EditableEmail($email);
$new = $editable->setDomain($domain);
$this->assertNotSame($editable, $new);
$this->assertSame($domain, $new->getDomain());
$this->assertSame($email->getLocalPart(), $new->getLocalPart());
2016-09-24 13:09:20 +03:00
}
2016-10-14 21:25:52 +03:00
public function providerSetDomain() : array
2016-09-24 13:09:20 +03:00
{
return [
[new Email('jane.doe@foo.bar'), 'gmail.com'],
[new Email('jane.doe@foo.bar'), 'foo.bar'],
[new Email('jane.doe@gmail.com'), 'foo.bar'],
[new Email('jane.doe@yandex.ru'), 'yandex.ru'],
[new Email('jane.doe@mail.ru'), 'mail.ru'],
[new Email('jane.doe@yandex.ru'), 'foo.bar'],
[new Email('jane.doe@mail.ru'), 'foo.bar'],
2016-09-24 13:09:20 +03:00
];
}
/**
* @dataProvider providerSetLocalPart
*
* @param EmailInterface $email
* @param string $localPart
*/
public function testLocalPart(EmailInterface $email, string $localPart)
{
2016-09-24 14:10:25 +03:00
$editable = new EditableEmail($email);
$new = $editable->setLocalPart($localPart);
$this->assertNotSame($editable, $new);
$this->assertSame($localPart, $new->getLocalPart());
$this->assertSame($email->getDomain(), $new->getDomain());
2016-09-24 13:09:20 +03:00
}
2016-10-14 21:25:52 +03:00
public function providerSetLocalPart() : array
2016-09-24 13:09:20 +03:00
{
return [
[new Email('jane.doe@foo.bar'), 'jane'],
[new Email('jane.doe@foo.bar'), 'john'],
[new Email('jane.doe@gmail.com'), 'jane.doe'],
[new Email('jane.doe@yandex.ru'), 'jane.doe'],
[new Email('jane.doe@mail.ru'), 'jane.doe'],
2016-09-24 13:09:20 +03:00
];
}
/**
* @dataProvider providerLowerCaseLocalPartIf
*
* @param EmailInterface $email
* @param bool $condition
2016-09-24 14:10:25 +03:00
* @param bool $isLowerCase
2016-09-24 13:09:20 +03:00
*/
2016-09-24 14:10:25 +03:00
public function testLowerCaseLocalPartIf(EmailInterface $email, bool $condition, bool $isLowerCase)
2016-09-24 13:09:20 +03:00
{
2016-09-24 14:10:25 +03:00
$editable = new EditableEmail($email);
$new = $editable->lowerCaseLocalPartIf($condition);
$this->assertSame(!$condition, $editable === $new);
$this->assertSame($isLowerCase, mb_strtolower($editable->getLocalPart()) === $new->getLocalPart());
2016-09-24 14:10:25 +03:00
$this->assertSame($email->getDomain(), $new->getDomain());
2016-09-24 13:09:20 +03:00
}
2016-10-14 21:25:52 +03:00
public function providerLowerCaseLocalPartIf() : array
2016-09-24 13:09:20 +03:00
{
return [
[new Email('john.doe@example.com', true), false, true],
[new Email('John.doe@example.com', true), false, false],
[new Email('John.doe@example.com', true), true, true],
];
}
}