mirror of
https://github.com/egulias/EmailValidator.git
synced 2026-08-25 01:18:26 +00:00
Initial approach to avoiding exceptions and better results. Failing test to remember to implement tokens
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Egulias\EmailValidator\Result;
|
||||
|
||||
use Egulias\EmailValidator\Result\Reason\Reason;
|
||||
|
||||
class InvalidEmail implements Result
|
||||
{
|
||||
private $token;
|
||||
private $reason;
|
||||
|
||||
public function __construct(Reason $reason, string $token)
|
||||
{
|
||||
$this->token = $token;
|
||||
$this->reason = $reason;
|
||||
}
|
||||
|
||||
public function isValid(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function description(): string
|
||||
{
|
||||
return $this->reason->description() . " in char " . $this->token;
|
||||
}
|
||||
|
||||
public function code(): int
|
||||
{
|
||||
return $this->reason->code();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Egulias\EmailValidator\Result\Reason;
|
||||
|
||||
class CharNotAllowed implements Reason
|
||||
{
|
||||
public function code() : int
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
public function description() : string
|
||||
{
|
||||
return "Character not allowed";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Egulias\EmailValidator\Result\Reason;
|
||||
|
||||
interface Reason
|
||||
{
|
||||
public function code() : int;
|
||||
public function description() : string;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Egulias\EmailValidator\Result;
|
||||
|
||||
interface Result
|
||||
{
|
||||
public function isValid() : bool;
|
||||
public function description() : string;
|
||||
public function code() : int;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Egulias\EmailValidator\Result;
|
||||
|
||||
class ValidEmail implements Result
|
||||
{
|
||||
public function isValid(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function description(): string
|
||||
{
|
||||
return "Valid email";
|
||||
}
|
||||
|
||||
public function code(): int
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class LexerTokensTest extends TestCase
|
||||
{
|
||||
public function testToken()
|
||||
{
|
||||
$this->fail("implement");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Egulias\EmailValidator\Result\ValidEmail;
|
||||
use Egulias\EmailValidator\Result\InvalidEmail;
|
||||
use Egulias\EmailValidator\Result\Reason\CharNotAllowed;
|
||||
|
||||
class ResultTest extends TestCase
|
||||
{
|
||||
public function testResultIsValidEmail()
|
||||
{
|
||||
$result = new ValidEmail();
|
||||
$expectedCode = 0;
|
||||
$expectedDescription = "Valid email";
|
||||
|
||||
$this->assertTrue($result->isValid());
|
||||
$this->assertEquals($expectedCode, $result->code());
|
||||
$this->assertEquals($expectedDescription, $result->description());
|
||||
}
|
||||
|
||||
public function testResultIsInvalidEmail()
|
||||
{
|
||||
$reason = new CharNotAllowed();
|
||||
$token = "T";
|
||||
$result = new InvalidEmail($reason, $token);
|
||||
$expectedCode = $reason->code();
|
||||
$expectedDescription = $reason->description() . " in char " . $token;
|
||||
|
||||
$this->assertFalse($result->isValid());
|
||||
$this->assertEquals($expectedCode, $result->code());
|
||||
$this->assertEquals($expectedDescription, $result->description());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user