From 6fb1eae38f42b56899859071becfa8a716796742 Mon Sep 17 00:00:00 2001 From: bkrukowski Date: Sat, 10 Sep 2016 00:41:21 +0200 Subject: [PATCH] Initial public release --- .coveralls.yml | 3 ++ .gitignore | 4 ++ .travis.yml | 21 +++++++++ LICENSE | 7 +++ composer.json | 30 ++++++++++++ phpunit.xml.dist | 25 ++++++++++ src/Gordianus.php | 65 +++++++++++++++++++++++++ src/InvalidEmailException.php | 7 +++ src/Services/GmailCOM.php | 21 +++++++++ src/Services/MultiDomain.php | 23 +++++++++ src/Services/ServiceInterface.php | 13 +++++ src/Services/TlenPL.php | 25 ++++++++++ src/Services/WWW33MailCOM.php | 21 +++++++++ tests/GordianusTest.php | 51 ++++++++++++++++++++ tests/Services/GmailCOMTest.php | 49 +++++++++++++++++++ tests/Services/MultiDomainTest.php | 73 +++++++++++++++++++++++++++++ tests/Services/WWW33MailCOMTest.php | 48 +++++++++++++++++++ 17 files changed, 486 insertions(+) create mode 100644 .coveralls.yml create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 LICENSE create mode 100644 composer.json create mode 100644 phpunit.xml.dist create mode 100644 src/Gordianus.php create mode 100644 src/InvalidEmailException.php create mode 100644 src/Services/GmailCOM.php create mode 100644 src/Services/MultiDomain.php create mode 100644 src/Services/ServiceInterface.php create mode 100644 src/Services/TlenPL.php create mode 100644 src/Services/WWW33MailCOM.php create mode 100644 tests/GordianusTest.php create mode 100644 tests/Services/GmailCOMTest.php create mode 100644 tests/Services/MultiDomainTest.php create mode 100644 tests/Services/WWW33MailCOMTest.php diff --git a/.coveralls.yml b/.coveralls.yml new file mode 100644 index 0000000..b81810c --- /dev/null +++ b/.coveralls.yml @@ -0,0 +1,3 @@ +coverage_clover: build/logs/clover.xml +json_path: build/logs/coveralls-upload.json +service_name: travis-ci \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..be0a1f4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/.idea/ +composer.lock +/vendor/ +/build/ \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..3fadbf3 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,21 @@ +language: php + +install: + - php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" + - php -r "if (hash_file('SHA384', 'composer-setup.php') === 'e115a8dc7871f15d853148a7fbac7da27d6c0030b848d9b3dc09e2a0388afed865e6a3d6b3c0fad45c48e2b5fc1196ae') { echo 'Installer verified' . PHP_EOL; } else { throw new \RuntimeException('Installer corrupt'); }" + - php composer-setup.php + - php -r "unlink('composer-setup.php');" + - travis_wait php composer.phar install --profile --no-interaction + - mkdir -p build/logs + +php: + - 7.0 + - hhvm + - nightly + +script: + - ./vendor/bin/phpunit + +after_script: + - travis_retry php vendor/bin/coveralls -v + - travis_retry php vendor/bin/codacycoverage clover ./build/logs/clover.xml diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..afbaf8a --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright (c) 2016 Bartłomiej Krukowski + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..a3e3faa --- /dev/null +++ b/composer.json @@ -0,0 +1,30 @@ +{ + "name": "bkrukowski/gordianus", + "description": "Remove aliases from email and get primary email account", + "type": "library", + "require": { + "php": "^7.0.0" + }, + "require-dev": { + "phpunit/phpunit": "^5.5.4", + "satooshi/php-coveralls": "^1.0.1", + "codacy/coverage": "^1.0.3" + }, + "license": "MIT", + "authors": [ + { + "name": "Bartłomiej Krukowski", + "email": "bartlomiej@krukowski.me" + } + ], + "autoload": { + "psr-4": { + "bkrukowski\\Gordianus\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "bkrukowski\\Gordianus\\Tests\\": "tests/" + } + } +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..886ebf4 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,25 @@ + + + + + + + tests + + + + + + ./src + + + + + + + \ No newline at end of file diff --git a/src/Gordianus.php b/src/Gordianus.php new file mode 100644 index 0000000..29a43b1 --- /dev/null +++ b/src/Gordianus.php @@ -0,0 +1,65 @@ +services = $services; + } + + /** + * @param string $email + * @return string + * @throws InvalidEmailException + */ + public function getPrimaryEmail(string $email) + { + $this->validateEmailAddress($email); + $email = strtolower($email); + list(, $domain) = explode('@', $email); + $result = $email; + + foreach ($this->services as $serviceClass) { + /** @var ServiceInterface $service */ + $service = new $serviceClass(); + if ($service->isDomainSupported($domain)) { + $result = $service->getPrimaryEmail($result); + } + } + + 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}'!"); + } + } +} \ No newline at end of file diff --git a/src/InvalidEmailException.php b/src/InvalidEmailException.php new file mode 100644 index 0000000..68f36cb --- /dev/null +++ b/src/InvalidEmailException.php @@ -0,0 +1,7 @@ +getPrimaryDomain(); + } + + public function isDomainSupported(string $domain) : bool + { + return in_array($domain, $this->getDomainList()); + } + + abstract protected function getPrimaryDomain() : string; + + abstract protected function getDomainList() : array; +} \ No newline at end of file diff --git a/src/Services/ServiceInterface.php b/src/Services/ServiceInterface.php new file mode 100644 index 0000000..bd5d66e --- /dev/null +++ b/src/Services/ServiceInterface.php @@ -0,0 +1,13 @@ +expectException(InvalidEmailException::class); + } + (new Gordianus())->getPrimaryEmail($email); + } + + public function providerValidator() + { + return [ + ['john.doe@gmail.comm', true], + ['john doe@gmail.com', false], + ]; + } + + /** + * @dataProvider providerGetPrimaryEmail + * + * @param Gordianus $gordianus + * @param string $email + * @param string $expectedEmail + */ + public function testGetPrimaryEmail(Gordianus $gordianus, string $email, string $expectedEmail) + { + $this->assertSame($expectedEmail, $gordianus->getPrimaryEmail($email)); + } + + public function providerGetPrimaryEmail() + { + return [ + [new Gordianus([Gordianus::SERVICE_TLEN_PL]), 'john.doe+alias@gmail.com', 'john.doe+alias@gmail.com'], + [new Gordianus(), 'john.doe+alias@gmail.com', 'johndoe@gmail.com'], + ]; + } +} \ No newline at end of file diff --git a/tests/Services/GmailCOMTest.php b/tests/Services/GmailCOMTest.php new file mode 100644 index 0000000..f220938 --- /dev/null +++ b/tests/Services/GmailCOMTest.php @@ -0,0 +1,49 @@ +assertSame($outputEmail, (new GmailCOM())->getPrimaryEmail($inputEmail)); + } + + public function providerGetPrimaryEmail() + { + return [ + ['foo.bar@gmail.com', 'foobar@gmail.com'], + ['foobar+alias@gmail.com', 'foobar@gmail.com'], + ['fo.ob.ar+alias@gmail.com', 'foobar@gmail.com'], + ]; + } + + /** + * @dataProvider providerIsDomainSupported + * + * @param string $domain + * @param bool $result + */ + public function testIsDomainSupported(string $domain, bool $result) + { + $this->assertSame($result, (new GmailCOM())->isDomainSupported($domain)); + } + + public function providerIsDomainSupported() + { + return [ + ['gmail.com', true], + ['google.com', false], + ['gmailcom', false], + ['g.mail.com', false] + ]; + } +} \ No newline at end of file diff --git a/tests/Services/MultiDomainTest.php b/tests/Services/MultiDomainTest.php new file mode 100644 index 0000000..e9151d6 --- /dev/null +++ b/tests/Services/MultiDomainTest.php @@ -0,0 +1,73 @@ +assertSame($expected, $mock->isDomainSupported($domain)); + } + + public function providerIsDomainSupported() + { + return [ + [$this->getMultiDomainMock('foo.bar', ['foo.bar']), 'gmail.com', false], + [$this->getMultiDomainMock('foo.bar', ['foo.bar', 'gmail.com']), 'gmail.com', true], + ]; + } + + /** + * @dataProvider providerGetPrimaryDomain + * + * @param MultiDomain $mock + * @param string $email + * @param string $expectedEmail + */ + public function testGetPrimaryDomain(MultiDomain $mock, string $email, string $expectedEmail) + { + $this->assertSame($expectedEmail, $mock->getPrimaryEmail($email)); + } + + public function providerGetPrimaryDomain() + { + 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.bar2', 'name@foo.bar'], + ]; + } + + private function getMultiDomainMock(string $primaryDomain, array $supportedDomains) : MultiDomain + { + return new class ($primaryDomain, $supportedDomains) extends MultiDomain { + private $primaryDomain; + + private $supportedDomains; + + public function __construct(string $primaryDomain, array $supportedDomains) + { + $this->primaryDomain = $primaryDomain; + $this->supportedDomains = $supportedDomains; + } + + protected function getDomainList() : array + { + return $this->supportedDomains; + } + + protected function getPrimaryDomain() : string + { + return $this->primaryDomain; + } + }; + } +} \ No newline at end of file diff --git a/tests/Services/WWW33MailCOMTest.php b/tests/Services/WWW33MailCOMTest.php new file mode 100644 index 0000000..4fe82d9 --- /dev/null +++ b/tests/Services/WWW33MailCOMTest.php @@ -0,0 +1,48 @@ +assertSame($isSupported, (new WWW33MailCOM())->isDomainSupported($domain)); + } + + public function providerIsDomainSupported() + { + return [ + ['foo.33mail.com', true], + ['bar.33mail.com', true], + ['foo.34mail.com', false], + ['foo33mail.com', false], + ]; + } + + /** + * @dataProvider providerGetPrimaryEmail + * + * @param string $inputEmail + * @param string $expectedEmail + */ + public function testGetPrimaryEmail(string $inputEmail, string $expectedEmail) + { + $this->assertSame($expectedEmail, (new WWW33MailCOM())->getPrimaryEmail($inputEmail)); + } + + public function providerGetPrimaryEmail() + { + return [ + ['qwerty@name.33mail.com', 'name@name.33mail.com'], + ['lorem@ipsum.33mail.com', 'ipsum@ipsum.33mail.com'], + ]; + } +} \ No newline at end of file