-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9a11c3b
commit 21b9401
Showing
5 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace PHPFluent\ArrayStorage\Filter; | ||
|
||
use InvalidArgumentException; | ||
|
||
class OneOf implements Filter | ||
{ | ||
protected $filters = array(); | ||
|
||
public function __construct(array $filters) | ||
{ | ||
foreach ($filters as $filter) { | ||
if (! $filter instanceof Filter) { | ||
throw new InvalidArgumentException('Filter is not valid'); | ||
} | ||
$this->filters[] = $filter; | ||
} | ||
} | ||
|
||
public function getFilters() | ||
{ | ||
return $this->filters; | ||
} | ||
|
||
public function isValid($input) | ||
{ | ||
foreach ($this->getFilters() as $filter) { | ||
if (! $filter->isValid($input)) { | ||
continue; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace PHPFluent\ArrayStorage\Filter; | ||
|
||
/** | ||
* @covers PHPFluent\ArrayStorage\Filter\OneOf | ||
*/ | ||
class OneOfTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
protected function filter($return = false) | ||
{ | ||
return $this->getMock('PHPFluent\ArrayStorage\Filter\Filter'); | ||
} | ||
|
||
public function testShouldAcceptFiltersOnConstructor() | ||
{ | ||
$filters = array( | ||
$this->filter(), | ||
$this->filter(), | ||
); | ||
$filter = new OneOf($filters); | ||
|
||
$this->assertSame($filters, $filter->getFilters()); | ||
} | ||
|
||
/** | ||
* @expectedException InvalidArgumentException | ||
* @expectedExceptionMessage Filter is not valid | ||
*/ | ||
public function testShouldThrowsExceptionWhenFilterIsNotValid() | ||
{ | ||
$filters = array( | ||
$this->filter(), | ||
'foo', | ||
); | ||
$filter = new OneOf($filters); | ||
} | ||
|
||
public function testShouldReturnFalseIfAllFiltersAreInvalid() | ||
{ | ||
$filter1 = $this->filter(); | ||
$filter1 | ||
->expects($this->once()) | ||
->method('isValid') | ||
->will($this->returnValue(false)); | ||
|
||
$filter2 = $this->filter(); | ||
$filter2 | ||
->expects($this->once()) | ||
->method('isValid') | ||
->will($this->returnValue(false)); | ||
|
||
$filters = array( | ||
$filter1, | ||
$filter2, | ||
); | ||
|
||
$filter = new OneOf($filters); | ||
|
||
$this->assertFalse($filter->isValid('Value')); | ||
} | ||
|
||
public function testShouldReturnTrueIfAtLeastOneFilterIsValid() | ||
{ | ||
$filter1 = $this->filter(); | ||
$filter1 | ||
->expects($this->once()) | ||
->method('isValid') | ||
->will($this->returnValue(true)); | ||
|
||
$filter2 = $this->filter(); | ||
$filter2 | ||
->expects($this->never()) | ||
->method('isValid'); | ||
|
||
$filters = array( | ||
$filter1, | ||
$filter2, | ||
); | ||
|
||
$filter = new OneOf($filters); | ||
|
||
$this->assertTrue($filter->isValid('Value')); | ||
} | ||
} |