Skip to content

Commit e6adf50

Browse files
committed
Cover changes with unit test.
1 parent 2c46bd5 commit e6adf50

File tree

2 files changed

+130
-1
lines changed

2 files changed

+130
-1
lines changed

app/code/Magento/Elasticsearch/Test/Unit/Elasticsearch5/Model/Client/ElasticsearchTest.php

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Elasticsearch\Test\Unit\Elasticsearch5\Model\Client;
78

9+
use Magento\Elasticsearch\Elasticsearch5\Model\Client\Elasticsearch;
810
use Magento\Elasticsearch\Model\Client\Elasticsearch as ElasticsearchClient;
911
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1012

@@ -38,7 +40,7 @@ class ElasticsearchTest extends \PHPUnit\Framework\TestCase
3840
*
3941
* @return void
4042
*/
41-
protected function setUp()
43+
protected function setUp(): void
4244
{
4345
$this->elasticsearchClientMock = $this->getMockBuilder(\Elasticsearch\Client::class)
4446
->setMethods(
@@ -497,6 +499,40 @@ public function testDeleteMapping()
497499
);
498500
}
499501

502+
/**
503+
* Ensure that configuration returns correct url.
504+
*
505+
* @param array $options
506+
* @param string $expectedResult
507+
* @throws \Magento\Framework\Exception\LocalizedException
508+
* @throws \ReflectionException
509+
* @dataProvider getOptionsDataProvider
510+
*/
511+
public function testBuildConfig(array $options, $expectedResult): void
512+
{
513+
$buildConfig = new Elasticsearch($options);
514+
$config = $this->getPrivateMethod(Elasticsearch::class, 'buildConfig');
515+
$result = $config->invoke($buildConfig, $options);
516+
$this->assertEquals($expectedResult, $result['hosts'][0]);
517+
}
518+
519+
/**
520+
* Return private method for elastic search class.
521+
*
522+
* @param $className
523+
* @param $methodName
524+
* @return \ReflectionMethod
525+
* @throws \ReflectionException
526+
*/
527+
private function getPrivateMethod($className, $methodName)
528+
{
529+
$reflector = new \ReflectionClass($className);
530+
$method = $reflector->getMethod($methodName);
531+
$method->setAccessible(true);
532+
533+
return $method;
534+
}
535+
500536
/**
501537
* Test deleteMapping() method
502538
* @expectedException \Exception
@@ -545,6 +581,35 @@ public function testSuggest()
545581
$this->assertEquals([], $this->model->suggest($query));
546582
}
547583

584+
/**
585+
* Get options data provider.
586+
*/
587+
public function getOptionsDataProvider()
588+
{
589+
return [
590+
[
591+
'without_protocol' => [
592+
'hostname' => 'localhost',
593+
'port' => '9200',
594+
'timeout' => 15,
595+
'index' => 'magento2',
596+
'enableAuth' => 0,
597+
],
598+
'expected_result' => 'http://localhost:9200'
599+
],
600+
[
601+
'with_protocol' => [
602+
'hostname' => 'https://localhost',
603+
'port' => '9200',
604+
'timeout' => 15,
605+
'index' => 'magento2',
606+
'enableAuth' => 0,
607+
],
608+
'expected_result' => 'https://localhost:9200'
609+
]
610+
];
611+
}
612+
548613
/**
549614
* Get elasticsearch client options
550615
*

app/code/Magento/Elasticsearch6/Test/Unit/Model/Client/ElasticsearchTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Elasticsearch6\Test\Unit\Model\Client;
78

89
use Magento\Elasticsearch\Model\Client\Elasticsearch as ElasticsearchClient;
@@ -119,6 +120,69 @@ public function testConstructorWithOptions()
119120
$this->assertNotNull($result);
120121
}
121122

123+
/**
124+
* Ensure that configuration returns correct url.
125+
*
126+
* @param array $options
127+
* @param string $expectedResult
128+
* @throws \Magento\Framework\Exception\LocalizedException
129+
* @throws \ReflectionException
130+
* @dataProvider getOptionsDataProvider
131+
*/
132+
public function testBuildConfig(array $options, $expectedResult): void
133+
{
134+
$buildConfig = new Elasticsearch($options);
135+
$config = $this->getPrivateMethod(Elasticsearch::class, 'buildConfig');
136+
$result = $config->invoke($buildConfig, $options);
137+
$this->assertEquals($expectedResult, $result['hosts'][0]);
138+
}
139+
140+
/**
141+
* Return private method for elastic search class.
142+
*
143+
* @param $className
144+
* @param $methodName
145+
* @return \ReflectionMethod
146+
* @throws \ReflectionException
147+
*/
148+
private function getPrivateMethod($className, $methodName)
149+
{
150+
$reflector = new \ReflectionClass($className);
151+
$method = $reflector->getMethod($methodName);
152+
$method->setAccessible(true);
153+
154+
return $method;
155+
}
156+
157+
/**
158+
* Get options data provider.
159+
*/
160+
public function getOptionsDataProvider()
161+
{
162+
return [
163+
[
164+
'without_protocol' => [
165+
'hostname' => 'localhost',
166+
'port' => '9200',
167+
'timeout' => 15,
168+
'index' => 'magento2',
169+
'enableAuth' => 0,
170+
],
171+
'expected_result' => 'http://localhost:9200'
172+
],
173+
[
174+
'with_protocol' => [
175+
'hostname' => 'https://localhost',
176+
'port' => '9200',
177+
'timeout' => 15,
178+
'index' => 'magento2',
179+
'enableAuth' => 0,
180+
],
181+
'expected_result' => 'https://localhost:9200'
182+
]
183+
];
184+
}
185+
122186
/**
123187
* Test ping functionality
124188
*/

0 commit comments

Comments
 (0)