transparent-email/tests/TransparentEmailTest.php

78 lines
2.5 KiB
PHP
Raw Normal View History

2016-09-10 16:13:55 +03:00
<?php
2016-09-23 22:39:33 +03:00
declare(strict_types=1);
2016-09-10 16:13:55 +03:00
namespace bkrukowski\TransparentEmail\Tests;
2016-09-23 22:29:25 +03:00
use bkrukowski\TransparentEmail\ServiceCollector;
use bkrukowski\TransparentEmail\ServiceCollectorInterface;
use bkrukowski\TransparentEmail\Services\TlenPl;
2016-09-10 16:13:55 +03:00
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
*
2016-09-13 19:23:07 +03:00
* @param TransparentEmail $transparentEmail
2016-09-10 16:13:55 +03:00
* @param string $email
* @param string $expectedEmail
*/
2016-09-13 19:23:07 +03:00
public function testGetPrimaryEmail(TransparentEmail $transparentEmail, string $email, string $expectedEmail)
2016-09-10 16:13:55 +03:00
{
2016-09-13 19:23:07 +03:00
$this->assertSame($expectedEmail, $transparentEmail->getPrimaryEmail($email));
2016-09-10 16:13:55 +03:00
}
public function providerGetPrimaryEmail()
{
2016-09-23 22:29:25 +03:00
$emptyServiceCollector = $this->createServiceCollector();
$tlenServiceCollector = $this->createServiceCollector([TlenPl::class]);
2016-09-10 16:13:55 +03:00
return [
2016-09-10 16:18:48 +03:00
[
2016-09-23 22:29:25 +03:00
new TransparentEmail($tlenServiceCollector),
2016-09-10 16:18:48 +03:00
'john.doe+alias@gmail.com',
'john.doe+alias@gmail.com'
],
2016-09-10 16:13:55 +03:00
[new TransparentEmail(), 'john.doe+alias@gmail.com', 'johndoe@gmail.com'],
2016-09-23 22:29:25 +03:00
[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'],
2016-09-10 16:13:55 +03:00
[new TransparentEmail(), 'Jane.Doe+receipts@hotmail.com', 'jane.doe@hotmail.com'],
[new TransparentEmail(), 'Jane.Doe-receipts@yahoo.com', 'jane.doe@yahoo.com'],
];
}
2016-09-23 22:29:25 +03:00
private function createServiceCollector(array $classes = []) : ServiceCollectorInterface
{
$collector = new ServiceCollector();
foreach ($classes as $class) {
$collector->addService(new $class());
}
return $collector;
}
2016-09-10 16:13:55 +03:00
}