DevTestsRunCommand.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Developer\Console\Command;
  7. use Symfony\Component\Console\Command\Command;
  8. use Symfony\Component\Console\Input\InputArgument;
  9. use Symfony\Component\Console\Input\InputInterface;
  10. use Symfony\Component\Console\Input\InputOption;
  11. use Symfony\Component\Console\Output\OutputInterface;
  12. /**
  13. * Class DevTestsRunCommand
  14. *
  15. * Runs tests (unit, static, integration, etc.)
  16. */
  17. class DevTestsRunCommand extends Command
  18. {
  19. /**
  20. * input parameter parameter
  21. */
  22. const INPUT_ARG_TYPE = 'type';
  23. /**
  24. * PHPUnit arguments parameter
  25. */
  26. const INPUT_OPT_COMMAND_ARGUMENTS = 'arguments';
  27. const INPUT_OPT_COMMAND_ARGUMENTS_SHORT = 'c';
  28. /**
  29. * command name
  30. */
  31. const COMMAND_NAME = 'dev:tests:run';
  32. /**
  33. * Maps types (from user input) to phpunit test names
  34. *
  35. * @var array
  36. */
  37. private $types;
  38. /**
  39. * Maps phpunit test names to directory and target name
  40. *
  41. * @var array
  42. */
  43. private $commands;
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected function configure()
  48. {
  49. $this->setupTestInfo();
  50. $this->setName(self::COMMAND_NAME)
  51. ->setDescription('Runs tests');
  52. $this->addArgument(
  53. self::INPUT_ARG_TYPE,
  54. InputArgument::OPTIONAL,
  55. 'Type of test to run. Available types: ' . implode(', ', array_keys($this->types)),
  56. 'default'
  57. );
  58. $this->addOption(
  59. self::INPUT_OPT_COMMAND_ARGUMENTS,
  60. self::INPUT_OPT_COMMAND_ARGUMENTS_SHORT,
  61. InputOption::VALUE_REQUIRED,
  62. 'Additional arguments for PHPUnit. Example: "-c\'--filter=MyTest\'" (no spaces)',
  63. ''
  64. );
  65. parent::configure();
  66. }
  67. /**
  68. * Run the tests
  69. *
  70. * @param InputInterface $input
  71. * @param OutputInterface $output
  72. * @return int Non zero if invalid type, 0 otherwise
  73. */
  74. protected function execute(InputInterface $input, OutputInterface $output)
  75. {
  76. /* Validate type argument is valid */
  77. $type = $input->getArgument(self::INPUT_ARG_TYPE);
  78. if (!isset($this->types[$type])) {
  79. $output->writeln(
  80. 'Invalid type: "' . $type . '". Available types: ' . implode(', ', array_keys($this->types))
  81. );
  82. return 1;
  83. }
  84. $vendorDir = require BP . '/app/etc/vendor_path.php';
  85. $failures = [];
  86. $runCommands = $this->types[$type];
  87. foreach ($runCommands as $key) {
  88. list($dir, $options) = $this->commands[$key];
  89. $dirName = realpath(BP . '/dev/tests/' . $dir);
  90. chdir($dirName);
  91. $command = PHP_BINARY . ' ' . BP . '/' . $vendorDir . '/phpunit/phpunit/phpunit ' . $options;
  92. if ($commandArguments = $input->getOption(self::INPUT_OPT_COMMAND_ARGUMENTS)) {
  93. $command .= ' ' . $commandArguments;
  94. }
  95. $message = $dirName . '> ' . $command;
  96. $output->writeln(['', str_pad("---- {$message} ", 70, '-'), '']);
  97. passthru($command, $returnVal);
  98. if ($returnVal) {
  99. $failures[] = $message;
  100. }
  101. }
  102. $output->writeln(str_repeat('-', 70));
  103. if ($failures) {
  104. $output->writeln("FAILED - " . count($failures) . ' of ' . count($runCommands) . ":");
  105. foreach ($failures as $message) {
  106. $output->writeln(' - ' . $message);
  107. }
  108. // we must have an exit code higher than zero to indicate something was wrong
  109. return \Magento\Framework\Console\Cli::RETURN_FAILURE;
  110. } else {
  111. $output->writeln('PASSED (' . count($runCommands) . ')');
  112. }
  113. return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
  114. }
  115. /**
  116. * Fills in arrays that link test types to php unit tests and directories.
  117. *
  118. * @return void
  119. */
  120. private function setupTestInfo()
  121. {
  122. $this->commands = [
  123. 'unit' => ['../tests/unit', ''],
  124. 'unit-static' => ['../tests/static/framework/tests/unit', ''],
  125. 'unit-integration' => ['../tests/integration/framework/tests/unit', ''],
  126. 'integration' => ['../tests/integration', ''],
  127. 'integration-integrity' => ['../tests/integration', ' testsuite/Magento/Test/Integrity'],
  128. 'static-default' => ['../tests/static', ''],
  129. 'static-legacy' => ['../tests/static', ' testsuite/Magento/Test/Legacy'],
  130. 'static-integration-js' => ['../tests/static', ' testsuite/Magento/Test/Js/Exemplar'],
  131. ];
  132. $this->types = [
  133. 'all' => array_keys($this->commands),
  134. 'unit' => ['unit', 'unit-static', 'unit-integration'],
  135. 'integration' => ['integration'],
  136. 'integration-all' => ['integration', 'integration-integrity'],
  137. 'static' => ['static-default'],
  138. 'static-all' => ['static-default', 'static-legacy', 'static-integration-js'],
  139. 'integrity' => ['static-default', 'static-legacy', 'integration-integrity'],
  140. 'legacy' => ['static-legacy'],
  141. 'default' => [
  142. 'unit',
  143. 'unit-static',
  144. 'unit-integration',
  145. 'integration',
  146. 'static-default',
  147. ],
  148. ];
  149. }
  150. }