|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PHPFluent\ArrayStorage\Filter; |
| 4 | + |
| 5 | +/** |
| 6 | + * @covers PHPFluent\ArrayStorage\Filter\OneOf |
| 7 | + */ |
| 8 | +class OneOfTest extends \PHPUnit_Framework_TestCase |
| 9 | +{ |
| 10 | + protected function filter($return = false) |
| 11 | + { |
| 12 | + return $this->getMock('PHPFluent\ArrayStorage\Filter\Filter'); |
| 13 | + } |
| 14 | + |
| 15 | + public function testShouldAcceptFiltersOnConstructor() |
| 16 | + { |
| 17 | + $filters = array( |
| 18 | + $this->filter(), |
| 19 | + $this->filter(), |
| 20 | + ); |
| 21 | + $filter = new OneOf($filters); |
| 22 | + |
| 23 | + $this->assertSame($filters, $filter->getFilters()); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * @expectedException InvalidArgumentException |
| 28 | + * @expectedExceptionMessage Filter is not valid |
| 29 | + */ |
| 30 | + public function testShouldThrowsExceptionWhenFilterIsNotValid() |
| 31 | + { |
| 32 | + $filters = array( |
| 33 | + $this->filter(), |
| 34 | + 'foo', |
| 35 | + ); |
| 36 | + $filter = new OneOf($filters); |
| 37 | + } |
| 38 | + |
| 39 | + public function testShouldReturnFalseIfAllFiltersAreInvalid() |
| 40 | + { |
| 41 | + $filter1 = $this->filter(); |
| 42 | + $filter1 |
| 43 | + ->expects($this->once()) |
| 44 | + ->method('isValid') |
| 45 | + ->will($this->returnValue(false)); |
| 46 | + |
| 47 | + $filter2 = $this->filter(); |
| 48 | + $filter2 |
| 49 | + ->expects($this->once()) |
| 50 | + ->method('isValid') |
| 51 | + ->will($this->returnValue(false)); |
| 52 | + |
| 53 | + $filters = array( |
| 54 | + $filter1, |
| 55 | + $filter2, |
| 56 | + ); |
| 57 | + |
| 58 | + $filter = new OneOf($filters); |
| 59 | + |
| 60 | + $this->assertFalse($filter->isValid('Value')); |
| 61 | + } |
| 62 | + |
| 63 | + public function testShouldReturnTrueIfAtLeastOneFilterIsValid() |
| 64 | + { |
| 65 | + $filter1 = $this->filter(); |
| 66 | + $filter1 |
| 67 | + ->expects($this->once()) |
| 68 | + ->method('isValid') |
| 69 | + ->will($this->returnValue(true)); |
| 70 | + |
| 71 | + $filter2 = $this->filter(); |
| 72 | + $filter2 |
| 73 | + ->expects($this->never()) |
| 74 | + ->method('isValid'); |
| 75 | + |
| 76 | + $filters = array( |
| 77 | + $filter1, |
| 78 | + $filter2, |
| 79 | + ); |
| 80 | + |
| 81 | + $filter = new OneOf($filters); |
| 82 | + |
| 83 | + $this->assertTrue($filter->isValid('Value')); |
| 84 | + } |
| 85 | +} |
0 commit comments