ElasticaUtilTest   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 164
Duplicated Lines 26.83 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%
Metric Value
wmc 12
lcom 1
cbo 2
dl 44
loc 164
ccs 0
cts 122
cp 0
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A testHumanReadableFalse() 0 3 1
A testHumanReadableTrue() 0 3 1
A testBooleanQuery() 11 11 1
A testOneIncorrectWord() 11 11 1
A testOneIncorrectWordLowerCase() 11 11 1
A testPairOfConsecutiveIncorrectWords() 11 12 1
A testNoSuggestionsRequired() 0 12 1
A testParseExplanationNoTerms() 0 7 1
B testParseExplanationDoubleStartingBracket() 0 31 1
A testParseExplanationNoLeadingBrackets() 0 6 1
A testParseExplanationBracketsShowing() 0 6 1
A getSuggestionArray() 0 19 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
use SilverStripe\Elastica\ElasticaUtil;
4
5
6
/**
7
 * Test the functionality of ElasticaUtil class
8
 * @package elastica
9
 */
10
class ElasticaUtilTest extends ElasticsearchBaseTest {
11
12
	public function testHumanReadableFalse() {
13
		$this->assertEquals('No', ElasticaUtil::showBooleanHumanReadable(false));
14
	}
15
16
	public function testHumanReadableTrue() {
17
		$this->assertEquals('No', ElasticaUtil::showBooleanHumanReadable(false));
18
	}
19
20 View Code Duplication
	public function testPairOfConsecutiveIncorrectWords() {
21
		$sa = $this->getSuggestionArray('New Zealind raalway',
22
			'new zealand railway',
23
			'new <strong class="hl">zealand railway</strong>');
24
25
		$pair = ElasticaUtil::getPhraseSuggestion($sa);
26
		$expected = array(
27
			'suggestedQuery' => 'New Zealand railway',
28
			'suggestedQueryHighlighted' => 'New <strong class="hl">Zealand railway</strong>'
29
		);
30
		$this->assertEquals($expected, $pair);
31
	}
32
33
34 View Code Duplication
	public function testBooleanQuery() {
35
		$sa = $this->getSuggestionArray('New Zealind AND sheep',
36
			'new zealand and sheep',
37
			'new <strong class="hl">zealand</strong> and sheep');
38
		$pair = ElasticaUtil::getPhraseSuggestion($sa);
39
		$expected = array(
40
			'suggestedQuery' => 'New Zealand AND sheep',
41
			'suggestedQueryHighlighted' => 'New <strong class="hl">Zealand</strong> AND sheep'
42
		);
43
		$this->assertEquals($expected, $pair);
44
	}
45
46
47 View Code Duplication
	public function testOneIncorrectWord() {
48
		$sa = $this->getSuggestionArray('New Zealind',
49
			'new zealand',
50
			'new <strong class="hl">zealand</strong>');
51
		$pair = ElasticaUtil::getPhraseSuggestion($sa);
52
		$expected = array(
53
			'suggestedQuery' => 'New Zealand',
54
			'suggestedQueryHighlighted' => 'New <strong class="hl">Zealand</strong>'
55
		);
56
		$this->assertEquals($expected, $pair);
57
	}
58
59
60 View Code Duplication
	public function testOneIncorrectWordLowerCase() {
61
		$sa = $this->getSuggestionArray('new zealind',
62
			'new zealand',
63
			'new <strong class="hl">zealand</strong>');
64
		$pair = ElasticaUtil::getPhraseSuggestion($sa);
65
		$expected = array(
66
			'suggestedQuery' => 'new zealand',
67
			'suggestedQueryHighlighted' => 'new <strong class="hl">zealand</strong>'
68
		);
69
		$this->assertEquals($expected, $pair);
70
	}
71
72
73
74
	public function testNoSuggestionsRequired() {
75
		$suggestion = array(
76
			'text' => 'New Zealand',
77
			'offset' => 0,
78
			'length' => 11,
79
			'options' => array()
80
		);
81
		$sa = array();
82
		array_push($sa, $suggestion);
83
		$pair = ElasticaUtil::getPhraseSuggestion($sa);
84
		$this->assertEquals(null, $pair);
85
	}
86
87
88
	/**
89
	 * Test parsing no terms suggested
90
	 */
91
	public function testParseExplanationNoTerms() {
92
		#FIXME Possibly flickrphotoTO
93
		$explanation = "() -ConstantScore(_uid:FlickrPhoto#7369)";
94
		$expected = array();
95
		$terms = ElasticaUtil::parseSuggestionExplanation($explanation);
96
		$this->assertEquals($expected, $terms);
97
	}
98
99
100
	public function testParseExplanationDoubleStartingBracket() {
101
		$explanation = "((Title.standard:wellington Title.standard:view Description.standard:new ";
102
		$explanation .= "Description.standard:zealand Description.standard:wellington Description.standard:view Description.standard:including Description.standard:buildings Description.standard:exhibition Description.standard:aerial)~3) -ConstantScore(_uid:FlickrPhoto#3079)";
103
		$terms = ElasticaUtil::parseSuggestionExplanation($explanation);
104
105
		$termsDescription = $terms['Description.standard'];
106
		sort($termsDescription);
107
108
		$termsTitle = $terms['Title.standard'];
109
		sort($termsTitle);
110
111
		$expected = array(
112
			'Title.standard' => $termsTitle,
113
			'Description.standard' => $termsDescription
114
		);
115
116
		$terms = array(
117
			'Title.standard' => array('view', 'wellington'),
118
			'Description.standard' => array(
119
				'aerial',
120
				'buildings',
121
				'exhibition',
122
				'including',
123
				'new',
124
				'view',
125
				'wellington',
126
				'zealand'
127
			)
128
		);
129
		$this->assertEquals($expected, $terms);
130
	}
131
132
133
134
	public function testParseExplanationNoLeadingBrackets() {
135
		$explanation = "Title.standard:new -ConstantScore(_uid:FlickrPhotoTO#76)";
136
		$terms = ElasticaUtil::parseSuggestionExplanation($explanation);
137
		$expected = array('Title.standard' => array('new'));
138
		$this->assertEquals($expected, $terms);
139
	}
140
141
142
	public function testParseExplanationBracketsShowing() {
143
		$explanation = '(Description.standard:bay Description.standard:tram Description.standard:manners) -ConstantScore(_uid:FlickrPhoto#4645)';
144
		$terms = ElasticaUtil::parseSuggestionExplanation($explanation);
145
		$expected = array('Description.standard' => array('bay', 'tram', 'manners'));
146
		$this->assertEquals($expected, $terms);
147
	}
148
149
150
	/**
151
	 * Simulate a call to Elastica to get suggestions for a given phrase
152
	 * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
153
	 */
154
	private function getSuggestionArray($phrase, $suggestion, $highlightedSuggestion) {
155
		$result = array();
156
		$suggest1 = array();
157
		$suggest1['text'] = $phrase;
158
		$suggest1['offset'] = 0;
159
		$suggest1['length'] = strlen($phrase);
160
		$options = array();
161
		$option0 = array();
162
		$option0['text'] = $suggestion;
163
		$option0['highlighted'] = $highlightedSuggestion;
164
165
		//For completeness, currently not used
166
		$option0['score'] = 9.0792E-5;
167
168
		$options[0] = $option0;
169
		$suggest1['options'] = $options;
170
		array_push($result, $suggest1);
171
		return $result;
172
	}
173
}
174