Skip to content

Commit

Permalink
MCLOUD-5732: Add ECE-Tools warning about MySQL search being used (mag…
Browse files Browse the repository at this point in the history
  • Loading branch information
shiftedreality authored Apr 17, 2020
1 parent c408c0a commit 7e66df4
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 5 deletions.
1 change: 1 addition & 0 deletions config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<service id="Magento\MagentoCloud\Scenario\Exception\ProcessorException" autowire="false" />
<service id="Magento\MagentoCloud\Scenario\Exception\ValidationException" autowire="false" />
<service id="Magento\MagentoCloud\Service\ServiceMismatchException" autowire="false" />
<service id="Magento\MagentoCloud\Config\ValidatorException" autowire="false" />
<service id="Magento\MagentoCloud\Shell\Process" autowire="false" />
<service id="Magento\MagentoCloud\Shell\ProcessException" autowire="false" />
<service id="Magento\MagentoCloud\Step\Build\BackupData" autowire="false" />
Expand Down
1 change: 1 addition & 0 deletions scenario/deploy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<item name="json-format-variable " xsi:type="object">Magento\MagentoCloud\Config\Validator\Deploy\JsonFormatVariable</item>
<item name="service-version" xsi:type="object">Magento\MagentoCloud\Config\Validator\Deploy\ServiceVersion</item>
<item name="service-eol-warning" xsi:type="object">ServiceEol.Warnings</item>
<item name="deprecated-search-engine" xsi:type="object">Magento\MagentoCloud\Config\Validator\Deploy\DeprecatedSearchEngine</item>
</item>
<item name="notice" xsi:type="array">
<item name="service-eol-notice" xsi:type="object">ServiceEol.Notices</item>
Expand Down
2 changes: 2 additions & 0 deletions src/Config/SearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
class SearchEngine
{
public const ENGINE_MYSQL = 'mysql';

/**
* @var Environment
*/
Expand Down
52 changes: 52 additions & 0 deletions src/Config/Validator/Deploy/DeprecatedSearchEngine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MagentoCloud\Config\Validator\Deploy;

use Magento\MagentoCloud\Config\SearchEngine;
use Magento\MagentoCloud\Config\Validator;
use Magento\MagentoCloud\Config\ValidatorInterface;

/**
* Validates if a deprecated MySQL search engine is used.
*/
class DeprecatedSearchEngine implements ValidatorInterface
{
/**
* @var Validator\ResultFactory
*/
private $resultFactory;

/**
* @var SearchEngine
*/
private $searchEngine;

/**
* @param Validator\ResultFactory $resultFactory
* @param SearchEngine $searchEngine
*/
public function __construct(Validator\ResultFactory $resultFactory, SearchEngine $searchEngine)
{
$this->resultFactory = $resultFactory;
$this->searchEngine = $searchEngine;
}

/**
* @inheritDoc
*/
public function validate(): Validator\ResultInterface
{
if (SearchEngine::ENGINE_MYSQL === $this->searchEngine->getName()) {
return $this->resultFactory->error(
'The MySQL search configuration option is deprecated. Use Elasticsearch instead.'
);
}

return $this->resultFactory->success();
}
}
17 changes: 17 additions & 0 deletions src/Config/ValidatorException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MagentoCloud\Config;

use Magento\MagentoCloud\App\GenericException;

/**
* The generic Validator exception.
*/
class ValidatorException extends GenericException
{
}
2 changes: 2 additions & 0 deletions src/Config/ValidatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ interface ValidatorInterface

/**
* @return Validator\ResultInterface
*
* @throws ValidatorException
*/
public function validate(): Validator\ResultInterface;
}
2 changes: 1 addition & 1 deletion src/Step/StepInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ interface StepInterface
* @return void
* @throws StepException
*/
public function execute();
public function execute(); // The :void return type declaration that should be here would cause a BC issue
}
16 changes: 12 additions & 4 deletions src/Step/ValidateConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Magento\MagentoCloud\App\Logger;
use Magento\MagentoCloud\Config\Validator\Result\Error;
use Magento\MagentoCloud\Config\ValidatorException;
use Magento\MagentoCloud\Config\ValidatorInterface;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -42,7 +43,7 @@ public function __construct(
/**
* @inheritdoc
*/
public function execute()
public function execute(): void
{
$this->logger->notice('Validating configuration');

Expand All @@ -66,9 +67,12 @@ public function execute()

/**
* Returns all validation messages grouped by validation level.
* Converts validation level to integer value using @see Logger::toMonologLevel() method
* Converts validation level to integer value.
*
* @return array
*
* @throws StepException
* @see Logger::toMonologLevel()
*/
private function collectMessages(): array
{
Expand All @@ -83,13 +87,17 @@ private function collectMessages(): array
continue;
}

$result = $validator->validate();
try {
$result = $validator->validate();
} catch (ValidatorException $exception) {
throw new StepException($exception->getMessage(), $exception->getCode(), $exception);
}

if ($result instanceof Error) {
$messages[$level][] = '- ' . $result->getError();
if ($suggestion = $result->getSuggestion()) {
$messages[$level][] = implode(PHP_EOL, array_map(
function ($line) {
static function ($line) {
return ' ' . $line;
},
explode(PHP_EOL, $suggestion)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MagentoCloud\Test\Unit\Config\Validator\Deploy;

use Magento\MagentoCloud\Config\SearchEngine;
use Magento\MagentoCloud\Config\Validator\Deploy\DeprecatedSearchEngine;
use Magento\MagentoCloud\Config\Validator\Result\Error;
use Magento\MagentoCloud\Config\Validator\Result\Success;
use Magento\MagentoCloud\Config\Validator\ResultFactory;
use Magento\MagentoCloud\Config\ValidatorException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* @see DeprecatedSearchEngine
*/
class DeprecatedSearchEngineTest extends TestCase
{
/**
* @var DeprecatedSearchEngine
*/
private $validator;

/**
* @var ResultFactory|MockObject
*/
private $resultFactoryMock;

/**
* @var SearchEngine|MockObject
*/
private $searchEngineMock;

/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->resultFactoryMock = $this->createMock(ResultFactory::class);
$this->searchEngineMock = $this->createMock(SearchEngine::class);

$this->validator = new DeprecatedSearchEngine(
$this->resultFactoryMock,
$this->searchEngineMock
);
}

/**
* @throws ValidatorException
*/
public function testValidate(): void
{
$this->searchEngineMock->method('getName')
->willReturn(SearchEngine::ENGINE_MYSQL);
$this->resultFactoryMock->expects($this->once())
->method('error')
->with(
'The MySQL search configuration option is deprecated. Use Elasticsearch instead.'
)->willReturn(new Error('Some error'));

$this->validator->validate();
}

/**
* @throws ValidatorException
*/
public function testValidateWithError(): void
{
$this->searchEngineMock->method('getName')
->willReturn('es');
$this->resultFactoryMock->expects($this->once())
->method('success')
->willReturn(new Success());

$this->validator->validate();
}
}

0 comments on commit 7e66df4

Please sign in to comment.