CollectionTestCase::randomCollection()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
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;
12
13
use Cubiche\Core\Collections\CollectionInterface;
14
use Cubiche\Core\Comparable\Comparator;
15
use Cubiche\Core\Specification\Criteria;
16
17
/**
18
 * Collection Test Case class.
19
 *
20
 * @author Ivannis Suárez Jerez <[email protected]>
21
 * @author Karel Osorio Ramírez <[email protected]>
22
 */
23
abstract class CollectionTestCase extends TestCase
24
{
25
    /**
26
     * @param int $size
27
     *
28
     * @return CollectionInterface
29
     */
30
    abstract protected function randomCollection($size = null);
31
32
    /**
33
     * @param int $size
34
     *
35
     * @return mixed[]
36
     */
37
    protected function randomValues($size = null)
38
    {
39
        $items = array();
40
        if ($size === null) {
41
            $size = \rand(10, 20);
42
        }
43
        foreach (\range(0, $size - 1) as $value) {
44
            $items[$value] = $this->randomValue();
45
        }
46
47
        return $items;
48
    }
49
50
    /**
51
     * @return CollectionInterface
52
     */
53
    abstract protected function emptyCollection();
54
55
    /**
56
     * @return mixed
57
     */
58
    abstract protected function randomValue();
59
60
    /**
61
     * @return mixed
62
     */
63
    abstract protected function uniqueValue();
64
65
    /**
66
     * @return \Cubiche\Core\Comparable\ComparatorInterface
67
     */
68
    protected function comparator()
69
    {
70
        return new Comparator();
71
    }
72
73
    /**
74
     * Test class.
75
     */
76
    public function testClass()
77
    {
78
        $this
79
            ->testedClass
80
                ->implements(CollectionInterface::class)
81
        ;
82
    }
83
84
    /**
85
     * Test clear.
86
     */
87
    public function testClear()
88
    {
89
        $this
90
            ->given($randomCollection = $this->randomCollection())
91
            ->then()
92
                ->collection($randomCollection)
93
                    ->isNotEmpty()
94
            ->and()
95
            ->when($randomCollection->clear())
96
            ->then()
97
                ->collection($randomCollection)
98
                    ->isEmpty()
99
        ;
100
    }
101
102
    /**
103
     * Test count.
104
     */
105
    public function testCount()
106
    {
107
        $this
108
            ->given($collection = $this->randomCollection(5))
109
            ->then()
110
                ->integer($collection->count())
111
                    ->isEqualTo(5)
112
        ;
113
    }
114
115
    /**
116
     * Test getIterator.
117
     */
118
    public function testGetIterator()
119
    {
120
        $this
121
            ->given($collection = $this->randomCollection())
122
            ->then
123
                ->object($collection->getIterator())
124
                    ->isInstanceOf(\Traversable::class)
125
        ;
126
    }
127
128
    /**
129
     * Test toArray.
130
     */
131
    public function testToArray()
132
    {
133
        $this
134
            ->given($collection = $this->randomCollection())
135
            ->when($array = $collection->toArray())
136
                ->array($array)
137
                    ->isEqualTo(\iterator_to_array($collection->getIterator()));
138
    }
139
140
    /**
141
     * Test slice.
142
     */
143
    public function testSlice()
144
    {
145
        $this
146
            ->given($collection = $this->randomCollection())
147
            ->and($count = $collection->count())
148
            ->and($offset = rand(0, $count / 2))
149
            ->and($length = rand($count / 2, $count))
150
            ->and($maxCount = max([$count - $offset, 0]))
151
            ->when($slice = $collection->slice($offset, $length))
152
            ->then()
153
                ->collection($slice)
154
                    ->size()
155
                        ->isEqualTo(min($maxCount, $length))
156
        ;
157
    }
158
159
    /**
160
     * Test find.
161
     */
162
    public function testFind()
163
    {
164
        $this
165
            ->given(
166
                $unique = $this->uniqueValue(),
167
                $criteria = Criteria::same($unique),
168
                $emptyCollection = $this->emptyCollection()
169
            )
170
            ->when($findResult = $emptyCollection->find($criteria))
171
            ->then()
172
                ->collection($findResult)
173
                    ->isEmpty()
174
        ;
175
176
        $this
177
            ->given(
178
                $unique = $this->uniqueValue(),
179
                $criteria = Criteria::same($unique),
180
                $randomCollection = $this->randomCollection()
181
            )
182
            ->when($findResult = $randomCollection->find($criteria))
183
            ->then()
184
                ->collection($findResult)
185
                    ->isEmpty()
186
        ;
187
    }
188
189
    /**
190
     * Test sorted.
191
     */
192
    public function testSorted()
193
    {
194
        $this
195
            ->given(
196
                $comparator = $this->comparator(),
197
                $reverseComparator = $comparator->reverse(),
198
                $collection = $this->randomCollection()
199
            )
200
            ->when($sortedCollection = $collection->sorted($comparator))
201
            ->then()
202
                ->collection($sortedCollection)
203
                    ->isSortedUsing($comparator)
204
                ->collection($sortedCollection)
205
                    ->isNotSortedUsing($reverseComparator)
206
        ;
207
    }
208
}
209