AutocompleteControllerTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 22
ccs 0
cts 14
cp 0
rs 9.2
cc 1
eloc 13
nc 1
nop 0
crap 2
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
	public function setUp() {
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