Completed
Push — dev2 ( 764ed3...9e10b9 )
by Gordon
26:05 queued 17:34
created

ElasticaServiceTest::testListIndexes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4286
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
3
use SilverStripe\Elastica\ElasticaService;
4
use SilverStripe\Elastica\ReindexTask;
5
use SilverStripe\Elastica\DeleteIndexTask;
6
use SilverStripe\Elastica\Searchable;
7
8
/**
9
 * Test the functionality ElasticaService class
10
 * @package elastica
11
 */
12
class ElasticaServiceTest extends ElasticsearchBaseTest {
13
14
	public static $fixture_file = 'elastica/tests/lotsOfPhotos.yml';
15
16
17
	public function setup() {
18
		parent::setup();
19
		$this->service->setTestMode(true);
20
	}
21
22
23
	public function testCreateIndexInvalidLocale() {
24
		// fake locale
25
		$this->service->setLocale('sw_NZ');
26
		try {
27
			$this->invokeMethod($this->service, 'createIndex', array());
28
			$this->assertFalse(true, "Creation of index with unknown locale should have failed");
29
		} catch (Exception $e) {
30
			$this->assertTrue(true,"Creation of index with unknown locale failed as expected");
31
		}
32
33
34
	}
35
36
37
	public function testEnsureMapping() {
38
		$index = $this->service->getIndex();
39
		$mapping = $index->getMapping();
40
		$type = $index->getType('FlickrPhotoTO');
41
		$record = FlickrPhotoTO::get()->first();
42
		$mappingBefore = $this->invokeMethod($this->service, 'ensureMapping', array($type, $record));
43
44
		$this->assertEquals($mapping['FlickrPhotoTO'], $mappingBefore['FlickrPhotoTO']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ElasticaServiceTest>.

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...
45
46
		// Delete the index
47
		$task = new DeleteIndexTask($this->service);
48
		$task->run(null);
49
		$mappingAfter = $this->invokeMethod($this->service, 'ensureMapping', array($type, $record));
50
		$this->assertEquals($mappingBefore, $mappingAfter);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ElasticaServiceTest>.

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...
51
	}
52
53
54
	public function testEnsureIndex() {
55
		// Check that an index currently exists, it will from setup method
56
		$this->assertTrue($this->service->getIndex()->exists());
57
58
		// Ensure the index exists when it already exists case
59
		$this->invokeMethod($this->service, 'ensureIndex', array());
60
		$this->assertTrue($this->service->getIndex()->exists());
61
62
		// Delete and assert that it does not exist
63
		$this->service->getIndex()->delete();
64
		$this->assertFalse($this->service->getIndex()->exists());
65
66
		// Ensure the index exists when it does not exist case
67
		$this->invokeMethod($this->service, 'ensureIndex', array());
68
		$this->assertTrue($this->service->getIndex()->exists());
69
	}
70
71
72
	public function testShowInSearch() {
73
		$nDocsAtStart = $this->getNumberOfIndexedDocuments();
74
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
75
76
		$fp = Page::get()->first();
77
		$fp->ShowInSearch = false;
78
		$fp->write();
79
		$this->service->getIndex()->refresh();
80
81
		$this->checkNumberOfIndexedDocuments($nDocsAtStart-1);
82
83
		$fp->ShowInSearch = true;
84
		$fp->write();
85
		$this->service->getIndex()->refresh();
86
87
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
88
	}
89
90
91 View Code Duplication
	public function testUnpublish() {
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...
92
		$nDocsAtStart = $this->getNumberOfIndexedDocuments();
93
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
94
95
		$fp = Page::get()->first();
96
		$fp->doUnpublish();
97
98
		$this->service->getIndex()->refresh();
99
		$this->checkNumberOfIndexedDocuments($nDocsAtStart-1);
100
	}
101
102
103 View Code Duplication
	public function testDeleteFromSiteTree() {
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...
104
		$nDocsAtStart = $this->getNumberOfIndexedDocuments();
105
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
106
107
		$fp = Page::get()->first();
108
		$fp->delete();
109
110
		$this->service->getIndex()->refresh();
111
		$this->checkNumberOfIndexedDocuments($nDocsAtStart-1);
112
	}
113
114
115 View Code Duplication
	public function testDeleteDataObject() {
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...
116
		$nDocsAtStart = $this->getNumberOfIndexedDocuments();
117
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
118
119
		$fp = FlickrPhotoTO::get()->first();
120
		$fp->delete();
121
122
		$this->service->getIndex()->refresh();
123
		$this->checkNumberOfIndexedDocuments($nDocsAtStart-1);
124
	}
125
126
127
	public function testBulkIndexing() {
128
		//Reset the index, so that nothing has been indexed
129
		$this->service->reset();
130
131
		//Number of requests indexing wise made to Elasticsearch server
132
		$reqs = $this->service->getIndexingRequestCtr();
133
134
		$task = new ReindexTask($this->service);
135
136
		// null request is fine as no parameters used
137
		$task->run(null);
138
139
		//Check that the number of indexing requests has increased by 2
140
		$deltaReqs = $this->service->getIndexingRequestCtr() - $reqs;
141
		//One call is made for each of Page and FlickrPhotoTO
142
		$this->assertEquals(2,$deltaReqs);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ElasticaServiceTest>.

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...
143
144
		// default installed pages plus 100 FlickrPhotoTOs
145
		$this->checkNumberOfIndexedDocuments(103);
146
	}
147
148
	public function testNonBulkIndexing() {
149
		//Number of requests indexing wise made to Elasticsearch server
150
		$reqs = $this->service->getIndexingRequestCtr();
151
		$nDocsAtStart = $this->getNumberOfIndexedDocuments();
152
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
153
154
		//Check index size after each document indexed
155
		$fp = new FlickrPhotoTO();
156
		$fp->Title = 'The cat sits on the mat';
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<FlickrPhotoTO>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
157
		$fp->write();
158
		$this->checkNumberOfIndexedDocuments($nDocsAtStart+1);
159
160
		$fp2 = new FlickrPhotoTO();
161
		$fp2->Title = 'The cat sat on the hat';
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<FlickrPhotoTO>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
162
		$fp2->write();
163
		$this->checkNumberOfIndexedDocuments($nDocsAtStart+2);
164
165
		$fp3 = new FlickrPhotoTO();
166
		$fp3->Title = 'The bat flew around the cat';
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<FlickrPhotoTO>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
167
		$fp3->write();
168
		$this->checkNumberOfIndexedDocuments($nDocsAtStart+3);
169
170
		//Check that the number of indexing requests has increased by 3
171
		$deltaReqs = $this->service->getIndexingRequestCtr() - $reqs;
172
		$this->assertEquals(3,$deltaReqs);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ElasticaServiceTest>.

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...
173
	}
174
175
176
177
	/*
178
	Check for classes that should be there.   Others will possibly exist depending on
179
	which other modules are installed, hence no array comparison
180
	 */
181
	public function testGetIndexedClasses() {
182
		$this->service->setTestMode(false);
183
184
		$indexedClasses = $this->service->getIndexedClasses();
185
186
		// Just the non testable classes
187
		$this->assertContains('Page', $indexedClasses);
188
		$this->assertContains('SiteTree', $indexedClasses);
189
190
191
		// Get all classes including the test ones
192
		$this->service->setTestMode(true);
193
		$indexedClasses = $this->service->getIndexedClasses();
194
		$this->assertContains('Page', $indexedClasses);
195
		$this->assertContains('SiteTree', $indexedClasses);
196
		$this->assertContains('FlickrPhotoTO', $indexedClasses);
197
		$this->assertContains('FlickrSetTO', $indexedClasses);
198
		$this->assertContains('FlickrTagTO', $indexedClasses);
199
		$this->assertContains('SearchableTestPage', $indexedClasses);
200
201
	}
202
203
204
	/*
205
	-------------------------------
206
	FIXME: The following tests are problematic in that I'm not sure exactly what to check for
207
	to differentiate between deleted, reset, recreated etc.  It seems that checking an index
208
	status recreates a virgin copy of the index
209
	-------------------------------
210
	 */
211
212
	public function testResetIndex() {
213
		$index = $this->service->getIndex();
0 ignored issues
show
Unused Code introduced by
$index 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...
214
		$nDocsAtStart = $this->getNumberOfIndexedDocuments();
215
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
216
		$this->service->reset();
217
		$this->checkNumberOfIndexedDocuments(-1);
218
	}
219
220
221
	public function testDeleteIndex() {
222
		$index = $this->service->getIndex();
223
		$this->assertTrue($index->exists());
224
225
		// Check the number of documents
226
		$nDocsAtStart = $this->getNumberOfIndexedDocuments();
227
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
228
229
230
		// Delete the index
231
		$task = new DeleteIndexTask($this->service);
232
233
		//null request is fine as no parameters used
234
		$task->run(null);
235
236
		$this->checkNumberOfIndexedDocuments(-1);
237
238
		//FIXME better options for testing here?
239
	}
240
241
242
	public function testListIndexes() {
243
		$message = 'This is a test trace';
244
		$trace = $this->service->listIndexes($message);
245
		$this->assertContains('elastica_ss_module_test_en_us', print_r($trace,1));
246
	}
247
}
248