Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DevTestsRunCommand.php
Go to the documentation of this file.
1 <?php
7 
8 use Symfony\Component\Console\Command\Command;
9 use Symfony\Component\Console\Input\InputArgument;
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Output\OutputInterface;
13 
19 class DevTestsRunCommand extends Command
20 {
24  const INPUT_ARG_TYPE = 'type';
25 
29  const INPUT_OPT_COMMAND_ARGUMENTS = 'arguments';
31 
35  const COMMAND_NAME = 'dev:tests:run';
36 
42  private $types;
43 
49  private $commands;
50 
54  protected function configure()
55  {
56  $this->setupTestInfo();
57  $this->setName(self::COMMAND_NAME)
58  ->setDescription('Runs tests');
59 
60  $this->addArgument(
61  self::INPUT_ARG_TYPE,
62  InputArgument::OPTIONAL,
63  'Type of test to run. Available types: ' . implode(', ', array_keys($this->types)),
64  'default'
65  );
66  $this->addOption(
67  self::INPUT_OPT_COMMAND_ARGUMENTS,
68  self::INPUT_OPT_COMMAND_ARGUMENTS_SHORT,
69  InputOption::VALUE_REQUIRED,
70  'Additional arguments for PHPUnit. Example: "-c\'--filter=MyTest\'" (no spaces)',
71  ''
72  );
73  parent::configure();
74  }
75 
83  protected function execute(InputInterface $input, OutputInterface $output)
84  {
85  /* Validate type argument is valid */
86  $type = $input->getArgument(self::INPUT_ARG_TYPE);
87  if (!isset($this->types[$type])) {
88  $output->writeln(
89  'Invalid type: "' . $type . '". Available types: ' . implode(', ', array_keys($this->types))
90  );
91  return 1;
92  }
93 
94  $vendorDir = require BP . '/app/etc/vendor_path.php';
95 
96  $failures = [];
97  $runCommands = $this->types[$type];
98  foreach ($runCommands as $key) {
99  list($dir, $options) = $this->commands[$key];
100  $dirName = realpath(BP . '/dev/tests/' . $dir);
101  chdir($dirName);
102  $command = PHP_BINARY . ' ' . BP . '/' . $vendorDir . '/phpunit/phpunit/phpunit ' . $options;
103  if ($commandArguments = $input->getOption(self::INPUT_OPT_COMMAND_ARGUMENTS)) {
104  $command .= ' ' . $commandArguments;
105  }
106  $message = $dirName . '> ' . $command;
107  $output->writeln(['', str_pad("---- {$message} ", 70, '-'), '']);
108  passthru($command, $returnVal);
109  if ($returnVal) {
110  $failures[] = $message;
111  }
112  }
113 
114  $output->writeln(str_repeat('-', 70));
115  if ($failures) {
116  $output->writeln("FAILED - " . count($failures) . ' of ' . count($runCommands) . ":");
117  foreach ($failures as $message) {
118  $output->writeln(' - ' . $message);
119  }
120  // we must have an exit code higher than zero to indicate something was wrong
121  return \Magento\Framework\Console\Cli::RETURN_FAILURE;
122  } else {
123  $output->writeln('PASSED (' . count($runCommands) . ')');
124  }
125  return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
126  }
127 
133  private function setupTestInfo()
134  {
135  $this->commands = [
136  'unit' => ['../tests/unit', ''],
137  'unit-static' => ['../tests/static/framework/tests/unit', ''],
138  'unit-integration' => ['../tests/integration/framework/tests/unit', ''],
139  'integration' => ['../tests/integration', ''],
140  'integration-integrity' => ['../tests/integration', ' testsuite/Magento/Test/Integrity'],
141  'static-default' => ['../tests/static', ''],
142  'static-legacy' => ['../tests/static', ' testsuite/Magento/Test/Legacy'],
143  'static-integration-js' => ['../tests/static', ' testsuite/Magento/Test/Js/Exemplar'],
144  ];
145  $this->types = [
146  'all' => array_keys($this->commands),
147  'unit' => ['unit', 'unit-static', 'unit-integration'],
148  'integration' => ['integration'],
149  'integration-all' => ['integration', 'integration-integrity'],
150  'static' => ['static-default'],
151  'static-all' => ['static-default', 'static-legacy', 'static-integration-js'],
152  'integrity' => ['static-default', 'static-legacy', 'integration-integrity'],
153  'legacy' => ['static-legacy'],
154  'default' => [
155  'unit',
156  'unit-static',
157  'unit-integration',
158  'integration',
159  'static-default',
160  ],
161  ];
162  }
163 }
if(!file_exists(VENDOR_PATH)) $vendorDir
Definition: autoload.php:25
$message
$type
Definition: item.phtml:13
execute(InputInterface $input, OutputInterface $output)
const BP
Definition: autoload.php:14
passthru($command, &$return_var=null)