mirror of
https://github.com/egulias/EmailValidator.git
synced 2026-08-17 18:41:27 +00:00
MultipleErrors->reason always returns [0] and should return first item from associative array (#305)
* Patch MultipleErrors->reason method by getting always the first item - Added Tests and a custom Exception * Return EmptyReason instead of throwing exception when MultipleErrors has no reasons (validations accomplished) Co-authored-by: Samuel Vicent <samuel.vicent@takeachef.com>
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Egulias\EmailValidator\Result;
|
||||
|
||||
use Egulias\EmailValidator\Result\InvalidEmail;
|
||||
use Egulias\EmailValidator\Result\Reason\EmptyReason;
|
||||
use Egulias\EmailValidator\Result\Reason\Reason;
|
||||
|
||||
/**
|
||||
@@ -34,7 +34,9 @@ class MultipleErrors extends InvalidEmail
|
||||
|
||||
public function reason() : Reason
|
||||
{
|
||||
return $this->reasons[0];
|
||||
return 0 !== count($this->reasons)
|
||||
? current($this->reasons)
|
||||
: new EmptyReason();
|
||||
}
|
||||
|
||||
public function description() : string
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Egulias\EmailValidator\Result\Reason;
|
||||
|
||||
class EmptyReason implements Reason
|
||||
{
|
||||
public function code() : int
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public function description() : string
|
||||
{
|
||||
return 'Empty reason';
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,11 @@
|
||||
|
||||
namespace Egulias\EmailValidator\Tests\EmailValidator\Reason;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Egulias\EmailValidator\Result\MultipleErrors;
|
||||
use Egulias\EmailValidator\Result\Reason\EmptyReason;
|
||||
use Egulias\EmailValidator\Tests\EmailValidator\Dummy\AnotherDummyReason;
|
||||
use Egulias\EmailValidator\Tests\EmailValidator\Dummy\DummyReason;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class MultipleErrorsTest extends TestCase
|
||||
{
|
||||
@@ -35,4 +36,31 @@ class MultipleErrorsTest extends TestCase
|
||||
$this->assertEquals($expectedReason, $multiError->description());
|
||||
$this->assertEquals($error1, $multiError->reason());
|
||||
}
|
||||
|
||||
public function testRetrieveFirstReasonWithReasonCodeEqualsZero(): void
|
||||
{
|
||||
$error1 = new DummyReason();
|
||||
|
||||
$multiError = new MultipleErrors();
|
||||
$multiError->addReason($error1);
|
||||
|
||||
$this->assertEquals($error1, $multiError->reason());
|
||||
}
|
||||
|
||||
public function testRetrieveFirstReasonWithReasonCodeDistinctToZero(): void
|
||||
{
|
||||
$error1 = new AnotherDummyReason();
|
||||
|
||||
$multiError = new MultipleErrors();
|
||||
$multiError->addReason($error1);
|
||||
|
||||
$this->assertEquals($error1, $multiError->reason());
|
||||
}
|
||||
|
||||
public function testRetrieveFirstReasonWithNoReasonAdded()
|
||||
{
|
||||
$emptyReason = new EmptyReason();
|
||||
$multiError = new MultipleErrors();
|
||||
$this->assertEquals($emptyReason, $multiError->reason());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user