AggregationUnitTest::testTwoAggregatesSelected()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1
Metric Value
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.4286
cc 1
eloc 8
nc 1
nop 0
crap 1
1
<?php
2
3
use SilverStripe\Elastica\ElasticSearcher;
4
use SilverStripe\Elastica\QueryGenerator;
5
6
/**
7
 * Test the functionality of the Searchable extension
8
 * @package elastica
9
 */
10
class AggregationUnitTest extends ElasticsearchBaseTest {
11
	public static $fixture_file = 'elastica/tests/lotsOfPhotos.yml';
12
13
14 1
	public function testAllFieldsQuery() {
15
		//Use a text search against all fields
16 1
		$resultList = $this->search('New Zealand', null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
17 1
		$this->assertEquals(10, $resultList->count());
18
19
		// check the query formation
20 1
		$query = $resultList->getQuery()->toArray();
21 1
		$this->assertEquals(0, $query['from']);
22 1
		$this->assertEquals(10, $query['size']);
23
24 1
		$sort = array('TakenAt' => 'desc');
25
26 1
		$this->assertFalse(isset($query['sort']), 'Sort should not be set as text is being used');
27
28 1
		$expected = array('query' => 'New Zealand', 'lenient' => 1);
29 1
		$this->assertEquals($expected, $query['query']['query_string']);
30
31
32
		// check the aggregate results
33 1
		$aggregations = $resultList->getAggregations();
34 1
		$this->aggregationByName($aggregations);
35
36 1
		error_log('AGGS');
37 1
		foreach ($aggregations as $agg) {
38 1
			error_log($agg->Name);
39 1
			foreach ($agg->Buckets as $bucket) {
40 1
				error_log("\t$bucket->Key -> $bucket->DocumentCount");
41 1
			}
42 1
		}
43
44
		//Asserting name of aggregate as ISO
45 1
		$agg = $aggregations['ISO'];
46 1
		$this->assertEquals("ISO", $agg->Name);
47 1
		$buckets = $agg->Buckets->toArray();
48
49
		//Asserting aggregate of ISO, 64 has count 5
50 1
		$this->assertEquals("64", $buckets[0]->Key);
51 1
		$this->assertEquals(5, $buckets[0]->DocumentCount);
52
53
		//Asserting aggregate of ISO, 100 has count 11
54 1
		$this->assertEquals("100", $buckets[1]->Key);
55 1
		$this->assertEquals(11, $buckets[1]->DocumentCount);
56
57
		//Asserting aggregate of ISO, 200 has count 12
58 1
		$this->assertEquals("200", $buckets[2]->Key);
59 1
		$this->assertEquals(12, $buckets[2]->DocumentCount);
60
61
		//Asserting aggregate of ISO, 400 has count 13
62 1
		$this->assertEquals("400", $buckets[3]->Key);
63 1
		$this->assertEquals(13, $buckets[3]->DocumentCount);
64
65
		//Asserting aggregate of ISO, 800 has count 15
66 1
		$this->assertEquals("800", $buckets[4]->Key);
67 1
		$this->assertEquals(15, $buckets[4]->DocumentCount);
68
69
		//Asserting aggregate of ISO, 1600 has count 13
70 1
		$this->assertEquals("1600", $buckets[5]->Key);
71 1
		$this->assertEquals(13, $buckets[5]->DocumentCount);
72
73
		//Asserting aggregate of ISO, 2000 has count 11
74 1
		$this->assertEquals("2000", $buckets[6]->Key);
75 1
		$this->assertEquals(11, $buckets[6]->DocumentCount);
76
77
		//Asserting aggregate of ISO, 3200 has count 19
78 1
		$this->assertEquals("3200", $buckets[7]->Key);
79 1
		$this->assertEquals(19, $buckets[7]->DocumentCount);
80
81
		//Asserting name of aggregate as Focal Length
82 1
		$agg = $aggregations['Focal Length'];
83 1
		$this->assertEquals("Focal Length", $agg->Name);
84 1
		$buckets = $agg->Buckets->toArray();
85
86
		//Asserting aggregate of Focal Length, 24 has count 12
87 1
		$this->assertEquals("24", $buckets[0]->Key);
88 1
		$this->assertEquals(12, $buckets[0]->DocumentCount);
89
90
		//Asserting aggregate of Focal Length, 50 has count 11
91 1
		$this->assertEquals("50", $buckets[1]->Key);
92 1
		$this->assertEquals(11, $buckets[1]->DocumentCount);
93
94
		//Asserting aggregate of Focal Length, 80 has count 11
95 1
		$this->assertEquals("80", $buckets[2]->Key);
96 1
		$this->assertEquals(11, $buckets[2]->DocumentCount);
97
98
		//Asserting aggregate of Focal Length, 90 has count 20
99 1
		$this->assertEquals("90", $buckets[3]->Key);
100 1
		$this->assertEquals(20, $buckets[3]->DocumentCount);
101
102
		//Asserting aggregate of Focal Length, 120 has count 11
103 1
		$this->assertEquals("120", $buckets[4]->Key);
104 1
		$this->assertEquals(11, $buckets[4]->DocumentCount);
105
106
		//Asserting aggregate of Focal Length, 150 has count 17
107 1
		$this->assertEquals("150", $buckets[5]->Key);
108 1
		$this->assertEquals(17, $buckets[5]->DocumentCount);
109
110
		//Asserting aggregate of Focal Length, 200 has count 17
111 1
		$this->assertEquals("200", $buckets[6]->Key);
112 1
		$this->assertEquals(17, $buckets[6]->DocumentCount);
113
114
		//Asserting name of aggregate as Shutter Speed
115 1
		$agg = $aggregations['Shutter Speed'];
116 1
		$this->assertEquals("Shutter Speed", $agg->Name);
117 1
		$buckets = $agg->Buckets->toArray();
118
119
		//Asserting aggregate of Shutter Speed, 2/250 has count 17
120 1
		$this->assertEquals("2/250", $buckets[0]->Key);
121 1
		$this->assertEquals(17, $buckets[0]->DocumentCount);
122
123
		//Asserting aggregate of Shutter Speed, 1/100 has count 15
124 1
		$this->assertEquals("1/100", $buckets[1]->Key);
125 1
		$this->assertEquals(15, $buckets[1]->DocumentCount);
126
127
		//Asserting aggregate of Shutter Speed, 1/30 has count 17
128 1
		$this->assertEquals("1/30", $buckets[2]->Key);
129 1
		$this->assertEquals(17, $buckets[2]->DocumentCount);
130
131
		//Asserting aggregate of Shutter Speed, 1/15 has count 9
132 1
		$this->assertEquals("1/15", $buckets[3]->Key);
133 1
		$this->assertEquals(9, $buckets[3]->DocumentCount);
134
135
		//Asserting aggregate of Shutter Speed, 1/2 has count 18
136 1
		$this->assertEquals("1/2", $buckets[4]->Key);
137 1
		$this->assertEquals(18, $buckets[4]->DocumentCount);
138
139
		//Asserting aggregate of Shutter Speed, 1 has count 1
140 1
		$this->assertEquals("1", $buckets[5]->Key);
141 1
		$this->assertEquals(1, $buckets[5]->DocumentCount);
142
143
		//Asserting aggregate of Shutter Speed, 2 has count 10
144 1
		$this->assertEquals("2", $buckets[6]->Key);
145 1
		$this->assertEquals(10, $buckets[6]->DocumentCount);
146
147
		//Asserting aggregate of Shutter Speed, 6 has count 12
148 1
		$this->assertEquals("6", $buckets[7]->Key);
149 1
		$this->assertEquals(12, $buckets[7]->DocumentCount);
150
151
		//Asserting name of aggregate as Aperture
152 1
		$agg = $aggregations['Aperture'];
153 1
		$this->assertEquals("Aperture", $agg->Name);
154 1
		$buckets = $agg->Buckets->toArray();
155
156
		//Asserting aggregate of Aperture, 2.8 has count 20
157 1
		$this->assertEquals("2.8", $buckets[0]->Key);
158 1
		$this->assertEquals(20, $buckets[0]->DocumentCount);
159
160
		//Asserting aggregate of Aperture, 5.6 has count 23
161 1
		$this->assertEquals("5.6", $buckets[1]->Key);
162 1
		$this->assertEquals(23, $buckets[1]->DocumentCount);
163
164
		//Asserting aggregate of Aperture, 11 has count 17
165 1
		$this->assertEquals("11", $buckets[2]->Key);
166 1
		$this->assertEquals(17, $buckets[2]->DocumentCount);
167
168
		//Asserting aggregate of Aperture, 16 has count 16
169 1
		$this->assertEquals("16", $buckets[3]->Key);
170 1
		$this->assertEquals(16, $buckets[3]->DocumentCount);
171
172
		//Asserting aggregate of Aperture, 22 has count 23
173 1
		$this->assertEquals("22", $buckets[4]->Key);
174 1
		$this->assertEquals(23, $buckets[4]->DocumentCount);
175
176
		//Asserting name of aggregate as Aspect
177 1
		$agg = $aggregations['Aspect'];
178 1
		$this->assertEquals("Aspect", $agg->Name);
179 1
		$buckets = $agg->Buckets->toArray();
180
181
		//Asserting aggregate of Aspect, Panoramic has count 9
182 1
		$this->assertEquals("Panoramic", $buckets[0]->Key);
183 1
		$this->assertEquals(9, $buckets[0]->DocumentCount);
184
185
		//Asserting aggregate of Aspect, Horizontal has count 31
186 1
		$this->assertEquals("Horizontal", $buckets[1]->Key);
187 1
		$this->assertEquals(31, $buckets[1]->DocumentCount);
188
189
		//Asserting aggregate of Aspect, Square has count 16
190 1
		$this->assertEquals("Square", $buckets[2]->Key);
191 1
		$this->assertEquals(16, $buckets[2]->DocumentCount);
192
193
		//Asserting aggregate of Aspect, Vertical has count 38
194 1
		$this->assertEquals("Vertical", $buckets[3]->Key);
195 1
		$this->assertEquals(38, $buckets[3]->DocumentCount);
196
197
		//Asserting aggregate of Aspect, Tallest has count 5
198 1
		$this->assertEquals("Tallest", $buckets[4]->Key);
199 1
		$this->assertEquals(5, $buckets[4]->DocumentCount);
200
201 1
	}
202
203
204 9
	public function testAllFieldsEmptyQuery() {
205
		//Use a text search against all fields
206 1
		$resultList = $this->search('', null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
207 1
		$this->assertEquals(10, $resultList->count());
208
209
		//check query
210 1
		$query = $resultList->getQuery()->toArray();
211 1
		$this->assertEquals(0, $query['from']);
212 1
		$this->assertEquals(10, $query['size']);
213
214 1
		$sort = array('TakenAt' => 'desc');
215 1
		$this->assertEquals($sort, $query['sort']);
216 1
		$this->assertFalse(isset($query['query']));
217
218 1
		$aggs = array();
219 1
		$aggs['Aperture'] = array();
220 1
		$aggs['ShutterSpeed'] = array();
221 1
		$aggs['FocalLength35mm'] = array();
222 1
		$aggs['ISO'] = array();
223 1
		$aggs['Aspect'] = array();
224
225
		//check aggregations
226 1
		$aggregations = $resultList->getAggregations();
227 1
		$this->aggregationByName($aggregations);
228
229
		//Asserting name of aggregate as ISO
230 1
		$agg = $aggregations['ISO'];
231 1
		$this->assertEquals("ISO", $agg->Name);
232 1
		$buckets = $agg->Buckets->toArray();
233 1
		$bucketSum = 0;
234
235
		//Asserting aggregate of ISO, 64 has count 5
236 1
		$this->assertEquals("64", $buckets[0]->Key);
237 1
		$this->assertEquals(5, $buckets[0]->DocumentCount);
238 1
		$bucketSum += $buckets[0]->DocumentCount;
239
240
		//Asserting aggregate of ISO, 100 has count 11
241 1
		$this->assertEquals("100", $buckets[1]->Key);
242 1
		$this->assertEquals(11, $buckets[1]->DocumentCount);
243 1
		$bucketSum += $buckets[1]->DocumentCount;
244
245
		//Asserting aggregate of ISO, 200 has count 12
246 1
		$this->assertEquals("200", $buckets[2]->Key);
247 1
		$this->assertEquals(12, $buckets[2]->DocumentCount);
248 1
		$bucketSum += $buckets[2]->DocumentCount;
249
250
		//Asserting aggregate of ISO, 400 has count 13
251 1
		$this->assertEquals("400", $buckets[3]->Key);
252 1
		$this->assertEquals(13, $buckets[3]->DocumentCount);
253 1
		$bucketSum += $buckets[3]->DocumentCount;
254
255
		//Asserting aggregate of ISO, 800 has count 16
256 1
		$this->assertEquals("800", $buckets[4]->Key);
257 1
		$this->assertEquals(16, $buckets[4]->DocumentCount);
258 1
		$bucketSum += $buckets[4]->DocumentCount;
259
260
		//Asserting aggregate of ISO, 1600 has count 13
261 1
		$this->assertEquals("1600", $buckets[5]->Key);
262 1
		$this->assertEquals(13, $buckets[5]->DocumentCount);
263 1
		$bucketSum += $buckets[5]->DocumentCount;
264
265
		//Asserting aggregate of ISO, 2000 has count 11
266 1
		$this->assertEquals("2000", $buckets[6]->Key);
267 1
		$this->assertEquals(11, $buckets[6]->DocumentCount);
268 1
		$bucketSum += $buckets[6]->DocumentCount;
269
270
		//Asserting aggregate of ISO, 3200 has count 19
271 1
		$this->assertEquals("3200", $buckets[7]->Key);
272 1
		$this->assertEquals(19, $buckets[7]->DocumentCount);
273 1
		$bucketSum += $buckets[7]->DocumentCount;
274 1
		$this->assertEquals(100, $bucketSum);
275
276
		//Asserting name of aggregate as Focal Length
277 1
		$agg = $aggregations['Focal Length'];
278 1
		$this->assertEquals("Focal Length", $agg->Name);
279 1
		$buckets = $agg->Buckets->toArray();
280 1
		$bucketSum = 0;
281
282
		//Asserting aggregate of Focal Length, 24 has count 12
283 1
		$this->assertEquals("24", $buckets[0]->Key);
284 1
		$this->assertEquals(12, $buckets[0]->DocumentCount);
285 1
		$bucketSum += $buckets[0]->DocumentCount;
286
287
		//Asserting aggregate of Focal Length, 50 has count 11
288 1
		$this->assertEquals("50", $buckets[1]->Key);
289 1
		$this->assertEquals(11, $buckets[1]->DocumentCount);
290 1
		$bucketSum += $buckets[1]->DocumentCount;
291
292
		//Asserting aggregate of Focal Length, 80 has count 11
293 1
		$this->assertEquals("80", $buckets[2]->Key);
294 1
		$this->assertEquals(11, $buckets[2]->DocumentCount);
295 9
		$bucketSum += $buckets[2]->DocumentCount;
296
297
		//Asserting aggregate of Focal Length, 90 has count 20
298 2
		$this->assertEquals("90", $buckets[3]->Key);
299 1
		$this->assertEquals(20, $buckets[3]->DocumentCount);
300 1
		$bucketSum += $buckets[3]->DocumentCount;
301
302
		//Asserting aggregate of Focal Length, 120 has count 12
303 1
		$this->assertEquals("120", $buckets[4]->Key);
304 1
		$this->assertEquals(12, $buckets[4]->DocumentCount);
305 1
		$bucketSum += $buckets[4]->DocumentCount;
306
307
		//Asserting aggregate of Focal Length, 150 has count 17
308 9
		$this->assertEquals("150", $buckets[5]->Key);
309 1
		$this->assertEquals(17, $buckets[5]->DocumentCount);
310 1
		$bucketSum += $buckets[5]->DocumentCount;
311
312
		//Asserting aggregate of Focal Length, 200 has count 17
313 1
		$this->assertEquals("200", $buckets[6]->Key);
314 1
		$this->assertEquals(17, $buckets[6]->DocumentCount);
315 1
		$bucketSum += $buckets[6]->DocumentCount;
316 1
		$this->assertEquals(100, $bucketSum);
317
318
		//Asserting name of aggregate as Shutter Speed
319 1
		$agg = $aggregations['Shutter Speed'];
320 1
		$this->assertEquals("Shutter Speed", $agg->Name);
321 1
		$buckets = $agg->Buckets->toArray();
322 1
		$bucketSum = 0;
323
324
		//Asserting aggregate of Shutter Speed, 2/250 has count 17
325 1
		$this->assertEquals("2/250", $buckets[0]->Key);
326 1
		$this->assertEquals(17, $buckets[0]->DocumentCount);
327 1
		$bucketSum += $buckets[0]->DocumentCount;
328
329
		//Asserting aggregate of Shutter Speed, 1/100 has count 15
330 1
		$this->assertEquals("1/100", $buckets[1]->Key);
331 1
		$this->assertEquals(15, $buckets[1]->DocumentCount);
332 1
		$bucketSum += $buckets[1]->DocumentCount;
333
334
		//Asserting aggregate of Shutter Speed, 1/30 has count 17
335 1
		$this->assertEquals("1/30", $buckets[2]->Key);
336 1
		$this->assertEquals(17, $buckets[2]->DocumentCount);
337 1
		$bucketSum += $buckets[2]->DocumentCount;
338
339
		//Asserting aggregate of Shutter Speed, 1/15 has count 10
340 1
		$this->assertEquals("1/15", $buckets[3]->Key);
341 1
		$this->assertEquals(10, $buckets[3]->DocumentCount);
342 1
		$bucketSum += $buckets[3]->DocumentCount;
343
344
		//Asserting aggregate of Shutter Speed, 1/2 has count 18
345 1
		$this->assertEquals("1/2", $buckets[4]->Key);
346 1
		$this->assertEquals(18, $buckets[4]->DocumentCount);
347 1
		$bucketSum += $buckets[4]->DocumentCount;
348
349
		//Asserting aggregate of Shutter Speed, 1 has count 1
350 1
		$this->assertEquals("1", $buckets[5]->Key);
351 1
		$this->assertEquals(1, $buckets[5]->DocumentCount);
352
353
		//Asserting aggregate of Shutter Speed, 2 has count 10
354 1
		$this->assertEquals("2", $buckets[6]->Key);
355 1
		$this->assertEquals(10, $buckets[6]->DocumentCount);
356
357
		//Asserting aggregate of Shutter Speed, 6 has count 12
358 1
		$this->assertEquals("6", $buckets[7]->Key);
359 1
		$this->assertEquals(12, $buckets[7]->DocumentCount);
360
361
		//Asserting name of aggregate as Aperture
362 1
		$agg = $aggregations['Aperture'];
363 1
		$this->assertEquals("Aperture", $agg->Name);
364 1
		$buckets = $agg->Buckets->toArray();
365 1
		$bucketSum = 0;
366
367
		//Asserting aggregate of Aperture, 2.8 has count 21
368 1
		$this->assertEquals("2.8", $buckets[0]->Key);
369 1
		$this->assertEquals(21, $buckets[0]->DocumentCount);
370 1
		$bucketSum += $buckets[0]->DocumentCount;
371
372
		//Asserting aggregate of Aperture, 5.6 has count 23
373 1
		$this->assertEquals("5.6", $buckets[1]->Key);
374 1
		$this->assertEquals(23, $buckets[1]->DocumentCount);
375 1
		$bucketSum += $buckets[1]->DocumentCount;
376
377
		//Asserting aggregate of Aperture, 11 has count 17
378 1
		$this->assertEquals("11", $buckets[2]->Key);
379 1
		$this->assertEquals(17, $buckets[2]->DocumentCount);
380 1
		$bucketSum += $buckets[2]->DocumentCount;
381
382
		//Asserting aggregate of Aperture, 16 has count 16
383 1
		$this->assertEquals("16", $buckets[3]->Key);
384 1
		$this->assertEquals(16, $buckets[3]->DocumentCount);
385 1
		$bucketSum += $buckets[3]->DocumentCount;
386
387
		//Asserting aggregate of Aperture, 22 has count 23
388 1
		$this->assertEquals("22", $buckets[4]->Key);
389 1
		$this->assertEquals(23, $buckets[4]->DocumentCount);
390 1
		$bucketSum += $buckets[4]->DocumentCount;
391 1
		$this->assertEquals(100, $bucketSum);
392
393
		//Asserting name of aggregate as Aspect
394 1
		$agg = $aggregations['Aspect'];
395 1
		$this->assertEquals("Aspect", $agg->Name);
396 1
		$buckets = $agg->Buckets->toArray();
397 1
		$bucketSum = 0;
398
399
		//Asserting aggregate of Aspect, Panoramic has count 9
400 1
		$this->assertEquals("Panoramic", $buckets[0]->Key);
401 1
		$this->assertEquals(9, $buckets[0]->DocumentCount);
402 1
		$bucketSum += $buckets[0]->DocumentCount;
403
404
		//Asserting aggregate of Aspect, Horizontal has count 31
405 1
		$this->assertEquals("Horizontal", $buckets[1]->Key);
406 1
		$this->assertEquals(31, $buckets[1]->DocumentCount);
407 1
		$bucketSum += $buckets[1]->DocumentCount;
408
409
		//Asserting aggregate of Aspect, Square has count 16
410 1
		$this->assertEquals("Square", $buckets[2]->Key);
411 1
		$this->assertEquals(16, $buckets[2]->DocumentCount);
412 1
		$bucketSum += $buckets[2]->DocumentCount;
413
414
		//Asserting aggregate of Aspect, Vertical has count 39
415 1
		$this->assertEquals("Vertical", $buckets[3]->Key);
416 1
		$this->assertEquals(39, $buckets[3]->DocumentCount);
417 1
		$bucketSum += $buckets[3]->DocumentCount;
418
419
		//Asserting aggregate of Aspect, Tallest has count 5
420 1
		$this->assertEquals("Tallest", $buckets[4]->Key);
421 1
		$this->assertEquals(5, $buckets[4]->DocumentCount);
422 1
		$bucketSum += $buckets[4]->DocumentCount;
423 1
		$this->assertEquals(100, $bucketSum);
424
425 1
	}
426
427
428
	/*
429
	Search for an empty query against the Title and Description fields
430
	 */
431 2
	public function testAggregationWithEmptyQuery() {
432 1
		$resultList = $this->search('');
433
434
		//assert there are actually some results
435 1
		$this->assertGreaterThan(0,$resultList->getTotalItems());
436 1
		$aggregations = $resultList->getAggregations()->toArray();
437 1
		$this->aggregationByName($aggregations);
438
439
		/*
440
		For all of the aggregates, all results are returned due to empty query string, so the number
441
		of aggregates should always add up to 100.  Check some values at the database level for
442
		further confirmation also
443
		FIXME - finish DB checks
444
		 */
445
446
		//Asserting name of aggregate as ISO
447 1
		$agg = $aggregations['ISO'];
448 1
		$this->assertEquals("ISO", $agg->Name);
449 1
		$buckets = $agg->Buckets->toArray();
450 1
		$bucketSum = 0;
451
452
		//Asserting aggregate of ISO, 64 has count 5
453 1
		$this->assertEquals("64", $buckets[0]->Key);
454 1
		$this->assertEquals(5, $buckets[0]->DocumentCount);
455 1
		$this->assertEquals(FlickrPhotoTO::get()->filter('ISO',64)->count(),5);
456 1
		$bucketSum += $buckets[0]->DocumentCount;
457
458
		//Asserting aggregate of ISO, 100 has count 11
459 1
		$this->assertEquals("100", $buckets[1]->Key);
460 1
		$this->assertEquals(11, $buckets[1]->DocumentCount);
461 1
		$this->assertEquals(FlickrPhotoTO::get()->filter('ISO',100)->count(),11);
462 1
		$bucketSum += $buckets[1]->DocumentCount;
463
464
		//Asserting aggregate of ISO, 200 has count 12
465 1
		$this->assertEquals("200", $buckets[2]->Key);
466 1
		$this->assertEquals(12, $buckets[2]->DocumentCount);
467 1
		$this->assertEquals(FlickrPhotoTO::get()->filter('ISO',200)->count(),12);
468 1
		$bucketSum += $buckets[2]->DocumentCount;
469
470
		//Asserting aggregate of ISO, 400 has count 13
471 2
		$this->assertEquals("400", $buckets[3]->Key);
472 1
		$this->assertEquals(13, $buckets[3]->DocumentCount);
473 1
		$this->assertEquals(FlickrPhotoTO::get()->filter('ISO',400)->count(),13);
474 1
		$bucketSum += $buckets[3]->DocumentCount;
475
476
		//Asserting aggregate of ISO, 800 has count 16
477 1
		$this->assertEquals("800", $buckets[4]->Key);
478 1
		$this->assertEquals(16, $buckets[4]->DocumentCount);
479 1
		$this->assertEquals(FlickrPhotoTO::get()->filter('ISO',800)->count(),16);
480 1
		$bucketSum += $buckets[4]->DocumentCount;
481
482
		//Asserting aggregate of ISO, 1600 has count 13
483 1
		$this->assertEquals("1600", $buckets[5]->Key);
484 1
		$this->assertEquals(13, $buckets[5]->DocumentCount);
485 1
		$this->assertEquals(FlickrPhotoTO::get()->filter('ISO',1600)->count(),13);
486 1
		$bucketSum += $buckets[5]->DocumentCount;
487
488
		//Asserting aggregate of ISO, 2000 has count 11
489 1
		$this->assertEquals("2000", $buckets[6]->Key);
490 1
		$this->assertEquals(11, $buckets[6]->DocumentCount);
491 1
		$this->assertEquals(FlickrPhotoTO::get()->filter('ISO',2000)->count(),11);
492 1
		$bucketSum += $buckets[6]->DocumentCount;
493
494
		//Asserting aggregate of ISO, 3200 has count 19
495 1
		$this->assertEquals("3200", $buckets[7]->Key);
496 1
		$this->assertEquals(19, $buckets[7]->DocumentCount);
497 1
		$this->assertEquals(FlickrPhotoTO::get()->filter('ISO',3200)->count(),19);
498 1
		$bucketSum += $buckets[7]->DocumentCount;
499 1
		$this->assertEquals(100, $bucketSum);
500
501
		//Asserting name of aggregate as Focal Length
502 1
		$agg = $aggregations['Focal Length'];
503 1
		$this->assertEquals("Focal Length", $agg->Name);
504 1
		$buckets = $agg->Buckets->toArray();
505 1
		$bucketSum = 0;
506
507
		//Asserting aggregate of Focal Length, 24 has count 12
508 1
		$this->assertEquals("24", $buckets[0]->Key);
509 1
		$this->assertEquals(12, $buckets[0]->DocumentCount);
510 1
		$this->assertEquals(FlickrPhotoTO::get()->filter('FocalLength35mm',24)->count(),12);
511 1
		$bucketSum += $buckets[0]->DocumentCount;
512
513
		//Asserting aggregate of Focal Length, 50 has count 11
514 1
		$this->assertEquals("50", $buckets[1]->Key);
515 1
		$this->assertEquals(11, $buckets[1]->DocumentCount);
516 1
		$this->assertEquals(FlickrPhotoTO::get()->filter('FocalLength35mm',50)->count(),11);
517 1
		$bucketSum += $buckets[1]->DocumentCount;
518
519
		//Asserting aggregate of Focal Length, 80 has count 11
520 1
		$this->assertEquals("80", $buckets[2]->Key);
521 1
		$this->assertEquals(11, $buckets[2]->DocumentCount);
522 1
		$this->assertEquals(FlickrPhotoTO::get()->filter('FocalLength35mm',80)->count(),11);
523 1
		$bucketSum += $buckets[2]->DocumentCount;
524
525
		//Asserting aggregate of Focal Length, 90 has count 20
526 1
		$this->assertEquals("90", $buckets[3]->Key);
527 1
		$this->assertEquals(20, $buckets[3]->DocumentCount);
528 1
		$this->assertEquals(FlickrPhotoTO::get()->filter('FocalLength35mm',90)->count(),20);
529 1
		$bucketSum += $buckets[3]->DocumentCount;
530
531
		//Asserting aggregate of Focal Length, 120 has count 12
532 1
		$this->assertEquals("120", $buckets[4]->Key);
533 1
		$this->assertEquals(12, $buckets[4]->DocumentCount);
534 1
		$this->assertEquals(FlickrPhotoTO::get()->filter('FocalLength35mm',120)->count(),12);
535 1
		$bucketSum += $buckets[4]->DocumentCount;
536
537
		//Asserting aggregate of Focal Length, 150 has count 17
538 1
		$this->assertEquals("150", $buckets[5]->Key);
539 1
		$this->assertEquals(17, $buckets[5]->DocumentCount);
540 1
		$this->assertEquals(FlickrPhotoTO::get()->filter('FocalLength35mm',150)->count(),17);
541 1
		$bucketSum += $buckets[5]->DocumentCount;
542
543
		//Asserting aggregate of Focal Length, 200 has count 17
544 1
		$this->assertEquals("200", $buckets[6]->Key);
545 1
		$this->assertEquals(17, $buckets[6]->DocumentCount);
546 1
		$this->assertEquals(FlickrPhotoTO::get()->filter('FocalLength35mm',200)->count(),17);
547 1
		$bucketSum += $buckets[6]->DocumentCount;
548 1
		$this->assertEquals(100, $bucketSum);
549
550
		//Asserting name of aggregate as Shutter Speed
551 1
		$agg = $aggregations['Shutter Speed'];
552 1
		$this->assertEquals("Shutter Speed", $agg->Name);
553 1
		$buckets = $agg->Buckets->toArray();
554 1
		$bucketSum = 0;
555
556
		//Asserting aggregate of Shutter Speed, 2/250 has count 17
557 1
		$this->assertEquals("2/250", $buckets[0]->Key);
558 1
		$this->assertEquals(17, $buckets[0]->DocumentCount);
559 1
		$bucketSum += $buckets[0]->DocumentCount;
560
561
		//Asserting aggregate of Shutter Speed, 1/100 has count 15
562 1
		$this->assertEquals("1/100", $buckets[1]->Key);
563 1
		$this->assertEquals(15, $buckets[1]->DocumentCount);
564 1
		$bucketSum += $buckets[1]->DocumentCount;
565
566
		//Asserting aggregate of Shutter Speed, 1/30 has count 17
567 1
		$this->assertEquals("1/30", $buckets[2]->Key);
568 1
		$this->assertEquals(17, $buckets[2]->DocumentCount);
569 1
		$bucketSum += $buckets[2]->DocumentCount;
570
571
		//Asserting aggregate of Shutter Speed, 1/15 has count 10
572 1
		$this->assertEquals("1/15", $buckets[3]->Key);
573 1
		$this->assertEquals(10, $buckets[3]->DocumentCount);
574 1
		$bucketSum += $buckets[3]->DocumentCount;
575
576
		//Asserting aggregate of Shutter Speed, 1/2 has count 18
577 1
		$this->assertEquals("1/2", $buckets[4]->Key);
578 1
		$this->assertEquals(18, $buckets[4]->DocumentCount);
579 1
		$bucketSum += $buckets[4]->DocumentCount;
580
581
		//Asserting aggregate of Shutter Speed, 1 has count 1
582 1
		$this->assertEquals("1", $buckets[5]->Key);
583 1
		$this->assertEquals(1, $buckets[5]->DocumentCount);
584
585
		//Asserting aggregate of Shutter Speed, 2 has count 10
586 1
		$this->assertEquals("2", $buckets[6]->Key);
587 1
		$this->assertEquals(10, $buckets[6]->DocumentCount);
588
589
		//Asserting aggregate of Shutter Speed, 6 has count 12
590 1
		$this->assertEquals("6", $buckets[7]->Key);
591 1
		$this->assertEquals(12, $buckets[7]->DocumentCount);
592
593
		//Asserting name of aggregate as Aperture
594 1
		$agg = $aggregations['Aperture'];
595 1
		$this->assertEquals("Aperture", $agg->Name);
596 1
		$buckets = $agg->Buckets->toArray();
597 1
		$bucketSum = 0;
598
599
		//Asserting aggregate of Aperture, 2.8 has count 21
600 1
		$this->assertEquals("2.8", $buckets[0]->Key);
601 1
		$this->assertEquals(21, $buckets[0]->DocumentCount);
602 1
		$bucketSum += $buckets[0]->DocumentCount;
603
604
		//Asserting aggregate of Aperture, 5.6 has count 23
605 1
		$this->assertEquals("5.6", $buckets[1]->Key);
606 1
		$this->assertEquals(23, $buckets[1]->DocumentCount);
607 1
		$bucketSum += $buckets[1]->DocumentCount;
608
609
		//Asserting aggregate of Aperture, 11 has count 17
610 1
		$this->assertEquals("11", $buckets[2]->Key);
611 1
		$this->assertEquals(17, $buckets[2]->DocumentCount);
612 1
		$bucketSum += $buckets[2]->DocumentCount;
613
614
		//Asserting aggregate of Aperture, 16 has count 16
615 1
		$this->assertEquals("16", $buckets[3]->Key);
616 1
		$this->assertEquals(16, $buckets[3]->DocumentCount);
617 1
		$bucketSum += $buckets[3]->DocumentCount;
618
619
		//Asserting aggregate of Aperture, 22 has count 23
620 1
		$this->assertEquals("22", $buckets[4]->Key);
621 1
		$this->assertEquals(23, $buckets[4]->DocumentCount);
622 1
		$bucketSum += $buckets[4]->DocumentCount;
623 1
		$this->assertEquals(100, $bucketSum);
624
625
		//Asserting name of aggregate as Aspect
626 1
		$agg = $aggregations['Aspect'];
627 1
		$this->assertEquals("Aspect", $agg->Name);
628 1
		$buckets = $agg->Buckets->toArray();
629 1
		$bucketSum = 0;
630
631
		//Asserting aggregate of Aspect, Panoramic has count 9
632 1
		$this->assertEquals("Panoramic", $buckets[0]->Key);
633 1
		$this->assertEquals(9, $buckets[0]->DocumentCount);
634 1
		$bucketSum += $buckets[0]->DocumentCount;
635
636
		//Asserting aggregate of Aspect, Horizontal has count 31
637 1
		$this->assertEquals("Horizontal", $buckets[1]->Key);
638 1
		$this->assertEquals(31, $buckets[1]->DocumentCount);
639 1
		$bucketSum += $buckets[1]->DocumentCount;
640
641
		//Asserting aggregate of Aspect, Square has count 16
642 1
		$this->assertEquals("Square", $buckets[2]->Key);
643 1
		$this->assertEquals(16, $buckets[2]->DocumentCount);
644 1
		$bucketSum += $buckets[2]->DocumentCount;
645
646
		//Asserting aggregate of Aspect, Vertical has count 39
647 1
		$this->assertEquals("Vertical", $buckets[3]->Key);
648 1
		$this->assertEquals(39, $buckets[3]->DocumentCount);
649 1
		$bucketSum += $buckets[3]->DocumentCount;
650
651
		//Asserting aggregate of Aspect, Tallest has count 5
652 1
		$this->assertEquals("Tallest", $buckets[4]->Key);
653 1
		$this->assertEquals(5, $buckets[4]->DocumentCount);
654 1
		$bucketSum += $buckets[4]->DocumentCount;
655 1
		$this->assertEquals(100, $bucketSum);
656 1
	}
657
658
659
	/*
660
	Search for the query 'New Zealand' against the Title and Description fields
661
	 */
662 1
	public function testAggregationWithQuery() {
663 1
		$resultList = $this->search('New Zealand');
664 1
		$aggregations = $resultList->getAggregations()->toArray();
665 1
		$this->aggregationByName($aggregations);
666
667
		//Asserting name of aggregate as ISO
668 1
		$agg = $aggregations['ISO'];
669
670 1
		$this->assertEquals("ISO", $agg->Name);
671 1
		$buckets = $agg->Buckets->toArray();
672
673
		//Asserting aggregate of ISO, 64 has count 5
674 1
		$this->assertEquals("64", $buckets[0]->Key);
675 1
		$this->assertEquals(5, $buckets[0]->DocumentCount);
676
677
		//Asserting aggregate of ISO, 100 has count 11
678 1
		$this->assertEquals("100", $buckets[1]->Key);
679 1
		$this->assertEquals(11, $buckets[1]->DocumentCount);
680
681
		//Asserting aggregate of ISO, 200 has count 12
682 1
		$this->assertEquals("200", $buckets[2]->Key);
683 1
		$this->assertEquals(12, $buckets[2]->DocumentCount);
684
685
		//Asserting aggregate of ISO, 400 has count 13
686 1
		$this->assertEquals("400", $buckets[3]->Key);
687 1
		$this->assertEquals(13, $buckets[3]->DocumentCount);
688
689
		//Asserting aggregate of ISO, 800 has count 16
690 1
		$this->assertEquals("800", $buckets[4]->Key);
691 1
		$this->assertEquals(16, $buckets[4]->DocumentCount);
692
693
		//Asserting aggregate of ISO, 1600 has count 13
694 1
		$this->assertEquals("1600", $buckets[5]->Key);
695 1
		$this->assertEquals(13, $buckets[5]->DocumentCount);
696
697
		//Asserting aggregate of ISO, 2000 has count 11
698 1
		$this->assertEquals("2000", $buckets[6]->Key);
699 1
		$this->assertEquals(11, $buckets[6]->DocumentCount);
700
701
		//Asserting aggregate of ISO, 3200 has count 19
702 1
		$this->assertEquals("3200", $buckets[7]->Key);
703 1
		$this->assertEquals(19, $buckets[7]->DocumentCount);
704
705
		//Asserting name of aggregate as Focal Length
706 1
		$agg = $aggregations['Focal Length'];
707 1
		$this->assertEquals("Focal Length", $agg->Name);
708 1
		$buckets = $agg->Buckets->toArray();
709
710
		//Asserting aggregate of Focal Length, 24 has count 12
711 1
		$this->assertEquals("24", $buckets[0]->Key);
712 1
		$this->assertEquals(12, $buckets[0]->DocumentCount);
713
714
		//Asserting aggregate of Focal Length, 50 has count 11
715 1
		$this->assertEquals("50", $buckets[1]->Key);
716 1
		$this->assertEquals(11, $buckets[1]->DocumentCount);
717
718
		//Asserting aggregate of Focal Length, 80 has count 11
719 1
		$this->assertEquals("80", $buckets[2]->Key);
720 1
		$this->assertEquals(11, $buckets[2]->DocumentCount);
721
722
		//Asserting aggregate of Focal Length, 90 has count 20
723 1
		$this->assertEquals("90", $buckets[3]->Key);
724 1
		$this->assertEquals(20, $buckets[3]->DocumentCount);
725
726
		//Asserting aggregate of Focal Length, 120 has count 12
727 1
		$this->assertEquals("120", $buckets[4]->Key);
728 1
		$this->assertEquals(12, $buckets[4]->DocumentCount);
729
730
		//Asserting aggregate of Focal Length, 150 has count 17
731 1
		$this->assertEquals("150", $buckets[5]->Key);
732 1
		$this->assertEquals(17, $buckets[5]->DocumentCount);
733
734
		//Asserting aggregate of Focal Length, 200 has count 17
735 1
		$this->assertEquals("200", $buckets[6]->Key);
736 1
		$this->assertEquals(17, $buckets[6]->DocumentCount);
737
738
		//Asserting name of aggregate as Shutter Speed
739 1
		$agg = $aggregations['Shutter Speed'];
740 1
		$this->assertEquals("Shutter Speed", $agg->Name);
741 1
		$buckets = $agg->Buckets->toArray();
742
743
		//Asserting aggregate of Shutter Speed, 2/250 has count 17
744 1
		$this->assertEquals("2/250", $buckets[0]->Key);
745 1
		$this->assertEquals(17, $buckets[0]->DocumentCount);
746
747
		//Asserting aggregate of Shutter Speed, 1/100 has count 15
748 1
		$this->assertEquals("1/100", $buckets[1]->Key);
749 1
		$this->assertEquals(15, $buckets[1]->DocumentCount);
750
751
		//Asserting aggregate of Shutter Speed, 1/30 has count 17
752 1
		$this->assertEquals("1/30", $buckets[2]->Key);
753 1
		$this->assertEquals(17, $buckets[2]->DocumentCount);
754
755
		//Asserting aggregate of Shutter Speed, 1/15 has count 10
756 1
		$this->assertEquals("1/15", $buckets[3]->Key);
757 1
		$this->assertEquals(10, $buckets[3]->DocumentCount);
758
759
		//Asserting aggregate of Shutter Speed, 1/2 has count 18
760 1
		$this->assertEquals("1/2", $buckets[4]->Key);
761 1
		$this->assertEquals(18, $buckets[4]->DocumentCount);
762
763
		//Asserting aggregate of Shutter Speed, 1 has count 1
764 1
		$this->assertEquals("1", $buckets[5]->Key);
765 1
		$this->assertEquals(1, $buckets[5]->DocumentCount);
766
767
		//Asserting aggregate of Shutter Speed, 2 has count 10
768 1
		$this->assertEquals("2", $buckets[6]->Key);
769 1
		$this->assertEquals(10, $buckets[6]->DocumentCount);
770
771
		//Asserting aggregate of Shutter Speed, 6 has count 12
772 1
		$this->assertEquals("6", $buckets[7]->Key);
773 1
		$this->assertEquals(12, $buckets[7]->DocumentCount);
774
775
		//Asserting name of aggregate as Aperture
776 1
		$agg = $aggregations['Aperture'];
777 1
		$this->assertEquals("Aperture", $agg->Name);
778 1
		$buckets = $agg->Buckets->toArray();
779
780
		//Asserting aggregate of Aperture, 2.8 has count 21
781 1
		$this->assertEquals("2.8", $buckets[0]->Key);
782 1
		$this->assertEquals(21, $buckets[0]->DocumentCount);
783
784
		//Asserting aggregate of Aperture, 5.6 has count 23
785 1
		$this->assertEquals("5.6", $buckets[1]->Key);
786 1
		$this->assertEquals(23, $buckets[1]->DocumentCount);
787
788
		//Asserting aggregate of Aperture, 11 has count 17
789 1
		$this->assertEquals("11", $buckets[2]->Key);
790 1
		$this->assertEquals(17, $buckets[2]->DocumentCount);
791
792
		//Asserting aggregate of Aperture, 16 has count 16
793 1
		$this->assertEquals("16", $buckets[3]->Key);
794 1
		$this->assertEquals(16, $buckets[3]->DocumentCount);
795
796
		//Asserting aggregate of Aperture, 22 has count 23
797 1
		$this->assertEquals("22", $buckets[4]->Key);
798 1
		$this->assertEquals(23, $buckets[4]->DocumentCount);
799
800
		//Asserting name of aggregate as Aspect
801 1
		$agg = $aggregations['Aspect'];
802 1
		$this->assertEquals("Aspect", $agg->Name);
803 1
		$buckets = $agg->Buckets->toArray();
804
805
		//Asserting aggregate of Aspect, Panoramic has count 9
806 1
		$this->assertEquals("Panoramic", $buckets[0]->Key);
807 1
		$this->assertEquals(9, $buckets[0]->DocumentCount);
808
809
		//Asserting aggregate of Aspect, Horizontal has count 31
810 1
		$this->assertEquals("Horizontal", $buckets[1]->Key);
811 1
		$this->assertEquals(31, $buckets[1]->DocumentCount);
812
813
		//Asserting aggregate of Aspect, Square has count 16
814 1
		$this->assertEquals("Square", $buckets[2]->Key);
815 1
		$this->assertEquals(16, $buckets[2]->DocumentCount);
816
817
		//Asserting aggregate of Aspect, Vertical has count 39
818 1
		$this->assertEquals("Vertical", $buckets[3]->Key);
819 1
		$this->assertEquals(39, $buckets[3]->DocumentCount);
820
821
		//Asserting aggregate of Aspect, Tallest has count 5
822 1
		$this->assertEquals("Tallest", $buckets[4]->Key);
823 1
		$this->assertEquals(5, $buckets[4]->DocumentCount);
824 1
	}
825
826
827 1
	public function testOneAggregateSelected() {
828 1
		$fields = array('Title' => 1, 'Description' => 1);
829 1
		$resultList = $this->search('New Zealand', $fields);
830 1
		$originalAggregations = $resultList->getAggregations()->toArray();
831
832 1
		$filters = array('ISO' => 3200);
833 1
		$resultListFiltered = $this->search('New Zealand', $fields,$filters);
834 1
		$filteredAggregations = $resultListFiltered->getAggregations()->toArray();
835
836 1
		$this->checkDrillingDownHasHappened($filteredAggregations, $originalAggregations);
837 1
	}
838
839
840 1
	public function testTwoAggregatesSelected() {
841 1
		$fields = array('Title' => 1, 'Description' => 1);
842 1
		$resultList = $this->search('New Zealand', $fields, array('ISO' => 400));
843 1
		$originalAggregations = $resultList->getAggregations()->toArray();
844
845 1
		$filters = array('ISO' => 400, 'Aspect' => 'Vertical' );
846 1
		$resultListFiltered = $this->search('New Zealand', $fields,$filters);
847 1
		$filteredAggregations = $resultListFiltered->getAggregations()->toArray();
848
849 1
		$this->checkDrillingDownHasHappened($filteredAggregations, $originalAggregations);
850 1
	}
851
852
853 1
	public function testThreeAggregatesSelected() {
854 1
		$ct = FlickrPhotoTO::get()->count();
0 ignored issues
show
Unused Code introduced by
$ct is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
855
856 1
		$fields = array('Title' => 1, 'Description' => 1);
857 1
		$resultList = $this->search('New Zealand', $fields, array('ISO' => 400,
858 1
										'Aspect' => 'Vertical'));
859 1
		$originalAggregations = $resultList->getAggregations()->toArray();
860
861 1
		$filters = array('ISO' => 400, 'Aspect' => 'Vertical', 'Aperture' => 5 );
862 1
		$resultListFiltered = $this->search('New Zealand', $fields,$filters);
863 1
		$filteredAggregations = $resultListFiltered->getAggregations()->toArray();
864
865 1
		$this->checkDrillingDownHasHappened($filteredAggregations, $originalAggregations);
866 1
	}
867
868
869
	/*
870
	Shutter speed is a special case as values are manipulated in order to maintain correct sort order
871
	 */
872 1
	public function testAggregateShutterSpeed() {
873 1
		$fields = array('Title' => 1, 'Description' => 1);
874 1
		$resultList = $this->search('', $fields, array('ShutterSpeed' => '1/100'));
875 1
		$aggregations = $resultList->getAggregations()->toArray();
0 ignored issues
show
Unused Code introduced by
$aggregations is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
876
877
		// 15 instances in the YML file
878 1
		$this->assertEquals(15, $resultList->getTotalItems());
879
880
		// Shutter speed of 1 second is a special case, only 1 case
881 1
		$resultList = $this->search('Image taken from page 471', $fields, array('ShutterSpeed' => '1'));
882 1
		$this->assertEquals(1, $resultList->getTotalItems());
883
884
		// test outlying vals and ensure they do not crash the code
885 1
		$resultList = $this->search('Image taken from page 471', $fields, array('ShutterSpeed' => ''));
886 1
		$this->assertEquals(0, $resultList->getTotalItems());
887
888 1
		$resultList = $this->search('Image taken from page 471', $fields, array('ShutterSpeed' => '/'));
889 1
		$this->assertEquals(0, $resultList->getTotalItems());
890
891 1
		$resultList = $this->search('Image taken from page 471', $fields, array('ShutterSpeed' => '2/'));
892 1
		$this->assertEquals(0, $resultList->getTotalItems());
893
894 1
		$resultList = $this->search('Image taken from page 471', $fields, array('ShutterSpeed' => '/2'));
895 1
		$this->assertEquals(0, $resultList->getTotalItems());
896 1
	}
897
898
899 3
	private function checkDrillingDownHasHappened($filteredAggregations, $originalAggregations) {
900 3
		$aggCtr = 0;
901
902 3
		foreach ($filteredAggregations as $filteredAgg) {
903 3
			$origAgg = $originalAggregations[$aggCtr];
904 3
			$bucketCtr = 0;
0 ignored issues
show
Unused Code introduced by
$bucketCtr is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
905 3
			$origBuckets = $origAgg->Buckets->toArray();
906 3
			$filteredBuckets = $filteredAgg->Buckets->toArray();
907
908 3
			$origCounts = array();
909 3
			$filteredCounts = array();
910
911 3
			foreach ($origBuckets as $bucket) {
912 3
				$origCounts[$bucket->Key] = $bucket->DocumentCount;
913 3
			}
914
915 3
			foreach ($filteredBuckets as $bucket) {
916 3
				$filteredCounts[$bucket->Key] = $bucket->DocumentCount;
917 3
			}
918
919 3
			$akf = array_keys($filteredCounts);
920
921 3
			foreach ($akf as $aggregateKey) {
922 3
				$this->assertGreaterThanOrEqual(
923 3
					$filteredCounts[$aggregateKey], $origCounts[$aggregateKey]
924 3
				);
925 3
			}
926
927 3
			$aggCtr++;
928 3
		}
929 3
	}
930
931
932
	/*
933
	ResultList and ElasticSearcher both have accessors to the aggregates.  Check they are the same
934
	 */
935 1 View Code Duplication
	public function testGetAggregations() {
936 1
		$es = new ElasticSearcher();
937 1
		$es->setStart(0);
938 1
		$es->setPageLength(10);
939
		//$es->addFilter('IsInSiteTree', false);
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
940 1
		$es->setClasses('FlickrPhotoTO');
941 1
		$es->setQueryResultManipulator('FlickrPhotoTOElasticaSearchHelper');
0 ignored issues
show
Documentation introduced by
'FlickrPhotoTOElasticaSearchHelper' is of type string, but the function expects a object<SilverStripe\Elas...a\ElasticaSearchHelper>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
942 1
		$resultList = $es->search('New Zealand');
943 1
		$this->assertEquals($resultList->getAggregations(), $es->getAggregations());
944 1
	}
945
946
947 1
	public function testAggregationNonExistentField() {
948 1
		$filters = array();
949 1
		$es = new ElasticSearcher();
950 1
		$es->setStart(0);
951 1
		$es->setPageLength(10);
952
		//$es->addFilter('IsInSiteTree', false);
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
953 1
		$es->setClasses('FlickrPhotoTO');
954 1
		$es->setQueryResultManipulator('FlickrPhotoTOElasticaSearchHelper');
0 ignored issues
show
Documentation introduced by
'FlickrPhotoTOElasticaSearchHelper' is of type string, but the function expects a object<SilverStripe\Elas...a\ElasticaSearchHelper>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
955
956
		//Add filters
957 1
		foreach ($filters as $key => $value) {
958
			$es->addFilter($key,$value);
959 1
		}
960
961 1
		$es->showResultsForEmptySearch();
962
963
		try {
964 1
			$resultList = $es->search('whatever', array('Wibble' => 2));
0 ignored issues
show
Unused Code introduced by
$resultList is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
965
			$this->fail('The field Wibble should cause this to error out with an exception');
966 1
		} catch (Exception $e) {
967 1
			$this->assertEquals('Field Wibble does not exist', $e->getMessage());
968
		}
969
970 1
	}
971
972
973
	/**
974
	 * Test searching
975
	 * http://stackoverflow.com/questions/28305250/elasticsearch-customize-score-for-synonyms-stemming
976
	 */
977 8
	private function search($queryText,$fields = array('Title' => 1, 'Description' => 1),
978
		$filters = array()) {
979 8
		$es = new ElasticSearcher();
980 8
		$es->setStart(0);
981 8
		$es->setPageLength(10);
982 8
		$es->setClasses('FlickrPhotoTO');
983 8
		$es->setQueryResultManipulator('FlickrPhotoTOElasticaSearchHelper');
0 ignored issues
show
Documentation introduced by
'FlickrPhotoTOElasticaSearchHelper' is of type string, but the function expects a object<SilverStripe\Elas...a\ElasticaSearchHelper>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
984
985
		//Add filters
986 8
		foreach ($filters as $key => $value) {
987 4
			error_log("ADDING FILTER RESULT $key, $value");
988 4
			$es->addFilter($key,$value);
989 8
		}
990
991 8
		$es->showResultsForEmptySearch();
992
993 8
		$resultList = $es->search($queryText, $fields);
994
995 8
		return $resultList;
996
	}
997
998
999
	/**
1000
	 * Change array keyed by number to that of aggregation name.
1001
	 * Order of aggs returns differs on Travis for reasons unknown
1002
	 * @param  array &$aggregations Referenced aggregations to tweak
1003
	 */
1004 4
	private function aggregationByName(&$aggregations) {
1005 4
		$ctr = 0;
1006 4
		foreach ($aggregations as $agg) {
1007 4
			$aggregations[$agg->Name] = $agg;
1008 4
			unset($aggregations[$ctr]);
1009 4
			$ctr++;
1010 4
		}
1011 4
	}
1012
1013
}
1014