mirror of
https://github.com/elyby/php-tempmailbuster.git
synced 2024-11-24 07:49:45 +05:30
Implemented TempMailBuster class and his tests.
Extended tests for Storage class
This commit is contained in:
parent
f65846ad9b
commit
3a2d7c7e63
70
src/TempMailBuster.php
Normal file
70
src/TempMailBuster.php
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
<?php
|
||||||
|
namespace Ely\TempMailBuster;
|
||||||
|
|
||||||
|
class TempMailBuster
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var Storage
|
||||||
|
*/
|
||||||
|
private $storage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Storage
|
||||||
|
*/
|
||||||
|
public function getStorage()
|
||||||
|
{
|
||||||
|
return $this->storage;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate passed email over all storage domains and return true if domain not found
|
||||||
|
* in lists. False otherwise.
|
||||||
|
* @see TempMailBuster::getDomain() for info about accepted $email formats
|
||||||
|
*
|
||||||
|
* @param string $email
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function validate($email)
|
||||||
|
{
|
||||||
|
$domain = $this->getDomain($email);
|
||||||
|
$tempMailsRegex = $this->buildRegex($this->getStorage()->getBlacklist());
|
||||||
|
$match = preg_match($tempMailsRegex, $domain);
|
||||||
|
|
||||||
|
// TODO: add support for whitelists
|
||||||
|
|
||||||
|
return !$match;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Catches the domain part of passed E-mail address. Expected values is:
|
||||||
|
* - full E-mail: erickskrauch@ely.by
|
||||||
|
* - domain, starting with @: @ely.by
|
||||||
|
* - domain itself: ely.by
|
||||||
|
*
|
||||||
|
* @param string $email
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getDomain($email)
|
||||||
|
{
|
||||||
|
$parts = explode('@', $email);
|
||||||
|
return array_pop($parts);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $list
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function buildRegex(array $list)
|
||||||
|
{
|
||||||
|
return '/^(' . implode('|', $list) . ')$/';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Storage $storage
|
||||||
|
*/
|
||||||
|
public function __construct(Storage $storage)
|
||||||
|
{
|
||||||
|
$this->storage = $storage;
|
||||||
|
}
|
||||||
|
}
|
@ -12,18 +12,18 @@ class StorageTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testAppendToBlacklist()
|
public function testAppendToBlacklist()
|
||||||
{
|
{
|
||||||
$storage = new Storage(['item1']);
|
$storage = new Storage(['item1']);
|
||||||
$storage->appendToBlacklist(['item2']);
|
$this->assertEquals($storage, $storage->appendToBlacklist(['item2']));
|
||||||
$this->assertEquals(['item1', 'item2'], $storage->getBlacklist());
|
$this->assertEquals(['item1', 'item2'], $storage->getBlacklist());
|
||||||
|
|
||||||
$storage = new Storage(['item1']);
|
$storage = new Storage(['item1']);
|
||||||
$storage->appendToBlacklist('item2');
|
$this->assertEquals($storage, $storage->appendToBlacklist('item2'));
|
||||||
$this->assertEquals(['item1', 'item2'], $storage->getBlacklist());
|
$this->assertEquals(['item1', 'item2'], $storage->getBlacklist());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSetBlacklist()
|
public function testSetBlacklist()
|
||||||
{
|
{
|
||||||
$storage = new Storage(['item1']);
|
$storage = new Storage(['item1']);
|
||||||
$storage->setBlacklist(['item2']);
|
$this->assertEquals($storage, $storage->setBlacklist(['item2']));
|
||||||
$this->assertEquals(['item2'], $storage->getBlacklist());
|
$this->assertEquals(['item2'], $storage->getBlacklist());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
54
tests/TempMailBusterTest.php
Normal file
54
tests/TempMailBusterTest.php
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
namespace Ely\TempMailBuster;
|
||||||
|
|
||||||
|
class TempMailBusterTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
public function testGetStorage()
|
||||||
|
{
|
||||||
|
$storage = new Storage(['test']);
|
||||||
|
$object = new TempMailBuster($storage);
|
||||||
|
$this->assertEquals($storage, $object->getStorage());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetDomain()
|
||||||
|
{
|
||||||
|
$object = new TempMailBuster(new Storage());
|
||||||
|
$this->assertEquals('ely.by', $this->callGetDomain($object, 'erickskrauch@ely.by'));
|
||||||
|
$this->assertEquals('ely.by', $this->callGetDomain($object, '@ely.by'));
|
||||||
|
$this->assertEquals('ely.by', $this->callGetDomain($object, 'ely.by'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testBuildRegex()
|
||||||
|
{
|
||||||
|
$object = new TempMailBuster(new Storage());
|
||||||
|
$this->assertEquals('/^(simple)$/', $this->callBuildRegex($object, ['simple']));
|
||||||
|
$this->assertEquals('/^(simple|another)$/', $this->callBuildRegex($object, ['simple', 'another']));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testValidate()
|
||||||
|
{
|
||||||
|
$storage = new Storage(['mojang\.com']);
|
||||||
|
$object = new TempMailBuster($storage);
|
||||||
|
$this->assertFalse($object->validate('notch@mojang.com'));
|
||||||
|
$this->assertTrue($object->validate('jeb@mojang1.com'));
|
||||||
|
$this->assertTrue($object->validate('erickskrauch@ely.by'));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function callGetDomain($object, $email)
|
||||||
|
{
|
||||||
|
$class = new \ReflectionClass($object);
|
||||||
|
$method = $class->getMethod('getDomain');
|
||||||
|
$method->setAccessible(true);
|
||||||
|
|
||||||
|
return $method->invokeArgs($object, [$email]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function callBuildRegex($object, array $list)
|
||||||
|
{
|
||||||
|
$class = new \ReflectionClass($object);
|
||||||
|
$method = $class->getMethod('buildRegex');
|
||||||
|
$method->setAccessible(true);
|
||||||
|
|
||||||
|
return $method->invokeArgs($object, [$list]);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user