1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Cubiche package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Cubiche |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace Cubiche\Core\Collections\Tests\Units\DataSource; |
11
|
|
|
|
12
|
|
|
use Cubiche\Core\Comparable\Comparator; |
13
|
|
|
use Cubiche\Core\Comparable\ComparatorInterface; |
14
|
|
|
use Cubiche\Core\Specification\Criteria; |
15
|
|
|
use Cubiche\Core\Specification\SpecificationInterface; |
16
|
|
|
use Cubiche\Core\Collections\DataSource\DataSourceInterface; |
17
|
|
|
use Cubiche\Core\Collections\Tests\Units\TestCase; |
18
|
|
|
use mageekguy\atoum\adapter as Adapter; |
19
|
|
|
use mageekguy\atoum\annotations\extractor as Extractor; |
20
|
|
|
use mageekguy\atoum\asserter\generator as Generator; |
21
|
|
|
use mageekguy\atoum\test\assertion\manager as Manager; |
22
|
|
|
use mageekguy\atoum\tools\variable\analyzer as Analyzer; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* DataSourceTestCase class. |
26
|
|
|
* |
27
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
abstract class DataSourceTestCase extends TestCase |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @param Adapter $adapter |
33
|
|
|
* @param Extractor $annotationExtractor |
34
|
|
|
* @param Generator $asserterGenerator |
35
|
|
|
* @param Manager $assertionManager |
36
|
|
|
* @param \Closure $reflectionClassFactory |
37
|
|
|
* @param \Closure $phpExtensionFactory |
38
|
|
|
* @param Analyzer $analyzer |
39
|
|
|
*/ |
40
|
|
|
public function __construct( |
41
|
|
|
Adapter $adapter = null, |
42
|
|
|
Extractor $annotationExtractor = null, |
43
|
|
|
Generator $asserterGenerator = null, |
44
|
|
|
Manager $assertionManager = null, |
45
|
|
|
\Closure $reflectionClassFactory = null, |
46
|
|
|
\Closure $phpExtensionFactory = null, |
47
|
|
|
Analyzer $analyzer = null |
48
|
|
|
) { |
49
|
|
|
parent::__construct( |
50
|
|
|
$adapter, |
51
|
|
|
$annotationExtractor, |
52
|
|
|
$asserterGenerator, |
53
|
|
|
$assertionManager, |
54
|
|
|
$reflectionClassFactory, |
55
|
|
|
$phpExtensionFactory, |
56
|
|
|
$analyzer |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
$this->getAssertionManager() |
60
|
|
|
->setHandler( |
61
|
|
|
'randomDataSource', |
62
|
|
|
function ( |
63
|
|
|
SpecificationInterface $searchCriteria = null, |
64
|
|
|
ComparatorInterface $sortCriteria = null, |
65
|
|
|
$offset = null, |
66
|
|
|
$length = null |
67
|
|
|
) { |
68
|
|
|
return $this->randomDataSource( |
69
|
|
|
$searchCriteria, |
70
|
|
|
$sortCriteria, |
71
|
|
|
$offset, |
72
|
|
|
$length |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
) |
76
|
|
|
->setHandler( |
77
|
|
|
'emptyDataSource', |
78
|
|
|
function () { |
79
|
|
|
return $this->emptyDataSource(); |
80
|
|
|
} |
81
|
|
|
) |
82
|
|
|
->setHandler( |
83
|
|
|
'uniqueValue', |
84
|
|
|
function () { |
85
|
|
|
return $this->uniqueValue(); |
86
|
|
|
} |
87
|
|
|
) |
88
|
|
|
; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param SpecificationInterface|null $searchCriteria |
93
|
|
|
* @param ComparatorInterface|null $sortCriteria |
94
|
|
|
* @param null $offset |
95
|
|
|
* @param null $length |
96
|
|
|
* |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
|
|
abstract protected function randomDataSource( |
100
|
|
|
SpecificationInterface $searchCriteria = null, |
101
|
|
|
ComparatorInterface $sortCriteria = null, |
102
|
|
|
$offset = null, |
103
|
|
|
$length = null |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return DataSourceInterface |
108
|
|
|
*/ |
109
|
|
|
abstract protected function emptyDataSource(); |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return mixed |
113
|
|
|
*/ |
114
|
|
|
abstract protected function uniqueValue(); |
115
|
|
|
|
116
|
|
|
/* |
117
|
|
|
* Test create. |
118
|
|
|
*/ |
119
|
|
|
public function testCreate() |
120
|
|
|
{ |
121
|
|
|
$this |
122
|
|
|
->given($datasource = $this->randomDataSource()) |
123
|
|
|
->then |
124
|
|
|
->datasource($datasource) |
125
|
|
|
->isInstanceOf(DataSourceInterface::class) |
126
|
|
|
; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/* |
130
|
|
|
* Test count. |
131
|
|
|
*/ |
132
|
|
|
public function testCount() |
133
|
|
|
{ |
134
|
|
|
$this |
135
|
|
|
->given($datasource = $this->randomDataSource()) |
136
|
|
|
->then |
137
|
|
|
->integer($datasource->count()) |
138
|
|
|
->isGreaterThan(0) |
139
|
|
|
; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/* |
143
|
|
|
* Test length. |
144
|
|
|
*/ |
145
|
|
View Code Duplication |
public function testLength() |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
$this |
148
|
|
|
->given($datasource = $this->randomDataSource()) |
149
|
|
|
->then |
150
|
|
|
->variable($datasource->length()) |
151
|
|
|
->isNull() |
152
|
|
|
->given($datasource = $this->randomDataSource(null, null, null, 10)) |
153
|
|
|
->then |
154
|
|
|
->variable($datasource->length()) |
155
|
|
|
->isEqualTo(10) |
156
|
|
|
; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/* |
160
|
|
|
* Test offset. |
161
|
|
|
*/ |
162
|
|
View Code Duplication |
public function testOffset() |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
$this |
165
|
|
|
->given($datasource = $this->randomDataSource()) |
166
|
|
|
->then |
167
|
|
|
->variable($datasource->offset()) |
168
|
|
|
->isNull() |
169
|
|
|
->given($datasource = $this->randomDataSource(null, null, 8)) |
170
|
|
|
->then |
171
|
|
|
->variable($datasource->offset()) |
172
|
|
|
->isEqualTo(8) |
173
|
|
|
; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/* |
177
|
|
|
* Test searchCriteria. |
178
|
|
|
*/ |
179
|
|
View Code Duplication |
public function testSearchCriteria() |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
$this |
182
|
|
|
->given($datasource = $this->randomDataSource()) |
183
|
|
|
->then |
184
|
|
|
->variable($datasource->searchCriteria()) |
185
|
|
|
->isNull() |
186
|
|
|
->given( |
187
|
|
|
$searchCriteria = Criteria::false(), |
188
|
|
|
$datasource = $this->randomDataSource($searchCriteria) |
189
|
|
|
) |
190
|
|
|
->then |
191
|
|
|
->variable($datasource->searchCriteria()) |
192
|
|
|
->isEqualTo($searchCriteria) |
193
|
|
|
; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/* |
197
|
|
|
* Test sortCriteria. |
198
|
|
|
*/ |
199
|
|
View Code Duplication |
public function testSortCriteria() |
|
|
|
|
200
|
|
|
{ |
201
|
|
|
$this |
202
|
|
|
->given($datasource = $this->randomDataSource()) |
203
|
|
|
->then |
204
|
|
|
->variable($datasource->sortCriteria()) |
205
|
|
|
->isNull() |
206
|
|
|
->given( |
207
|
|
|
$sortCriteria = new Comparator(), |
208
|
|
|
$datasource = $this->randomDataSource(null, $sortCriteria) |
209
|
|
|
) |
210
|
|
|
->then |
211
|
|
|
->variable($datasource->sortCriteria()) |
212
|
|
|
->isEqualTo($sortCriteria) |
213
|
|
|
; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/* |
217
|
|
|
* Test isSorted. |
218
|
|
|
*/ |
219
|
|
View Code Duplication |
public function testIsSorted() |
|
|
|
|
220
|
|
|
{ |
221
|
|
|
$this |
222
|
|
|
->given($datasource = $this->randomDataSource()) |
223
|
|
|
->then |
224
|
|
|
->boolean($datasource->isSorted()) |
225
|
|
|
->isFalse() |
226
|
|
|
->given( |
227
|
|
|
$sortCriteria = new Comparator(), |
228
|
|
|
$datasource = $this->randomDataSource(null, $sortCriteria) |
229
|
|
|
) |
230
|
|
|
->then |
231
|
|
|
->boolean($datasource->isSorted()) |
232
|
|
|
->isTrue() |
233
|
|
|
; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/* |
237
|
|
|
* Test isFiltered. |
238
|
|
|
*/ |
239
|
|
View Code Duplication |
public function testIsFiltered() |
|
|
|
|
240
|
|
|
{ |
241
|
|
|
$this |
242
|
|
|
->given($datasource = $this->randomDataSource()) |
243
|
|
|
->then |
244
|
|
|
->boolean($datasource->isFiltered()) |
245
|
|
|
->isFalse() |
246
|
|
|
->given( |
247
|
|
|
$searchCriteria = Criteria::false(), |
248
|
|
|
$datasource = $this->randomDataSource($searchCriteria) |
249
|
|
|
) |
250
|
|
|
->then |
251
|
|
|
->boolean($datasource->isFiltered()) |
252
|
|
|
->isTrue() |
253
|
|
|
; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/* |
257
|
|
|
* Test isSliced. |
258
|
|
|
*/ |
259
|
|
|
public function testIsSliced() |
260
|
|
|
{ |
261
|
|
|
$this |
262
|
|
|
->given($datasource = $this->randomDataSource()) |
263
|
|
|
->then |
264
|
|
|
->boolean($datasource->isSliced()) |
265
|
|
|
->isFalse() |
266
|
|
|
->given($datasource = $this->randomDataSource(null, null, 8)) |
267
|
|
|
->then |
268
|
|
|
->boolean($datasource->isSliced()) |
269
|
|
|
->isTrue() |
270
|
|
|
->given($datasource = $this->randomDataSource(null, null, null, 10)) |
271
|
|
|
->then |
272
|
|
|
->boolean($datasource->isSliced()) |
273
|
|
|
->isTrue() |
274
|
|
|
; |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.