|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types = 1); |
| 7 | + |
| 8 | +namespace Magento\FunctionalTestingFramework\Console; |
| 9 | + |
| 10 | +use Magento\FunctionalTestingFramework\Suite\Handlers\SuiteObjectHandler; |
| 11 | +use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig; |
| 12 | +use Magento\FunctionalTestingFramework\Test\Handlers\TestObjectHandler; |
| 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; |
| 19 | +use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException; |
| 20 | + |
| 21 | +class RunTestFailedCommand extends BaseGenerateCommand |
| 22 | +{ |
| 23 | + const DEFAULT_TEST_GROUP = 'default'; |
| 24 | + |
| 25 | + /** |
| 26 | + * Configures the current command. |
| 27 | + * |
| 28 | + * @return void |
| 29 | + */ |
| 30 | + protected function configure() |
| 31 | + { |
| 32 | + $this->setName('run:failed') |
| 33 | + ->setDescription('Execute a set of tests referenced via group annotations') |
| 34 | + ->addOption( |
| 35 | + 'skip-generate', |
| 36 | + 'k', |
| 37 | + InputOption::VALUE_NONE, |
| 38 | + "only execute a group of tests without generating from source xml" |
| 39 | + )->addOption( |
| 40 | + "force", |
| 41 | + 'f', |
| 42 | + InputOption::VALUE_NONE, |
| 43 | + 'force generation of tests regardless of Magento Instance Configuration' |
| 44 | + ); |
| 45 | + |
| 46 | + parent::configure(); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Executes the current command. |
| 51 | + * |
| 52 | + * @param InputInterface $input |
| 53 | + * @param OutputInterface $output |
| 54 | + * @return integer|null|void |
| 55 | + * @throws \Exception |
| 56 | + * |
| 57 | + * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
| 58 | + */ |
| 59 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 60 | + { |
| 61 | + $skipGeneration = $input->getOption('skip-generate'); |
| 62 | + $force = $input->getOption('force'); |
| 63 | +// $groups = $input->getArgument('groups'); |
| 64 | + $remove = $input->getOption('remove'); |
| 65 | + |
| 66 | + if ($skipGeneration and $remove) { |
| 67 | + // "skip-generate" and "remove" options cannot be used at the same time |
| 68 | + throw new TestFrameworkException( |
| 69 | + "\"skip-generate\" and \"remove\" options can not be used at the same time." |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + // Create Mftf Configuration |
| 74 | + MftfApplicationConfig::create( |
| 75 | + $force, |
| 76 | + MftfApplicationConfig::GENERATION_PHASE, |
| 77 | + false, |
| 78 | + false |
| 79 | + ); |
| 80 | + |
| 81 | + if (!$skipGeneration) { |
| 82 | + $testConfiguration = $this->getFailedTestList(); |
| 83 | + $command = $this->getApplication()->find('generate:tests'); |
| 84 | + $args = [ |
| 85 | + '--tests' => $testConfiguration, |
| 86 | + '--force' => $force, |
| 87 | + '--remove' => $remove |
| 88 | + ]; |
| 89 | + |
| 90 | + $command->run(new ArrayInput($args), $output); |
| 91 | + } |
| 92 | + |
| 93 | + $codeceptionCommand = realpath(PROJECT_ROOT . '/vendor/bin/codecept') . ' run functional --verbose --steps'; |
| 94 | + |
| 95 | + $process = new Process($codeceptionCommand); |
| 96 | + $process->setWorkingDirectory(TESTS_BP); |
| 97 | + $process->setIdleTimeout(600); |
| 98 | + $process->setTimeout(0); |
| 99 | + $process->run( |
| 100 | + function ($type, $buffer) use ($output) { |
| 101 | + $output->write($buffer); |
| 102 | + } |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Returns a json string of tests that failed on the last run |
| 108 | + * |
| 109 | + * @return string[] |
| 110 | + */ |
| 111 | + private function getFailedTestList() |
| 112 | + { |
| 113 | + $failedTestPath = TESTS_BP . |
| 114 | + DIRECTORY_SEPARATOR . |
| 115 | + "tests" . |
| 116 | + DIRECTORY_SEPARATOR . |
| 117 | + "_output" . |
| 118 | + DIRECTORY_SEPARATOR . |
| 119 | + "failed"; |
| 120 | + |
| 121 | + $failedTestDetails = ['tests' => [], 'suites' => []]; |
| 122 | + |
| 123 | + if (realpath($failedTestPath)) { |
| 124 | + |
| 125 | + $testList = file($failedTestPath,FILE_IGNORE_NEW_LINES); |
| 126 | + |
| 127 | + foreach ($testList as $test) { |
| 128 | + $testInfo = explode(DIRECTORY_SEPARATOR, $test); |
| 129 | + $testName = explode(":", $testInfo[count($testInfo) - 1])[1]; |
| 130 | + $suiteName = $testInfo[count($testInfo) - 2]; |
| 131 | + |
| 132 | + if ($suiteName == self::DEFAULT_TEST_GROUP) { |
| 133 | + array_push($failedTestDetails['tests'], $testName); |
| 134 | + } else { |
| 135 | + $failedTestDetails['suites'] = array_merge_recursive( |
| 136 | + $failedTestDetails['suites'], |
| 137 | + [$suiteName => $testName] |
| 138 | + ); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + $testConfigurationJson = json_encode($failedTestDetails); |
| 143 | + return $testConfigurationJson; |
| 144 | + } |
| 145 | +} |
0 commit comments