mirror of
https://github.com/retailcrm/transparent-email.git
synced 2024-11-24 14:26:05 +03:00
Pass email as value object
This commit is contained in:
parent
8a31309423
commit
f7e1b62e81
@ -21,8 +21,11 @@ To detect multi-accounts on your website.
|
||||
## Usage
|
||||
|
||||
```php
|
||||
$cleaner = new \bkrukowski\TransparentEmail\TransparentEmail();
|
||||
$transformedEmail = $cleaner->getPrimaryEmail('John.Doe+alias@gmail.com');
|
||||
use \bkrukowski\TransparentEmail\TransparentEmail;
|
||||
use \bkrukowski\TransparentEmail\Emails\Email;
|
||||
|
||||
$cleaner = new TransparentEmail();
|
||||
$transformedEmail = $cleaner->getPrimaryEmail(new Email('John.Doe+alias@gmail.com'));
|
||||
```
|
||||
|
||||
## Yahoo.com
|
||||
|
27
src/Emails/Email.php
Normal file
27
src/Emails/Email.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace bkrukowski\TransparentEmail\Emails;
|
||||
|
||||
class Email implements EmailInterface
|
||||
{
|
||||
use EmailTrait;
|
||||
|
||||
public function __construct(string $email, bool $caseSensitive = false)
|
||||
{
|
||||
$this->validateEmail($email);
|
||||
list($this->localPart, $this->domain) = explode('@', $email);
|
||||
if (!$caseSensitive) {
|
||||
$this->localPart = strtolower($this->localPart);
|
||||
}
|
||||
$this->domain = strtolower($this->domain);
|
||||
}
|
||||
|
||||
private function validateEmail(string $email)
|
||||
{
|
||||
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||
throw new InvalidEmailException("Email '{$email}' is not valid!");
|
||||
}
|
||||
}
|
||||
}
|
15
src/Emails/EmailInterface.php
Normal file
15
src/Emails/EmailInterface.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace bkrukowski\TransparentEmail\Emails;
|
||||
|
||||
interface EmailInterface
|
||||
{
|
||||
public function getLocalPart() : string;
|
||||
|
||||
/**
|
||||
* @return string Domain in lowercase
|
||||
*/
|
||||
public function getDomain() : string;
|
||||
|
||||
public function __toString() : string;
|
||||
}
|
27
src/Emails/EmailTrait.php
Normal file
27
src/Emails/EmailTrait.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace bkrukowski\TransparentEmail\Emails;
|
||||
|
||||
trait EmailTrait
|
||||
{
|
||||
private $localPart;
|
||||
|
||||
private $domain;
|
||||
|
||||
public function getLocalPart() : string
|
||||
{
|
||||
return $this->localPart;
|
||||
}
|
||||
|
||||
public function getDomain() : string
|
||||
{
|
||||
return $this->domain;
|
||||
}
|
||||
|
||||
public function __toString() : string
|
||||
{
|
||||
return $this->localPart . '@' . $this->domain;
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace bkrukowski\TransparentEmail;
|
||||
namespace bkrukowski\TransparentEmail\Emails;
|
||||
|
||||
class InvalidEmailException extends \Exception
|
||||
{
|
56
src/Emails/MutableEmail.php
Normal file
56
src/Emails/MutableEmail.php
Normal file
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace bkrukowski\TransparentEmail\Emails;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class MutableEmail implements EmailInterface
|
||||
{
|
||||
use EmailTrait;
|
||||
|
||||
public function __construct(EmailInterface $email)
|
||||
{
|
||||
$this->localPart = $email->getLocalPart();
|
||||
$this->domain = $email->getDomain();
|
||||
}
|
||||
|
||||
public function removeFromLocalPart(string $toRemove) : MutableEmail
|
||||
{
|
||||
$this->localPart = str_replace($toRemove, '', $this->localPart);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeSuffixAlias(string $delimiter) : MutableEmail
|
||||
{
|
||||
$this->localPart = explode($delimiter, $this->localPart, 2)[0];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setDomain(string $domain) : MutableEmail
|
||||
{
|
||||
$this->domain = $domain;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setLocalPart(string $localPart) : MutableEmail
|
||||
{
|
||||
$this->localPart = $localPart;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function lowerCaseLocalPartIf(bool $condition) : MutableEmail
|
||||
{
|
||||
if ($condition) {
|
||||
$this->localPart = strtolower($this->localPart);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
@ -4,14 +4,13 @@ declare(strict_types=1);
|
||||
|
||||
namespace bkrukowski\TransparentEmail\Services;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
use bkrukowski\TransparentEmail\Emails\EmailInterface;
|
||||
|
||||
class AppsGoogleCom extends GmailCom
|
||||
{
|
||||
public function isDomainSupported(string $domain) : bool
|
||||
public function isSupported(EmailInterface $email) : bool
|
||||
{
|
||||
getmxrr($domain, $mxhosts);
|
||||
getmxrr($email->getDomain(), $mxhosts);
|
||||
$regex1 = '#\\.googlemail\\.com$#';
|
||||
$regex2 = '#\\.google\\.com$#';
|
||||
foreach ($mxhosts as $host) {
|
||||
|
@ -4,28 +4,34 @@ declare(strict_types=1);
|
||||
|
||||
namespace bkrukowski\TransparentEmail\Services;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
use bkrukowski\TransparentEmail\Emails\MutableEmail;
|
||||
use bkrukowski\TransparentEmail\Emails\EmailInterface;
|
||||
|
||||
class GmailCom implements ServiceInterface
|
||||
{
|
||||
public function getPrimaryEmail(string $email) : string
|
||||
public function getPrimaryEmail(EmailInterface $email) : EmailInterface
|
||||
{
|
||||
list($name, $domain) = explode('@', strtolower($email));
|
||||
|
||||
return explode('+', str_replace('.', '', $name))[0] . '@' . $this->mapDomain($domain);
|
||||
return (new MutableEmail($email))
|
||||
->removeFromLocalPart('.')
|
||||
->removeSuffixAlias('+')
|
||||
->lowerCaseLocalPartIf(true)
|
||||
->setDomain($this->mapDomain($email->getDomain()));
|
||||
}
|
||||
|
||||
public function isDomainSupported(string $domain) : bool
|
||||
public function isSupported(EmailInterface $email) : bool
|
||||
{
|
||||
return in_array(strtolower($domain), ['gmail.com', 'googlemail.com']);
|
||||
return in_array($email->getDomain(), ['gmail.com', 'googlemail.com']);
|
||||
}
|
||||
|
||||
protected function getDomainMapping() : array
|
||||
{
|
||||
return [
|
||||
'googlemail.com' => 'gmail.com',
|
||||
];
|
||||
}
|
||||
|
||||
private function mapDomain(string $domain) : string
|
||||
{
|
||||
$mapping = [
|
||||
'googlemail.com' => 'gmail.com',
|
||||
];
|
||||
return $mapping[$domain] ?? $domain;
|
||||
return $this->getDomainMapping()[$domain] ?? $domain;
|
||||
}
|
||||
}
|
@ -4,23 +4,21 @@ declare(strict_types=1);
|
||||
|
||||
namespace bkrukowski\TransparentEmail\Services;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
use bkrukowski\TransparentEmail\Emails\MutableEmail;
|
||||
use bkrukowski\TransparentEmail\Emails\EmailInterface;
|
||||
|
||||
abstract class MultiDomain implements ServiceInterface
|
||||
{
|
||||
public function getPrimaryEmail(string $email) : string
|
||||
public function getPrimaryEmail(EmailInterface $email) : EmailInterface
|
||||
{
|
||||
if (!$this->isCaseSensitive()) {
|
||||
$email = strtolower($email);
|
||||
return (new MutableEmail($email))
|
||||
->setDomain($this->getPrimaryDomain())
|
||||
->lowerCaseLocalPartIf(!$this->isCaseSensitive());
|
||||
}
|
||||
|
||||
return explode('@', $email)[0] . '@' . $this->getPrimaryDomain();
|
||||
}
|
||||
|
||||
public function isDomainSupported(string $domain) : bool
|
||||
public function isSupported(EmailInterface $email) : bool
|
||||
{
|
||||
return in_array(strtolower($domain), $this->getDomainList());
|
||||
return in_array($email->getDomain(), $this->getDomainList(), true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -4,20 +4,20 @@ declare(strict_types=1);
|
||||
|
||||
namespace bkrukowski\TransparentEmail\Services;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
use bkrukowski\TransparentEmail\Emails\MutableEmail;
|
||||
use bkrukowski\TransparentEmail\Emails\EmailInterface;
|
||||
|
||||
class OutlookCom implements ServiceInterface
|
||||
{
|
||||
public function getPrimaryEmail(string $email) : string
|
||||
public function getPrimaryEmail(EmailInterface $email) : EmailInterface
|
||||
{
|
||||
list($name, $domain) = explode('@', strtolower($email));
|
||||
|
||||
return explode('+', $name)[0] . '@' . $domain;
|
||||
return (new MutableEmail($email))
|
||||
->removeSuffixAlias('+')
|
||||
->lowerCaseLocalPartIf(true);
|
||||
}
|
||||
|
||||
public function isDomainSupported(string $domain) : bool
|
||||
public function isSupported(EmailInterface $email) : bool
|
||||
{
|
||||
return in_array(strtolower($domain), ['outlook.com', 'hotmail.com']);
|
||||
return in_array($email->getDomain(), ['outlook.com', 'hotmail.com'], true);
|
||||
}
|
||||
}
|
@ -2,12 +2,11 @@
|
||||
|
||||
namespace bkrukowski\TransparentEmail\Services;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
use bkrukowski\TransparentEmail\Emails\EmailInterface;
|
||||
|
||||
interface ServiceInterface
|
||||
{
|
||||
public function isDomainSupported(string $domain) : bool;
|
||||
public function isSupported(EmailInterface $email) : bool;
|
||||
|
||||
public function getPrimaryEmail(string $email) : string;
|
||||
public function getPrimaryEmail(EmailInterface $email) : EmailInterface;
|
||||
}
|
@ -6,7 +6,6 @@ namespace bkrukowski\TransparentEmail\Services;
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
* @internal
|
||||
*/
|
||||
class TlenPl extends MultiDomain
|
||||
{
|
||||
|
@ -4,20 +4,19 @@ declare(strict_types=1);
|
||||
|
||||
namespace bkrukowski\TransparentEmail\Services;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
use bkrukowski\TransparentEmail\Emails\MutableEmail;
|
||||
use bkrukowski\TransparentEmail\Emails\EmailInterface;
|
||||
|
||||
class Www33MailCom implements ServiceInterface
|
||||
{
|
||||
public function getPrimaryEmail(string $email) : string
|
||||
public function getPrimaryEmail(EmailInterface $email) : EmailInterface
|
||||
{
|
||||
list(, $domain) = explode('@', strtolower($email));
|
||||
|
||||
return preg_replace('/\\.33mail\\.com$/', '', $domain) . '@' . $domain;
|
||||
return (new MutableEmail($email))
|
||||
->setLocalPart(preg_replace('/\\.33mail\\.com$/', '', $email->getDomain()));
|
||||
}
|
||||
|
||||
public function isDomainSupported(string $domain) : bool
|
||||
public function isSupported(EmailInterface $email) : bool
|
||||
{
|
||||
return (bool) preg_match('/\\.33mail\\.com$/', strtolower($domain));
|
||||
return (bool) preg_match('/\\.33mail\\.com$/', $email->getDomain());
|
||||
}
|
||||
}
|
@ -4,20 +4,20 @@ declare(strict_types=1);
|
||||
|
||||
namespace bkrukowski\TransparentEmail\Services;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
use bkrukowski\TransparentEmail\Emails\MutableEmail;
|
||||
use bkrukowski\TransparentEmail\Emails\EmailInterface;
|
||||
|
||||
class YahooCom implements ServiceInterface
|
||||
{
|
||||
public function getPrimaryEmail(string $email) : string
|
||||
public function getPrimaryEmail(EmailInterface $email) : EmailInterface
|
||||
{
|
||||
list($name, $domain) = explode('@', strtolower($email));
|
||||
|
||||
return explode('-', $name, 2)[0] . '@' . $domain;
|
||||
return (new MutableEmail($email))
|
||||
->removeSuffixAlias('-')
|
||||
->lowerCaseLocalPartIf(true);
|
||||
}
|
||||
|
||||
public function isDomainSupported(string $domain) : bool
|
||||
public function isSupported(EmailInterface $email) : bool
|
||||
{
|
||||
return strtolower($domain) === 'yahoo.com';
|
||||
return $email->getDomain() === 'yahoo.com';
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace bkrukowski\TransparentEmail;
|
||||
|
||||
use bkrukowski\TransparentEmail\Emails\EmailInterface;
|
||||
use bkrukowski\TransparentEmail\Services\ServiceInterface;
|
||||
|
||||
class TransparentEmail
|
||||
@ -13,48 +14,19 @@ class TransparentEmail
|
||||
*/
|
||||
private $services;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $caseSensitive;
|
||||
|
||||
public function __construct(ServiceCollectorInterface $services = null, bool $caseSensitive = false)
|
||||
public function __construct(ServiceCollectorInterface $services = null)
|
||||
{
|
||||
$this->services = $services ?: new DefaultServiceCollector();
|
||||
$this->caseSensitive = $caseSensitive;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $email
|
||||
* @return string
|
||||
* @throws InvalidEmailException
|
||||
*/
|
||||
public function getPrimaryEmail(string $email)
|
||||
public function getPrimaryEmail(EmailInterface $email) : EmailInterface
|
||||
{
|
||||
$this->validateEmailAddress($email);
|
||||
if (!$this->caseSensitive) {
|
||||
$email = strtolower($email);
|
||||
}
|
||||
$domain = strtolower(explode('@', $email)[1]);
|
||||
$result = $email;
|
||||
|
||||
foreach ($this->services as $service) {
|
||||
if ($service->isDomainSupported($domain)) {
|
||||
$result = $service->getPrimaryEmail($result);
|
||||
if ($service->isSupported($email)) {
|
||||
return $service->getPrimaryEmail($email);
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $email
|
||||
* @throws InvalidEmailException
|
||||
*/
|
||||
private function validateEmailAddress(string $email)
|
||||
{
|
||||
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
|
||||
throw new InvalidEmailException("Invalid email '{$email}'!");
|
||||
}
|
||||
return $email;
|
||||
}
|
||||
}
|
45
tests/Emails/EmailTest.php
Normal file
45
tests/Emails/EmailTest.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace bkrukowski\TransparentEmail\Tests\Emails;
|
||||
|
||||
use bkrukowski\TransparentEmail\Emails\Email;
|
||||
use bkrukowski\TransparentEmail\Emails\InvalidEmailException;
|
||||
|
||||
class EmailTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider providerConstructor
|
||||
*
|
||||
* @param string $email
|
||||
* @param bool $expectedException
|
||||
* @param string $localPart
|
||||
* @param string $domain
|
||||
* @param bool $caseSensitive
|
||||
*/
|
||||
public function testConstructor(
|
||||
string $email,
|
||||
bool $expectedException,
|
||||
string $localPart = '',
|
||||
string $domain = '',
|
||||
bool $caseSensitive = false
|
||||
) {
|
||||
if ($expectedException) {
|
||||
$this->expectException(InvalidEmailException::class);
|
||||
}
|
||||
$object = new Email($email, $caseSensitive);
|
||||
$this->assertSame($localPart, $object->getLocalPart());
|
||||
$this->assertSame($domain, $object->getDomain());
|
||||
}
|
||||
|
||||
public function providerConstructor()
|
||||
{
|
||||
return [
|
||||
['john doe@example.com', true],
|
||||
['.johndoe@example.com', true],
|
||||
['Jane.Doe@Example.COM', false, 'Jane.Doe', 'example.com', true],
|
||||
['Jane.Doe@Example.COM', false, 'jane.doe', 'example.com'],
|
||||
];
|
||||
}
|
||||
}
|
126
tests/Emails/MutableEmailTest.php
Normal file
126
tests/Emails/MutableEmailTest.php
Normal file
@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
namespace bkrukowski\TransparentEmail\Tests\Emails;
|
||||
|
||||
use bkrukowski\TransparentEmail\Emails\MutableEmail;
|
||||
use bkrukowski\TransparentEmail\Emails\Email;
|
||||
use bkrukowski\TransparentEmail\Emails\EmailInterface;
|
||||
|
||||
class MutableEmailTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider providerRemoveFromLocalPart
|
||||
*
|
||||
* @param EmailInterface $email
|
||||
* @param string $toRemove
|
||||
*/
|
||||
public function testRemoveFromLocalPart(EmailInterface $email, string $toRemove)
|
||||
{
|
||||
$editable = new MutableEmail($email);
|
||||
$this->assertSame($editable, $editable->removeFromLocalPart($toRemove));
|
||||
$this->assertNotContains((string) $editable, $toRemove);
|
||||
$this->assertSame($email->getDomain(), $editable->getDomain());
|
||||
}
|
||||
|
||||
public function providerRemoveFromLocalPart()
|
||||
{
|
||||
return [
|
||||
[new Email('jane.doe.1990@gmail.com'), '.'],
|
||||
[new Email('janedoe1990@gmail.com'), '.'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerRemoveSuffixAlias
|
||||
*
|
||||
* @param EmailInterface $email
|
||||
* @param string $delimiter
|
||||
* @param string $expected
|
||||
*/
|
||||
public function testRemoveSuffixAlias(EmailInterface $email, string $delimiter, string $expected)
|
||||
{
|
||||
$editable = new MutableEmail($email);
|
||||
$this->assertSame($editable, $editable->removeSuffixAlias($delimiter));
|
||||
$this->assertEquals($expected, $editable);
|
||||
$this->assertSame($email->getDomain(), $editable->getDomain());
|
||||
}
|
||||
|
||||
public function providerRemoveSuffixAlias()
|
||||
{
|
||||
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'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerSetDomain
|
||||
*
|
||||
* @param EmailInterface $email
|
||||
* @param string $domain
|
||||
*/
|
||||
public function testSetDomain(EmailInterface $email, string $domain)
|
||||
{
|
||||
$editable = new MutableEmail($email);
|
||||
$this->assertSame($editable, $editable->setDomain($domain));
|
||||
$this->assertSame($domain, $editable->getDomain());
|
||||
$this->assertSame($email->getLocalPart(), $editable->getLocalPart());
|
||||
}
|
||||
|
||||
public function providerSetDomain()
|
||||
{
|
||||
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'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerSetLocalPart
|
||||
*
|
||||
* @param EmailInterface $email
|
||||
* @param string $localPart
|
||||
*/
|
||||
public function testLocalPart(EmailInterface $email, string $localPart)
|
||||
{
|
||||
$editable = new MutableEmail($email);
|
||||
$this->assertSame($editable, $editable->setLocalPart($localPart));
|
||||
$this->assertSame($localPart, $editable->getLocalPart());
|
||||
$this->assertSame($email->getDomain(), $editable->getDomain());
|
||||
}
|
||||
|
||||
public function providerSetLocalPart()
|
||||
{
|
||||
return [
|
||||
[new Email('jane.doe@foo.bar'), 'jane'],
|
||||
[new Email('jane.doe@foo.bar'), 'john'],
|
||||
[new Email('jane.doe@gmail.com'), 'jane.doe'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerLowerCaseLocalPartIf
|
||||
*
|
||||
* @param EmailInterface $email
|
||||
* @param bool $condition
|
||||
* @param bool $expected
|
||||
*/
|
||||
public function testLowerCaseLocalPartIf(EmailInterface $email, bool $condition, bool $expected)
|
||||
{
|
||||
$editable = new MutableEmail($email);
|
||||
$this->assertSame($editable, $editable->lowerCaseLocalPartIf($condition));
|
||||
$this->assertSame($expected, strtolower($editable->getLocalPart()) === $editable->getLocalPart());
|
||||
$this->assertSame($email->getDomain(), $editable->getDomain());
|
||||
}
|
||||
|
||||
public function providerLowerCaseLocalPartIf()
|
||||
{
|
||||
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],
|
||||
];
|
||||
}
|
||||
}
|
@ -4,26 +4,29 @@ declare(strict_types=1);
|
||||
|
||||
namespace bkrukowski\TransparentEmail\Tests\Services;
|
||||
|
||||
use bkrukowski\TransparentEmail\Emails\Email;
|
||||
use bkrukowski\TransparentEmail\Services\AppsGoogleCom;
|
||||
|
||||
class AppsGoogleComTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider providerIsDomainSupported
|
||||
* @dataProvider providerIsSupported
|
||||
*
|
||||
* @param string $domain
|
||||
* @param bool $result
|
||||
*/
|
||||
public function testIsDomainSupported(string $domain, bool $result)
|
||||
public function testIsSupported(string $domain, bool $result)
|
||||
{
|
||||
$this->assertSame($result, (new AppsGoogleCom())->isDomainSupported($domain));
|
||||
$this->assertSame($result, (new AppsGoogleCom())->isSupported(new Email('Jane.Doe@' . $domain, true)));
|
||||
}
|
||||
|
||||
public function providerIsDomainSupported()
|
||||
public function providerIsSupported()
|
||||
{
|
||||
return [
|
||||
['example.com', false],
|
||||
['EXAMPLE.COM', false],
|
||||
['krukowski.me', true],
|
||||
['KRUKOWSKI.ME', true],
|
||||
];
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace bkrukowski\TransparentEmail\Tests\Services;
|
||||
|
||||
use bkrukowski\TransparentEmail\Emails\Email;
|
||||
use bkrukowski\TransparentEmail\Services\GmailCom;
|
||||
|
||||
class GmailComTest extends \PHPUnit_Framework_TestCase
|
||||
@ -16,7 +17,7 @@ class GmailComTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testGetPrimaryEmail(string $inputEmail, string $outputEmail)
|
||||
{
|
||||
$this->assertSame($outputEmail, (new GmailCom())->getPrimaryEmail($inputEmail));
|
||||
$this->assertEquals($outputEmail, (new GmailCom())->getPrimaryEmail(new Email($inputEmail)));
|
||||
}
|
||||
|
||||
public function providerGetPrimaryEmail()
|
||||
@ -31,24 +32,26 @@ class GmailComTest extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerIsDomainSupported
|
||||
* @dataProvider providerIsSupported
|
||||
*
|
||||
* @param string $domain
|
||||
* @param bool $result
|
||||
*/
|
||||
public function testIsDomainSupported(string $domain, bool $result)
|
||||
public function testIsSupported(string $domain, bool $result)
|
||||
{
|
||||
$this->assertSame($result, (new GmailCom())->isDomainSupported($domain));
|
||||
$this->assertSame($result, (new GmailCom())->isSupported(new Email('Jane.Doe@' . $domain)));
|
||||
}
|
||||
|
||||
public function providerIsDomainSupported()
|
||||
public function providerIsSupported()
|
||||
{
|
||||
return [
|
||||
['gmail.com', true],
|
||||
['gmail.COM', true],
|
||||
['google.com', false],
|
||||
['gmailcom', false],
|
||||
['g.mail.com', false]
|
||||
['test.gmailcom', false],
|
||||
['g.mail.com', false],
|
||||
['googlemail.com', true],
|
||||
['GoogleMail.Com', true],
|
||||
];
|
||||
}
|
||||
}
|
@ -4,23 +4,24 @@ declare(strict_types=1);
|
||||
|
||||
namespace bkrukowski\TransparentEmail\Tests\Services;
|
||||
|
||||
use bkrukowski\TransparentEmail\Emails\Email;
|
||||
use bkrukowski\TransparentEmail\Services\MultiDomain;
|
||||
|
||||
class MultiDomainTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider providerIsDomainSupported
|
||||
* @dataProvider providerIsSupported
|
||||
*
|
||||
* @param MultiDomain $mock
|
||||
* @param string $domain
|
||||
* @param bool $expected
|
||||
*/
|
||||
public function testIsDomainSupported(MultiDomain $mock, string $domain, bool $expected)
|
||||
public function testIsSupported(MultiDomain $mock, string $domain, bool $expected)
|
||||
{
|
||||
$this->assertSame($expected, $mock->isDomainSupported($domain));
|
||||
$this->assertSame($expected, $mock->isSupported(new Email('Jane.Doe@' . $domain, true)));
|
||||
}
|
||||
|
||||
public function providerIsDomainSupported()
|
||||
public function providerIsSupported()
|
||||
{
|
||||
return [
|
||||
[$this->getMultiDomainMock('foo.bar', ['foo.bar']), 'gmail.com', false],
|
||||
@ -37,9 +38,8 @@ class MultiDomainTest extends \PHPUnit_Framework_TestCase
|
||||
* @param string $email
|
||||
* @param string $expectedEmail
|
||||
*/
|
||||
public function testGetPrimaryDomain(MultiDomain $mock, string $email, string $expectedEmail)
|
||||
{
|
||||
$this->assertSame($expectedEmail, $mock->getPrimaryEmail($email));
|
||||
public function testGetPrimaryDomain(MultiDomain $mock, string $email, string $expectedEmail) {
|
||||
$this->assertEquals($expectedEmail, $mock->getPrimaryEmail(new Email($email, true)));
|
||||
}
|
||||
|
||||
public function providerGetPrimaryDomain()
|
||||
@ -47,6 +47,7 @@ class MultiDomainTest extends \PHPUnit_Framework_TestCase
|
||||
return [
|
||||
[$this->getMultiDomainMock('foo.bar', ['foo.bar', 'foo.bar2']), 'name@foo.bar', 'name@foo.bar'],
|
||||
[$this->getMultiDomainMock('foo.bar', ['foo.bar', 'foo.bar2'], true), 'Name@foo.bar', 'Name@foo.bar'],
|
||||
[$this->getMultiDomainMock('foo.bar', ['foo.bar', 'foo.bar2'], true), 'Name@FOO.bar', 'Name@foo.bar'],
|
||||
[$this->getMultiDomainMock('foo.bar', ['foo.bar', 'foo.bar2']), 'name@foo.bar2', 'name@foo.bar'],
|
||||
];
|
||||
}
|
||||
|
@ -4,22 +4,23 @@ declare(strict_types=1);
|
||||
|
||||
namespace bkrukowski\TransparentEmail\Tests\Services;
|
||||
|
||||
use bkrukowski\TransparentEmail\Emails\Email;
|
||||
use bkrukowski\TransparentEmail\Services\OutlookCom;
|
||||
|
||||
class OutlookComTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider providerIsDomainSupported
|
||||
* @dataProvider providerIsSupported
|
||||
*
|
||||
* @param string $domain
|
||||
* @param bool $result
|
||||
*/
|
||||
public function testIsDomainSupported(string $domain, bool $result)
|
||||
public function testIsSupported(string $domain, bool $result)
|
||||
{
|
||||
$this->assertSame($result, (new OutlookCom())->isDomainSupported($domain));
|
||||
$this->assertSame($result, (new OutlookCom())->isSupported(new Email('Jane.Doe@' . $domain, true)));
|
||||
}
|
||||
|
||||
public function providerIsDomainSupported()
|
||||
public function providerIsSupported()
|
||||
{
|
||||
return [
|
||||
['outlook.com', true],
|
||||
@ -39,7 +40,7 @@ class OutlookComTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testGetPrimaryEmail(string $inputEmail, string $expectedEmail)
|
||||
{
|
||||
$this->assertSame($expectedEmail, (new OutlookCom())->getPrimaryEmail($inputEmail));
|
||||
$this->assertEquals($expectedEmail, (new OutlookCom())->getPrimaryEmail(new Email($inputEmail, true)));
|
||||
}
|
||||
|
||||
public function providerGetPrimaryEmail()
|
||||
|
@ -4,22 +4,23 @@ declare(strict_types=1);
|
||||
|
||||
namespace bkrukowski\TransparentEmail\Tests\Services;
|
||||
|
||||
use bkrukowski\TransparentEmail\Emails\Email;
|
||||
use bkrukowski\TransparentEmail\Services\Www33MailCom;
|
||||
|
||||
class Www33MailComTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider providerIsDomainSupported
|
||||
* @dataProvider providerIsSupported
|
||||
*
|
||||
* @param string $domain
|
||||
* @param bool $isSupported
|
||||
*/
|
||||
public function testIsDomainSupported(string $domain, bool $isSupported)
|
||||
public function testIsSupported(string $domain, bool $isSupported)
|
||||
{
|
||||
$this->assertSame($isSupported, (new Www33MailCom())->isDomainSupported($domain));
|
||||
$this->assertSame($isSupported, (new Www33MailCom())->isSupported(new Email('Jane.Doe@' . $domain, true)));
|
||||
}
|
||||
|
||||
public function providerIsDomainSupported()
|
||||
public function providerIsSupported()
|
||||
{
|
||||
return [
|
||||
['foo.33mail.com', true],
|
||||
@ -38,7 +39,7 @@ class Www33MailComTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testGetPrimaryEmail(string $inputEmail, string $expectedEmail)
|
||||
{
|
||||
$this->assertSame($expectedEmail, (new Www33MailCom())->getPrimaryEmail($inputEmail));
|
||||
$this->assertEquals($expectedEmail, (new Www33MailCom())->getPrimaryEmail(new Email($inputEmail, true)));
|
||||
}
|
||||
|
||||
public function providerGetPrimaryEmail()
|
||||
|
@ -4,22 +4,23 @@ declare(strict_types=1);
|
||||
|
||||
namespace bkrukowski\TransparentEmail\Tests\Services;
|
||||
|
||||
use bkrukowski\TransparentEmail\Emails\Email;
|
||||
use bkrukowski\TransparentEmail\Services\YahooCom;
|
||||
|
||||
class YahooComTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider providerIsDomainSupported
|
||||
* @dataProvider providerIsSupported
|
||||
*
|
||||
* @param string $domain
|
||||
* @param bool $result
|
||||
*/
|
||||
public function testIsDomainSupported(string $domain, bool $result)
|
||||
{
|
||||
$this->assertSame($result, (new YahooCom())->isDomainSupported($domain));
|
||||
$this->assertSame($result, (new YahooCom())->isSupported(new Email('Jane.Doe@' . $domain, true)));
|
||||
}
|
||||
|
||||
public function providerIsDomainSupported()
|
||||
public function providerIsSupported()
|
||||
{
|
||||
return [
|
||||
['yahoo.com', true],
|
||||
@ -40,7 +41,7 @@ class YahooComTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testGetPrimaryEmail(string $inputEmail, string $expectedEmail)
|
||||
{
|
||||
$this->assertSame($expectedEmail, (new YahooCom())->getPrimaryEmail($inputEmail));
|
||||
$this->assertEquals($expectedEmail, (new YahooCom())->getPrimaryEmail(new Email($inputEmail, true)));
|
||||
}
|
||||
|
||||
public function providerGetPrimaryEmail()
|
||||
|
@ -4,46 +4,29 @@ declare(strict_types=1);
|
||||
|
||||
namespace bkrukowski\TransparentEmail\Tests;
|
||||
|
||||
use bkrukowski\TransparentEmail\Emails\Email;
|
||||
use bkrukowski\TransparentEmail\ServiceCollector;
|
||||
use bkrukowski\TransparentEmail\ServiceCollectorInterface;
|
||||
use bkrukowski\TransparentEmail\Services\TlenPl;
|
||||
use bkrukowski\TransparentEmail\TransparentEmail;
|
||||
use bkrukowski\TransparentEmail\InvalidEmailException;
|
||||
|
||||
class TransparentEmailTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider providerValidator
|
||||
*
|
||||
* @param string $email
|
||||
* @param bool $validEmail
|
||||
*/
|
||||
public function testValidator(string $email, bool $validEmail)
|
||||
{
|
||||
if (!$validEmail) {
|
||||
$this->expectException(InvalidEmailException::class);
|
||||
}
|
||||
(new TransparentEmail())->getPrimaryEmail($email);
|
||||
}
|
||||
|
||||
public function providerValidator()
|
||||
{
|
||||
return [
|
||||
['john.doe@gmail.comm', true],
|
||||
['john doe@gmail.com', false],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerGetPrimaryEmail
|
||||
*
|
||||
* @param TransparentEmail $transparentEmail
|
||||
* @param string $email
|
||||
* @param string $expectedEmail
|
||||
* @param bool $caseSensitive
|
||||
*/
|
||||
public function testGetPrimaryEmail(TransparentEmail $transparentEmail, string $email, string $expectedEmail)
|
||||
{
|
||||
$this->assertSame($expectedEmail, $transparentEmail->getPrimaryEmail($email));
|
||||
public function testGetPrimaryEmail(
|
||||
TransparentEmail $transparentEmail,
|
||||
string $email,
|
||||
string $expectedEmail,
|
||||
bool $caseSensitive = false
|
||||
) {
|
||||
$this->assertEquals($expectedEmail, $transparentEmail->getPrimaryEmail(new Email($email, $caseSensitive)));
|
||||
}
|
||||
|
||||
public function providerGetPrimaryEmail()
|
||||
@ -59,8 +42,8 @@ class TransparentEmailTest extends \PHPUnit_Framework_TestCase
|
||||
],
|
||||
[new TransparentEmail(), 'john.doe+alias@gmail.com', 'johndoe@gmail.com'],
|
||||
[new TransparentEmail($emptyServiceCollector), 'John.Doe@example.com', 'john.doe@example.com'],
|
||||
[new TransparentEmail($emptyServiceCollector, true), 'John.Doe@example.com', 'John.Doe@example.com'],
|
||||
[new TransparentEmail(null, true), 'John.Doe@gmail.com', 'johndoe@gmail.com'],
|
||||
[new TransparentEmail($emptyServiceCollector), 'John.Doe@example.com', 'John.Doe@example.com', true],
|
||||
[new TransparentEmail(), 'John.Doe@gmail.com', 'johndoe@gmail.com', true],
|
||||
[new TransparentEmail(), 'Jane.Doe+receipts@hotmail.com', 'jane.doe@hotmail.com'],
|
||||
[new TransparentEmail(), 'Jane.Doe-receipts@yahoo.com', 'jane.doe@yahoo.com'],
|
||||
];
|
||||
|
Loading…
Reference in New Issue
Block a user