transparent-email/tests/TransparentEmailTest.php

87 lines
3.2 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-24 13:09:20 +03:00
use bkrukowski\TransparentEmail\Emails\Email;
2016-09-24 13:42:00 +03:00
use bkrukowski\TransparentEmail\Emails\EmailInterface;
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\TransparentEmailFactory;
2020-02-19 11:37:51 +03:00
use PHPUnit\Framework\TestCase;
2016-09-10 16:13:55 +03:00
2020-02-19 11:37:51 +03:00
class TransparentEmailTest extends TestCase
2016-09-10 16:13:55 +03:00
{
/**
* @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-24 13:09:20 +03:00
* @param bool $caseSensitive
2016-09-10 16:13:55 +03:00
*/
2016-09-24 13:09:20 +03:00
public function testGetPrimaryEmail(
TransparentEmail $transparentEmail,
string $email,
string $expectedEmail,
bool $caseSensitive = false
) {
$this->assertEquals($expectedEmail, $transparentEmail->getPrimaryEmail(new Email($email, $caseSensitive)));
2016-09-10 16:13:55 +03:00
}
2016-10-14 21:25:52 +03:00
public function providerGetPrimaryEmail() : array
2016-09-10 16:13:55 +03:00
{
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-10-26 21:13:11 +03:00
[(new TransparentEmailFactory())->createDefault(), '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'],
2016-09-24 13:09:20 +03:00
[new TransparentEmail($emptyServiceCollector), 'John.Doe@example.com', 'John.Doe@example.com', true],
2016-10-26 21:13:11 +03:00
[(new TransparentEmailFactory())->createDefault(), 'John.Doe@gmail.com', 'johndoe@gmail.com', true],
[(new TransparentEmailFactory())->createDefault(), 'Jane.Doe+receipts@hotmail.com', 'jane.doe@hotmail.com'],
[(new TransparentEmailFactory())->createDefault(), 'Jane.Doe-receipts@yahoo.com', 'jane.doe@yahoo.com'],
2016-09-10 16:13:55 +03:00
];
}
2016-09-23 22:29:25 +03:00
2016-09-24 13:42:00 +03:00
/**
* @dataProvider providerDefault
*
* @param EmailInterface $inputEmail
* @param string $expectedEmail
*/
public function testDefault(EmailInterface $inputEmail, string $expectedEmail)
{
$outputEmail = ((new TransparentEmailFactory())->createDefault())->getPrimaryEmail($inputEmail);
$this->assertEquals($expectedEmail, $outputEmail);
2016-09-24 13:42:00 +03:00
}
2016-10-14 21:25:52 +03:00
public function providerDefault() : array
2016-09-24 13:42:00 +03:00
{
return [
[new Email('John.Doe+spam@gmail.com', true), 'johndoe@gmail.com'],
[new Email('Jane.Doe+spam@outlook.com', true), 'jane.doe@outlook.com'],
[new Email('John.Doe@tlen.pl', true), 'john.doe@o2.pl'],
[new Email('ALIAS@janedoe.33mail.com', true), 'janedoe@janedoe.33mail.com'],
[new Email('John.Doe-facebook@yahoo.com', true), 'john.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
}