Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BuildProjectCommand.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types = 1);
7 
9 
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 Symfony\Component\Console\Input\InputInterface;
16 use Symfony\Component\Console\Output\OutputInterface;
17 use Symfony\Component\Process\Process;
19 use Symfony\Component\Yaml\Yaml;
20 
27 class BuildProjectCommand extends Command
28 {
30  const CREDENTIALS_FILE_PATH = TESTS_BP . DIRECTORY_SEPARATOR . '.credentials.example';
31 
37  private $envProcessor;
38 
44  protected function configure()
45  {
46  $this->setName('build:project')
47  ->setDescription('Generate configuration files for the project. Build the Codeception project.')
48  ->addOption(
49  "upgrade",
50  'u',
51  InputOption::VALUE_NONE,
52  'upgrade existing MFTF tests according to last major release requiements'
53  );
54  $this->envProcessor = new EnvProcessor(TESTS_BP . DIRECTORY_SEPARATOR . '.env');
55  $env = $this->envProcessor->getEnv();
56  foreach ($env as $key => $value) {
57  $this->addOption($key, null, InputOption::VALUE_REQUIRED, '', $value);
58  }
59  }
60 
71  protected function execute(InputInterface $input, OutputInterface $output)
72  {
73  $resetCommand = new CleanProjectCommand();
74  $resetOptions = new ArrayInput([]);
75  $resetCommand->run($resetOptions, $output);
76 
77  $this->generateConfigFiles($output);
78 
79  $setupEnvCommand = new SetupEnvCommand();
80  $commandInput = [];
81  $options = $input->getOptions();
82  $env = array_keys($this->envProcessor->getEnv());
83  foreach ($options as $key => $value) {
84  if (in_array($key, $env)) {
85  $commandInput['--' . $key] = $value;
86  }
87  }
88  $commandInput = new ArrayInput($commandInput);
89  $setupEnvCommand->run($commandInput, $output);
90 
91  // TODO can we just import the codecept symfony command?
92  $codeceptBuildCommand = realpath(PROJECT_ROOT . '/vendor/bin/codecept') . ' build';
93  $process = new Process($codeceptBuildCommand);
94  $process->setWorkingDirectory(TESTS_BP);
95  $process->setIdleTimeout(600);
96  $process->setTimeout(0);
97  $process->run(
98  function ($type, $buffer) use ($output) {
99  if ($output->isVerbose()) {
100  $output->write($buffer);
101  }
102  }
103  );
104 
105  if ($input->getOption('upgrade')) {
106  $upgradeCommand = new UpgradeTestsCommand();
107  $upgradeOptions = new ArrayInput(['path' => TESTS_MODULE_PATH]);
108  $upgradeCommand->run($upgradeOptions, $output);
109  }
110  }
111 
118  private function generateConfigFiles(OutputInterface $output)
119  {
120  $fileSystem = new Filesystem();
121  //Find travel path from codeception.yml to FW_BP
122  $relativePath = $fileSystem->makePathRelative(FW_BP, TESTS_BP);
123 
124  if (!$fileSystem->exists(TESTS_BP . DIRECTORY_SEPARATOR . 'codeception.yml')) {
125  // read in the codeception.yml file
126  $configDistYml = Yaml::parse(file_get_contents(realpath(FW_BP . "/etc/config/codeception.dist.yml")));
127  $configDistYml['paths']['support'] = $relativePath . 'src/Magento/FunctionalTestingFramework';
128  $configDistYml['paths']['envs'] = $relativePath . 'etc/_envs';
129  $configYmlText = Yaml::dump($configDistYml, self::DEFAULT_YAML_INLINE_DEPTH);
130 
131  // dump output to new codeception.yml file
132  file_put_contents(TESTS_BP . DIRECTORY_SEPARATOR . 'codeception.yml', $configYmlText);
133  $output->writeln("codeception.yml configuration successfully applied.");
134  }
135 
136  if ($output->isVerbose()) {
137  $output->writeln("codeception.yml applied to " . TESTS_BP . DIRECTORY_SEPARATOR . 'codeception.yml');
138  }
139 
140  // copy the functional suite yml, will only copy if there are differences between the template the destination
141  $fileSystem->copy(
142  realpath(FW_BP . '/etc/config/functional.suite.dist.yml'),
143  TESTS_BP . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'functional.suite.yml'
144  );
145  $output->writeln('functional.suite.yml configuration successfully applied.');
146 
147  if ($output->isVerbose()) {
148  $output->writeln("functional.suite.yml applied to " .
149  TESTS_BP . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'functional.suite.yml');
150  }
151 
152  $fileSystem->copy(
153  FW_BP . '/etc/config/.credentials.example',
154  self::CREDENTIALS_FILE_PATH
155  );
156 
157  // copy command.php into magento instance
158  if (MAGENTO_BP === FW_BP) {
159  $output->writeln('MFTF standalone detected, command.php copy not applied.');
160  } else {
161  $fileSystem->copy(
162  realpath(FW_BP . '/etc/config/command.php'),
163  TESTS_BP . DIRECTORY_SEPARATOR . "utils" . DIRECTORY_SEPARATOR .'command.php'
164  );
165  $output->writeln('command.php copied to ' .
166  TESTS_BP . DIRECTORY_SEPARATOR . "utils" . DIRECTORY_SEPARATOR .'command.php');
167  }
168 
169  // Remove and Create Log File
170  $logPath = LoggingUtil::getInstance()->getLoggingPath();
171  $fileSystem->remove($logPath);
172  $fileSystem->touch($logPath);
173  $fileSystem->chmod($logPath, 0777);
174 
175  $output->writeln('.credentials.example successfully applied.');
176  }
177 }
const PROJECT_ROOT
Definition: _bootstrap.php:8
execute(InputInterface $input, OutputInterface $output)
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
$relativePath
Definition: get.php:35