Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoRFCWarningsValidation now has an InvalidEmail instance if invalid email is passed #126

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great :) (just deleted a comment about this ;) )

}
}
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.