Skip to content

Commit 2abefef

Browse files
committed
#60: Build interactive configuration tool for populating .env properties
1 parent 08c089a commit 2abefef

File tree

7 files changed

+242
-3
lines changed

7 files changed

+242
-3
lines changed

.env.example

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
MAGENTO_BASE_URL=http://devdocs.magento.com/
66

77
#*** Set the Admin Username and Password for your Magento instance ***#
8-
MAGENTO_BACKEND_NAME=
9-
MAGENTO_ADMIN_USERNAME=
10-
MAGENTO_ADMIN_PASSWORD=
8+
MAGENTO_BACKEND_NAME=admin
9+
MAGENTO_ADMIN_USERNAME=admin
10+
MAGENTO_ADMIN_PASSWORD=123123q
1111

1212
#*** Path to CLI entry point and command parameter name. Uncomment and change if folder structure differs from standard Magento installation
1313
#MAGENTO_CLI_COMMAND_PATH=dev/tests/acceptance/utils/command.php

RoboFile.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function cloneFiles()
3333
*/
3434
function buildProject()
3535
{
36+
$this->writeln("<error>This command will be removed in MFTF v3.0.0. Please use bin/mftf build:project instead.</error>\n");
3637
$this->cloneFiles();
3738
$this->_exec('vendor'. DIRECTORY_SEPARATOR .'bin'. DIRECTORY_SEPARATOR .'codecept build');
3839
}

bin/mftf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env php
2+
3+
<?php
4+
/**
5+
* Copyright © Magento, Inc. All rights reserved.
6+
* See COPYING.txt for license details.
7+
*/
8+
9+
require_once __DIR__ . '/../bootstrap.php';
10+
11+
use Symfony\Component\Console\Application;
12+
13+
$application = new Application();
14+
$application->setName('Magento Functional Testing Framework CLI');
15+
$application->setVersion('1.0.0');
16+
$application->add(new Magento\FunctionalTestingFramework\Console\SetupEnvCommand());
17+
$application->add(new Magento\FunctionalTestingFramework\Console\BuildProjectCommand());
18+
$application->run();

bootstrap.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
require_once 'vendor/autoload.php';
8+
define('BP', __DIR__);
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types = 1);
8+
9+
namespace Magento\FunctionalTestingFramework\Console;
10+
11+
use Symfony\Component\Console\Command\Command;
12+
use Symfony\Component\Console\Input\ArrayInput;
13+
use Symfony\Component\Filesystem\Filesystem;
14+
use Symfony\Component\Console\Input\InputOption;
15+
use Magento\FunctionalTestingFramework\Util\Env\EnvProcessor;
16+
use \Symfony\Component\Console\Input\InputInterface;
17+
use \Symfony\Component\Console\Output\OutputInterface;
18+
19+
class BuildProjectCommand extends Command
20+
{
21+
/**
22+
* Array with environment variables for the project.
23+
*
24+
* @var array
25+
*/
26+
private $env = [];
27+
28+
/**
29+
* @inheritdoc
30+
*/
31+
protected function configure()
32+
{
33+
$this->setName('build:project');
34+
$this->setDescription('Generate configuration files for the project. Build the Codeception project.');
35+
$env = $this->getEnv();
36+
foreach ($env as $key => $value) {
37+
$this->addOption($key, null, InputOption::VALUE_REQUIRED, '', $value);
38+
}
39+
}
40+
41+
/**
42+
* @inheritdoc
43+
*/
44+
protected function execute(InputInterface $input, OutputInterface $output)
45+
{
46+
$fileSystem = new Filesystem();
47+
$fileSystem->copy(
48+
BP . DIRECTORY_SEPARATOR . 'codeception.dist.yml',
49+
BP . DIRECTORY_SEPARATOR . 'codeception.yml'
50+
);
51+
$output->writeln("codeception.yml configuration successfully applied.\n");
52+
$fileSystem->copy(
53+
BP . DIRECTORY_SEPARATOR . 'dev' . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'functional' . DIRECTORY_SEPARATOR . 'MFTF.suite.dist.yml',
54+
BP . DIRECTORY_SEPARATOR . 'dev' . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'functional' . DIRECTORY_SEPARATOR . 'MFTF.suite.yml'
55+
);
56+
$output->writeln("MFTF.suite.yml configuration successfully applied.\n");
57+
$setupEnvCommand = new SetupEnvCommand();
58+
$commandInput = [];
59+
$options = $input->getOptions();
60+
$env = array_keys($this->getEnv());
61+
foreach ($options as $key => $value) {
62+
if (in_array($key, $env)) {
63+
$commandInput['--' . $key] = $value;
64+
}
65+
}
66+
$commandInput = new ArrayInput($commandInput);
67+
$setupEnvCommand->run($commandInput, $output);
68+
}
69+
70+
/**
71+
* Retrieves '.env.example' file as associative array.
72+
*
73+
* @return array
74+
*/
75+
private function getEnv(): array
76+
{
77+
if (empty($this->env)) {
78+
$this->env = EnvProcessor::parseEvnFile(BP);
79+
}
80+
return $this->env;
81+
}
82+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types = 1);
8+
9+
namespace Magento\FunctionalTestingFramework\Console;
10+
11+
use Symfony\Component\Console\Command\Command;
12+
use Symfony\Component\Console\Input\InputOption;
13+
use Symfony\Component\Console\Exception\InvalidOptionException;
14+
use Magento\FunctionalTestingFramework\Util\Env\EnvProcessor;
15+
use \Symfony\Component\Console\Input\InputInterface;
16+
use \Symfony\Component\Console\Output\OutputInterface;
17+
18+
class SetupEnvCommand extends Command
19+
{
20+
/**
21+
* Array with environment variables for the project.
22+
*
23+
* @var array
24+
*/
25+
private $env = [];
26+
27+
/**
28+
* @inheritdoc
29+
*/
30+
protected function configure()
31+
{
32+
$this->setName('setup:env');
33+
$this->setDescription("Generate .env file.");
34+
$env = $this->getEnv();
35+
foreach ($env as $key => $value) {
36+
$this->addOption($key, null, InputOption::VALUE_REQUIRED, '', $value);
37+
}
38+
}
39+
40+
/**
41+
* @inheritdoc
42+
*/
43+
protected function execute(InputInterface $input, OutputInterface $output)
44+
{
45+
$config = $this->getEnv();
46+
$userEnv = [];
47+
foreach ($config as $key => $value) {
48+
if ($input->getOption($key) === '') {
49+
throw new InvalidOptionException(sprintf("Parameter $key cannot be empty.", $key));
50+
}
51+
$userEnv[$key] = $input->getOption($key);
52+
}
53+
EnvProcessor::putEnvFile($userEnv, BP);
54+
$output->writeln(".env configuration successfully applied.\n");
55+
}
56+
57+
/**
58+
* Retrieves '.env.example' file as associative array.
59+
*
60+
* @return array
61+
*/
62+
private function getEnv(): array
63+
{
64+
if (empty($this->env)) {
65+
$this->env = EnvProcessor::parseEvnFile(BP);
66+
}
67+
return $this->env;
68+
}
69+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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\FunctionalTestingFramework\Util\Env;
9+
10+
/**
11+
* Helper class EnvProcessor for reading and writing .env files.
12+
*
13+
* @package Magento\FunctionalTestingFramework\Util\Env
14+
*/
15+
class EnvProcessor
16+
{
17+
/**
18+
* Constants with env file names.
19+
*/
20+
const ENV_EXAMPLE_FILE = '.env.example';
21+
const ENV_FILE = '.env';
22+
23+
/**
24+
* Serves for parsing '.env.example' file into associative array.
25+
*
26+
* @param string $fileDir
27+
* @return array
28+
*/
29+
public static function parseEvnFile(string $fileDir = ''): array
30+
{
31+
$envLines = file(
32+
$fileDir . DIRECTORY_SEPARATOR . self::ENV_EXAMPLE_FILE,
33+
FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
34+
);
35+
$env = [];
36+
foreach ($envLines as $line) {
37+
// do not use commented out lines
38+
if (strpos($line, '#') !== 0) {
39+
list($key, $value) = explode('=', $line);
40+
$env[$key] = $value;
41+
}
42+
}
43+
return $env;
44+
}
45+
46+
/**
47+
* Serves for putting array with environment variables into .env file.
48+
*
49+
* @param array $config
50+
* @param string $destination
51+
* @return void
52+
*/
53+
public static function putEnvFile(array $config = [], string $destination = '')
54+
{
55+
$envData = '';
56+
foreach ($config as $key => $value) {
57+
$envData .= $key . '=' . $value . PHP_EOL;
58+
}
59+
file_put_contents($destination . DIRECTORY_SEPARATOR . self::ENV_FILE, $envData);
60+
}
61+
}

0 commit comments

Comments
 (0)