2016-04-27 12:52:44 +05:30
|
|
|
<?php
|
|
|
|
namespace Ely\TempMailBuster;
|
|
|
|
|
2016-04-29 04:25:57 +05:30
|
|
|
class ValidatorTest extends \PHPUnit_Framework_TestCase
|
2016-04-27 12:52:44 +05:30
|
|
|
{
|
2016-04-29 02:07:24 +05:30
|
|
|
public function testValidate()
|
|
|
|
{
|
2016-04-29 04:25:57 +05:30
|
|
|
$object = new Validator(new Storage());
|
2016-04-29 02:07:24 +05:30
|
|
|
$this->assertTrue($object->validate('notch@mojang.com'));
|
|
|
|
|
2016-04-29 04:25:57 +05:30
|
|
|
$object = new Validator(new Storage());
|
2016-04-29 02:07:24 +05:30
|
|
|
$object->whitelistMode();
|
|
|
|
$this->assertFalse($object->validate('notch@mojang.com'));
|
|
|
|
|
2016-04-29 04:25:57 +05:30
|
|
|
$object = new Validator(new Storage(['mojang\.com']));
|
2016-04-29 02:07:24 +05:30
|
|
|
$this->assertFalse($object->validate('notch@mojang.com'));
|
|
|
|
$this->assertTrue($object->validate('erickskrauch@ely.by'));
|
|
|
|
|
2016-04-29 04:25:57 +05:30
|
|
|
$object = new Validator(new Storage(['gmail\.com']));
|
2016-04-29 02:07:24 +05:30
|
|
|
$object->whitelistMode();
|
|
|
|
$this->assertFalse($object->validate('team@ely.by'));
|
|
|
|
$this->assertTrue($object->validate('erickskrauch@gmail.com'));
|
|
|
|
|
2016-04-29 04:25:57 +05:30
|
|
|
$object = new Validator(new Storage(['mojang\.com', 'ely\.by']), new Storage(['ely\.by']));
|
2016-04-29 02:07:24 +05:30
|
|
|
$this->assertFalse($object->validate('notch@mojang.com'));
|
|
|
|
$this->assertTrue($object->validate('team@ely.by'));
|
|
|
|
|
2016-04-29 04:25:57 +05:30
|
|
|
$object = new Validator(new Storage(['gmail\.com', 'mail\.ru']), new Storage(['mail\.ru']));
|
2016-04-29 02:07:24 +05:30
|
|
|
$object->whitelistMode();
|
|
|
|
$this->assertTrue($object->validate('erickskrauch@gmail.com'));
|
|
|
|
$this->assertFalse($object->validate('random@mail.ru'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetPrimaryStorage()
|
2016-04-27 12:52:44 +05:30
|
|
|
{
|
|
|
|
$storage = new Storage(['test']);
|
2016-04-29 04:25:57 +05:30
|
|
|
$object = new Validator($storage);
|
2016-04-29 02:07:24 +05:30
|
|
|
$this->assertEquals($storage, $object->getPrimaryStorage());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetPrimaryStorage()
|
|
|
|
{
|
|
|
|
$storage = new Storage(['test2']);
|
2016-04-29 04:25:57 +05:30
|
|
|
$object = new Validator(new Storage(['test1']));
|
2016-04-29 02:07:24 +05:30
|
|
|
$this->assertEquals($object, $object->setPrimaryStorage($storage));
|
|
|
|
$this->assertEquals($storage, $object->getPrimaryStorage());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetSecondaryStorage()
|
|
|
|
{
|
2016-04-29 04:25:57 +05:30
|
|
|
$object = new Validator(new Storage());
|
2016-04-29 02:07:24 +05:30
|
|
|
$this->assertNull($object->getSecondaryStorage());
|
|
|
|
|
|
|
|
$storage = new Storage(['test']);
|
2016-04-29 04:25:57 +05:30
|
|
|
$object = new Validator(new Storage(), $storage);
|
2016-04-29 02:07:24 +05:30
|
|
|
$this->assertEquals($storage, $object->getSecondaryStorage());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetSecondaryStorage()
|
|
|
|
{
|
|
|
|
$storage = new Storage(['test2']);
|
2016-04-29 04:25:57 +05:30
|
|
|
$object = new Validator(new Storage());
|
2016-04-29 02:07:24 +05:30
|
|
|
$this->assertEquals($object, $object->setSecondaryStorage($storage));
|
|
|
|
$this->assertEquals($storage, $object->getSecondaryStorage());
|
|
|
|
$object->setSecondaryStorage(null);
|
|
|
|
$this->assertNull($object->getSecondaryStorage(), 'Set by null should work');
|
|
|
|
$object->setSecondaryStorage($storage);
|
|
|
|
$object->setSecondaryStorage();
|
|
|
|
$this->assertNull($object->getSecondaryStorage(), 'Set by empty value should work');
|
2016-04-27 12:52:44 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetDomain()
|
|
|
|
{
|
2016-04-29 04:25:57 +05:30
|
|
|
$object = new Validator(new Storage());
|
2016-04-27 12:52:44 +05:30
|
|
|
$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'));
|
|
|
|
}
|
|
|
|
|
2016-04-29 02:07:24 +05:30
|
|
|
public function testIsIsWhitelistMode()
|
2016-04-27 12:52:44 +05:30
|
|
|
{
|
2016-04-29 04:25:57 +05:30
|
|
|
$object = new Validator(new Storage());
|
2016-04-29 02:07:24 +05:30
|
|
|
$this->assertFalse($object->isIsWhitelistMode(), 'Default should be false');
|
|
|
|
$object->whitelistMode();
|
|
|
|
$this->assertTrue($object->isIsWhitelistMode());
|
2016-04-27 12:52:44 +05:30
|
|
|
}
|
|
|
|
|
2016-04-29 02:07:24 +05:30
|
|
|
public function testWhitelistMode()
|
2016-04-27 12:52:44 +05:30
|
|
|
{
|
2016-04-29 04:25:57 +05:30
|
|
|
$object = new Validator(new Storage());
|
2016-04-29 02:07:24 +05:30
|
|
|
$this->assertEquals($object, $object->whitelistMode());
|
|
|
|
$this->assertTrue($object->isIsWhitelistMode(), 'Default value should change mode to whitelist');
|
|
|
|
$object->whitelistMode(false);
|
|
|
|
$this->assertFalse($object->isIsWhitelistMode());
|
|
|
|
$object->whitelistMode(true);
|
|
|
|
$this->assertTrue($object->isIsWhitelistMode());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBuildRegex()
|
|
|
|
{
|
2016-04-29 04:25:57 +05:30
|
|
|
$object = new Validator(new Storage());
|
2016-04-29 02:07:24 +05:30
|
|
|
$this->assertEquals('/^(simple)$/', $this->callBuildRegex($object, ['simple']));
|
|
|
|
$this->assertEquals('/^(simple|another)$/', $this->callBuildRegex($object, ['simple', 'another']));
|
2016-04-27 12:52:44 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|