1<?php
2
3/**
4 * Hoa
5 *
6 *
7 * @license
8 *
9 * New BSD License
10 *
11 * Copyright © 2007-2017, Hoa community. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are met:
15 *     * Redistributions of source code must retain the above copyright
16 *       notice, this list of conditions and the following disclaimer.
17 *     * Redistributions in binary form must reproduce the above copyright
18 *       notice, this list of conditions and the following disclaimer in the
19 *       documentation and/or other materials provided with the distribution.
20 *     * Neither the name of the Hoa nor the names of its contributors may be
21 *       used to endorse or promote products derived from this software without
22 *       specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37namespace Hoa\Iterator\Test\Unit;
38
39use Hoa\Iterator as LUT;
40use Hoa\Test;
41
42/**
43 * Class \Hoa\Iterator\Test\Unit\CallbackFilter.
44 *
45 * Test suite of the callback filter iterator.
46 *
47 * @copyright  Copyright © 2007-2017 Hoa community
48 * @license    New BSD License
49 */
50class CallbackFilter extends Test\Unit\Suite
51{
52    public function case_classic()
53    {
54        $this
55            ->given(
56                $foobar = $this->getDummyIterator(),
57                $filter = new LUT\CallbackFilter(
58                    $foobar,
59                    function ($value) {
60                        return false === in_array($value, ['a', 'e', 'i', 'o', 'u']);
61                    }
62                )
63            )
64            ->when($result = iterator_to_array($filter))
65            ->then
66                ->array($result)
67                    ->isEqualTo([
68                        0 => 'f',
69                        3 => 'b',
70                        5 => 'r'
71                    ]);
72    }
73
74    public function case_all_callback_parameters()
75    {
76        $self = $this;
77
78        $this
79            ->given(
80                $foobar = $this->getDummyIterator(),
81                $keys   = [],
82                $values = [],
83                $filter = new LUT\CallbackFilter(
84                    $foobar,
85                    function ($value, $key, $iterator) use (
86                        $self,
87                        $foobar,
88                        &$keys,
89                        &$values
90                    ) {
91                        $self
92                            ->object($iterator)
93                                ->isIdenticalTo($foobar);
94
95                        $keys[]   = $key;
96                        $values[] = $value;
97
98                        return false === in_array($value, ['a', 'e', 'i', 'o', 'u']);
99                    }
100                )
101            )
102            ->when($result = iterator_to_array($filter))
103            ->then
104                ->array($result)
105                    ->isEqualTo([
106                        0 => 'f',
107                        3 => 'b',
108                        5 => 'r'
109                    ])
110                ->array(array_combine($keys, $values))
111                    ->isEqualTo(iterator_to_array($foobar));
112    }
113
114    public function case_remove_all()
115    {
116        $this
117            ->given(
118                $foobar = $this->getDummyIterator(),
119                $filter = new LUT\CallbackFilter(
120                    $foobar,
121                    function () {
122                        return false;
123                    }
124                )
125            )
126            ->when($result = iterator_to_array($filter))
127            ->then
128                ->array($result)
129                    ->isEmpty();
130    }
131
132    public function case_remove_none()
133    {
134        $this
135            ->given(
136                $foobar = $this->getDummyIterator(),
137                $filter = new LUT\CallbackFilter(
138                    $foobar,
139                    function () {
140                        return true;
141                    }
142                )
143            )
144            ->when(
145                $foobarResult = iterator_to_array($foobar),
146                $filterResult = iterator_to_array($filter)
147            )
148            ->then
149                ->array($foobarResult)
150                    ->isEqualTo($filterResult);
151    }
152
153    public function case_recursive()
154    {
155        $this
156            ->given(
157                $foobar = $this->getDummyRecursiveIterator(),
158                $filter = new LUT\Recursive\CallbackFilter(
159                    $foobar,
160                    function ($value) {
161                        return false === in_array($value, ['a', 'e', 'i', 'o', 'u']);
162                    }
163                ),
164                $iterator = new LUT\Recursive\Iterator($filter)
165            )
166            ->when($result = iterator_to_array($iterator, false))
167            ->then
168                ->array($result)
169                    ->isEqualTo([
170                        0 => 'f',
171                        1 => 'b',
172                        2 => 'r'
173                    ]);
174    }
175
176    public function case_recursive_remove_all()
177    {
178        $this
179            ->given(
180                $foobar = $this->getDummyRecursiveIterator(),
181                $filter = new LUT\Recursive\CallbackFilter(
182                    $foobar,
183                    function () {
184                        return false;
185                    }
186                ),
187                $iterator = new LUT\Recursive\Iterator($filter)
188            )
189            ->when($result = iterator_to_array($iterator))
190            ->then
191                ->array($result)
192                    ->isEmpty();
193    }
194
195    public function case_recursive_remove_none()
196    {
197        $this
198            ->given(
199                $foobar = $this->getDummyRecursiveIterator(),
200                $filter = new LUT\Recursive\CallbackFilter(
201                    $foobar,
202                    function () {
203                        return true;
204                    }
205                ),
206                $foobarIterator = new LUT\Recursive\Iterator($foobar),
207                $filterIterator = new LUT\Recursive\Iterator($filter)
208            )
209            ->when(
210                $foobarResult = iterator_to_array($foobarIterator),
211                $filterResult = iterator_to_array($filterIterator)
212            )
213            ->then
214                ->array($foobarResult)
215                    ->isEqualTo($filterResult);
216    }
217
218    protected function getDummyIterator()
219    {
220        return new LUT\Map(['f', 'o', 'o', 'b', 'a', 'r']);
221    }
222
223    protected function getDummyRecursiveIterator()
224    {
225        return new LUT\Recursive\Map([['f', 'o', 'o'], ['b', 'a', 'r']]);
226    }
227}
228