Skip to content

Commit 88bf1a8

Browse files
oshmyheliukshiftedreality
authored andcommitted
MAGECLOUD-3470: Add validator for SearchEngine config integrity (#449)
1 parent 9cb4b55 commit 88bf1a8

File tree

3 files changed

+192
-0
lines changed

3 files changed

+192
-0
lines changed

src/App/Container.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ function () {
248248
'validators' => [
249249
ValidatorInterface::LEVEL_CRITICAL => [
250250
$this->container->make(ConfigValidator\Deploy\DatabaseConfiguration::class),
251+
$this->container->make(ConfigValidator\Deploy\SearchConfiguration::class),
251252
$this->container->make(ConfigValidator\Deploy\ResourceConfiguration::class),
252253
$this->container->make(ConfigValidator\Deploy\SessionConfiguration::class),
253254
$this->container->make(ConfigValidator\Deploy\ElasticSuiteIntegrity::class),
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MagentoCloud\Config\Validator\Deploy;
9+
10+
use Magento\MagentoCloud\Config\ConfigMerger;
11+
use Magento\MagentoCloud\Config\Stage\DeployInterface;
12+
use Magento\MagentoCloud\Config\Validator;
13+
use Magento\MagentoCloud\Config\Validator\ResultFactory;
14+
use Magento\MagentoCloud\Config\ValidatorInterface;
15+
16+
/**
17+
* Validates SEARCH_CONFIGURATION variable
18+
*/
19+
class SearchConfiguration implements ValidatorInterface
20+
{
21+
/**
22+
* @var ResultFactory
23+
*/
24+
private $resultFactory;
25+
26+
/**
27+
* @var DeployInterface
28+
*/
29+
private $stageConfig;
30+
31+
/**
32+
* @var ConfigMerger
33+
*/
34+
private $configMerger;
35+
36+
/**
37+
* @param ResultFactory $resultFactory
38+
* @param DeployInterface $stageConfig
39+
* @param ConfigMerger $configMerger
40+
*/
41+
public function __construct(
42+
ResultFactory $resultFactory,
43+
DeployInterface $stageConfig,
44+
ConfigMerger $configMerger
45+
) {
46+
$this->resultFactory = $resultFactory;
47+
$this->stageConfig = $stageConfig;
48+
$this->configMerger = $configMerger;
49+
}
50+
51+
/**
52+
* Checks that SEARCH_CONFIGURATION variable contains at least 'engine' option if _merge was not set
53+
*
54+
* @return Validator\ResultInterface
55+
*/
56+
public function validate(): Validator\ResultInterface
57+
{
58+
$searchConfig = $this->stageConfig->get(DeployInterface::VAR_SEARCH_CONFIGURATION);
59+
if ($this->configMerger->isEmpty($searchConfig) || $this->configMerger->isMergeRequired($searchConfig)) {
60+
return $this->resultFactory->success();
61+
}
62+
63+
if (!isset($searchConfig['engine'])) {
64+
return $this->resultFactory->error(
65+
sprintf('Variable "%s" is not configured properly', DeployInterface::VAR_SEARCH_CONFIGURATION),
66+
'At least engine option must be configured'
67+
);
68+
}
69+
70+
return $this->resultFactory->success();
71+
}
72+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MagentoCloud\Test\Unit\Config\Validator\Deploy;
9+
10+
use Magento\MagentoCloud\Config\ConfigMerger;
11+
use Magento\MagentoCloud\Config\Stage\DeployInterface;
12+
use Magento\MagentoCloud\Config\Validator\Deploy\SearchConfiguration;
13+
use Magento\MagentoCloud\Config\Validator\Result\Error;
14+
use Magento\MagentoCloud\Config\Validator\Result\Success;
15+
use Magento\MagentoCloud\Config\Validator\ResultFactory;
16+
use PHPUnit\Framework\MockObject\MockObject;
17+
use PHPUnit\Framework\TestCase;
18+
19+
/**
20+
* @inheritdoc
21+
*/
22+
class SearchConfigurationTest extends TestCase
23+
{
24+
/**
25+
* @var SearchConfiguration
26+
*/
27+
private $validator;
28+
29+
/**
30+
* @var ResultFactory|MockObject
31+
*/
32+
private $resultFactoryMock;
33+
34+
/**
35+
* @var DeployInterface|MockObject
36+
*/
37+
private $stageConfigMock;
38+
39+
/**
40+
* @inheritdoc
41+
*/
42+
protected function setUp()
43+
{
44+
$this->resultFactoryMock = $this->createConfiguredMock(ResultFactory::class, [
45+
'success' => $this->createMock(Success::class),
46+
'error' => $this->createMock(Error::class)
47+
]);
48+
$this->stageConfigMock = $this->getMockForAbstractClass(DeployInterface::class);
49+
50+
$this->validator = new SearchConfiguration(
51+
$this->resultFactoryMock,
52+
$this->stageConfigMock,
53+
new ConfigMerger()
54+
);
55+
}
56+
57+
/**
58+
* @param array $searchConfiguration
59+
* @param string $expectedResultClass
60+
* @dataProvider validateDataProvider
61+
*/
62+
public function testValidate(array $searchConfiguration, string $expectedResultClass)
63+
{
64+
$this->stageConfigMock->expects($this->once())
65+
->method('get')
66+
->with(DeployInterface::VAR_SEARCH_CONFIGURATION)
67+
->willReturn($searchConfiguration);
68+
69+
$this->assertInstanceOf($expectedResultClass, $this->validator->validate());
70+
}
71+
72+
/**
73+
* @return array
74+
*/
75+
public function validateDataProvider(): array
76+
{
77+
return [
78+
[
79+
[],
80+
Success::class,
81+
],
82+
[
83+
[
84+
'_merge' => true
85+
],
86+
Success::class,
87+
],
88+
[
89+
[
90+
'elasticsearh_host' => '9200',
91+
],
92+
Error::class,
93+
],
94+
[
95+
96+
[
97+
'elasticsearh_host' => '9200',
98+
'_merge' => true,
99+
],
100+
Success::class,
101+
],
102+
[
103+
104+
[
105+
'engine' => 'mysql',
106+
],
107+
Success::class,
108+
],
109+
[
110+
111+
[
112+
'engine' => 'mysql',
113+
'_merge' => true,
114+
],
115+
Success::class,
116+
],
117+
];
118+
}
119+
}

0 commit comments

Comments
 (0)