Completed
Push — dev2 ( f1c508...aa5ccf )
by Gordon
03:08
created

AggregationUnitTest::testGetResults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 0
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
	public function testAllFieldsQuery() {
15
		//Use a text search against all fields
16
		$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
		$this->assertEquals(10, $resultList->count());
18
19
		// check the query formation
20
		$query = $resultList->getQuery()->toArray();
21
		$this->assertEquals(0, $query['from']);
22
		$this->assertEquals(10, $query['size']);
23
24
		$sort = array('TakenAt' => 'desc');
25
26
		$this->assertFalse(isset($query['sort']), 'Sort should not be set as text is being used');
27
28
		$expected = array('query' => 'New Zealand', 'lenient' => 1);
29
		$this->assertEquals($expected, $query['query']['query_string']);
30
31
32
		// check the aggregate results
33
		$aggregations = $resultList->getAggregations();
34
35
		//Asserting name of aggregate as ISO
36
		$agg = $aggregations[0];
37
		$this->assertEquals("ISO", $agg->Name);
38
		$buckets = $agg->Buckets->toArray();
39
40
		//Asserting aggregate of ISO, 64 has count 5
41
		$this->assertEquals("64", $buckets[0]->Key);
42
		$this->assertEquals(5, $buckets[0]->DocumentCount);
43
44
		//Asserting aggregate of ISO, 100 has count 11
45
		$this->assertEquals("100", $buckets[1]->Key);
46
		$this->assertEquals(11, $buckets[1]->DocumentCount);
47
48
		//Asserting aggregate of ISO, 200 has count 12
49
		$this->assertEquals("200", $buckets[2]->Key);
50
		$this->assertEquals(12, $buckets[2]->DocumentCount);
51
52
		//Asserting aggregate of ISO, 400 has count 13
53
		$this->assertEquals("400", $buckets[3]->Key);
54
		$this->assertEquals(13, $buckets[3]->DocumentCount);
55
56
		//Asserting aggregate of ISO, 800 has count 15
57
		$this->assertEquals("800", $buckets[4]->Key);
58
		$this->assertEquals(15, $buckets[4]->DocumentCount);
59
60
		//Asserting aggregate of ISO, 1600 has count 13
61
		$this->assertEquals("1600", $buckets[5]->Key);
62
		$this->assertEquals(13, $buckets[5]->DocumentCount);
63
64
		//Asserting aggregate of ISO, 2000 has count 11
65
		$this->assertEquals("2000", $buckets[6]->Key);
66
		$this->assertEquals(11, $buckets[6]->DocumentCount);
67
68
		//Asserting aggregate of ISO, 3200 has count 19
69
		$this->assertEquals("3200", $buckets[7]->Key);
70
		$this->assertEquals(19, $buckets[7]->DocumentCount);
71
72
		//Asserting name of aggregate as Focal Length
73
		$agg = $aggregations[1];
74
		$this->assertEquals("Focal Length", $agg->Name);
75
		$buckets = $agg->Buckets->toArray();
76
77
		//Asserting aggregate of Focal Length, 24 has count 12
78
		$this->assertEquals("24", $buckets[0]->Key);
79
		$this->assertEquals(12, $buckets[0]->DocumentCount);
80
81
		//Asserting aggregate of Focal Length, 50 has count 11
82
		$this->assertEquals("50", $buckets[1]->Key);
83
		$this->assertEquals(11, $buckets[1]->DocumentCount);
84
85
		//Asserting aggregate of Focal Length, 80 has count 11
86
		$this->assertEquals("80", $buckets[2]->Key);
87
		$this->assertEquals(11, $buckets[2]->DocumentCount);
88
89
		//Asserting aggregate of Focal Length, 90 has count 20
90
		$this->assertEquals("90", $buckets[3]->Key);
91
		$this->assertEquals(20, $buckets[3]->DocumentCount);
92
93
		//Asserting aggregate of Focal Length, 120 has count 11
94
		$this->assertEquals("120", $buckets[4]->Key);
95
		$this->assertEquals(11, $buckets[4]->DocumentCount);
96
97
		//Asserting aggregate of Focal Length, 150 has count 17
98
		$this->assertEquals("150", $buckets[5]->Key);
99
		$this->assertEquals(17, $buckets[5]->DocumentCount);
100
101
		//Asserting aggregate of Focal Length, 200 has count 17
102
		$this->assertEquals("200", $buckets[6]->Key);
103
		$this->assertEquals(17, $buckets[6]->DocumentCount);
104
105
		//Asserting name of aggregate as Shutter Speed
106
		$agg = $aggregations[2];
107
		$this->assertEquals("Shutter Speed", $agg->Name);
108
		$buckets = $agg->Buckets->toArray();
109
110
		//Asserting aggregate of Shutter Speed, 2/250 has count 17
111
		$this->assertEquals("2/250", $buckets[0]->Key);
112
		$this->assertEquals(17, $buckets[0]->DocumentCount);
113
114
		//Asserting aggregate of Shutter Speed, 1/100 has count 15
115
		$this->assertEquals("1/100", $buckets[1]->Key);
116
		$this->assertEquals(15, $buckets[1]->DocumentCount);
117
118
		//Asserting aggregate of Shutter Speed, 1/30 has count 17
119
		$this->assertEquals("1/30", $buckets[2]->Key);
120
		$this->assertEquals(17, $buckets[2]->DocumentCount);
121
122
		//Asserting aggregate of Shutter Speed, 1/15 has count 9
123
		$this->assertEquals("1/15", $buckets[3]->Key);
124
		$this->assertEquals(9, $buckets[3]->DocumentCount);
125
126
		//Asserting aggregate of Shutter Speed, 1/2 has count 18
127
		$this->assertEquals("1/2", $buckets[4]->Key);
128
		$this->assertEquals(18, $buckets[4]->DocumentCount);
129
130
		//Asserting aggregate of Shutter Speed, 1 has count 1
131
		$this->assertEquals("1", $buckets[5]->Key);
132
		$this->assertEquals(1, $buckets[5]->DocumentCount);
133
134
		//Asserting aggregate of Shutter Speed, 2 has count 10
135
		$this->assertEquals("2", $buckets[6]->Key);
136
		$this->assertEquals(10, $buckets[6]->DocumentCount);
137
138
		//Asserting aggregate of Shutter Speed, 6 has count 12
139
		$this->assertEquals("6", $buckets[7]->Key);
140
		$this->assertEquals(12, $buckets[7]->DocumentCount);
141
142
		//Asserting name of aggregate as Aperture
143
		$agg = $aggregations[3];
144
		$this->assertEquals("Aperture", $agg->Name);
145
		$buckets = $agg->Buckets->toArray();
146
147
		//Asserting aggregate of Aperture, 2.8 has count 20
148
		$this->assertEquals("2.8", $buckets[0]->Key);
149
		$this->assertEquals(20, $buckets[0]->DocumentCount);
150
151
		//Asserting aggregate of Aperture, 5.6 has count 23
152
		$this->assertEquals("5.6", $buckets[1]->Key);
153
		$this->assertEquals(23, $buckets[1]->DocumentCount);
154
155
		//Asserting aggregate of Aperture, 11 has count 17
156
		$this->assertEquals("11", $buckets[2]->Key);
157
		$this->assertEquals(17, $buckets[2]->DocumentCount);
158
159
		//Asserting aggregate of Aperture, 16 has count 16
160
		$this->assertEquals("16", $buckets[3]->Key);
161
		$this->assertEquals(16, $buckets[3]->DocumentCount);
162
163
		//Asserting aggregate of Aperture, 22 has count 23
164
		$this->assertEquals("22", $buckets[4]->Key);
165
		$this->assertEquals(23, $buckets[4]->DocumentCount);
166
167
		//Asserting name of aggregate as Aspect
168
		$agg = $aggregations[4];
169
		$this->assertEquals("Aspect", $agg->Name);
170
		$buckets = $agg->Buckets->toArray();
171
172
		//Asserting aggregate of Aspect, Panoramic has count 9
173
		$this->assertEquals("Panoramic", $buckets[0]->Key);
174
		$this->assertEquals(9, $buckets[0]->DocumentCount);
175
176
		//Asserting aggregate of Aspect, Horizontal has count 31
177
		$this->assertEquals("Horizontal", $buckets[1]->Key);
178
		$this->assertEquals(31, $buckets[1]->DocumentCount);
179
180
		//Asserting aggregate of Aspect, Square has count 16
181
		$this->assertEquals("Square", $buckets[2]->Key);
182
		$this->assertEquals(16, $buckets[2]->DocumentCount);
183
184
		//Asserting aggregate of Aspect, Vertical has count 38
185
		$this->assertEquals("Vertical", $buckets[3]->Key);
186
		$this->assertEquals(38, $buckets[3]->DocumentCount);
187
188
		//Asserting aggregate of Aspect, Tallest has count 5
189
		$this->assertEquals("Tallest", $buckets[4]->Key);
190
		$this->assertEquals(5, $buckets[4]->DocumentCount);
191
192
	}
193
194
195
	public function testAllFieldsEmptyQuery() {
196
		//Use a text search against all fields
197
		$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...
198
		$this->assertEquals(10, $resultList->count());
199
200
		//check query
201
		$query = $resultList->getQuery()->toArray();
202
		$this->assertEquals(0, $query['from']);
203
		$this->assertEquals(10, $query['size']);
204
205
		$sort = array('TakenAt' => 'desc');
206
		$this->assertEquals($sort, $query['sort']);
207
		$this->assertFalse(isset($query['query']));
208
209
		$aggs = array();
210
		$aggs['Aperture'] = array();
211
		$aggs['ShutterSpeed'] = array();
212
		$aggs['FocalLength35mm'] = array();
213
		$aggs['ISO'] = array();
214
		$aggs['Aspect'] = array();
215
216
		//check aggregations
217
		$aggregations = $resultList->getAggregations();
218
		//Asserting name of aggregate as ISO
219
		$agg = $aggregations[0];
220
		$this->assertEquals("ISO", $agg->Name);
221
		$buckets = $agg->Buckets->toArray();
222
		$bucketSum = 0;
223
224
		//Asserting aggregate of ISO, 64 has count 5
225
		$this->assertEquals("64", $buckets[0]->Key);
226
		$this->assertEquals(5, $buckets[0]->DocumentCount);
227
		$bucketSum += $buckets[0]->DocumentCount;
228
229
		//Asserting aggregate of ISO, 100 has count 11
230
		$this->assertEquals("100", $buckets[1]->Key);
231
		$this->assertEquals(11, $buckets[1]->DocumentCount);
232
		$bucketSum += $buckets[1]->DocumentCount;
233
234
		//Asserting aggregate of ISO, 200 has count 12
235
		$this->assertEquals("200", $buckets[2]->Key);
236
		$this->assertEquals(12, $buckets[2]->DocumentCount);
237
		$bucketSum += $buckets[2]->DocumentCount;
238
239
		//Asserting aggregate of ISO, 400 has count 13
240
		$this->assertEquals("400", $buckets[3]->Key);
241
		$this->assertEquals(13, $buckets[3]->DocumentCount);
242
		$bucketSum += $buckets[3]->DocumentCount;
243
244
		//Asserting aggregate of ISO, 800 has count 16
245
		$this->assertEquals("800", $buckets[4]->Key);
246
		$this->assertEquals(16, $buckets[4]->DocumentCount);
247
		$bucketSum += $buckets[4]->DocumentCount;
248
249
		//Asserting aggregate of ISO, 1600 has count 13
250
		$this->assertEquals("1600", $buckets[5]->Key);
251
		$this->assertEquals(13, $buckets[5]->DocumentCount);
252
		$bucketSum += $buckets[5]->DocumentCount;
253
254
		//Asserting aggregate of ISO, 2000 has count 11
255
		$this->assertEquals("2000", $buckets[6]->Key);
256
		$this->assertEquals(11, $buckets[6]->DocumentCount);
257
		$bucketSum += $buckets[6]->DocumentCount;
258
259
		//Asserting aggregate of ISO, 3200 has count 19
260
		$this->assertEquals("3200", $buckets[7]->Key);
261
		$this->assertEquals(19, $buckets[7]->DocumentCount);
262
		$bucketSum += $buckets[7]->DocumentCount;
263
		$this->assertEquals(100, $bucketSum);
264
265
		//Asserting name of aggregate as Focal Length
266
		$agg = $aggregations[1];
267
		$this->assertEquals("Focal Length", $agg->Name);
268
		$buckets = $agg->Buckets->toArray();
269
		$bucketSum = 0;
270
271
		//Asserting aggregate of Focal Length, 24 has count 12
272
		$this->assertEquals("24", $buckets[0]->Key);
273
		$this->assertEquals(12, $buckets[0]->DocumentCount);
274
		$bucketSum += $buckets[0]->DocumentCount;
275
276
		//Asserting aggregate of Focal Length, 50 has count 11
277
		$this->assertEquals("50", $buckets[1]->Key);
278
		$this->assertEquals(11, $buckets[1]->DocumentCount);
279
		$bucketSum += $buckets[1]->DocumentCount;
280
281
		//Asserting aggregate of Focal Length, 80 has count 11
282
		$this->assertEquals("80", $buckets[2]->Key);
283
		$this->assertEquals(11, $buckets[2]->DocumentCount);
284
		$bucketSum += $buckets[2]->DocumentCount;
285
286
		//Asserting aggregate of Focal Length, 90 has count 20
287
		$this->assertEquals("90", $buckets[3]->Key);
288
		$this->assertEquals(20, $buckets[3]->DocumentCount);
289
		$bucketSum += $buckets[3]->DocumentCount;
290
291
		//Asserting aggregate of Focal Length, 120 has count 12
292
		$this->assertEquals("120", $buckets[4]->Key);
293
		$this->assertEquals(12, $buckets[4]->DocumentCount);
294
		$bucketSum += $buckets[4]->DocumentCount;
295
296
		//Asserting aggregate of Focal Length, 150 has count 17
297
		$this->assertEquals("150", $buckets[5]->Key);
298
		$this->assertEquals(17, $buckets[5]->DocumentCount);
299
		$bucketSum += $buckets[5]->DocumentCount;
300
301
		//Asserting aggregate of Focal Length, 200 has count 17
302
		$this->assertEquals("200", $buckets[6]->Key);
303
		$this->assertEquals(17, $buckets[6]->DocumentCount);
304
		$bucketSum += $buckets[6]->DocumentCount;
305
		$this->assertEquals(100, $bucketSum);
306
307
		//Asserting name of aggregate as Shutter Speed
308
		$agg = $aggregations[2];
309
		$this->assertEquals("Shutter Speed", $agg->Name);
310
		$buckets = $agg->Buckets->toArray();
311
		$bucketSum = 0;
312
313
		//Asserting aggregate of Shutter Speed, 2/250 has count 17
314
		$this->assertEquals("2/250", $buckets[0]->Key);
315
		$this->assertEquals(17, $buckets[0]->DocumentCount);
316
		$bucketSum += $buckets[0]->DocumentCount;
317
318
		//Asserting aggregate of Shutter Speed, 1/100 has count 15
319
		$this->assertEquals("1/100", $buckets[1]->Key);
320
		$this->assertEquals(15, $buckets[1]->DocumentCount);
321
		$bucketSum += $buckets[1]->DocumentCount;
322
323
		//Asserting aggregate of Shutter Speed, 1/30 has count 17
324
		$this->assertEquals("1/30", $buckets[2]->Key);
325
		$this->assertEquals(17, $buckets[2]->DocumentCount);
326
		$bucketSum += $buckets[2]->DocumentCount;
327
328
		//Asserting aggregate of Shutter Speed, 1/15 has count 10
329
		$this->assertEquals("1/15", $buckets[3]->Key);
330
		$this->assertEquals(10, $buckets[3]->DocumentCount);
331
		$bucketSum += $buckets[3]->DocumentCount;
332
333
		//Asserting aggregate of Shutter Speed, 1/2 has count 18
334
		$this->assertEquals("1/2", $buckets[4]->Key);
335
		$this->assertEquals(18, $buckets[4]->DocumentCount);
336
		$bucketSum += $buckets[4]->DocumentCount;
337
338
		//Asserting aggregate of Shutter Speed, 1 has count 1
339
		$this->assertEquals("1", $buckets[5]->Key);
340
		$this->assertEquals(1, $buckets[5]->DocumentCount);
341
342
		//Asserting aggregate of Shutter Speed, 2 has count 10
343
		$this->assertEquals("2", $buckets[6]->Key);
344
		$this->assertEquals(10, $buckets[6]->DocumentCount);
345
346
		//Asserting aggregate of Shutter Speed, 6 has count 12
347
		$this->assertEquals("6", $buckets[7]->Key);
348
		$this->assertEquals(12, $buckets[7]->DocumentCount);
349
350
		//Asserting name of aggregate as Aperture
351
		$agg = $aggregations[3];
352
		$this->assertEquals("Aperture", $agg->Name);
353
		$buckets = $agg->Buckets->toArray();
354
		$bucketSum = 0;
355
356
		//Asserting aggregate of Aperture, 2.8 has count 21
357
		$this->assertEquals("2.8", $buckets[0]->Key);
358
		$this->assertEquals(21, $buckets[0]->DocumentCount);
359
		$bucketSum += $buckets[0]->DocumentCount;
360
361
		//Asserting aggregate of Aperture, 5.6 has count 23
362
		$this->assertEquals("5.6", $buckets[1]->Key);
363
		$this->assertEquals(23, $buckets[1]->DocumentCount);
364
		$bucketSum += $buckets[1]->DocumentCount;
365
366
		//Asserting aggregate of Aperture, 11 has count 17
367
		$this->assertEquals("11", $buckets[2]->Key);
368
		$this->assertEquals(17, $buckets[2]->DocumentCount);
369
		$bucketSum += $buckets[2]->DocumentCount;
370
371
		//Asserting aggregate of Aperture, 16 has count 16
372
		$this->assertEquals("16", $buckets[3]->Key);
373
		$this->assertEquals(16, $buckets[3]->DocumentCount);
374
		$bucketSum += $buckets[3]->DocumentCount;
375
376
		//Asserting aggregate of Aperture, 22 has count 23
377
		$this->assertEquals("22", $buckets[4]->Key);
378
		$this->assertEquals(23, $buckets[4]->DocumentCount);
379
		$bucketSum += $buckets[4]->DocumentCount;
380
		$this->assertEquals(100, $bucketSum);
381
382
		//Asserting name of aggregate as Aspect
383
		$agg = $aggregations[4];
384
		$this->assertEquals("Aspect", $agg->Name);
385
		$buckets = $agg->Buckets->toArray();
386
		$bucketSum = 0;
387
388
		//Asserting aggregate of Aspect, Panoramic has count 9
389
		$this->assertEquals("Panoramic", $buckets[0]->Key);
390
		$this->assertEquals(9, $buckets[0]->DocumentCount);
391
		$bucketSum += $buckets[0]->DocumentCount;
392
393
		//Asserting aggregate of Aspect, Horizontal has count 31
394
		$this->assertEquals("Horizontal", $buckets[1]->Key);
395
		$this->assertEquals(31, $buckets[1]->DocumentCount);
396
		$bucketSum += $buckets[1]->DocumentCount;
397
398
		//Asserting aggregate of Aspect, Square has count 16
399
		$this->assertEquals("Square", $buckets[2]->Key);
400
		$this->assertEquals(16, $buckets[2]->DocumentCount);
401
		$bucketSum += $buckets[2]->DocumentCount;
402
403
		//Asserting aggregate of Aspect, Vertical has count 39
404
		$this->assertEquals("Vertical", $buckets[3]->Key);
405
		$this->assertEquals(39, $buckets[3]->DocumentCount);
406
		$bucketSum += $buckets[3]->DocumentCount;
407
408
		//Asserting aggregate of Aspect, Tallest has count 5
409
		$this->assertEquals("Tallest", $buckets[4]->Key);
410
		$this->assertEquals(5, $buckets[4]->DocumentCount);
411
		$bucketSum += $buckets[4]->DocumentCount;
412
		$this->assertEquals(100, $bucketSum);
413
414
	}
415
416
417
	/*
418
	Search for an empty query against the Title and Description fields
419
	 */
420
	public function testAggregationWithEmptyQuery() {
421
		$resultList = $this->search('');
422
423
		//assert there are actually some results
424
		$this->assertGreaterThan(0,$resultList->getTotalItems());
425
		$aggregations = $resultList->getAggregations()->toArray();
426
427
		/*
428
		For all of the aggregates, all results are returned due to empty query string, so the number
429
		of aggregates should always add up to 100.  Check some values at the database level for
430
		further confirmation also
431
		FIXME - finish DB checks
432
		 */
433
434
		//Asserting name of aggregate as ISO
435
		$agg = $aggregations[0];
436
		$this->assertEquals("ISO", $agg->Name);
437
		$buckets = $agg->Buckets->toArray();
438
		$bucketSum = 0;
439
440
		//Asserting aggregate of ISO, 64 has count 5
441
		$this->assertEquals("64", $buckets[0]->Key);
442
		$this->assertEquals(5, $buckets[0]->DocumentCount);
443
		$this->assertEquals(FlickrPhotoTO::get()->filter('ISO',64)->count(),5);
444
		$bucketSum += $buckets[0]->DocumentCount;
445
446
		//Asserting aggregate of ISO, 100 has count 11
447
		$this->assertEquals("100", $buckets[1]->Key);
448
		$this->assertEquals(11, $buckets[1]->DocumentCount);
449
		$this->assertEquals(FlickrPhotoTO::get()->filter('ISO',100)->count(),11);
450
		$bucketSum += $buckets[1]->DocumentCount;
451
452
		//Asserting aggregate of ISO, 200 has count 12
453
		$this->assertEquals("200", $buckets[2]->Key);
454
		$this->assertEquals(12, $buckets[2]->DocumentCount);
455
		$this->assertEquals(FlickrPhotoTO::get()->filter('ISO',200)->count(),12);
456
		$bucketSum += $buckets[2]->DocumentCount;
457
458
		//Asserting aggregate of ISO, 400 has count 13
459
		$this->assertEquals("400", $buckets[3]->Key);
460
		$this->assertEquals(13, $buckets[3]->DocumentCount);
461
		$this->assertEquals(FlickrPhotoTO::get()->filter('ISO',400)->count(),13);
462
		$bucketSum += $buckets[3]->DocumentCount;
463
464
		//Asserting aggregate of ISO, 800 has count 16
465
		$this->assertEquals("800", $buckets[4]->Key);
466
		$this->assertEquals(16, $buckets[4]->DocumentCount);
467
		$this->assertEquals(FlickrPhotoTO::get()->filter('ISO',800)->count(),16);
468
		$bucketSum += $buckets[4]->DocumentCount;
469
470
		//Asserting aggregate of ISO, 1600 has count 13
471
		$this->assertEquals("1600", $buckets[5]->Key);
472
		$this->assertEquals(13, $buckets[5]->DocumentCount);
473
		$this->assertEquals(FlickrPhotoTO::get()->filter('ISO',1600)->count(),13);
474
		$bucketSum += $buckets[5]->DocumentCount;
475
476
		//Asserting aggregate of ISO, 2000 has count 11
477
		$this->assertEquals("2000", $buckets[6]->Key);
478
		$this->assertEquals(11, $buckets[6]->DocumentCount);
479
		$this->assertEquals(FlickrPhotoTO::get()->filter('ISO',2000)->count(),11);
480
		$bucketSum += $buckets[6]->DocumentCount;
481
482
		//Asserting aggregate of ISO, 3200 has count 19
483
		$this->assertEquals("3200", $buckets[7]->Key);
484
		$this->assertEquals(19, $buckets[7]->DocumentCount);
485
		$this->assertEquals(FlickrPhotoTO::get()->filter('ISO',3200)->count(),19);
486
		$bucketSum += $buckets[7]->DocumentCount;
487
		$this->assertEquals(100, $bucketSum);
488
489
		//Asserting name of aggregate as Focal Length
490
		$agg = $aggregations[1];
491
		$this->assertEquals("Focal Length", $agg->Name);
492
		$buckets = $agg->Buckets->toArray();
493
		$bucketSum = 0;
494
495
		//Asserting aggregate of Focal Length, 24 has count 12
496
		$this->assertEquals("24", $buckets[0]->Key);
497
		$this->assertEquals(12, $buckets[0]->DocumentCount);
498
		$this->assertEquals(FlickrPhotoTO::get()->filter('FocalLength35mm',24)->count(),12);
499
		$bucketSum += $buckets[0]->DocumentCount;
500
501
		//Asserting aggregate of Focal Length, 50 has count 11
502
		$this->assertEquals("50", $buckets[1]->Key);
503
		$this->assertEquals(11, $buckets[1]->DocumentCount);
504
		$this->assertEquals(FlickrPhotoTO::get()->filter('FocalLength35mm',50)->count(),11);
505
		$bucketSum += $buckets[1]->DocumentCount;
506
507
		//Asserting aggregate of Focal Length, 80 has count 11
508
		$this->assertEquals("80", $buckets[2]->Key);
509
		$this->assertEquals(11, $buckets[2]->DocumentCount);
510
		$this->assertEquals(FlickrPhotoTO::get()->filter('FocalLength35mm',80)->count(),11);
511
		$bucketSum += $buckets[2]->DocumentCount;
512
513
		//Asserting aggregate of Focal Length, 90 has count 20
514
		$this->assertEquals("90", $buckets[3]->Key);
515
		$this->assertEquals(20, $buckets[3]->DocumentCount);
516
		$this->assertEquals(FlickrPhotoTO::get()->filter('FocalLength35mm',90)->count(),20);
517
		$bucketSum += $buckets[3]->DocumentCount;
518
519
		//Asserting aggregate of Focal Length, 120 has count 12
520
		$this->assertEquals("120", $buckets[4]->Key);
521
		$this->assertEquals(12, $buckets[4]->DocumentCount);
522
		$this->assertEquals(FlickrPhotoTO::get()->filter('FocalLength35mm',120)->count(),12);
523
		$bucketSum += $buckets[4]->DocumentCount;
524
525
		//Asserting aggregate of Focal Length, 150 has count 17
526
		$this->assertEquals("150", $buckets[5]->Key);
527
		$this->assertEquals(17, $buckets[5]->DocumentCount);
528
		$this->assertEquals(FlickrPhotoTO::get()->filter('FocalLength35mm',150)->count(),17);
529
		$bucketSum += $buckets[5]->DocumentCount;
530
531
		//Asserting aggregate of Focal Length, 200 has count 17
532
		$this->assertEquals("200", $buckets[6]->Key);
533
		$this->assertEquals(17, $buckets[6]->DocumentCount);
534
		$this->assertEquals(FlickrPhotoTO::get()->filter('FocalLength35mm',200)->count(),17);
535
		$bucketSum += $buckets[6]->DocumentCount;
536
		$this->assertEquals(100, $bucketSum);
537
538
		//Asserting name of aggregate as Shutter Speed
539
		$agg = $aggregations[2];
540
		$this->assertEquals("Shutter Speed", $agg->Name);
541
		$buckets = $agg->Buckets->toArray();
542
		$bucketSum = 0;
543
544
		//Asserting aggregate of Shutter Speed, 2/250 has count 17
545
		$this->assertEquals("2/250", $buckets[0]->Key);
546
		$this->assertEquals(17, $buckets[0]->DocumentCount);
547
		$bucketSum += $buckets[0]->DocumentCount;
548
549
		//Asserting aggregate of Shutter Speed, 1/100 has count 15
550
		$this->assertEquals("1/100", $buckets[1]->Key);
551
		$this->assertEquals(15, $buckets[1]->DocumentCount);
552
		$bucketSum += $buckets[1]->DocumentCount;
553
554
		//Asserting aggregate of Shutter Speed, 1/30 has count 17
555
		$this->assertEquals("1/30", $buckets[2]->Key);
556
		$this->assertEquals(17, $buckets[2]->DocumentCount);
557
		$bucketSum += $buckets[2]->DocumentCount;
558
559
		//Asserting aggregate of Shutter Speed, 1/15 has count 10
560
		$this->assertEquals("1/15", $buckets[3]->Key);
561
		$this->assertEquals(10, $buckets[3]->DocumentCount);
562
		$bucketSum += $buckets[3]->DocumentCount;
563
564
		//Asserting aggregate of Shutter Speed, 1/2 has count 18
565
		$this->assertEquals("1/2", $buckets[4]->Key);
566
		$this->assertEquals(18, $buckets[4]->DocumentCount);
567
		$bucketSum += $buckets[4]->DocumentCount;
568
569
		//Asserting aggregate of Shutter Speed, 1 has count 1
570
		$this->assertEquals("1", $buckets[5]->Key);
571
		$this->assertEquals(1, $buckets[5]->DocumentCount);
572
573
		//Asserting aggregate of Shutter Speed, 2 has count 10
574
		$this->assertEquals("2", $buckets[6]->Key);
575
		$this->assertEquals(10, $buckets[6]->DocumentCount);
576
577
		//Asserting aggregate of Shutter Speed, 6 has count 12
578
		$this->assertEquals("6", $buckets[7]->Key);
579
		$this->assertEquals(12, $buckets[7]->DocumentCount);
580
581
		//Asserting name of aggregate as Aperture
582
		$agg = $aggregations[3];
583
		$this->assertEquals("Aperture", $agg->Name);
584
		$buckets = $agg->Buckets->toArray();
585
		$bucketSum = 0;
586
587
		//Asserting aggregate of Aperture, 2.8 has count 21
588
		$this->assertEquals("2.8", $buckets[0]->Key);
589
		$this->assertEquals(21, $buckets[0]->DocumentCount);
590
		$bucketSum += $buckets[0]->DocumentCount;
591
592
		//Asserting aggregate of Aperture, 5.6 has count 23
593
		$this->assertEquals("5.6", $buckets[1]->Key);
594
		$this->assertEquals(23, $buckets[1]->DocumentCount);
595
		$bucketSum += $buckets[1]->DocumentCount;
596
597
		//Asserting aggregate of Aperture, 11 has count 17
598
		$this->assertEquals("11", $buckets[2]->Key);
599
		$this->assertEquals(17, $buckets[2]->DocumentCount);
600
		$bucketSum += $buckets[2]->DocumentCount;
601
602
		//Asserting aggregate of Aperture, 16 has count 16
603
		$this->assertEquals("16", $buckets[3]->Key);
604
		$this->assertEquals(16, $buckets[3]->DocumentCount);
605
		$bucketSum += $buckets[3]->DocumentCount;
606
607
		//Asserting aggregate of Aperture, 22 has count 23
608
		$this->assertEquals("22", $buckets[4]->Key);
609
		$this->assertEquals(23, $buckets[4]->DocumentCount);
610
		$bucketSum += $buckets[4]->DocumentCount;
611
		$this->assertEquals(100, $bucketSum);
612
613
		//Asserting name of aggregate as Aspect
614
		$agg = $aggregations[4];
615
		$this->assertEquals("Aspect", $agg->Name);
616
		$buckets = $agg->Buckets->toArray();
617
		$bucketSum = 0;
618
619
		//Asserting aggregate of Aspect, Panoramic has count 9
620
		$this->assertEquals("Panoramic", $buckets[0]->Key);
621
		$this->assertEquals(9, $buckets[0]->DocumentCount);
622
		$bucketSum += $buckets[0]->DocumentCount;
623
624
		//Asserting aggregate of Aspect, Horizontal has count 31
625
		$this->assertEquals("Horizontal", $buckets[1]->Key);
626
		$this->assertEquals(31, $buckets[1]->DocumentCount);
627
		$bucketSum += $buckets[1]->DocumentCount;
628
629
		//Asserting aggregate of Aspect, Square has count 16
630
		$this->assertEquals("Square", $buckets[2]->Key);
631
		$this->assertEquals(16, $buckets[2]->DocumentCount);
632
		$bucketSum += $buckets[2]->DocumentCount;
633
634
		//Asserting aggregate of Aspect, Vertical has count 39
635
		$this->assertEquals("Vertical", $buckets[3]->Key);
636
		$this->assertEquals(39, $buckets[3]->DocumentCount);
637
		$bucketSum += $buckets[3]->DocumentCount;
638
639
		//Asserting aggregate of Aspect, Tallest has count 5
640
		$this->assertEquals("Tallest", $buckets[4]->Key);
641
		$this->assertEquals(5, $buckets[4]->DocumentCount);
642
		$bucketSum += $buckets[4]->DocumentCount;
643
		$this->assertEquals(100, $bucketSum);
644
	}
645
646
647
	/*
648
	Search for the query 'New Zealand' against the Title and Description fields
649
	 */
650
	public function testAggregationWithQuery() {
651
		$resultList = $this->search('New Zealand');
652
		$aggregations = $resultList->getAggregations()->toArray();
653
654
655
		//Asserting name of aggregate as ISO
656
		$agg = $aggregations[0];
657
658
		print_r($agg);
659
660
		$this->assertEquals("ISO", $agg->Name);
661
		$buckets = $agg->Buckets->toArray();
662
663
		//Asserting aggregate of ISO, 64 has count 5
664
		$this->assertEquals("64", $buckets[0]->Key);
665
		$this->assertEquals(5, $buckets[0]->DocumentCount);
666
667
		//Asserting aggregate of ISO, 100 has count 11
668
		$this->assertEquals("100", $buckets[1]->Key);
669
		$this->assertEquals(11, $buckets[1]->DocumentCount);
670
671
		//Asserting aggregate of ISO, 200 has count 12
672
		$this->assertEquals("200", $buckets[2]->Key);
673
		$this->assertEquals(12, $buckets[2]->DocumentCount);
674
675
		//Asserting aggregate of ISO, 400 has count 13
676
		$this->assertEquals("400", $buckets[3]->Key);
677
		$this->assertEquals(13, $buckets[3]->DocumentCount);
678
679
		//Asserting aggregate of ISO, 800 has count 16
680
		$this->assertEquals("800", $buckets[4]->Key);
681
		$this->assertEquals(16, $buckets[4]->DocumentCount);
682
683
		//Asserting aggregate of ISO, 1600 has count 13
684
		$this->assertEquals("1600", $buckets[5]->Key);
685
		$this->assertEquals(13, $buckets[5]->DocumentCount);
686
687
		//Asserting aggregate of ISO, 2000 has count 11
688
		$this->assertEquals("2000", $buckets[6]->Key);
689
		$this->assertEquals(11, $buckets[6]->DocumentCount);
690
691
		//Asserting aggregate of ISO, 3200 has count 19
692
		$this->assertEquals("3200", $buckets[7]->Key);
693
		$this->assertEquals(19, $buckets[7]->DocumentCount);
694
695
		//Asserting name of aggregate as Focal Length
696
		$agg = $aggregations[1];
697
		$this->assertEquals("Focal Length", $agg->Name);
698
		$buckets = $agg->Buckets->toArray();
699
700
		//Asserting aggregate of Focal Length, 24 has count 12
701
		$this->assertEquals("24", $buckets[0]->Key);
702
		$this->assertEquals(12, $buckets[0]->DocumentCount);
703
704
		//Asserting aggregate of Focal Length, 50 has count 11
705
		$this->assertEquals("50", $buckets[1]->Key);
706
		$this->assertEquals(11, $buckets[1]->DocumentCount);
707
708
		//Asserting aggregate of Focal Length, 80 has count 11
709
		$this->assertEquals("80", $buckets[2]->Key);
710
		$this->assertEquals(11, $buckets[2]->DocumentCount);
711
712
		//Asserting aggregate of Focal Length, 90 has count 20
713
		$this->assertEquals("90", $buckets[3]->Key);
714
		$this->assertEquals(20, $buckets[3]->DocumentCount);
715
716
		//Asserting aggregate of Focal Length, 120 has count 12
717
		$this->assertEquals("120", $buckets[4]->Key);
718
		$this->assertEquals(12, $buckets[4]->DocumentCount);
719
720
		//Asserting aggregate of Focal Length, 150 has count 17
721
		$this->assertEquals("150", $buckets[5]->Key);
722
		$this->assertEquals(17, $buckets[5]->DocumentCount);
723
724
		//Asserting aggregate of Focal Length, 200 has count 17
725
		$this->assertEquals("200", $buckets[6]->Key);
726
		$this->assertEquals(17, $buckets[6]->DocumentCount);
727
728
		//Asserting name of aggregate as Shutter Speed
729
		$agg = $aggregations[2];
730
		$this->assertEquals("Shutter Speed", $agg->Name);
731
		$buckets = $agg->Buckets->toArray();
732
733
734
		foreach ($buckets as $bucket) {
735
			echo "{$bucket->Key} => {$bucket->DocumentCount}\n";
736
		}
737
738
		//Asserting aggregate of Shutter Speed, 2/250 has count 17
739
		$this->assertEquals("2/250", $buckets[0]->Key);
740
		$this->assertEquals(17, $buckets[0]->DocumentCount);
741
742
		//Asserting aggregate of Shutter Speed, 1/100 has count 15
743
		$this->assertEquals("1/100", $buckets[1]->Key);
744
		$this->assertEquals(15, $buckets[1]->DocumentCount);
745
746
		//Asserting aggregate of Shutter Speed, 1/30 has count 17
747
		$this->assertEquals("1/30", $buckets[2]->Key);
748
		$this->assertEquals(17, $buckets[2]->DocumentCount);
749
750
		//Asserting aggregate of Shutter Speed, 1/15 has count 10
751
		$this->assertEquals("1/15", $buckets[3]->Key);
752
		$this->assertEquals(10, $buckets[3]->DocumentCount);
753
754
		//Asserting aggregate of Shutter Speed, 1/2 has count 18
755
		$this->assertEquals("1/2", $buckets[4]->Key);
756
		$this->assertEquals(18, $buckets[4]->DocumentCount);
757
758
		//Asserting aggregate of Shutter Speed, 1 has count 1
759
		$this->assertEquals("1", $buckets[5]->Key);
760
		$this->assertEquals(1, $buckets[5]->DocumentCount);
761
762
		//Asserting aggregate of Shutter Speed, 2 has count 10
763
		$this->assertEquals("2", $buckets[6]->Key);
764
		$this->assertEquals(10, $buckets[6]->DocumentCount);
765
766
		//Asserting aggregate of Shutter Speed, 6 has count 12
767
		$this->assertEquals("6", $buckets[7]->Key);
768
		$this->assertEquals(12, $buckets[7]->DocumentCount);
769
770
		//Asserting name of aggregate as Aperture
771
		$agg = $aggregations[3];
772
		$this->assertEquals("Aperture", $agg->Name);
773
		$buckets = $agg->Buckets->toArray();
774
775
		//Asserting aggregate of Aperture, 2.8 has count 21
776
		$this->assertEquals("2.8", $buckets[0]->Key);
777
		$this->assertEquals(21, $buckets[0]->DocumentCount);
778
779
		//Asserting aggregate of Aperture, 5.6 has count 23
780
		$this->assertEquals("5.6", $buckets[1]->Key);
781
		$this->assertEquals(23, $buckets[1]->DocumentCount);
782
783
		//Asserting aggregate of Aperture, 11 has count 17
784
		$this->assertEquals("11", $buckets[2]->Key);
785
		$this->assertEquals(17, $buckets[2]->DocumentCount);
786
787
		//Asserting aggregate of Aperture, 16 has count 16
788
		$this->assertEquals("16", $buckets[3]->Key);
789
		$this->assertEquals(16, $buckets[3]->DocumentCount);
790
791
		//Asserting aggregate of Aperture, 22 has count 23
792
		$this->assertEquals("22", $buckets[4]->Key);
793
		$this->assertEquals(23, $buckets[4]->DocumentCount);
794
795
		//Asserting name of aggregate as Aspect
796
		$agg = $aggregations[4];
797
		$this->assertEquals("Aspect", $agg->Name);
798
		$buckets = $agg->Buckets->toArray();
799
800
		//Asserting aggregate of Aspect, Panoramic has count 9
801
		$this->assertEquals("Panoramic", $buckets[0]->Key);
802
		$this->assertEquals(9, $buckets[0]->DocumentCount);
803
804
		//Asserting aggregate of Aspect, Horizontal has count 31
805
		$this->assertEquals("Horizontal", $buckets[1]->Key);
806
		$this->assertEquals(31, $buckets[1]->DocumentCount);
807
808
		//Asserting aggregate of Aspect, Square has count 16
809
		$this->assertEquals("Square", $buckets[2]->Key);
810
		$this->assertEquals(16, $buckets[2]->DocumentCount);
811
812
		//Asserting aggregate of Aspect, Vertical has count 39
813
		$this->assertEquals("Vertical", $buckets[3]->Key);
814
		$this->assertEquals(39, $buckets[3]->DocumentCount);
815
816
		//Asserting aggregate of Aspect, Tallest has count 5
817
		$this->assertEquals("Tallest", $buckets[4]->Key);
818
		$this->assertEquals(5, $buckets[4]->DocumentCount);
819
	}
820
821
822
	public function testOneAggregateSelected() {
823
		$fields = array('Title' => 1, 'Description' => 1);
824
		$resultList = $this->search('New Zealand', $fields);
825
		$originalAggregations = $resultList->getAggregations()->toArray();
826
827
		$filters = array('ISO' => 3200);
828
		$resultListFiltered = $this->search('New Zealand', $fields,$filters);
829
		$filteredAggregations = $resultListFiltered->getAggregations()->toArray();
830
831
		$this->checkDrillingDownHasHappened($filteredAggregations, $originalAggregations);
832
	}
833
834
835
	public function testTwoAggregatesSelected() {
836
		$fields = array('Title' => 1, 'Description' => 1);
837
		$resultList = $this->search('New Zealand', $fields, array('ISO' => 400));
838
		$originalAggregations = $resultList->getAggregations()->toArray();
839
840
		$filters = array('ISO' => 400, 'Aspect' => 'Vertical' );
841
		$resultListFiltered = $this->search('New Zealand', $fields,$filters);
842
		$filteredAggregations = $resultListFiltered->getAggregations()->toArray();
843
844
		$this->checkDrillingDownHasHappened($filteredAggregations, $originalAggregations);
845
	}
846
847
848
	public function testThreeAggregatesSelected() {
849
		$ct = FlickrPhotoTO::get()->count();
850
		echo "There are $ct flickr photos\n";
851
852
		$fields = array('Title' => 1, 'Description' => 1);
853
		$resultList = $this->search('New Zealand', $fields, array('ISO' => 400,
854
										'Aspect' => 'Vertical'));
855
		$originalAggregations = $resultList->getAggregations()->toArray();
856
857
		$filters = array('ISO' => 400, 'Aspect' => 'Vertical', 'Aperture' => 5 );
858
		$resultListFiltered = $this->search('New Zealand', $fields,$filters);
859
		$filteredAggregations = $resultListFiltered->getAggregations()->toArray();
860
861
		$this->checkDrillingDownHasHappened($filteredAggregations, $originalAggregations);
862
	}
863
864
865
	/*
866
	Shutter speed is a special case as values are manipulated in order to maintain correct sort order
867
	 */
868
	public function testAggregateShutterSpeed() {
869
		$fields = array('Title' => 1, 'Description' => 1);
870
		$resultList = $this->search('', $fields, array('ShutterSpeed' => '1/100'));
871
		$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...
872
873
		// 15 instances in the YML file
874
		$this->assertEquals(15, $resultList->getTotalItems());
875
876
		// Shutter speed of 1 second is a special case, only 1 case
877
		$resultList = $this->search('Image taken from page 471', $fields, array('ShutterSpeed' => '1'));
878
		$this->assertEquals(1, $resultList->getTotalItems());
879
880
		// test outlying vals and ensure they do not crash the code
881
		$resultList = $this->search('Image taken from page 471', $fields, array('ShutterSpeed' => ''));
882
		$this->assertEquals(0, $resultList->getTotalItems());
883
884
		$resultList = $this->search('Image taken from page 471', $fields, array('ShutterSpeed' => '/'));
885
		$this->assertEquals(0, $resultList->getTotalItems());
886
887
		$resultList = $this->search('Image taken from page 471', $fields, array('ShutterSpeed' => '2/'));
888
		$this->assertEquals(0, $resultList->getTotalItems());
889
890
		$resultList = $this->search('Image taken from page 471', $fields, array('ShutterSpeed' => '/2'));
891
		$this->assertEquals(0, $resultList->getTotalItems());
892
	}
893
894
895
	private function checkDrillingDownHasHappened($filteredAggregations, $originalAggregations) {
896
		$aggCtr = 0;
897
898
		foreach ($filteredAggregations as $filteredAgg) {
899
			$origAgg = $originalAggregations[$aggCtr];
900
			$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...
901
			$origBuckets = $origAgg->Buckets->toArray();
902
			$filteredBuckets = $filteredAgg->Buckets->toArray();
903
904
			$origCounts = array();
905
			$filteredCounts = array();
906
907
			foreach ($origBuckets as $bucket) {
908
				$origCounts[$bucket->Key] = $bucket->DocumentCount;
909
			}
910
911
			foreach ($filteredBuckets as $bucket) {
912
				$filteredCounts[$bucket->Key] = $bucket->DocumentCount;
913
			}
914
915
			$akf = array_keys($filteredCounts);
916
917
			foreach ($akf as $aggregateKey) {
918
				$this->assertGreaterThanOrEqual(
919
					$filteredCounts[$aggregateKey], $origCounts[$aggregateKey]
920
				);
921
			}
922
923
			$aggCtr++;
924
		}
925
	}
926
927
928
	/*
929
	ResultList and ElasticSearcher both have accessors to the aggregates.  Check they are the same
930
	 */
931
	public function testGetAggregations() {
932
		$es = new ElasticSearcher();
933
		$es->setStart(0);
934
		$es->setPageLength(10);
935
		//$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...
936
		$es->setClasses('FlickrPhotoTO');
937
		$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...
938
		$resultList = $es->search('New Zealand');
939
		$this->assertEquals($resultList->getAggregations(), $es->getAggregations());
940
	}
941
942
943
	public function testAggregationNonExistentField() {
944
		$filters = array();
945
		$es = new ElasticSearcher();
946
		$es->setStart(0);
947
		$es->setPageLength(10);
948
		//$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...
949
		$es->setClasses('FlickrPhotoTO');
950
		$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...
951
952
		//Add filters
953
		foreach ($filters as $key => $value) {
954
			$es->addFilter($key,$value);
955
		}
956
957
		$es->showResultsForEmptySearch();
958
959
		try {
960
			$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...
961
			$this->fail('The field Wibble should cause this to error out with an exception');
962
		} catch (Exception $e) {
963
			$this->assertEquals('Field Wibble does not exist', $e->getMessage());
964
		}
965
966
	}
967
968
969
	/**
970
	 * Test searching
971
	 * http://stackoverflow.com/questions/28305250/elasticsearch-customize-score-for-synonyms-stemming
972
	 */
973
	private function search($queryText,$fields = array('Title' => 1, 'Description' => 1),
974
		$filters = array()) {
975
		$es = new ElasticSearcher();
976
		$es->setStart(0);
977
		$es->setPageLength(10);
978
		//$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...
979
		$es->setClasses('FlickrPhotoTO');
980
		$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...
981
982
		//Add filters
983
		foreach ($filters as $key => $value) {
984
			echo "ADDING FILTER:$key => $value\n";
985
			$es->addFilter($key,$value);
986
		}
987
988
		$es->showResultsForEmptySearch();
989
990
		$resultList = $es->search($queryText, $fields);
991
		$ctr = 0;
992
993
		echo "{$resultList->count()} items found searching for '$queryText'\n\n";
994
		foreach ($resultList as $result) {
995
			$ctr++;
996
			echo("($ctr) ".$result->Title."\n");
997
			if ($result->SearchHighlightsByField->Content) {
998
				foreach ($result->SearchHighlightsByField->Content as $highlight) {
999
					echo("- ".$highlight->Snippet);
1000
				}
1001
			}
1002
		}
1003
1004
		return $resultList;
1005
	}
1006
1007
}
1008