Skip to content

Commit

Permalink
NoRFCWarningsValidation now has an InvalidEmail instance if invalid e…
Browse files Browse the repository at this point in the history
…mail is passed (#126)

* NoRFCWarningsValidation now has an InvalidEmail instance if invalid email is passed

* fix mentioned point
  • Loading branch information
issei-m authored and egulias committed Aug 3, 2016
1 parent 4f0f902 commit 4d446bf
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 24 deletions.
11 changes: 11 additions & 0 deletions EmailValidator/Validation/Error/RFCWarnings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Egulias\EmailValidator\Validation\Error;

use Egulias\EmailValidator\Exception\InvalidEmail;

class RFCWarnings extends InvalidEmail
{
const CODE = 997;
const REASON = 'Warnings were found.';
}
30 changes: 29 additions & 1 deletion EmailValidator/Validation/NoRFCWarningsValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,39 @@
namespace Egulias\EmailValidator\Validation;

use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Exception\InvalidEmail;
use Egulias\EmailValidator\Validation\Error\RFCWarnings;

class NoRFCWarningsValidation extends RFCValidation
{
/**
* @var InvalidEmail
*/
private $error;

/**
* {@inheritdoc}
*/
public function isValid($email, EmailLexer $emailLexer)
{
return parent::isValid($email, $emailLexer) && empty($this->getWarnings());
if (!parent::isValid($email, $emailLexer)) {
return false;
}

if (empty($this->getWarnings())) {
return true;
}

$this->error = new RFCWarnings();

return false;
}

/**
* {@inheritdoc}
*/
public function getError()
{
return $this->error ?: parent::getError();
}
}
35 changes: 35 additions & 0 deletions Tests/EmailValidator/Validation/NoRFCWarningsValidationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Egulias\Tests\EmailValidator\Validation;

use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Exception\NoDomainPart;
use Egulias\EmailValidator\Validation\Error\RFCWarnings;
use Egulias\EmailValidator\Validation\NoRFCWarningsValidation;

class NoRFCWarningsValidationTest extends \PHPUnit_Framework_TestCase
{
public function testInvalidEmailIsInvalid()
{
$validation = new NoRFCWarningsValidation();

$this->assertFalse($validation->isValid('non-email-string', new EmailLexer()));
$this->assertInstanceOf(NoDomainPart::class, $validation->getError());
}

public function testEmailWithWarningsIsInvalid()
{
$validation = new NoRFCWarningsValidation();

$this->assertFalse($validation->isValid(str_repeat('x', 254).'@example.com', new EmailLexer())); // too long email
$this->assertInstanceOf(RFCWarnings::class, $validation->getError());
}

public function testEmailWithoutWarningsIsValid()
{
$validation = new NoRFCWarningsValidation();

$this->assertTrue($validation->isValid('[email protected]', new EmailLexer()));
$this->assertNull($validation->getError());
}
}
23 changes: 0 additions & 23 deletions Tests/EmailValidator/Validation/NoWarningsRFCValidationTest.php

This file was deleted.

0 comments on commit 4d446bf

Please sign in to comment.