Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GenerateTestsCommand.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types = 1);
7 
9 
17 use Symfony\Component\Console\Input\InputArgument;
18 use Symfony\Component\Console\Input\InputInterface;
19 use Symfony\Component\Console\Input\InputOption;
20 use Symfony\Component\Console\Output\OutputInterface;
21 
23 {
29  protected function configure()
30  {
31  $this->setName('generate:tests')
32  ->setDescription('This command generates all test files and suites based on xml declarations')
33  ->addArgument(
34  'name',
35  InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
36  'name(s) of specific tests to generate'
37  )->addOption("config", 'c', InputOption::VALUE_REQUIRED, 'default, singleRun, or parallel', 'default')
38  ->addOption(
39  "force",
40  'f',
41  InputOption::VALUE_NONE,
42  'force generation of tests regardless of Magento Instance Configuration'
43  )->addOption(
44  'time',
45  'i',
46  InputOption::VALUE_REQUIRED,
47  'Used in combination with a parallel configuration, determines desired group size (in minutes)',
48  10
49  )->addOption(
50  'tests',
51  't',
52  InputOption::VALUE_REQUIRED,
53  'A parameter accepting a JSON string used to determine the test configuration'
54  )->addOption(
55  'debug',
56  'd',
57  InputOption::VALUE_NONE,
58  'run extra validation when generating tests'
59  );
60 
61  parent::configure();
62  }
63 
74  protected function execute(InputInterface $input, OutputInterface $output)
75  {
76  $tests = $input->getArgument('name');
77  $config = $input->getOption('config');
78  $json = $input->getOption('tests');
79  $force = $input->getOption('force');
80  $time = $input->getOption('time') * 60 * 1000; // convert from minutes to milliseconds
81  $debug = $input->getOption('debug');
82  $remove = $input->getOption('remove');
83 
84  $verbose = $output->isVerbose();
85 
86  if ($json !== null && !json_decode($json)) {
87  // stop execution if we have failed to properly parse any json passed in by the user
88  throw new TestFrameworkException("JSON could not be parsed: " . json_last_error_msg());
89  }
90 
91  if ($config === 'parallel' && $time <= 0) {
92  // stop execution if the user has given us an invalid argument for time argument during parallel generation
93  throw new TestFrameworkException("time option cannot be less than or equal to 0");
94  }
95 
96  // Remove previous GENERATED_DIR if --remove option is used
97  if ($remove) {
98  $this->removeGeneratedDirectory($output, $verbose || $debug);
99  }
100 
101  $testConfiguration = $this->createTestConfiguration($json, $tests, $force, $debug, $verbose);
102 
103  // create our manifest file here
104  $testManifest = TestManifestFactory::makeManifest($config, $testConfiguration['suites']);
105  TestGenerator::getInstance(null, $testConfiguration['tests'])->createAllTestFiles($testManifest);
106 
107  if ($config == 'parallel') {
109  $testManifest->createTestGroups($time);
110  }
111 
112  if (empty($tests)) {
113  SuiteGenerator::getInstance()->generateAllSuites($testManifest);
114  }
115 
116  $testManifest->generate();
117 
118  $output->writeln("Generate Tests Command Run");
119  }
120 
133  private function createTestConfiguration($json, array $tests, bool $force, bool $debug, bool $verbose)
134  {
135  // set our application configuration so we can references the user options in our framework
137  $force,
139  $verbose,
140  $debug
141  );
142 
143  $testConfiguration = [];
144  $testConfiguration['tests'] = $tests;
145  $testConfiguration['suites'] = [];
146 
147  $testConfiguration = $this->parseTestsConfigJson($json, $testConfiguration);
148 
149  // if we have references to specific tests, we resolve the test objects and pass them to the config
150  if (!empty($testConfiguration['tests'])) {
151  $testObjects = [];
152 
153  foreach ($testConfiguration['tests'] as $test) {
154  $testObjects[$test] = TestObjectHandler::getInstance()->getObject($test);
155  }
156 
157  $testConfiguration['tests'] = $testObjects;
158  }
159 
160  return $testConfiguration;
161  }
162 
171  private function parseTestsConfigJson($json, array $testConfiguration)
172  {
173  if ($json === null) {
174  return $testConfiguration;
175  }
176 
177  $jsonTestConfiguration = [];
178  $testConfigArray = json_decode($json, true);
179 
180  $jsonTestConfiguration['tests'] = $testConfigArray['tests'] ?? null;
181  ;
182  $jsonTestConfiguration['suites'] = $testConfigArray['suites'] ?? null;
183  return $jsonTestConfiguration;
184  }
185 }
$config
Definition: fraud_order.php:17
removeGeneratedDirectory(OutputInterface $output, bool $verbose)
static getInstance($dir=null, $tests=[], $debug=false)
static create($forceGenerate, $phase, $verboseEnabled, $debugEnabled)
static makeManifest($runConfig, $suiteConfiguration, $testPath=TestGenerator::DEFAULT_DIR)
$debug
Definition: router.php:22