DevTestsRunCommand.php 5.46 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Developer\Console\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * Class DevTestsRunCommand
 *
 * Runs tests (unit, static, integration, etc.)
 */
class DevTestsRunCommand extends Command
{
    /**
     * input parameter parameter
     */
    const INPUT_ARG_TYPE = 'type';

    /**
     * PHPUnit arguments parameter
     */
    const INPUT_OPT_COMMAND_ARGUMENTS       = 'arguments';
    const INPUT_OPT_COMMAND_ARGUMENTS_SHORT = 'c';

    /**
     * command name
     */
    const COMMAND_NAME = 'dev:tests:run';

    /**
     * Maps types (from user input) to phpunit test names
     *
     * @var array
     */
    private $types;

    /**
     * Maps phpunit test names to directory and target name
     *
     * @var array
     */
    private $commands;

    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this->setupTestInfo();
        $this->setName(self::COMMAND_NAME)
            ->setDescription('Runs tests');

        $this->addArgument(
            self::INPUT_ARG_TYPE,
            InputArgument::OPTIONAL,
            'Type of test to run. Available types: ' . implode(', ', array_keys($this->types)),
            'default'
        );
        $this->addOption(
            self::INPUT_OPT_COMMAND_ARGUMENTS,
            self::INPUT_OPT_COMMAND_ARGUMENTS_SHORT,
            InputOption::VALUE_REQUIRED,
            'Additional arguments for PHPUnit. Example: "-c\'--filter=MyTest\'" (no spaces)',
            ''
        );
        parent::configure();
    }

    /**
     * Run the tests
     *
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return int Non zero if invalid type, 0 otherwise
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        /* Validate type argument is valid */
        $type = $input->getArgument(self::INPUT_ARG_TYPE);
        if (!isset($this->types[$type])) {
            $output->writeln(
                'Invalid type: "' . $type . '". Available types: ' . implode(', ', array_keys($this->types))
            );
            return 1;
        }

        $vendorDir = require BP . '/app/etc/vendor_path.php';

        $failures = [];
        $runCommands = $this->types[$type];
        foreach ($runCommands as $key) {
            list($dir, $options) = $this->commands[$key];
            $dirName = realpath(BP . '/dev/tests/' . $dir);
            chdir($dirName);
            $command = PHP_BINARY . ' ' . BP . '/' . $vendorDir . '/phpunit/phpunit/phpunit ' . $options;
            if ($commandArguments = $input->getOption(self::INPUT_OPT_COMMAND_ARGUMENTS)) {
                $command .= ' ' . $commandArguments;
            }
            $message = $dirName . '> ' . $command;
            $output->writeln(['', str_pad("---- {$message} ", 70, '-'), '']);
            passthru($command, $returnVal);
            if ($returnVal) {
                $failures[] = $message;
            }
        }

        $output->writeln(str_repeat('-', 70));
        if ($failures) {
            $output->writeln("FAILED - " . count($failures) . ' of ' . count($runCommands) . ":");
            foreach ($failures as $message) {
                $output->writeln(' - ' . $message);
            }
            // we must have an exit code higher than zero to indicate something was wrong
            return \Magento\Framework\Console\Cli::RETURN_FAILURE;
        } else {
            $output->writeln('PASSED (' . count($runCommands) . ')');
        }
        return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
    }

    /**
     * Fills in arrays that link test types to php unit tests and directories.
     *
     * @return void
     */
    private function setupTestInfo()
    {
        $this->commands = [
            'unit'                   => ['../tests/unit', ''],
            'unit-static'            => ['../tests/static/framework/tests/unit', ''],
            'unit-integration'       => ['../tests/integration/framework/tests/unit', ''],
            'integration'            => ['../tests/integration', ''],
            'integration-integrity'  => ['../tests/integration', ' testsuite/Magento/Test/Integrity'],
            'static-default'         => ['../tests/static', ''],
            'static-legacy'          => ['../tests/static', ' testsuite/Magento/Test/Legacy'],
            'static-integration-js'  => ['../tests/static', ' testsuite/Magento/Test/Js/Exemplar'],
        ];
        $this->types = [
            'all'             => array_keys($this->commands),
            'unit'            => ['unit', 'unit-static', 'unit-integration'],
            'integration'     => ['integration'],
            'integration-all' => ['integration', 'integration-integrity'],
            'static'          => ['static-default'],
            'static-all'      => ['static-default', 'static-legacy', 'static-integration-js'],
            'integrity'       => ['static-default', 'static-legacy', 'integration-integrity'],
            'legacy'          => ['static-legacy'],
            'default'         => [
                'unit',
                'unit-static',
                'unit-integration',
                'integration',
                'static-default',
            ],
        ];
    }
}