Added EmailService

This commit is contained in:
Marcin Kurczewski 2014-08-31 16:57:59 +02:00
parent 7be8061aa8
commit 538b88952e
2 changed files with 42 additions and 0 deletions

View file

@ -0,0 +1,14 @@
<?php
namespace Szurubooru\Services;
class EmailService
{
public function validateEmail($email)
{
if (!$email)
return;
if (!preg_match('/^[^@]+@[^@]+\.\w+$/', $email))
throw new \DomainException('Specified e-mail appears to be invalid.');
}
}

View file

@ -0,0 +1,28 @@
<?php
namespace Szurubooru\Tests\Services;
class EmailServiceTest extends \PHPUnit_Framework_TestCase
{
public function testEmailWithoutAt()
{
$emailService = new \Szurubooru\Services\EmailService();
$this->setExpectedException(\DomainException::class);
$emailService->validateEmail('ghost');
}
public function testEmailWithoutDotInDomain()
{
$emailService = new \Szurubooru\Services\EmailService();
$this->setExpectedException(\DomainException::class);
$emailService->validateEmail('ghost@cemetery');
}
public function testValidEmail()
{
$emailService = new \Szurubooru\Services\EmailService();
$this->assertNull($emailService->validateEmail('ghost@cemetery.consulting'));
}
}