Skip to content

Commit f7a7c9c

Browse files
author
Martynas Sudintas
committed
added unit tests for connection
1 parent 53ef50b commit f7a7c9c

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

Tests/Unit/Client/ConnectionTest.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,124 @@ public function testUpdateSettingsForce()
209209
$this->assertEquals('foo', $connection->getIndexName(), 'Index name is not correct.');
210210
}
211211

212+
/**
213+
* Data provider for testing setting multiple mapping.
214+
*
215+
* @return array
216+
*/
217+
public function getTestSetMultipleMappingData()
218+
{
219+
return [
220+
// Case #0: no cleanup.
221+
[
222+
[
223+
'type1' => [
224+
'properties' => [],
225+
],
226+
'type2' => [
227+
'properties' => [],
228+
],
229+
],
230+
['type1', 'type2', 'oldType1'],
231+
false,
232+
],
233+
// Case #1: with cleanup.
234+
[
235+
[
236+
'type1' => [
237+
'properties' => [],
238+
],
239+
],
240+
['type1'],
241+
true,
242+
],
243+
];
244+
}
245+
246+
/**
247+
* Tests setting multiple mapping.
248+
*
249+
* @param array $mapping
250+
* @param array $expectedTypes
251+
* @param bool $cleanUp
252+
*
253+
* @dataProvider getTestSetMultipleMappingData
254+
*/
255+
public function testSetMultipleMapping($mapping, $expectedTypes, $cleanUp)
256+
{
257+
$connection = new Connection(
258+
$this->getClient(),
259+
[
260+
'body' => [
261+
'mappings' => [
262+
'oldType1' => [
263+
'properties' => [],
264+
],
265+
],
266+
],
267+
]
268+
);
269+
$connection->setMultipleMapping($mapping, $cleanUp);
270+
271+
foreach ($expectedTypes as $expectedType) {
272+
$map = $connection->getMapping($expectedType);
273+
$this->assertArrayHasKey('properties', $map);
274+
}
275+
}
276+
277+
/**
278+
* Tests getMappingFromIndex method when returns empty array.
279+
*/
280+
public function testGetMappingFromIndex()
281+
{
282+
$indices = $this
283+
->getMockBuilder('Elasticsearch\Namespaces\IndicesNamespace')
284+
->disableOriginalConstructor()
285+
->getMock();
286+
$indices
287+
->expects($this->once())
288+
->method('getMapping')
289+
->with(['index' => 'foo'])
290+
->will($this->returnValue(['baz' => []]));
291+
292+
$client = $this
293+
->getMockBuilder('Elasticsearch\Client')
294+
->disableOriginalConstructor()
295+
->getMock();
296+
$client
297+
->expects($this->any())
298+
->method('indices')
299+
->will($this->returnValue($indices));
300+
301+
$connection = new Connection($client, ['index' => 'foo']);
302+
$this->assertEmpty($connection->getMappingFromIndex());
303+
}
304+
305+
/**
306+
* Tests if exception is thrown with undefined action.
307+
*
308+
* @expectedException \LogicException
309+
* @expectedExceptionMessage Unknown warmers action
310+
*/
311+
public function testWarmersActionException()
312+
{
313+
$warmerMock = $this->getMock('ONGR\ElasticsearchBundle\Cache\WarmerInterface');
314+
$warmerMock
315+
->expects($this->once())
316+
->method('warmUp');
317+
$warmerMock
318+
->expects($this->once())
319+
->method('getName');
320+
321+
$connection = new Connection($this->getClient(), []);
322+
$connection->addWarmer($warmerMock);
323+
324+
$object = new \ReflectionObject($connection);
325+
$method = $object->getMethod('warmersAction');
326+
$method->setAccessible(true);
327+
$method->invokeArgs($connection, ['undefined']);
328+
}
329+
212330
/**
213331
* Returns client instance with indices namespace set.
214332
*

0 commit comments

Comments
 (0)