From f7e1b62e81a68c58fb0eb921bcfdd09ace576e7f Mon Sep 17 00:00:00 2001 From: bkrukowski Date: Sat, 24 Sep 2016 12:09:20 +0200 Subject: [PATCH] Pass email as value object --- README.md | 7 +- src/Emails/Email.php | 27 +++++ src/Emails/EmailInterface.php | 15 +++ src/Emails/EmailTrait.php | 27 +++++ src/{ => Emails}/InvalidEmailException.php | 2 +- src/Emails/MutableEmail.php | 56 +++++++++ src/Services/AppsGoogleCom.php | 9 +- src/Services/GmailCom.php | 32 +++--- src/Services/MultiDomain.php | 20 ++-- src/Services/OutlookCom.php | 18 +-- src/Services/ServiceInterface.php | 9 +- src/Services/TlenPl.php | 1 - src/Services/Www33MailCom.php | 17 ++- src/Services/YahooCom.php | 18 +-- src/TransparentEmail.php | 40 +------ tests/Emails/EmailTest.php | 45 ++++++++ tests/Emails/MutableEmailTest.php | 126 +++++++++++++++++++++ tests/Services/AppsGoogleComTest.php | 11 +- tests/Services/GmailComTest.php | 17 +-- tests/Services/MultiDomainTest.php | 15 +-- tests/Services/OutlookComTest.php | 11 +- tests/Services/Www33MailComTest.php | 11 +- tests/Services/YahooComTest.php | 9 +- tests/TransparentEmailTest.php | 39 ++----- 24 files changed, 423 insertions(+), 159 deletions(-) create mode 100644 src/Emails/Email.php create mode 100644 src/Emails/EmailInterface.php create mode 100644 src/Emails/EmailTrait.php rename src/{ => Emails}/InvalidEmailException.php (55%) create mode 100644 src/Emails/MutableEmail.php create mode 100644 tests/Emails/EmailTest.php create mode 100644 tests/Emails/MutableEmailTest.php diff --git a/README.md b/README.md index 2ed67b2..84616fb 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Emails/Email.php b/src/Emails/Email.php new file mode 100644 index 0000000..f94b955 --- /dev/null +++ b/src/Emails/Email.php @@ -0,0 +1,27 @@ +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!"); + } + } +} \ No newline at end of file diff --git a/src/Emails/EmailInterface.php b/src/Emails/EmailInterface.php new file mode 100644 index 0000000..28fa4e1 --- /dev/null +++ b/src/Emails/EmailInterface.php @@ -0,0 +1,15 @@ +localPart; + } + + public function getDomain() : string + { + return $this->domain; + } + + public function __toString() : string + { + return $this->localPart . '@' . $this->domain; + } +} \ No newline at end of file diff --git a/src/InvalidEmailException.php b/src/Emails/InvalidEmailException.php similarity index 55% rename from src/InvalidEmailException.php rename to src/Emails/InvalidEmailException.php index fe48abd..43327a8 100644 --- a/src/InvalidEmailException.php +++ b/src/Emails/InvalidEmailException.php @@ -1,6 +1,6 @@ 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; + } +} \ No newline at end of file diff --git a/src/Services/AppsGoogleCom.php b/src/Services/AppsGoogleCom.php index ee88ff9..59f34e8 100644 --- a/src/Services/AppsGoogleCom.php +++ b/src/Services/AppsGoogleCom.php @@ -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) { diff --git a/src/Services/GmailCom.php b/src/Services/GmailCom.php index 6e399d3..49cc461 100644 --- a/src/Services/GmailCom.php +++ b/src/Services/GmailCom.php @@ -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; } } \ No newline at end of file diff --git a/src/Services/MultiDomain.php b/src/Services/MultiDomain.php index e882b60..fe33d70 100644 --- a/src/Services/MultiDomain.php +++ b/src/Services/MultiDomain.php @@ -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 explode('@', $email)[0] . '@' . $this->getPrimaryDomain(); + return (new MutableEmail($email)) + ->setDomain($this->getPrimaryDomain()) + ->lowerCaseLocalPartIf(!$this->isCaseSensitive()); } - 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); } /** diff --git a/src/Services/OutlookCom.php b/src/Services/OutlookCom.php index c185b9f..60bfdcf 100644 --- a/src/Services/OutlookCom.php +++ b/src/Services/OutlookCom.php @@ -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); } } \ No newline at end of file diff --git a/src/Services/ServiceInterface.php b/src/Services/ServiceInterface.php index 948c06f..5624026 100644 --- a/src/Services/ServiceInterface.php +++ b/src/Services/ServiceInterface.php @@ -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; } \ No newline at end of file diff --git a/src/Services/TlenPl.php b/src/Services/TlenPl.php index 1dec5c2..5e91f10 100644 --- a/src/Services/TlenPl.php +++ b/src/Services/TlenPl.php @@ -6,7 +6,6 @@ namespace bkrukowski\TransparentEmail\Services; /** * @codeCoverageIgnore - * @internal */ class TlenPl extends MultiDomain { diff --git a/src/Services/Www33MailCom.php b/src/Services/Www33MailCom.php index 6750667..996eab2 100644 --- a/src/Services/Www33MailCom.php +++ b/src/Services/Www33MailCom.php @@ -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()); } } \ No newline at end of file diff --git a/src/Services/YahooCom.php b/src/Services/YahooCom.php index bcfcc25..6ff47ef 100644 --- a/src/Services/YahooCom.php +++ b/src/Services/YahooCom.php @@ -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'; } } \ No newline at end of file diff --git a/src/TransparentEmail.php b/src/TransparentEmail.php index 4172243..5758b1d 100644 --- a/src/TransparentEmail.php +++ b/src/TransparentEmail.php @@ -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; } } \ No newline at end of file diff --git a/tests/Emails/EmailTest.php b/tests/Emails/EmailTest.php new file mode 100644 index 0000000..c5b1fc0 --- /dev/null +++ b/tests/Emails/EmailTest.php @@ -0,0 +1,45 @@ +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'], + ]; + } +} \ No newline at end of file diff --git a/tests/Emails/MutableEmailTest.php b/tests/Emails/MutableEmailTest.php new file mode 100644 index 0000000..05f973d --- /dev/null +++ b/tests/Emails/MutableEmailTest.php @@ -0,0 +1,126 @@ +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], + ]; + } +} \ No newline at end of file diff --git a/tests/Services/AppsGoogleComTest.php b/tests/Services/AppsGoogleComTest.php index ee16fbd..bcb0ba0 100644 --- a/tests/Services/AppsGoogleComTest.php +++ b/tests/Services/AppsGoogleComTest.php @@ -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], ]; } } \ No newline at end of file diff --git a/tests/Services/GmailComTest.php b/tests/Services/GmailComTest.php index f371b75..3eac764 100644 --- a/tests/Services/GmailComTest.php +++ b/tests/Services/GmailComTest.php @@ -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], ]; } } \ No newline at end of file diff --git a/tests/Services/MultiDomainTest.php b/tests/Services/MultiDomainTest.php index 9ff7911..60f843e 100644 --- a/tests/Services/MultiDomainTest.php +++ b/tests/Services/MultiDomainTest.php @@ -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'], ]; } diff --git a/tests/Services/OutlookComTest.php b/tests/Services/OutlookComTest.php index f53a4cc..4849971 100644 --- a/tests/Services/OutlookComTest.php +++ b/tests/Services/OutlookComTest.php @@ -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() diff --git a/tests/Services/Www33MailComTest.php b/tests/Services/Www33MailComTest.php index ddd7978..e6a5ef4 100644 --- a/tests/Services/Www33MailComTest.php +++ b/tests/Services/Www33MailComTest.php @@ -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() diff --git a/tests/Services/YahooComTest.php b/tests/Services/YahooComTest.php index ec2d47a..7ce476c 100644 --- a/tests/Services/YahooComTest.php +++ b/tests/Services/YahooComTest.php @@ -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() diff --git a/tests/TransparentEmailTest.php b/tests/TransparentEmailTest.php index 907c765..cd34838 100644 --- a/tests/TransparentEmailTest.php +++ b/tests/TransparentEmailTest.php @@ -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'], ];