Skip to content

Commit

Permalink
MultipleErrors->reason always returns [0] and should return first ite…
Browse files Browse the repository at this point in the history
…m 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 <[email protected]>
  • Loading branch information
samuelvi and Samuel Vicent authored Oct 11, 2021
1 parent c81f18a commit ee0db30
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/Result/MultipleErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -34,7 +34,9 @@ public function getReasons() : array

public function reason() : Reason
{
return $this->reasons[0];
return 0 !== count($this->reasons)
? current($this->reasons)
: new EmptyReason();
}

public function description() : string
Expand Down
16 changes: 16 additions & 0 deletions src/Result/Reason/EmptyReason.php
Original file line number Diff line number Diff line change
@@ -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';
}
}
30 changes: 29 additions & 1 deletion tests/EmailValidator/Reason/MultipleErrorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -35,4 +36,31 @@ public function testRegisterDifferentReasons()
$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());
}
}

0 comments on commit ee0db30

Please sign in to comment.