From 53d5f0852f2803eb45369260e7eed81f6aee8dc9 Mon Sep 17 00:00:00 2001 From: bkrukowski Date: Fri, 23 Sep 2016 21:29:25 +0200 Subject: [PATCH] Added ServiceCollectorInterface --- src/DefaultServiceCollector.php | 34 +++++++++++++++++++++++++ src/ServiceCollector.php | 27 ++++++++++++++++++++ src/ServiceCollectorInterface.php | 13 ++++++++++ src/TransparentEmail.php | 36 ++++++--------------------- tests/DefaultServiceCollectorTest.php | 21 ++++++++++++++++ tests/ServiceCollectorTest.php | 28 +++++++++++++++++++++ tests/TransparentEmailTest.php | 24 +++++++++++++++--- 7 files changed, 150 insertions(+), 33 deletions(-) create mode 100644 src/DefaultServiceCollector.php create mode 100644 src/ServiceCollector.php create mode 100644 src/ServiceCollectorInterface.php create mode 100644 tests/DefaultServiceCollectorTest.php create mode 100644 tests/ServiceCollectorTest.php diff --git a/src/DefaultServiceCollector.php b/src/DefaultServiceCollector.php new file mode 100644 index 0000000..239e1ed --- /dev/null +++ b/src/DefaultServiceCollector.php @@ -0,0 +1,34 @@ +getAllServicesClasses() as $class) { + $this->addService(new $class()); + } + } + + private function getAllServicesClasses() : array + { + return [ + GmailCom::class, + OutlookCom::class, + YahooCom::class, + Www33MailCom::class, + TlenPl::class, + AppsGoogleCom::class, + ]; + } +} \ No newline at end of file diff --git a/src/ServiceCollector.php b/src/ServiceCollector.php new file mode 100644 index 0000000..8258942 --- /dev/null +++ b/src/ServiceCollector.php @@ -0,0 +1,27 @@ +services[] = $service; + } + + /** + * @inheritdoc + */ + public function getIterator() + { + foreach ($this->services as $service) { + yield $service; + } + } +} \ No newline at end of file diff --git a/src/ServiceCollectorInterface.php b/src/ServiceCollectorInterface.php new file mode 100644 index 0000000..9d98605 --- /dev/null +++ b/src/ServiceCollectorInterface.php @@ -0,0 +1,13 @@ +services = $services; + $this->services = $services ?: new DefaultServiceCollector(); $this->caseSensitiveLocalPart = $caseSensitiveLocalPart; } @@ -55,12 +36,9 @@ class TransparentEmail $domain = strtolower(explode('@', $email)[1]); $result = $email; - foreach ($this->services as $serviceClass) { - /** @var ServiceInterface $service */ - $service = new $serviceClass(); + foreach ($this->services as $service) { if ($service->isDomainSupported($domain)) { $result = $service->getPrimaryEmail($result); - break; } } diff --git a/tests/DefaultServiceCollectorTest.php b/tests/DefaultServiceCollectorTest.php new file mode 100644 index 0000000..c5dab4c --- /dev/null +++ b/tests/DefaultServiceCollectorTest.php @@ -0,0 +1,21 @@ +assertInstanceOf(ServiceInterface::class, $service); + } + $this->assertTrue($atLeastOne); + } +} \ No newline at end of file diff --git a/tests/ServiceCollectorTest.php b/tests/ServiceCollectorTest.php new file mode 100644 index 0000000..38f77f5 --- /dev/null +++ b/tests/ServiceCollectorTest.php @@ -0,0 +1,28 @@ +createMock(ServiceInterface::class); + $collector = new ServiceCollector(); + $collector->addService($service); + + $contains = false; + foreach ($collector->getIterator() as $currentService) { + if ($currentService === $service) { + $contains = true; + break; + } + } + + $this->assertTrue($contains); + } +} \ No newline at end of file diff --git a/tests/TransparentEmailTest.php b/tests/TransparentEmailTest.php index 95dbd4e..efa42f1 100644 --- a/tests/TransparentEmailTest.php +++ b/tests/TransparentEmailTest.php @@ -2,6 +2,9 @@ namespace bkrukowski\TransparentEmail\Tests; +use bkrukowski\TransparentEmail\ServiceCollector; +use bkrukowski\TransparentEmail\ServiceCollectorInterface; +use bkrukowski\TransparentEmail\Services\TlenPl; use bkrukowski\TransparentEmail\TransparentEmail; use bkrukowski\TransparentEmail\InvalidEmailException; @@ -43,18 +46,31 @@ class TransparentEmailTest extends \PHPUnit_Framework_TestCase public function providerGetPrimaryEmail() { + $emptyServiceCollector = $this->createServiceCollector(); + $tlenServiceCollector = $this->createServiceCollector([TlenPl::class]); + return [ [ - new TransparentEmail([TransparentEmail::SERVICE_TLEN_PL]), + new TransparentEmail($tlenServiceCollector), 'john.doe+alias@gmail.com', 'john.doe+alias@gmail.com' ], [new TransparentEmail(), 'john.doe+alias@gmail.com', 'johndoe@gmail.com'], - [new TransparentEmail([]), 'John.Doe@example.com', 'john.doe@example.com'], - [new TransparentEmail([], true), 'John.Doe@example.com', 'John.Doe@example.com'], - [new TransparentEmail(TransparentEmail::ALL_SERVICES, true), 'John.Doe@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(), 'Jane.Doe+receipts@hotmail.com', 'jane.doe@hotmail.com'], [new TransparentEmail(), 'Jane.Doe-receipts@yahoo.com', 'jane.doe@yahoo.com'], ]; } + + private function createServiceCollector(array $classes = []) : ServiceCollectorInterface + { + $collector = new ServiceCollector(); + foreach ($classes as $class) { + $collector->addService(new $class()); + } + + return $collector; + } } \ No newline at end of file