IteratorDataSourceTests::testFindOne()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 29
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Cubiche\Core\Collections\Tests\Units\DataSource;
12
13
use Cubiche\Core\Collections\DataSource\IteratorDataSource;
14
use Cubiche\Core\Comparable\CallbackComparator;
15
use Cubiche\Core\Comparable\Comparator;
16
use Cubiche\Core\Comparable\ComparatorInterface;
17
use Cubiche\Core\Equatable\Tests\Fixtures\Value;
18
use Cubiche\Core\Specification\Criteria;
19
use Cubiche\Core\Specification\SpecificationInterface;
20
21
/**
22
 * Iterator Data Source Tests Class.
23
 *
24
 * @author Ivannis Suárez Jerez <[email protected]>
25
 * @author Karel Osorio Ramírez <[email protected]>
26
 */
27
class IteratorDataSourceTests extends DataSourceTestCase
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected function randomDataSource(
33
        SpecificationInterface $searchCriteria = null,
34
        ComparatorInterface $sortCriteria = null,
35
        $offset = null,
36
        $length = null
37
    ) {
38
        return new IteratorDataSource(
39
            $this->generator(rand(10, 20)),
40
            $searchCriteria,
41
            $sortCriteria,
42
            $offset,
43
            $length
44
        );
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    protected function emptyDataSource()
51
    {
52
        return new IteratorDataSource($this->generator(0));
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    protected function uniqueValue()
59
    {
60
        return new Value(1000);
61
    }
62
63
    /**
64
     * @param int $count
65
     *
66
     * @return Generator
67
     */
68
    protected function generator($count)
69
    {
70
        if ($count > 0) {
71
            foreach (range(0, $count) as $value) {
72
                yield new Value($value);
73
            }
74
        }
75
    }
76
77
    /*
78
     * Test create.
79
     */
80
    public function testCreate()
81
    {
82
        parent::testCreate();
83
84
        $this
85
            ->given($datasource = $this->randomDataSource())
86
            ->then
87
                ->datasource($datasource)
88
                    ->isInstanceOf(IteratorDataSource::class)
89
        ;
90
    }
91
92
    /**
93
     * Test findOne.
94
     */
95
    public function testFindOne()
96
    {
97
        $this
98
            ->given($emptyDataSource = $this->emptyDataSource())
99
            ->when($findResult = $emptyDataSource->findOne())
100
            ->then
101
                ->variable($findResult)
102
                    ->isNull()
103
        ;
104
105
        $this
106
            ->given($randomDataSource = $this->randomDataSource())
107
            ->when($findResult = $randomDataSource->findOne())
108
            ->then
109
                ->variable($findResult)
110
                    ->isNotNull()
111
        ;
112
113
        $this
114
            ->given($datasource = $this->randomDataSource(Criteria::method('value')->gt(2), null, 1, 3))
115
            ->when($findResult = $datasource->findOne())
116
            ->then
117
                ->integer($findResult->value())
118
                    ->isGreaterThan(2)
119
        ;
120
121
        $this
122
            ->given(
123
                $comparator = new Comparator(),
124
                $sortCriteria = new CallbackComparator(function ($a, $b) use ($comparator) {
125
                    return -1 * $comparator->compare($a, $b);
126
                }),
127
                $datasource = $this->randomDataSource(Criteria::method('value')->lt(10), $sortCriteria)
128
            )
129
            ->when($findResult = $datasource->findOne())
130
            ->then
131
                ->integer($findResult->value())
132
                    ->isEqualTo(9)
133
        ;
134
    }
135
136
    /*
137
     * Test filteredDataSource.
138
     */
139
    public function testFilteredDataSource()
140
    {
141
        $this
142
            ->given(
143
                $searchCriteria = Criteria::false(),
144
                $emptyDataSource = $this->emptyDataSource()
145
            )
146
            ->then
147
                ->boolean($emptyDataSource->isFiltered())
148
                    ->isFalse()
149
            ->and
150
            ->when($filteredDataSource = $emptyDataSource->filteredDataSource($searchCriteria))
151
            ->then
152
                ->boolean($filteredDataSource->isFiltered())
153
                    ->isTrue()
154
                ->variable($filteredDataSource->searchCriteria())
155
                    ->isEqualTo($searchCriteria)
156
        ;
157
158
        $this
159
            ->given(
160
                $searchCriteria = Criteria::true(),
161
                $filteredCriteria = Criteria::false(),
162
                $randomDataSource = $this->randomDataSource($searchCriteria)
163
            )
164
            ->then
165
                ->boolean($randomDataSource->isFiltered())
166
                    ->isTrue()
167
            ->and
168
            ->when($filteredDataSource = $randomDataSource->filteredDataSource($filteredCriteria))
169
            ->then
170
                ->boolean($filteredDataSource->isFiltered())
171
                    ->isTrue()
172
                ->variable($filteredDataSource->searchCriteria())
173
                    ->isEqualTo($searchCriteria->andX($filteredCriteria))
174
        ;
175
    }
176
177
    /*
178
     * Test slicedDataSource.
179
     */
180
    public function testSlicedDataSource()
181
    {
182
        $this
183
            ->given($emptyDataSource = $this->emptyDataSource())
184
            ->then
185
                ->boolean($emptyDataSource->isSliced())
186
                    ->isFalse()
187
            ->and
188
            ->when($slicedDataSource = $emptyDataSource->slicedDataSource(2))
189
            ->then
190
                ->boolean($slicedDataSource->isSliced())
191
                    ->isTrue()
192
                ->integer($slicedDataSource->offset())
193
                    ->isEqualTo(2)
194
                ->variable($slicedDataSource->length())
195
                    ->isNull()
196
        ;
197
198
        $this
199
            ->given($randomDataSource = $this->randomDataSource(null, null, 2, 10))
200
            ->then
201
                ->boolean($randomDataSource->isSliced())
202
                    ->isTrue()
203
                ->integer($randomDataSource->offset())
204
                    ->isEqualTo(2)
205
                ->integer($randomDataSource->length())
206
                    ->isEqualTo(10)
207
            ->and
208
            ->when($slicedDataSource = $randomDataSource->slicedDataSource(2, 4))
209
            ->then
210
                ->boolean($slicedDataSource->isSliced())
211
                    ->isTrue()
212
                ->integer($slicedDataSource->offset())
213
                    ->isEqualTo(4)
214
                ->integer($slicedDataSource->length())
215
                    ->isEqualTo(4)
216
        ;
217
218
        $this
219
            ->given($randomDataSource = $this->randomDataSource(null, null, 2, 10))
220
            ->then
221
                ->boolean($randomDataSource->isSliced())
222
                    ->isTrue()
223
                ->integer($randomDataSource->offset())
224
                    ->isEqualTo(2)
225
                ->integer($randomDataSource->length())
226
                    ->isEqualTo(10)
227
            ->and
228
            ->when($slicedDataSource = $randomDataSource->slicedDataSource(8, 4))
229
            ->then
230
                ->boolean($slicedDataSource->isSliced())
231
                    ->isTrue()
232
                ->integer($slicedDataSource->offset())
233
                    ->isEqualTo(10)
234
                ->integer($slicedDataSource->length())
235
                    ->isEqualTo(2)
236
        ;
237
238
        $this
239
            ->given($randomDataSource = $this->randomDataSource(null, null, 2, 4))
240
            ->then
241
                ->boolean($randomDataSource->isSliced())
242
                    ->isTrue()
243
                ->variable($randomDataSource->offset())
244
                    ->isEqualTo(2)
245
                ->variable($randomDataSource->length())
246
                    ->isEqualTo(4)
247
            ->and
248
            ->when($slicedDataSource = $randomDataSource->slicedDataSource(8, 4))
249
            ->then
250
                ->boolean($slicedDataSource->isSliced())
251
                    ->isTrue()
252
                ->variable($slicedDataSource->offset())
253
                    ->isEqualTo(10)
254
                ->variable($slicedDataSource->length())
255
                    ->isEqualTo(0)
256
        ;
257
258
        $this
259
            ->given($randomDataSource = $this->randomDataSource(null, null, 2, 4))
260
            ->let($iterator = $randomDataSource->getIterator())
261
            ->then()
262
                ->object($iterator)
263
                    ->isInstanceOf(\Traversable::class)
264
                ->integer(\iterator_count($iterator))
265
                    ->isEqualTo(4);
266
    }
267
268
    /*
269
     * Test sortedDataSource.
270
     */
271
    public function testSortedDataSource()
272
    {
273
        $this
274
            ->given(
275
                $sortCriteria = new Comparator(),
276
                $datasource = $this->randomDataSource()
277
            )
278
            ->then
279
                ->boolean($datasource->isSorted())
280
                    ->isFalse()
281
            ->and
282
            ->when($sortedDataSource = $datasource->sortedDataSource($sortCriteria))
283
            ->then
284
                ->boolean($sortedDataSource->isSorted())
285
                    ->isTrue()
286
                ->variable($sortedDataSource->sortCriteria())
287
                    ->isEqualTo($sortCriteria)
288
        ;
289
    }
290
}
291