Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RunTestGroupCommand.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types = 1);
7 
9 
13 use Symfony\Component\Console\Input\ArrayInput;
14 use Symfony\Component\Console\Input\InputArgument;
15 use Symfony\Component\Console\Input\InputInterface;
16 use Symfony\Component\Console\Input\InputOption;
17 use Symfony\Component\Console\Output\OutputInterface;
18 use Symfony\Component\Process\Process;
20 
22 {
28  protected function configure()
29  {
30  $this->setName('run:group')
31  ->setDescription('Execute a set of tests referenced via group annotations')
32  ->addOption(
33  'skip-generate',
34  'k',
35  InputOption::VALUE_NONE,
36  "only execute a group of tests without generating from source xml"
37  )->addOption(
38  "force",
39  'f',
40  InputOption::VALUE_NONE,
41  'force generation of tests regardless of Magento Instance Configuration'
42  )->addArgument(
43  'groups',
44  InputArgument::IS_ARRAY | InputArgument::REQUIRED,
45  'group names to be executed via codeception'
46  );
47 
48  parent::configure();
49  }
50 
61  protected function execute(InputInterface $input, OutputInterface $output)
62  {
63  $skipGeneration = $input->getOption('skip-generate');
64  $force = $input->getOption('force');
65  $groups = $input->getArgument('groups');
66  $remove = $input->getOption('remove');
67 
68  if ($skipGeneration and $remove) {
69  // "skip-generate" and "remove" options cannot be used at the same time
70  throw new TestFrameworkException(
71  "\"skip-generate\" and \"remove\" options can not be used at the same time."
72  );
73  }
74 
75  // Create Mftf Configuration
77  $force,
79  false,
80  false
81  );
82 
83  if (!$skipGeneration) {
84  $testConfiguration = $this->getGroupAndSuiteConfiguration($groups);
85  $command = $this->getApplication()->find('generate:tests');
86  $args = [
87  '--tests' => $testConfiguration,
88  '--force' => $force,
89  '--remove' => $remove
90  ];
91 
92  $command->run(new ArrayInput($args), $output);
93  }
94 
95  $codeceptionCommand = realpath(PROJECT_ROOT . '/vendor/bin/codecept') . ' run functional --verbose --steps';
96 
97  foreach ($groups as $group) {
98  $codeceptionCommand .= " -g {$group}";
99  }
100 
101  $process = new Process($codeceptionCommand);
102  $process->setWorkingDirectory(TESTS_BP);
103  $process->setIdleTimeout(600);
104  $process->setTimeout(0);
105  $process->run(
106  function ($type, $buffer) use ($output) {
107  $output->write($buffer);
108  }
109  );
110  }
111 
119  private function getGroupAndSuiteConfiguration(array $groups)
120  {
121  $testConfiguration['tests'] = [];
122  $testConfiguration['suites'] = null;
123  $availableSuites = SuiteObjectHandler::getInstance()->getAllObjects();
124 
125  foreach ($groups as $group) {
126  if (array_key_exists($group, $availableSuites)) {
127  $testConfiguration['suites'][$group] = [];
128  }
129 
130  $testConfiguration['tests'] = array_merge(
131  $testConfiguration['tests'],
132  array_keys(TestObjectHandler::getInstance()->getTestsByGroup($group))
133  );
134  }
135 
136  $testConfigurationJson = json_encode($testConfiguration);
137  return $testConfigurationJson;
138  }
139 }
const PROJECT_ROOT
Definition: _bootstrap.php:8
$group
Definition: sections.phtml:16
$type
Definition: item.phtml:13
static create($forceGenerate, $phase, $verboseEnabled, $debugEnabled)
execute(InputInterface $input, OutputInterface $output)