Pass email as value object

This commit is contained in:
bkrukowski 2016-09-24 12:09:20 +02:00
parent 8a31309423
commit f7e1b62e81
24 changed files with 423 additions and 159 deletions

View File

@ -21,8 +21,11 @@ To detect multi-accounts on your website.
## Usage ## Usage
```php ```php
$cleaner = new \bkrukowski\TransparentEmail\TransparentEmail(); use \bkrukowski\TransparentEmail\TransparentEmail;
$transformedEmail = $cleaner->getPrimaryEmail('John.Doe+alias@gmail.com'); use \bkrukowski\TransparentEmail\Emails\Email;
$cleaner = new TransparentEmail();
$transformedEmail = $cleaner->getPrimaryEmail(new Email('John.Doe+alias@gmail.com'));
``` ```
## Yahoo.com ## Yahoo.com

27
src/Emails/Email.php Normal file
View 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!");
}
}
}

View 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
View 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;
}
}

View File

@ -1,6 +1,6 @@
<?php <?php
namespace bkrukowski\TransparentEmail; namespace bkrukowski\TransparentEmail\Emails;
class InvalidEmailException extends \Exception class InvalidEmailException extends \Exception
{ {

View 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;
}
}

View File

@ -4,14 +4,13 @@ declare(strict_types=1);
namespace bkrukowski\TransparentEmail\Services; namespace bkrukowski\TransparentEmail\Services;
/** use bkrukowski\TransparentEmail\Emails\EmailInterface;
* @internal
*/
class AppsGoogleCom extends GmailCom 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$#'; $regex1 = '#\\.googlemail\\.com$#';
$regex2 = '#\\.google\\.com$#'; $regex2 = '#\\.google\\.com$#';
foreach ($mxhosts as $host) { foreach ($mxhosts as $host) {

View File

@ -4,28 +4,34 @@ declare(strict_types=1);
namespace bkrukowski\TransparentEmail\Services; namespace bkrukowski\TransparentEmail\Services;
/** use bkrukowski\TransparentEmail\Emails\MutableEmail;
* @internal use bkrukowski\TransparentEmail\Emails\EmailInterface;
*/
class GmailCom implements ServiceInterface class GmailCom implements ServiceInterface
{ {
public function getPrimaryEmail(string $email) : string public function getPrimaryEmail(EmailInterface $email) : EmailInterface
{ {
list($name, $domain) = explode('@', strtolower($email)); return (new MutableEmail($email))
->removeFromLocalPart('.')
return explode('+', str_replace('.', '', $name))[0] . '@' . $this->mapDomain($domain); ->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 private function mapDomain(string $domain) : string
{ {
$mapping = [ return $this->getDomainMapping()[$domain] ?? $domain;
'googlemail.com' => 'gmail.com',
];
return $mapping[$domain] ?? $domain;
} }
} }

View File

@ -4,23 +4,21 @@ declare(strict_types=1);
namespace bkrukowski\TransparentEmail\Services; namespace bkrukowski\TransparentEmail\Services;
/** use bkrukowski\TransparentEmail\Emails\MutableEmail;
* @internal use bkrukowski\TransparentEmail\Emails\EmailInterface;
*/
abstract class MultiDomain implements ServiceInterface abstract class MultiDomain implements ServiceInterface
{ {
public function getPrimaryEmail(string $email) : string public function getPrimaryEmail(EmailInterface $email) : EmailInterface
{ {
if (!$this->isCaseSensitive()) { return (new MutableEmail($email))
$email = strtolower($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);
} }
/** /**

View File

@ -4,20 +4,20 @@ declare(strict_types=1);
namespace bkrukowski\TransparentEmail\Services; namespace bkrukowski\TransparentEmail\Services;
/** use bkrukowski\TransparentEmail\Emails\MutableEmail;
* @internal use bkrukowski\TransparentEmail\Emails\EmailInterface;
*/
class OutlookCom implements ServiceInterface class OutlookCom implements ServiceInterface
{ {
public function getPrimaryEmail(string $email) : string public function getPrimaryEmail(EmailInterface $email) : EmailInterface
{ {
list($name, $domain) = explode('@', strtolower($email)); return (new MutableEmail($email))
->removeSuffixAlias('+')
return explode('+', $name)[0] . '@' . $domain; ->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);
} }
} }

View File

@ -2,12 +2,11 @@
namespace bkrukowski\TransparentEmail\Services; namespace bkrukowski\TransparentEmail\Services;
/** use bkrukowski\TransparentEmail\Emails\EmailInterface;
* @internal
*/
interface ServiceInterface 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;
} }

View File

@ -6,7 +6,6 @@ namespace bkrukowski\TransparentEmail\Services;
/** /**
* @codeCoverageIgnore * @codeCoverageIgnore
* @internal
*/ */
class TlenPl extends MultiDomain class TlenPl extends MultiDomain
{ {

View File

@ -4,20 +4,19 @@ declare(strict_types=1);
namespace bkrukowski\TransparentEmail\Services; namespace bkrukowski\TransparentEmail\Services;
/** use bkrukowski\TransparentEmail\Emails\MutableEmail;
* @internal use bkrukowski\TransparentEmail\Emails\EmailInterface;
*/
class Www33MailCom implements ServiceInterface class Www33MailCom implements ServiceInterface
{ {
public function getPrimaryEmail(string $email) : string public function getPrimaryEmail(EmailInterface $email) : EmailInterface
{ {
list(, $domain) = explode('@', strtolower($email)); return (new MutableEmail($email))
->setLocalPart(preg_replace('/\\.33mail\\.com$/', '', $email->getDomain()));
return preg_replace('/\\.33mail\\.com$/', '', $domain) . '@' . $domain;
} }
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());
} }
} }

View File

@ -4,20 +4,20 @@ declare(strict_types=1);
namespace bkrukowski\TransparentEmail\Services; namespace bkrukowski\TransparentEmail\Services;
/** use bkrukowski\TransparentEmail\Emails\MutableEmail;
* @internal use bkrukowski\TransparentEmail\Emails\EmailInterface;
*/
class YahooCom implements ServiceInterface class YahooCom implements ServiceInterface
{ {
public function getPrimaryEmail(string $email) : string public function getPrimaryEmail(EmailInterface $email) : EmailInterface
{ {
list($name, $domain) = explode('@', strtolower($email)); return (new MutableEmail($email))
->removeSuffixAlias('-')
return explode('-', $name, 2)[0] . '@' . $domain; ->lowerCaseLocalPartIf(true);
} }
public function isDomainSupported(string $domain) : bool public function isSupported(EmailInterface $email) : bool
{ {
return strtolower($domain) === 'yahoo.com'; return $email->getDomain() === 'yahoo.com';
} }
} }

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace bkrukowski\TransparentEmail; namespace bkrukowski\TransparentEmail;
use bkrukowski\TransparentEmail\Emails\EmailInterface;
use bkrukowski\TransparentEmail\Services\ServiceInterface; use bkrukowski\TransparentEmail\Services\ServiceInterface;
class TransparentEmail class TransparentEmail
@ -13,48 +14,19 @@ class TransparentEmail
*/ */
private $services; private $services;
/** public function __construct(ServiceCollectorInterface $services = null)
* @var bool
*/
private $caseSensitive;
public function __construct(ServiceCollectorInterface $services = null, bool $caseSensitive = false)
{ {
$this->services = $services ?: new DefaultServiceCollector(); $this->services = $services ?: new DefaultServiceCollector();
$this->caseSensitive = $caseSensitive;
} }
/** public function getPrimaryEmail(EmailInterface $email) : EmailInterface
* @param string $email
* @return string
* @throws InvalidEmailException
*/
public function getPrimaryEmail(string $email)
{ {
$this->validateEmailAddress($email);
if (!$this->caseSensitive) {
$email = strtolower($email);
}
$domain = strtolower(explode('@', $email)[1]);
$result = $email;
foreach ($this->services as $service) { foreach ($this->services as $service) {
if ($service->isDomainSupported($domain)) { if ($service->isSupported($email)) {
$result = $service->getPrimaryEmail($result); return $service->getPrimaryEmail($email);
} }
} }
return $result; return $email;
}
/**
* @param string $email
* @throws InvalidEmailException
*/
private function validateEmailAddress(string $email)
{
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
throw new InvalidEmailException("Invalid email '{$email}'!");
}
} }
} }

View 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'],
];
}
}

View 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],
];
}
}

View File

@ -4,26 +4,29 @@ declare(strict_types=1);
namespace bkrukowski\TransparentEmail\Tests\Services; namespace bkrukowski\TransparentEmail\Tests\Services;
use bkrukowski\TransparentEmail\Emails\Email;
use bkrukowski\TransparentEmail\Services\AppsGoogleCom; use bkrukowski\TransparentEmail\Services\AppsGoogleCom;
class AppsGoogleComTest extends \PHPUnit_Framework_TestCase class AppsGoogleComTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* @dataProvider providerIsDomainSupported * @dataProvider providerIsSupported
* *
* @param string $domain * @param string $domain
* @param bool $result * @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 [ return [
['example.com', false], ['example.com', false],
['EXAMPLE.COM', false],
['krukowski.me', true], ['krukowski.me', true],
['KRUKOWSKI.ME', true],
]; ];
} }
} }

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace bkrukowski\TransparentEmail\Tests\Services; namespace bkrukowski\TransparentEmail\Tests\Services;
use bkrukowski\TransparentEmail\Emails\Email;
use bkrukowski\TransparentEmail\Services\GmailCom; use bkrukowski\TransparentEmail\Services\GmailCom;
class GmailComTest extends \PHPUnit_Framework_TestCase class GmailComTest extends \PHPUnit_Framework_TestCase
@ -16,7 +17,7 @@ class GmailComTest extends \PHPUnit_Framework_TestCase
*/ */
public function testGetPrimaryEmail(string $inputEmail, string $outputEmail) 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() public function providerGetPrimaryEmail()
@ -31,24 +32,26 @@ class GmailComTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @dataProvider providerIsDomainSupported * @dataProvider providerIsSupported
* *
* @param string $domain * @param string $domain
* @param bool $result * @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 [ return [
['gmail.com', true], ['gmail.com', true],
['gmail.COM', true], ['gmail.COM', true],
['google.com', false], ['google.com', false],
['gmailcom', false], ['test.gmailcom', false],
['g.mail.com', false] ['g.mail.com', false],
['googlemail.com', true],
['GoogleMail.Com', true],
]; ];
} }
} }

View File

@ -4,23 +4,24 @@ declare(strict_types=1);
namespace bkrukowski\TransparentEmail\Tests\Services; namespace bkrukowski\TransparentEmail\Tests\Services;
use bkrukowski\TransparentEmail\Emails\Email;
use bkrukowski\TransparentEmail\Services\MultiDomain; use bkrukowski\TransparentEmail\Services\MultiDomain;
class MultiDomainTest extends \PHPUnit_Framework_TestCase class MultiDomainTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* @dataProvider providerIsDomainSupported * @dataProvider providerIsSupported
* *
* @param MultiDomain $mock * @param MultiDomain $mock
* @param string $domain * @param string $domain
* @param bool $expected * @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 [ return [
[$this->getMultiDomainMock('foo.bar', ['foo.bar']), 'gmail.com', false], [$this->getMultiDomainMock('foo.bar', ['foo.bar']), 'gmail.com', false],
@ -37,9 +38,8 @@ class MultiDomainTest extends \PHPUnit_Framework_TestCase
* @param string $email * @param string $email
* @param string $expectedEmail * @param string $expectedEmail
*/ */
public function testGetPrimaryDomain(MultiDomain $mock, string $email, string $expectedEmail) public function testGetPrimaryDomain(MultiDomain $mock, string $email, string $expectedEmail) {
{ $this->assertEquals($expectedEmail, $mock->getPrimaryEmail(new Email($email, true)));
$this->assertSame($expectedEmail, $mock->getPrimaryEmail($email));
} }
public function providerGetPrimaryDomain() public function providerGetPrimaryDomain()
@ -47,6 +47,7 @@ class MultiDomainTest extends \PHPUnit_Framework_TestCase
return [ return [
[$this->getMultiDomainMock('foo.bar', ['foo.bar', 'foo.bar2']), 'name@foo.bar', 'name@foo.bar'], [$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'], true), 'Name@FOO.bar', 'Name@foo.bar'],
[$this->getMultiDomainMock('foo.bar', ['foo.bar', 'foo.bar2']), 'name@foo.bar2', 'name@foo.bar'], [$this->getMultiDomainMock('foo.bar', ['foo.bar', 'foo.bar2']), 'name@foo.bar2', 'name@foo.bar'],
]; ];
} }

View File

@ -4,22 +4,23 @@ declare(strict_types=1);
namespace bkrukowski\TransparentEmail\Tests\Services; namespace bkrukowski\TransparentEmail\Tests\Services;
use bkrukowski\TransparentEmail\Emails\Email;
use bkrukowski\TransparentEmail\Services\OutlookCom; use bkrukowski\TransparentEmail\Services\OutlookCom;
class OutlookComTest extends \PHPUnit_Framework_TestCase class OutlookComTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* @dataProvider providerIsDomainSupported * @dataProvider providerIsSupported
* *
* @param string $domain * @param string $domain
* @param bool $result * @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 [ return [
['outlook.com', true], ['outlook.com', true],
@ -39,7 +40,7 @@ class OutlookComTest extends \PHPUnit_Framework_TestCase
*/ */
public function testGetPrimaryEmail(string $inputEmail, string $expectedEmail) 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() public function providerGetPrimaryEmail()

View File

@ -4,22 +4,23 @@ declare(strict_types=1);
namespace bkrukowski\TransparentEmail\Tests\Services; namespace bkrukowski\TransparentEmail\Tests\Services;
use bkrukowski\TransparentEmail\Emails\Email;
use bkrukowski\TransparentEmail\Services\Www33MailCom; use bkrukowski\TransparentEmail\Services\Www33MailCom;
class Www33MailComTest extends \PHPUnit_Framework_TestCase class Www33MailComTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* @dataProvider providerIsDomainSupported * @dataProvider providerIsSupported
* *
* @param string $domain * @param string $domain
* @param bool $isSupported * @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 [ return [
['foo.33mail.com', true], ['foo.33mail.com', true],
@ -38,7 +39,7 @@ class Www33MailComTest extends \PHPUnit_Framework_TestCase
*/ */
public function testGetPrimaryEmail(string $inputEmail, string $expectedEmail) 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() public function providerGetPrimaryEmail()

View File

@ -4,22 +4,23 @@ declare(strict_types=1);
namespace bkrukowski\TransparentEmail\Tests\Services; namespace bkrukowski\TransparentEmail\Tests\Services;
use bkrukowski\TransparentEmail\Emails\Email;
use bkrukowski\TransparentEmail\Services\YahooCom; use bkrukowski\TransparentEmail\Services\YahooCom;
class YahooComTest extends \PHPUnit_Framework_TestCase class YahooComTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* @dataProvider providerIsDomainSupported * @dataProvider providerIsSupported
* *
* @param string $domain * @param string $domain
* @param bool $result * @param bool $result
*/ */
public function testIsDomainSupported(string $domain, 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 [ return [
['yahoo.com', true], ['yahoo.com', true],
@ -40,7 +41,7 @@ class YahooComTest extends \PHPUnit_Framework_TestCase
*/ */
public function testGetPrimaryEmail(string $inputEmail, string $expectedEmail) 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() public function providerGetPrimaryEmail()

View File

@ -4,46 +4,29 @@ declare(strict_types=1);
namespace bkrukowski\TransparentEmail\Tests; namespace bkrukowski\TransparentEmail\Tests;
use bkrukowski\TransparentEmail\Emails\Email;
use bkrukowski\TransparentEmail\ServiceCollector; use bkrukowski\TransparentEmail\ServiceCollector;
use bkrukowski\TransparentEmail\ServiceCollectorInterface; use bkrukowski\TransparentEmail\ServiceCollectorInterface;
use bkrukowski\TransparentEmail\Services\TlenPl; use bkrukowski\TransparentEmail\Services\TlenPl;
use bkrukowski\TransparentEmail\TransparentEmail; use bkrukowski\TransparentEmail\TransparentEmail;
use bkrukowski\TransparentEmail\InvalidEmailException;
class TransparentEmailTest extends \PHPUnit_Framework_TestCase 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 * @dataProvider providerGetPrimaryEmail
* *
* @param TransparentEmail $transparentEmail * @param TransparentEmail $transparentEmail
* @param string $email * @param string $email
* @param string $expectedEmail * @param string $expectedEmail
* @param bool $caseSensitive
*/ */
public function testGetPrimaryEmail(TransparentEmail $transparentEmail, string $email, string $expectedEmail) public function testGetPrimaryEmail(
{ TransparentEmail $transparentEmail,
$this->assertSame($expectedEmail, $transparentEmail->getPrimaryEmail($email)); string $email,
string $expectedEmail,
bool $caseSensitive = false
) {
$this->assertEquals($expectedEmail, $transparentEmail->getPrimaryEmail(new Email($email, $caseSensitive)));
} }
public function providerGetPrimaryEmail() 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(), 'john.doe+alias@gmail.com', 'johndoe@gmail.com'],
[new TransparentEmail($emptyServiceCollector), 'John.Doe@example.com', 'john.doe@example.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($emptyServiceCollector), 'John.Doe@example.com', 'John.Doe@example.com', true],
[new TransparentEmail(null, true), 'John.Doe@gmail.com', 'johndoe@gmail.com'], [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@hotmail.com', 'jane.doe@hotmail.com'],
[new TransparentEmail(), 'Jane.Doe-receipts@yahoo.com', 'jane.doe@yahoo.com'], [new TransparentEmail(), 'Jane.Doe-receipts@yahoo.com', 'jane.doe@yahoo.com'],
]; ];