Hoa central
Filter.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Iterator\Test\Unit;
38 
39 use Hoa\Iterator as LUT;
40 use Hoa\Test;
41 
50 class MyFilter extends LUT\Filter
51 {
52  public function accept()
53  {
54  return true;
55  }
56 }
57 
66 class Filter extends Test\Unit\Suite
67 {
68  public function case_classic()
69  {
70  $this
71  ->given(
72  $foobar = $this->getDummyIterator(),
73  $filter = new \Mock\Hoa\Iterator\Test\Unit\MyFilter($foobar),
74  $this->calling($filter)->accept = function () {
75  $value = $this->current();
76 
77  return false === in_array($value, ['a', 'e', 'i', 'o', 'u']);
78  }
79  )
80  ->when($result = iterator_to_array($filter))
81  ->then
82  ->array($result)
83  ->isEqualTo([
84  0 => 'f',
85  3 => 'b',
86  5 => 'r'
87  ]);
88  }
89 
90  public function case_remove_all()
91  {
92  $this
93  ->given(
94  $foobar = $this->getDummyIterator(),
95  $filter = new \Mock\Hoa\Iterator\Test\Unit\MyFilter($foobar),
96  $this->calling($filter)->accept = false
97  )
98  ->when($result = iterator_to_array($filter))
99  ->then
100  ->array($result)
101  ->isEmpty();
102  }
103 
104  public function case_remove_none()
105  {
106  $this
107  ->given(
108  $foobar = $this->getDummyIterator(),
109  $filter = new MyFilter($foobar)
110  )
111  ->when(
112  $foobarResult = iterator_to_array($foobar),
113  $filterResult = iterator_to_array($filter)
114  )
115  ->then
116  ->array($foobarResult)
117  ->isEqualTo($filterResult);
118  }
119 
120  protected function getDummyIterator()
121  {
122  return new LUT\Map(['f', 'o', 'o', 'b', 'a', 'r']);
123  }
124 }