Skip to content

Commit

Permalink
Add check for db, Redis and ES
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Sep 18, 2022
1 parent bbe2d1c commit 89c9922
Show file tree
Hide file tree
Showing 12 changed files with 372 additions and 84 deletions.
14 changes: 12 additions & 2 deletions Utilities/DbCheck.php → Check/DbCheck.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
<?php declare(strict_types=1);

namespace Yireo\IntegrationTestHelper\Utilities;
namespace Yireo\IntegrationTestHelper\Check;

use Laminas\Db\Adapter\Driver\Pdo\ConnectionFactory;
use PDO;
use Yireo\IntegrationTestHelper\Utilities\CurrentInstallConfig;

class DbCheck
{
public function checkDbConnection(array $config = []): bool
private CurrentInstallConfig $currentInstallConfig;

public function __construct(
CurrentInstallConfig $currentInstallConfig
) {
$this->currentInstallConfig = $currentInstallConfig;
}

public function checkDbConnection(): bool
{
$config = $this->currentInstallConfig->getValues();
$host = $config['db-host'];
$user = $config['db-user'];
$pass = $config['db-password'];
Expand Down
45 changes: 45 additions & 0 deletions Check/RedisCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types=1);

namespace Yireo\IntegrationTestHelper\Check;

use Cm_Cache_Backend_Redis;
use Yireo\IntegrationTestHelper\Utilities\CurrentInstallConfig;
use Zend_Cache_Exception;

class RedisCheck
{
private CurrentInstallConfig $currentInstallConfig;

/**
* @param CurrentInstallConfig $currentInstallConfig
*/
public function __construct(
CurrentInstallConfig $currentInstallConfig
) {
$this->currentInstallConfig = $currentInstallConfig;
}

/**
* @return bool
*/
public function checkRedisConnection(): bool
{
$config = $this->currentInstallConfig->getValues();
$server = $config['cache-backend-redis-server'];
$port = $config['cache-backend-redis-port'];
$dbName = $config['cache-backend-redis-db'];

try {
$redis = new Cm_Cache_Backend_Redis([
'server' => $server,
'port' => $port,
'database' => $dbName
]);
} catch (Zend_Cache_Exception $e) {
return false;
}

$info = $redis->getInfo();
return !empty($info);
}
}
27 changes: 27 additions & 0 deletions Check/SearchEngineCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types=1);

namespace Yireo\IntegrationTestHelper\Check;

use GuzzleHttp\Client;
use Yireo\IntegrationTestHelper\Utilities\CurrentInstallConfig;

class SearchEngineCheck
{
private CurrentInstallConfig $currentInstallConfig;
private Client $httpClient;

public function __construct(
CurrentInstallConfig $currentInstallConfig,
Client $httpClient
) {
$this->currentInstallConfig = $currentInstallConfig;
$this->httpClient = $httpClient;
}

public function checkSearchEngineConnection(): bool
{
$config = $this->currentInstallConfig->getValues();
$response = $this->httpClient->get($config['elasticsearch-host'].':'.$config['elasticsearch-port']);
return $response->getStatusCode() === 200;
}
}
119 changes: 81 additions & 38 deletions Console/Command/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,112 @@
namespace Yireo\IntegrationTestHelper\Console\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Yireo\IntegrationTestHelper\Utilities\DbCheck;
use Yireo\IntegrationTestHelper\Check\RedisCheck;
use Yireo\IntegrationTestHelper\Exception\IntegrationTesting\PhpUnitFile\ConstantNotFound;
use Yireo\IntegrationTestHelper\Exception\IntegrationTesting\PhpUnitFile\FileNotFound;
use Yireo\IntegrationTestHelper\Exception\IntegrationTesting\PhpUnitFile\InvalidContent;
use Yireo\IntegrationTestHelper\Utilities\CurrentInstallConfig;
use Yireo\IntegrationTestHelper\Check\DbCheck;
use Yireo\IntegrationTestHelper\Utilities\IntegrationTesting\InstallConfig;
use Yireo\IntegrationTestHelper\Utilities\IntegrationTesting\PhpUnitFile\Constant;
use Yireo\IntegrationTestHelper\Check\SearchEngineCheck;

class CheckCommand extends Command
{
/**
* @var Constant
*/
private $constant;

/**
* @var InstallConfig
*/
private $installConfig;
/**
* @var DbCheck
*/
private $dbCheck;

private Constant $constant;
private InstallConfig|CurrentInstallConfig $currentInstallConfig;
private DbCheck $dbCheck;
private SearchEngineCheck $searchEngineCheck;
private RedisCheck $redisCheck;

/**
* @param Constant $constant
* @param InstallConfig $installConfig
* @param CurrentInstallConfig $currentInstallConfig
* @param DbCheck $dbCheck
* @param SearchEngineCheck $searchEngineCheck
* @param RedisCheck $redisCheck
* @param string|null $name
*/
public function __construct(
Constant $constant,
InstallConfig $installConfig,
DbCheck $dbCheck,
string $name = null
)
{
Constant $constant,
CurrentInstallConfig $currentInstallConfig,
DbCheck $dbCheck,
SearchEngineCheck $searchEngineCheck,
RedisCheck $redisCheck,
string $name = null
) {
parent::__construct($name);
$this->constant = $constant;
$this->installConfig = $installConfig;
$this->currentInstallConfig = $currentInstallConfig;
$this->dbCheck = $dbCheck;
$this->searchEngineCheck = $searchEngineCheck;
$this->redisCheck = $redisCheck;
}


/**
* @return void
*/
protected function configure()
{
$this->setName('integration_tests:check')
->setDescription('Perform a simple check before running integration tests');
}


/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int|void
* @throws ConstantNotFound
* @throws FileNotFound
* @throws InvalidContent
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('TESTS_CLEANUP: ' . $this->constant->getValue('TESTS_CLEANUP'));
$output->writeln('TESTS_MAGENTO_MODE: ' . $this->constant->getValue('TESTS_MAGENTO_MODE'));
$output->writeln('DB host: ' . $this->installConfig->getConfigValue('db-host'));
$output->writeln('DB username: ' . $this->installConfig->getConfigValue('db-user'));
$output->writeln('DB password: ' . $this->installConfig->getConfigValue('db-password'));
$output->writeln('DB name: ' . $this->installConfig->getConfigValue('db-name'));
$output->writeln('DB reachable: ' . $this->getDbReachable());
$table = new Table($output);
$table->setHeaders(['Setting', 'Value']);
$table->addRow(['TESTS_CLEANUP', $this->constant->getValue('TESTS_CLEANUP')]);
$table->addRow(['TESTS_MAGENTO_MODE', $this->constant->getValue('TESTS_MAGENTO_MODE')]);
$table->addRow(['DB host', $this->currentInstallConfig->getValue('db-host')]);

$table->addRow(['DB username', $this->currentInstallConfig->getValue('db-user')]);
$table->addRow(['DB password', $this->currentInstallConfig->getValue('db-password')]);
$table->addRow(['DB name', $this->currentInstallConfig->getValue('db-name')]);
$table->addRow(['DB reachable', $this->getDbReachable()]);

$table->addRow(['ES host', $this->currentInstallConfig->getValue('elasticsearch-host')]);
$table->addRow(['ES port', $this->currentInstallConfig->getValue('elasticsearch-port')]);
$table->addRow(['ES reachable', $this->getSearchEngineReachable()]);

$table->addRow(['Redis host', $this->currentInstallConfig->getValue('cache-backend-redis-server')]);
$table->addRow(['Redis port', $this->currentInstallConfig->getValue('cache-backend-redis-port')]);
$table->addRow(['Redis reachable', $this->getRedisReachable()]);
$table->render();
}


/**
* @return string
*/
private function getDbReachable(): string
{
return $this->dbCheck->checkDbConnection(
$this->installConfig->getConfigValues()
) ? 'Yes' : 'No';
return $this->dbCheck->checkDbConnection() ? 'Yes' : 'No';
}

/**
* @return string
*/
private function getSearchEngineReachable(): string
{
return $this->searchEngineCheck->checkSearchEngineConnection() ? 'Yes' : 'No';
}

/**
* @return string
*/
private function getRedisReachable(): string
{
return $this->redisCheck->checkRedisConnection() ? 'Yes' : 'No';
}
}
}
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,26 @@ $disableModules = (new DisableModules())
```

Note that if there would be a module `Yireo_ExampleGraphQl` then this would be first disabled with `disableGraphQl()` and then re-enabled again with `enableByPattern('Yireo_')`. The ordering of your methods matters!

## Validating your configuration
The module also ships with a CLI command to easily check whether the currently returned `setup:install` flags make sense:
```bash
$ bin/magento integration_tests:check
+--------------------+--------------------+
| Setting | Value |
+--------------------+--------------------+
| TESTS_CLEANUP | enabled |
| TESTS_MAGENTO_MODE | developer |
| DB host | mysql57_production |
| DB username | root |
| DB password | root |
| DB name | m2_test |
| DB reachable | Yes |
| ES host | localhost |
| ES port | 9207 |
| ES reachable | Yes |
| Redis host | 127.0.0.1 |
| Redis port | 6379 |
| Redis reachable | Yes |
+--------------------+--------------------+
```
65 changes: 65 additions & 0 deletions Utilities/CurrentInstallConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php declare(strict_types=1);

namespace Yireo\IntegrationTestHelper\Utilities;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Console\Cli;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\DriverInterface;
use Magento\Setup\Console\Command\InstallCommand;
use Magento\Setup\Model\ConfigModel;
use Magento\Setup\Model\SearchConfigOptionsList;

class CurrentInstallConfig
{
private DirectoryList $directoryList;
private DefaultInstallConfig $defaultInstallConfig;
private DriverInterface $fileDriver;

public function __construct(
DirectoryList $directoryList,
DefaultInstallConfig $defaultInstallConfig,
Filesystem $filesystem
) {
$this->directoryList = $directoryList;
$this->defaultInstallConfig = $defaultInstallConfig;
$this->fileDriver = $filesystem->getDirectoryWrite(DirectoryList::PUB)->getDriver();
}

/**
* @return string[]
*/
public function getValues(): array
{
$integrationTestsDir = $this->directoryList->getRoot() . '/dev/tests/integration/';

$testsBaseDir = $integrationTestsDir;
$autoloadWrapper = \Magento\Framework\Autoload\AutoloaderRegistry::getAutoloader();

$autoloadWrapper->addPsr4('Magento\\TestFramework\\', "{$testsBaseDir}/framework/Magento/TestFramework/");
$autoloadWrapper->addPsr4('Magento\\', "{$testsBaseDir}/testsuite/Magento/");

$installConfig = $integrationTestsDir . '/etc/install-config-mysql.php';
if (!$this->fileDriver->isExists($installConfig)) {
$installConfig = $installConfig . '.dist';
}

$values = $this->defaultInstallConfig->getValues();
$values = array_merge($values, include($installConfig));
return $values;
}

/**
* @param string $key
* @return string|null
*/
public function getValue(string $key)
{
$values = $this->getValues();
if (isset($values[$key])) {
return $values[$key];
}

return null;
}
}
33 changes: 33 additions & 0 deletions Utilities/DefaultInstallConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types=1);

namespace Yireo\IntegrationTestHelper\Utilities;

use Magento\TestFramework\Bootstrap;

class DefaultInstallConfig
{
/**
* @return mixed[]
*/
public function getValues(): array
{
return [
'db-host' => 'localhost',
'db-user' => 'root',
'db-password' => 'root',
'db-name' => 'magento',
'db-prefix' => '',
'search-engine' => 'elasticsearch7',
'elasticsearch-host' => 'localhost',
'elasticsearch-port' => '9200',
'backend-frontname' => 'backend',
'admin-user' => Bootstrap::ADMIN_NAME,
'admin-password' => Bootstrap::ADMIN_PASSWORD,
'admin-email' => Bootstrap::ADMIN_EMAIL,
'admin-firstname' => Bootstrap::ADMIN_FIRSTNAME,
'admin-lastname' => Bootstrap::ADMIN_LASTNAME,
'allow-parallel-generation' => null,
'skip-db-validation' => null
];
}
}
Loading

0 comments on commit 89c9922

Please sign in to comment.