Completed
Push — dev2 ( a0c087...e268d7 )
by Gordon
03:48
created

QueryGeneratorTest::baseAggs()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 33

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 46
rs 8.9412
cc 1
eloc 33
nc 1
nop 0
1
<?php
2
use \SilverStripe\Elastica\ElasticSearcher;
3
use \SilverStripe\Elastica\QueryGenerator;
4
5
/**
6
 * Test query generation
7
 * @package elastica
8
 */
9
class QueryGeneratorTest extends ElasticsearchBaseTest {
10
	public static $fixture_file = 'elastica/tests/lotsOfPhotos.yml';
11
12
13
	public static $ignoreFixtureFileFor = array('testToQuoted*');
14
15
	public function testTextOnly() {
16
		$qg = new QueryGenerator();
17
		$qg->setQueryText('New Zealand');
18
		$qg->setFields(null);
19
		$qg->setSelectedFilters(null);
20
21
		//As the query is not empty it should not matter whether or not the show results for empty
22
		//query flag is set or not - test with true and false
23
24
		$qg->setShowResultsForEmptyQuery(false);
25
		$qs = array('query_string' => array('query' => 'New Zealand', 'lenient' => true));
26
		$expected = array(
27
			'query' => $qs,
28
			'size' => 10,
29
			'from' => 0,
30
			'suggest' => $this->getDefaultSuggest('New Zealand')
31
		);
32
33
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
34
35
		$qg->setShowResultsForEmptyQuery(true);
36
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
37
	}
38
39
40
	/**
41
	 * @param string $queryText
42
	 */
43
	private function getDefaultSuggest($queryText) {
44
		return array(
45
				'query-phrase-suggestions' => array(
46
					'phrase' => array(
47
						'field' => '_all',
48
						'size' => 4,
49
						'highlight' => array(
50
							'pre_tag' => '<strong class="hl">',
51
							'post_tag' => '</strong>'
52
						)
53
					),
54
					'text' => $queryText
55
				)
56
		);
57
	}
58
59
60
	public function testEmptyTextShowNone() {
61
		$qg = new QueryGenerator();
62
		$qg->setQueryText('');
63
		$qg->setFields(null);
64
		$qg->setSelectedFilters(null);
65
		$qg->setShowResultsForEmptyQuery(false);
66
67
		$qs = array('query_string' => array('query' => '', 'lenient' => true));
68
		$expected = array(
69
			'query' => $qs,
70
			'size' => 10,
71
			'from' => 0,
72
			'suggest' => $this->getDefaultSuggest('')
73
		);
74
75
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
76
	}
77
78
79
	public function testEmptyTextShowAll() {
80
		$qg = new QueryGenerator();
81
		$qg->setQueryText('');
82
		$qg->setFields(null);
83
		$qg->setSelectedFilters(null);
84
		$qg->setShowResultsForEmptyQuery(true);
85
86
		//In order to show all results an empty query works,
87
		//e.g. curl -XGET 'http://localhost:9200/elastica_ss_module_test_en_us/_search?pretty'
88
		$expected = array(
89
			'size' => 10,
90
			'from' => 0,
91
			'suggest' => $this->getDefaultSuggest('')
92
		);
93
94
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
95
	}
96
97
98 View Code Duplication
	public function testMultiMatchWithText() {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
		$qg = new QueryGenerator();
100
		$qg->setQueryText('New Zealand');
101
		$fields = array('Title' => 1, 'Description' => 1);
102
		$qg->setFields($fields);
103
		$qg->setSelectedFilters(null);
104
		$qg->setClasses('FlickrPhotoTO');
105
106
		//As the query is not empty it should not matter whether or not the show results for empty
107
		//query flag is set or not - test with true and false
108
109
		$qg->setShowResultsForEmptyQuery(false);
110
		$qs = array('multi_match' => array(
111
			'fields' => array('Title','Title.*','Description','Description.*'),
112
			'type' => 'most_fields',
113
			'query' => 'New Zealand',
114
			'lenient' => true
115
			)
116
		);
117
		$expected = array(
118
			'query' => $qs,
119
			'size' => 10,
120
			'from' => 0,
121
			'suggest' => $this->getDefaultSuggest('New Zealand')
122
		);
123
124
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
125
126
		$qg->setShowResultsForEmptyQuery(true);
127
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
128
	}
129
130
131
132 View Code Duplication
	public function testMultiMatchWithNoText() {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
		$qg = new QueryGenerator();
134
		$qg->setQueryText('');
135
		$fields = array('Title' => 1, 'Description' => 1);
136
		$qg->setFields($fields);
137
		$qg->setSelectedFilters(null);
138
		$qg->setClasses('FlickrPhotoTO');
139
140
		//As the query is not empty it should not matter whether or not the show results for empty
141
		//query flag is set or not - test with true and false
142
143
		//Case of empty query, do not show results
144
		$qg->setShowResultsForEmptyQuery(false);
145
		$qs = array(
146
			'multi_match' => array(
147
				'fields' => array('Title','Title.*','Description','Description.*'),
148
				'type' => 'most_fields',
149
				'query' => '',
150
				'lenient' => true
151
			)
152
		);
153
		$expected = array(
154
			'query' => $qs,
155
			'size' => 10,
156
			'from' => 0,
157
			'suggest' => $this->getDefaultSuggest('')
158
		);
159
160
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
161
162
163
		// Now the case of empty query and show results
164
		$qg->setShowResultsForEmptyQuery(true);
165
		unset($expected['query']);
166
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
167
	}
168
169
170
	// ---- tests with aggregations ----
171
172
173
	public function testEmptyTextShowNoResultsWithAggregations() {
174
		$this->assertFalse(false, 'This is not possible - an empty query returns 0 docs and 0 aggregations');
175
	}
176
177
178
	/*
179
	Test aggregations with and without text query
180
	 */
181
	public function testTextShowResultsWithAggregations() {
182
		$qg = new QueryGenerator();
183
		$qg->setQueryText('');
184
		$qg->setFields(null);
185
		$qg->setSelectedFilters(null);
186
		$qg->setShowResultsForEmptyQuery(true);
187
		$qg->setQueryResultManipulator('FlickrPhotoTOElasticaSearchHelper');
188
		$aggs = $this->baseAggs();
189
190
		//tests are complete
191
		$expected = array(
192
			'aggs' => $aggs,
193
			'size' => 10,
194
			'from' => 0,
195
			'sort' => array('TakenAt' => 'desc'),
196
			'suggest' => $this->getDefaultSuggest('')
197
		);
198
199
		print_r($qg->generateElasticaQuery());
200
201
		echo(json_encode($qg->generateElasticaQuery()->toArray()));
202
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
203
204
		$qg->setQueryText('New Zealand');
205
		echo(json_encode($qg->generateElasticaQuery()->toArray()));
206
		unset($expected['sort']);
207
		$expected['query'] = array('query_string' => array('query' => 'New Zealand', 'lenient' => true));
208
		$expected['suggest'] = $this->getDefaultSuggest('New Zealand');
209
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
210
211
		$qg->setShowResultsForEmptyQuery(false);
212
		$qg->setQueryText('New Zealand');
213
		echo(json_encode($qg->generateElasticaQuery()->toArray()));
214
		$expected['query'] = array('query_string' => array('query' => 'New Zealand', 'lenient' => true));
215
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
216
	}
217
218
219
	/*
220
	Should generate this working query:
221
	curl -XGET 'http://localhost:9200/elastica_ss_module_test_en_us/_search?pretty' -d '
222
	{
223
	  "query": {
224
	    "filtered": {
225
	      "filter": {
226
	        "term": {
227
	          "ISO": 400
228
	        }
229
	      }
230
	    }
231
	  },
232
	  "aggs": {
233
	    "Aperture": {
234
	      "terms": {
235
	        "field": "Aperture",
236
	        "size": 0,
237
	        "order": {
238
	          "_term": "asc"
239
	        }
240
	      }
241
	    },
242
	    "ShutterSpeed": {
243
	      "terms": {
244
	        "field": "ShutterSpeed",
245
	        "size": 0,
246
	        "order": {
247
	          "_term": "asc"
248
	        }
249
	      }
250
	    },
251
	    "FocalLength35mm": {
252
	      "terms": {
253
	        "field": "FocalLength35mm",
254
	        "size": 0,
255
	        "order": {
256
	          "_term": "asc"
257
	        }
258
	      }
259
	    },
260
	    "ISO": {
261
	      "terms": {
262
	        "field": "ISO",
263
	        "size": 0,
264
	        "order": {
265
	          "_term": "asc"
266
	        }
267
	      }
268
	    },
269
	    "Aspect": {
270
	      "range": {
271
	        "field": "AspectRatio",
272
	        "ranges": [{
273
	          "from": 1.0e-7,
274
	          "to": 0.3,
275
	          "key": "Panoramic"
276
	        }, {
277
	          "from": 0.3,
278
	          "to": 0.9,
279
	          "key": "Horizontal"
280
	        }, {
281
	          "from": 0.9,
282
	          "to": 1.2,
283
	          "key": "Square"
284
	        }, {
285
	          "from": 1.2,
286
	          "to": 1.79,
287
	          "key": "Vertical"
288
	        }, {
289
	          "from": 1.79,
290
	          "to": 10000000,
291
	          "key": "Tallest"
292
	        }]
293
	      }
294
	    }
295
	  },
296
	  "size": 10,
297
	  "from": 0
298
	}
299
	'
300
	 */
301
	public function testTextOneFilterAggregate() {
302
		$qg = new QueryGenerator();
303
		$qg->setQueryText('');
304
		$qg->setFields(null);
305
		$filters = array('ISO' => 400);
306
		$qg->setSelectedFilters($filters);
307
		$qg->setShowResultsForEmptyQuery(true);
308
		$qg->setQueryResultManipulator('FlickrPhotoTOElasticaSearchHelper');
309
		$aggs = $this->baseAggs();
310
311
		//FIXME - query needs removed in this case, leave as a reminder for now until
312
		//tests are complete
313
		$expected = array(
314
			'aggs' => $aggs,
315
			'size' => 10,
316
			'from' => 0,
317
			'query' => array(
318
				'filtered' => array(
319
					'filter' => array('term' => array('ISO' => 400))
320
				)
321
			),
322
			'sort' => array('TakenAt' => 'desc'),
323
			'suggest' => $this->getDefaultSuggest('')
324
		);
325
326
		echo(json_encode($qg->generateElasticaQuery()->toArray()));
327
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
328
329
		$qg->setQueryText('New Zealand');
330
		unset($expected['sort']); // use query text search relevance for sorting, ie default Elasticsearch
331
		$expected['query']['filtered']['query']['query_string'] = array('query' => 'New Zealand', 'lenient' => true);
332
		$expected['suggest'] = $this->getDefaultSuggest('New Zealand');
333
		echo(json_encode($qg->generateElasticaQuery()->toArray()));
334
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
335
	}
336
337
338
	public function testTextTwoFilterAggregate() {
339
		$qg = new QueryGenerator();
340
		$qg->setQueryText('');
341
		$qg->setFields(null);
342
		$filters = array('ISO' => 400, 'Aspect' => 'Square');
343
		$qg->setSelectedFilters($filters);
344
		$qg->setShowResultsForEmptyQuery(true);
345
		$qg->setQueryResultManipulator('FlickrPhotoTOElasticaSearchHelper');
346
		$aggs = $this->baseAggs();
347
348
		//FIXME - query needs removed in this case, leave as a reminder for now until
349
		//tests are complete
350
		$expected = array(
351
			'aggs' => $aggs,
352
			'size' => 10,
353
			'from' => 0,
354
			'query' => array(
355
				'filtered' => array(
356
					'filter' =>
357
					array('and' => array(
358
						0 => array( 'term' =>  array('ISO' => 400)),
359
						1 => array( 'range' => array(
360
							'AspectRatio' => array(
361
								'gte' => '0.9',
362
								'lt' => '1.2'
363
							)
364
						))
365
					)
366
				))
367
			),
368
			'sort' => array('TakenAt' => 'desc'),
369
			'suggest' => $this->getDefaultSuggest('')
370
		);
371
372
		echo(json_encode($qg->generateElasticaQuery()->toArray()));
373
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
374
375
		$qg->setQueryText('New Zealand');
376
		unset($expected['sort']); // use query text search relevance for sorting, ie default Elasticsearch
377
		$expected['query']['filtered']['query']['query_string'] = array('query' => 'New Zealand', 'lenient' => true);
378
		$expected['suggest'] = $this->getDefaultSuggest('New Zealand');
379
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
380
	}
381
382
383
	public function testTextThreeFilterAggregate() {
384
		$qg = new QueryGenerator();
385
		$qg->setQueryText('');
386
		$qg->setFields(null);
387
		$filters = array('ISO' => 400, 'Aspect' => 'Square', 'Aperture' => 5.6);
388
		$qg->setSelectedFilters($filters);
389
		$qg->setShowResultsForEmptyQuery(true);
390
		$qg->setQueryResultManipulator('FlickrPhotoTOElasticaSearchHelper');
391
		$aggs = $this->baseAggs();
392
393
		//FIXME - query needs removed in this case, leave as a reminder for now until
394
		//tests are complete
395
		$expected = array(
396
			'aggs' => $aggs,
397
			'size' => 10,
398
			'from' => 0,
399
			'query' => array(
400
				'filtered' => array('filter' =>
401
					array('and' => array(
402
						0 => array( 'term' =>  array('ISO' => 400)),
403
						1 => array( 'range' => array(
404
							'AspectRatio' => array(
405
								'gte' => '0.9',
406
								'lt' => '1.2'
407
							)
408
						)),
409
						2 => array( 'term' =>  array('Aperture' => 5.6)),
410
					)
411
				))
412
			),
413
			'sort' => array('TakenAt' => 'desc'),
414
			'suggest' => $this->getDefaultSuggest('')
415
		);
416
417
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
418
419
		$qg->setQueryText('New Zealand');
420
		unset($expected['sort']); // use query text search relevance for sorting, ie default Elasticsearch
421
		$expected['query']['filtered']['query']['query_string'] = array('query' => 'New Zealand', 'lenient' => true);
422
		$expected['suggest'] = $this->getDefaultSuggest('New Zealand');
423
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
424
	}
425
426
427
	public function testMultiMatchOneFilterAggregate() {
428
		$qg = new QueryGenerator();
429
		$qg->setQueryText('');
430
		$qg->setFields(array('Title' => 2, 'Content' => 1));
431
		$filters = array('ISO' => 400);
432
		$qg->setSelectedFilters($filters);
433
		$qg->setShowResultsForEmptyQuery(true);
434
		$qg->setQueryResultManipulator('FlickrPhotoTOElasticaSearchHelper');
435
		$aggs = $this->baseAggs();
436
437
		//FIXME - query needs removed in this case, leave as a reminder for now until
438
		//tests are complete
439
		$expected = array(
440
			'aggs' => $aggs,
441
			'size' => 10,
442
			'from' => 0,
443
			'query' => array(
444
				'filtered' => array(
445
					'filter' => array('term' => array('ISO' => 400))
446
				)
447
			),
448
			'sort' => array('TakenAt' => 'desc'),
449
			'suggest' => $this->getDefaultSuggest('')
450
		);
451
452
		echo(json_encode($qg->generateElasticaQuery()->toArray()));
453
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
454
455
		$qg->setQueryText('New Zealand');
456
		unset($expected['sort']); // use relevance of search sorting
457
458
		$expected['query']['filtered']['query']['multi_match'] = array(
459
			'query' => 'New Zealand',
460
			'lenient' => true,
461
			'fields' => array('Title^2', 'Title.*^2','Content', 'Content.*'),
462
			'type' => 'most_fields'
463
		);
464
465
		$expected['suggest'] = $this->getDefaultSuggest('New Zealand');
466
		print_r($qg->generateElasticaQuery()->toArray());
467
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
468
	}
469
470
471
472
	public function testMultiMatchTwoFilterAggregate() {
473
		$qg = new QueryGenerator();
474
		$qg->setQueryText('');
475
		$qg->setFields(array('Title' => 2, 'Content' => 1));
476
		$filters = array('ISO' => 400, 'Aspect' => 'Square');
477
		$qg->setSelectedFilters($filters);
478
		$qg->setShowResultsForEmptyQuery(true);
479
		$qg->setQueryResultManipulator('FlickrPhotoTOElasticaSearchHelper');
480
		$aggs = $this->baseAggs();
481
482
		//FIXME - query needs removed in this case, leave as a reminder for now until
483
		//tests are complete
484
		$expected = array(
485
			'aggs' => $aggs,
486
			'size' => 10,
487
			'from' => 0,
488
			'query' => array(
489
				'filtered' => array(
490
					'filter' =>
491
					array('and' => array(
492
						0 => array( 'term' =>  array('ISO' => 400)),
493
						1 => array( 'range' => array(
494
							'AspectRatio' => array(
495
								'gte' => '0.9',
496
								'lt' => '1.2'
497
							)
498
						))
499
					)
500
				))
501
			),
502
			'sort' => array('TakenAt' => 'desc'),
503
			'suggest' => $this->getDefaultSuggest('')
504
		);
505
506
		echo(json_encode($qg->generateElasticaQuery()->toArray()));
507
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
508
509
		$qg->setQueryText('New Zealand');
510
		unset($expected['sort']); // use relevance of search sorting
511
		$expected['query']['filtered']['query']['multi_match'] = array(
512
			'query' => 'New Zealand',
513
			'lenient' => true,
514
			'fields' => array('Title^2', 'Title.*^2','Content', 'Content.*'),
515
			'type' => 'most_fields'
516
		);
517
		$expected['suggest'] = $this->getDefaultSuggest('New Zealand');
518
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
519
	}
520
521
522
523
	public function testMultiMatchThreeFilterAggregate() {
524
		$qg = new QueryGenerator();
525
		$qg->setQueryText('');
526
		$qg->setFields(array('Title' => 2, 'Content' => 1));
527
		$filters = array('ISO' => 400, 'Aspect' => 'Square', 'Aperture' => 5.6);
528
		$qg->setSelectedFilters($filters);
529
		$qg->setShowResultsForEmptyQuery(true);
530
		$qg->setQueryResultManipulator('FlickrPhotoTOElasticaSearchHelper');
531
		$aggs = $this->baseAggs();
532
533
		//FIXME - query needs removed in this case, leave as a reminder for now until
534
		//tests are complete
535
		$expected = array(
536
			'aggs' => $aggs,
537
			'size' => 10,
538
			'from' => 0,
539
			'query' => array(
540
				'filtered' => array('filter' =>
541
					array('and' => array(
542
						0 => array( 'term' =>  array('ISO' => 400)),
543
						1 => array( 'range' => array(
544
							'AspectRatio' => array(
545
								'gte' => '0.9',
546
								'lt' => '1.2'
547
							)
548
						)),
549
						2 => array( 'term' =>  array('Aperture' => 5.6)),
550
					)
551
				))
552
			),
553
			'sort' => array('TakenAt' => 'desc'),
554
			'suggest' => $this->getDefaultSuggest('')
555
		);
556
557
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
558
559
		$qg->setQueryText('New Zealand');
560
		unset($expected['sort']);
561
		$expected['query']['filtered']['query']['multi_match'] = array(
562
			'query' => 'New Zealand',
563
			'lenient' => true,
564
			'fields' => array('Title^2', 'Title.*^2','Content', 'Content.*'),
565
			'type' => 'most_fields'
566
		);
567
		$expected['suggest'] = $this->getDefaultSuggest('New Zealand');
568
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
569
	}
570
571
572
573
574
	// ---- tests for field array to elasticsearch syntax
575 View Code Duplication
	public function testConvertWeightedFieldsForElasticaUnaryStrings() {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
576
		$qg = new QueryGenerator();
577
		$qg->setClasses('FlickrPhotoTO');
578
		$fields = array('Title' => 1, 'Description' => 1);
579
		$expected = array('Title', 'Title.*','Description', 'Description.*');
580
		$this->assertEquals($expected, $qg->convertWeightedFieldsForElastica($fields));
581
	}
582
583
584 View Code Duplication
	public function testConvertWeightedFieldsForElasticaMultipleStrings() {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
585
		$qg = new QueryGenerator();
586
		$qg->setClasses('FlickrPhotoTO');
587
		$fields = array('Title' => 2, 'Description' => 1);
588
		$expected = array('Title^2', 'Title.*^2','Description', 'Description.*');
589
		$this->assertEquals($expected, $qg->convertWeightedFieldsForElastica($fields));
590
	}
591
592
593
	public function testConvertWeightedFieldsForElasticaTestNonString() {
594
		$qg = new QueryGenerator();
595
		$qg->setClasses('FlickrPhotoTO');
596
		$fields = array('Aperture' => 2, 'FocalLength35mm' => 1);
597
		$expected = array('Aperture^2', 'FocalLength35mm');
598
		$this->assertEquals($expected, $qg->convertWeightedFieldsForElastica($fields));
599
	}
600
601
602
	public function testConvertWeightedFieldsForElasticaNonExistent() {
603
		$qg = new QueryGenerator();
604
		$qg->setClasses('FlickrPhotoTO');
605
		$fields = array('Aperture' => 2, 'FocalLength35mm' => 1, 'Wibble' => 2);
606
		try {
607
			$this->assertEquals('This test should fail', $qg->convertWeightedFieldsForElastica($fields));
608
			$this->fail('An exception should have been thrown as the field Wibble does not exist');
609
		} catch (Exception $e) {
610
			$this->assertEquals('Field Wibble does not exist', $e->getMessage());
611
		}
612
613
	}
614
615
616 View Code Duplication
	public function testSearchFieldsMappingForClasses() {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
617
		$qg = new QueryGenerator();
618
		$qg->setClasses('FlickrPhotoTO,Page');
619
		$fields = array('Title' => 2, 'Description' => 1);
620
		$expected = array('Title^2', 'Title.*^2','Description', 'Description.*');
621
		$this->assertEquals($expected, $qg->convertWeightedFieldsForElastica($fields));
622
623
		$qg->setClasses(array('FlickrPhotoTO','Page'));
0 ignored issues
show
Documentation introduced by
array('FlickrPhotoTO', 'Page') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

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...
624
		$this->assertEquals($expected, $qg->convertWeightedFieldsForElastica($fields));
625
626
	}
627
628
629
	public function testSearchFieldsMappingForClassesCaching() {
630
		QueryGenerator::resetCacheHitCounter();
631
		$cache = SS_Cache::factory('elasticsearch');
632
		// Previous tests may have altered this so start from a known position
633
		$cache->remove('SEARCHABLE_FIELDS_FlickrPhotoTO_Page');
634
		$qg = new QueryGenerator();
635
		$qg->setClasses('FlickrPhotoTO,Page');
636
		$fields = array('Title' => 2, 'Description' => 1);
637
		$expected = array('Title^2', 'Title.*^2','Description', 'Description.*');
638
		$this->assertEquals($expected, $qg->convertWeightedFieldsForElastica($fields));
639
640
		//Execute a 2nd time
641
		$this->assertEquals($expected, $qg->convertWeightedFieldsForElastica($fields));
642
		//Check for cache hit
643
		$this->assertEquals(1, QueryGenerator::getCacheHitCounter());
644
645
		//Execute a 3rd time
646
		$this->assertEquals($expected, $qg->convertWeightedFieldsForElastica($fields));
647
		//Check for cache hit
648
		$this->assertEquals(2, QueryGenerator::getCacheHitCounter());
649
	}
650
651
652 View Code Duplication
	public function testSearchFieldsMappingForSiteTree() {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
653
		$qg = new QueryGenerator();
654
		$qg->setClasses(null); // select all of site tree classes
655
		$fields = array('Title' => 2, 'Content' => 1);
656
		$expected = array('Title^2', 'Title.*^2','Content', 'Content.*');
657
		$this->assertEquals($expected, $qg->convertWeightedFieldsForElastica($fields));
658
659
		echo "--------------------\n";
660
		$qg->setClasses(array('FlickrPhotoTO','Page'));
0 ignored issues
show
Documentation introduced by
array('FlickrPhotoTO', 'Page') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

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...
661
		$this->assertEquals($expected, $qg->convertWeightedFieldsForElastica($fields));
662
663
	}
664
665
666
	public function testPagination() {
667
		$qg = new QueryGenerator();
668
		$qg->setQueryText('New Zealand');
669
		$qg->setFields(null);
670
		$qg->setSelectedFilters(null);
671
		$qg->setPageLength(12);
672
		$qg->setStart(24);
673
674
		//As the query is not empty it should not matter whether or not the show results for empty
675
		//query flag is set or not - test with true and false
676
677
		$qg->setShowResultsForEmptyQuery(false);
678
		$this->assertEquals(false, $qg->getShowResultsForEmptyQuery());
679
		$qs = array('query_string' => array('query' => 'New Zealand', 'lenient' => true));
680
		$expected = array(
681
			'query' => $qs,
682
			'size' => 12,
683
			'from' => 24,
684
			'suggest' => $this->getDefaultSuggest('New Zealand')
685
		);
686
687
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
688
689
		$qg->setShowResultsForEmptyQuery(true);
690
		$this->assertEquals(true, $qg->getShowResultsForEmptyQuery());
691
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
692
	}
693
694
695
	/**
696
	 * Get the basic aggregates that should be returned for the augmenter being tested
697
	 * @return array array of aggregations.  Tweak these in tests and assert as required.
698
	 */
699
	private function baseAggs() {
700
		$result = array();
701
		$result['Aperture'] = array(
702
			'terms' => array(
703
				'field' => 'Aperture',
704
				'size' => 0,
705
				'order' => array('_term' => 'asc')
706
			)
707
		);
708
		$result['ShutterSpeed'] =  array(
709
			'terms' => array(
710
				'field' => 'ShutterSpeed',
711
				'size' => 0,
712
				'order' => array('_term' => 'asc')
713
			)
714
		);
715
		$result['FocalLength35mm'] =  array(
716
			'terms' => array(
717
				'field' => 'FocalLength35mm',
718
				'size' => 0,
719
				'order' => array('_term' => 'asc')
720
			)
721
		);
722
		$result['ISO'] =  array(
723
			'terms' => array(
724
				'field' => 'ISO',
725
				'size' => 0,
726
				'order' => array('_term' => 'asc')
727
			)
728
		);
729
730
		$ranges = array();
731
		$ranges[0] = array('from' => '1.0E-7', 'to' => '0.3', 'key' => 'Panoramic');
732
		$ranges[1] = array('from' => '0.3', 'to' => '0.9', 'key' => 'Horizontal');
733
		$ranges[2] = array('from' => '0.9', 'to' => '1.2', 'key' => 'Square');
734
		$ranges[3] = array('from' => '1.2', 'to' => '1.79', 'key' => 'Vertical');
735
		$ranges[4] = array('from' => '1.79', 'to' => '10000000', 'key' => 'Tallest');
736
737
		$result['Aspect'] =  array(
738
			'range' => array(
739
				'field' => 'AspectRatio',
740
				'ranges' => $ranges
741
			)
742
		);
743
		return $result;
744
	}
745
746
747
		// ---- tests for the toQuotedCSV function ----
748
	public function testToQuotedCSVFromString() {
749
		$expected = "'Bangkok','Nonthaburi','Saraburi','Chiang Mai'";
750
		$items = 'Bangkok,Nonthaburi,Saraburi,Chiang Mai';
751
		$quoted = QueryGenerator::convertToQuotedCSV($items);
752
		$this->assertEquals($expected, $quoted);
753
	}
754
755
	public function testToQuotedCSVFromArray() {
756
		$expected = "'Bangkok','Nonthaburi','Saraburi','Chiang Mai'";
757
		$items = array('Bangkok','Nonthaburi','Saraburi','Chiang Mai');
758
		$quoted = QueryGenerator::convertToQuotedCSV($items);
759
		$this->assertEquals($expected, $quoted);
760
	}
761
762
	public function testToQuotedCSVEmptyString() {
763
		$quoted = QueryGenerator::convertToQuotedCSV('');
764
		$this->assertEquals('', $quoted);
765
	}
766
767
	public function testToQuotedCSVEmptyArray() {
768
		$quoted = QueryGenerator::convertToQuotedCSV(array());
769
		$this->assertEquals('', $quoted);
770
	}
771
772
	public function testToQuotedCSVNull() {
773
		$quoted = QueryGenerator::convertToQuotedCSV(null);
774
		$this->assertEquals('', $quoted);
775
	}
776
}
777