Completed
Push — dev2 ( 472000...b68a9c )
by Gordon
26:28 queued 11:32
created

ElasticaUtiTest::testOneIncorrectWordLowerCase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 11
loc 11
ccs 0
cts 11
cp 0
rs 9.4286
cc 1
eloc 9
nc 1
nop 0
crap 2
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));
1 ignored issue
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ElasticaUtilTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
14
	}
15
16
	public function testHumanReadableTrue() {
17
		$this->assertEquals('No', ElasticaUtil::showBooleanHumanReadable(false));
1 ignored issue
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ElasticaUtilTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
18
	}
19
20 View Code Duplication
	public function testPairOfConsecutiveIncorrectWords() {
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...
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);
1 ignored issue
show
Bug introduced by
Are you sure the assignment to $pair is correct as \SilverStripe\Elastica\E...etPhraseSuggestion($sa) (which targets SilverStripe\Elastica\El...::getPhraseSuggestion()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
26
		$expected = array(
27
			'suggestedQuery' => 'New Zealand railway',
28
			'suggestedQueryHighlighted' => 'New <strong class="hl">Zealand railway</strong>'
29
		);
30
		$this->assertEquals($expected, $pair);
1 ignored issue
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ElasticaUtilTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
31
	}
32
33
34 View Code Duplication
	public function testBooleanQuery() {
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...
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);
1 ignored issue
show
Bug introduced by
Are you sure the assignment to $pair is correct as \SilverStripe\Elastica\E...etPhraseSuggestion($sa) (which targets SilverStripe\Elastica\El...::getPhraseSuggestion()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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);
1 ignored issue
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ElasticaUtilTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
	}
45
46
47 View Code Duplication
	public function testOneIncorrectWord() {
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...
48
		$sa = $this->getSuggestionArray('New Zealind',
49
			'new zealand',
50
			'new <strong class="hl">zealand</strong>');
51
		$pair = ElasticaUtil::getPhraseSuggestion($sa);
1 ignored issue
show
Bug introduced by
Are you sure the assignment to $pair is correct as \SilverStripe\Elastica\E...etPhraseSuggestion($sa) (which targets SilverStripe\Elastica\El...::getPhraseSuggestion()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
52
		$expected = array(
53
			'suggestedQuery' => 'New Zealand',
54
			'suggestedQueryHighlighted' => 'New <strong class="hl">Zealand</strong>'
55
		);
56
		$this->assertEquals($expected, $pair);
1 ignored issue
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ElasticaUtilTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
	}
58
59
60 View Code Duplication
	public function testOneIncorrectWordLowerCase() {
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...
61
		$sa = $this->getSuggestionArray('new zealind',
62
			'new zealand',
63
			'new <strong class="hl">zealand</strong>');
64
		$pair = ElasticaUtil::getPhraseSuggestion($sa);
1 ignored issue
show
Bug introduced by
Are you sure the assignment to $pair is correct as \SilverStripe\Elastica\E...etPhraseSuggestion($sa) (which targets SilverStripe\Elastica\El...::getPhraseSuggestion()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
65
		$expected = array(
66
			'suggestedQuery' => 'new zealand',
67
			'suggestedQueryHighlighted' => 'new <strong class="hl">zealand</strong>'
68
		);
69
		$this->assertEquals($expected, $pair);
1 ignored issue
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ElasticaUtilTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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
84
		$expected = array();
0 ignored issues
show
Unused Code introduced by
$expected 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...
85
86
		$pair = ElasticaUtil::getPhraseSuggestion($sa);
1 ignored issue
show
Bug introduced by
Are you sure the assignment to $pair is correct as \SilverStripe\Elastica\E...etPhraseSuggestion($sa) (which targets SilverStripe\Elastica\El...::getPhraseSuggestion()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
87
		$this->assertEquals(null, $pair);
1 ignored issue
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ElasticaUtilTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
	}
89
90
91
	/**
92
	 * Test parsing no terms suggested
93
	 */
94
	public function testParseExplanationNoTerms() {
95
		#FIXME Possibly flickrphotoTO
96
		$explanation = "() -ConstantScore(_uid:FlickrPhoto#7369)";
97
		$expected = array();
98
		$terms = ElasticaUtil::parseSuggestionExplanation($explanation);
99
		$this->assertEquals($expected, $terms);
1 ignored issue
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ElasticaUtilTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
100
	}
101
102
103
	public function testParseExplanationDoubleStartingBracket() {
104
		$explanation = "((Title.standard:wellington Title.standard:view Description.standard:new ";
105
		$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)";
106
		$terms = ElasticaUtil::parseSuggestionExplanation($explanation);
107
108
		$termsDescription = $terms['Description.standard'];
109
		sort($termsDescription);
110
111
		$termsTitle = $terms['Title.standard'];
112
		sort($termsTitle);
113
114
		$expected = array(
115
			'Title.standard' => $termsTitle,
116
			'Description.standard' => $termsDescription
117
		);
118
119
		$terms = array(
120
			'Title.standard' => array('view', 'wellington'),
121
			'Description.standard' => array(
122
				'aerial',
123
				'buildings',
124
				'exhibition',
125
				'including',
126
				'new',
127
				'view',
128
				'wellington',
129
				'zealand'
130
			)
131
		);
132
		$this->assertEquals($expected, $terms);
1 ignored issue
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ElasticaUtilTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
133
	}
134
135
136
137
	public function testParseExplanationNoLeadingBrackets() {
138
		$explanation = "Title.standard:new -ConstantScore(_uid:FlickrPhotoTO#76)";
139
		$terms = ElasticaUtil::parseSuggestionExplanation($explanation);
140
		$expected = array('Title.standard' => array('new'));
141
		$this->assertEquals($expected, $terms);
1 ignored issue
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ElasticaUtilTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
142
	}
143
144
145
	public function testParseExplanationBracketsShowing() {
146
		$explanation = '(Description.standard:bay Description.standard:tram Description.standard:manners) -ConstantScore(_uid:FlickrPhoto#4645)';
147
		$terms = ElasticaUtil::parseSuggestionExplanation($explanation);
148
		$expected = array('Description.standard' => array('bay', 'tram', 'manners'));
149
		$this->assertEquals($expected, $terms);
1 ignored issue
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ElasticaUtilTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
150
	}
151
152
153
	/**
154
	 * Simulate a call to Elastica to get suggestions for a given phrase
155
	 * @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...
156
	 */
157
	private function getSuggestionArray($phrase, $suggestion, $highlightedSuggestion) {
158
		$result = array();
159
		$suggest1 = array();
160
		$suggest1['text'] = $phrase;
161
		$suggest1['offset'] = 0;
162
		$suggest1['length'] = strlen($phrase);
163
		$options = array();
164
		$option0 = array();
165
		$option0['text'] = $suggestion;
166
		$option0['highlighted'] = $highlightedSuggestion;
167
168
		//For completeness, currently not used
169
		$option0['score'] = 9.0792E-5;
170
171
		$options[0] = $option0;
172
		$suggest1['options'] = $options;
173
		array_push($result, $suggest1);
174
		return $result;
175
	}
176
}
177