Completed
Push — dev2 ( 76a09a...1cd385 )
by Gordon
14:51
created

AutocompleteControllerTest::testSiteTree()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 52
Code Lines 36

Duplication

Lines 52
Ratio 100 %
Metric Value
dl 52
loc 52
rs 9.493
cc 2
eloc 36
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
use SilverStripe\Elastica\ReindexTask;
4
5
/**
6
 * @package comments
7
 */
8
class AutocompleteControllerTest extends ElasticsearchFunctionalTestBase {
9
10
	public static $fixture_file = 'elastica/tests/lotsOfPhotos.yml';
11
12
13 View Code Duplication
	public function setup() {
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...
14
		parent::setup();
15
		$config = Config::inst()->get('FlickrPhotoTO', 'searchable_fields');
0 ignored issues
show
Unused Code introduced by
$config 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...
16
17
		\Config::inst()->update('FlickrPhotoTO', 'searchable_autocomplete', array('Title'));
18
19
20
		// Delete and assert that it does not exist
21
		$sql =  "SELECT ID,Name,ClazzName from SearchableField";
22
		$records = DB::query($sql);
0 ignored issues
show
Unused Code introduced by
$records 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...
23
24
		$filter = array('Name' => 'Title', 'ClazzName' => 'FlickrPhotoTO');
25
		$sf = SearchableField::get()->filter($filter)->first();
26
		$sql = "UPDATE ElasticSearchPage_ElasticaSearchableFields SET Searchable=1,".
27
				"EnableAutocomplete=1 where SearchableFieldID=".$sf->ID;
28
29
		DB::query($sql);
30
31
		$task = new ReindexTask($this->service);
32
		// null request is fine as no parameters used
33
		$task->run(null);
34
	}
35
36
37
38
	public function testDataObject() {
39
		$url = 'autocomplete/search?field=Title&classes=FlickrPhotoTO&query=the';
40
		$response = $this->get($url);
41
		$this->assertEquals(200, $response->getStatusCode());
42
		$body = $response->getBody();
43
44
		$result = json_decode($body);
45
46
		$this->assertEquals('the', $result->Query);
47
		$lquery = strtolower($result->Query);
48
		foreach ($result->suggestions as $suggestion) {
49
			$value = $suggestion->value;
50
			$value = strtolower($value);
51
			$this->assertContains($lquery, $value);
52
		}
53
54
		// make sure there were actually some results
55
		$this->assertEquals(10, sizeof($result->suggestions));
56
57
58
		//search for different capitlisation, should produce the same result
59
		$url = 'autocomplete/search?field=Title&classes=FlickrPhotoTO&query=ThE';
60
		$response = $this->get($url);
61
		$this->assertEquals(200, $response->getStatusCode());
62
		$body = $response->getBody();
63
		$result2 = json_decode($body);
64
		$this->assertEquals($result->suggestions, $result2->suggestions);
65
66
		//append a space should produce the same result
67
		$url = 'autocomplete/search?field=Title&classes=FlickrPhotoTO&query=ThE%20';
68
		$response = $this->get($url);
69
		$this->assertEquals(200, $response->getStatusCode());
70
		$body = $response->getBody();
71
		$result3 = json_decode($body);
72
		$this->assertEquals($result2->suggestions, $result3->suggestions);
73
74
75
		// test a non existent class, for now return blanks so as to avoid extra overhead as this
76
		// method is called often
77
		$url = 'autocomplete/search?field=FieldThatDoesNotExist&classes=FlickrPhotoTO&query=the';
78
		$response = $this->get($url);
79
		$this->assertEquals(200, $response->getStatusCode());
80
		$body = $response->getBody();
81
		$result4 = json_decode($body);
82
		$this->assertEquals(0, sizeof($result4->suggestions));
83
	}
84
}
85