You are here

protected function PHPUnit_TextUI_Command::handleArguments in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/phpunit/phpunit/src/TextUI/Command.php \PHPUnit_TextUI_Command::handleArguments()

Handles the command-line arguments.

A child class of PHPUnit_TextUI_Command can hook into the argument parsing by adding the switch(es) to the $longOptions array and point to a callback method that handles the switch(es) in the child class like this

<code> <?php class MyCommand extends PHPUnit_TextUI_Command { public function __construct() { // my-switch won't accept a value, it's an on/off $this->longOptions['my-switch'] = 'myHandler'; // my-secondswitch will accept a value - note the equals sign $this->longOptions['my-secondswitch='] = 'myOtherHandler'; }

// --my-switch -> myHandler() protected function myHandler() { }

// --my-secondswitch foo -> myOtherHandler('foo') protected function myOtherHandler ($value) { }

// You will also need this - the static keyword in the // PHPUnit_TextUI_Command will mean that it'll be // PHPUnit_TextUI_Command that gets instantiated, // not MyCommand public static function main($exit = true) { $command = new static;

return $command->run($_SERVER['argv'], $exit); }

} </code>

Parameters

array $argv:

1 call to PHPUnit_TextUI_Command::handleArguments()
PHPUnit_TextUI_Command::run in vendor/phpunit/phpunit/src/TextUI/Command.php

File

vendor/phpunit/phpunit/src/TextUI/Command.php, line 224

Class

PHPUnit_TextUI_Command
A TestRunner for the Command Line Interface (CLI) PHP SAPI Module.

Code

protected function handleArguments(array $argv) {
  if (defined('__PHPUNIT_PHAR__')) {
    $this->longOptions['check-version'] = null;
    $this->longOptions['selfupdate'] = null;
    $this->longOptions['self-update'] = null;
  }
  try {
    $this->options = PHPUnit_Util_Getopt::getopt($argv, 'd:c:hv', array_keys($this->longOptions));
  } catch (PHPUnit_Framework_Exception $e) {
    $this
      ->showError($e
      ->getMessage());
  }
  foreach ($this->options[0] as $option) {
    switch ($option[0]) {
      case '--colors':
        $this->arguments['colors'] = $option[1] ?: PHPUnit_TextUI_ResultPrinter::COLOR_AUTO;
        break;
      case '--bootstrap':
        $this->arguments['bootstrap'] = $option[1];
        break;
      case '--columns':
        if (is_numeric($option[1])) {
          $this->arguments['columns'] = (int) $option[1];
        }
        elseif ($option[1] == 'max') {
          $this->arguments['columns'] = 'max';
        }
        break;
      case 'c':
      case '--configuration':
        $this->arguments['configuration'] = $option[1];
        break;
      case '--coverage-clover':
        $this->arguments['coverageClover'] = $option[1];
        break;
      case '--coverage-crap4j':
        $this->arguments['coverageCrap4J'] = $option[1];
        break;
      case '--coverage-html':
        $this->arguments['coverageHtml'] = $option[1];
        break;
      case '--coverage-php':
        $this->arguments['coveragePHP'] = $option[1];
        break;
      case '--coverage-text':
        if ($option[1] === null) {
          $option[1] = 'php://stdout';
        }
        $this->arguments['coverageText'] = $option[1];
        $this->arguments['coverageTextShowUncoveredFiles'] = false;
        $this->arguments['coverageTextShowOnlySummary'] = false;
        break;
      case '--coverage-xml':
        $this->arguments['coverageXml'] = $option[1];
        break;
      case 'd':
        $ini = explode('=', $option[1]);
        if (isset($ini[0])) {
          if (isset($ini[1])) {
            ini_set($ini[0], $ini[1]);
          }
          else {
            ini_set($ini[0], true);
          }
        }
        break;
      case '--debug':
        $this->arguments['debug'] = true;
        break;
      case 'h':
      case '--help':
        $this
          ->showHelp();
        exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
        break;
      case '--filter':
        $this->arguments['filter'] = $option[1];
        break;
      case '--testsuite':
        $this->arguments['testsuite'] = $option[1];
        break;
      case '--group':
        $this->arguments['groups'] = explode(',', $option[1]);
        break;
      case '--exclude-group':
        $this->arguments['excludeGroups'] = explode(',', $option[1]);
        break;
      case '--test-suffix':
        $this->arguments['testSuffixes'] = explode(',', $option[1]);
        break;
      case '--include-path':
        $includePath = $option[1];
        break;
      case '--list-groups':
        $this->arguments['listGroups'] = true;
        break;
      case '--printer':
        $this->arguments['printer'] = $option[1];
        break;
      case '--loader':
        $this->arguments['loader'] = $option[1];
        break;
      case '--log-json':
        $this->arguments['jsonLogfile'] = $option[1];
        break;
      case '--log-junit':
        $this->arguments['junitLogfile'] = $option[1];
        break;
      case '--log-tap':
        $this->arguments['tapLogfile'] = $option[1];
        break;
      case '--process-isolation':
        $this->arguments['processIsolation'] = true;
        break;
      case '--repeat':
        $this->arguments['repeat'] = (int) $option[1];
        break;
      case '--stderr':
        $this->arguments['stderr'] = true;
        break;
      case '--stop-on-error':
        $this->arguments['stopOnError'] = true;
        break;
      case '--stop-on-failure':
        $this->arguments['stopOnFailure'] = true;
        break;
      case '--stop-on-incomplete':
        $this->arguments['stopOnIncomplete'] = true;
        break;
      case '--stop-on-risky':
        $this->arguments['stopOnRisky'] = true;
        break;
      case '--stop-on-skipped':
        $this->arguments['stopOnSkipped'] = true;
        break;
      case '--tap':
        $this->arguments['printer'] = 'PHPUnit_Util_Log_TAP';
        break;
      case '--testdox':
        $this->arguments['printer'] = 'PHPUnit_Util_TestDox_ResultPrinter_Text';
        break;
      case '--testdox-html':
        $this->arguments['testdoxHTMLFile'] = $option[1];
        break;
      case '--testdox-text':
        $this->arguments['testdoxTextFile'] = $option[1];
        break;
      case '--no-configuration':
        $this->arguments['useDefaultConfiguration'] = false;
        break;
      case '--no-coverage':
        $this->arguments['noCoverage'] = true;
        break;
      case '--no-globals-backup':
        $this->arguments['backupGlobals'] = false;
        break;
      case '--static-backup':
        $this->arguments['backupStaticAttributes'] = true;
        break;
      case 'v':
      case '--verbose':
        $this->arguments['verbose'] = true;
        break;
      case '--version':
        $this
          ->printVersionString();
        exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
        break;
      case '--report-useless-tests':
        $this->arguments['reportUselessTests'] = true;
        break;
      case '--strict-coverage':
        $this->arguments['strictCoverage'] = true;
        break;
      case '--strict-global-state':
        $this->arguments['disallowChangesToGlobalState'] = true;
        break;
      case '--disallow-test-output':
        $this->arguments['disallowTestOutput'] = true;
        break;
      case '--enforce-time-limit':
        $this->arguments['enforceTimeLimit'] = true;
        break;
      case '--disallow-todo-tests':
        $this->arguments['disallowTodoAnnotatedTests'] = true;
        break;
      case '--strict':
        $this->arguments['reportUselessTests'] = true;
        $this->arguments['strictCoverage'] = true;
        $this->arguments['disallowTestOutput'] = true;
        $this->arguments['enforceTimeLimit'] = true;
        $this->arguments['disallowTodoAnnotatedTests'] = true;
        $this->arguments['deprecatedStrictModeOption'] = true;
        break;
      case '--check-version':
        $this
          ->handleVersionCheck();
        break;
      case '--selfupdate':
      case '--self-update':
        $this
          ->handleSelfUpdate();
        break;
      default:
        $optionName = str_replace('--', '', $option[0]);
        if (isset($this->longOptions[$optionName])) {
          $handler = $this->longOptions[$optionName];
        }
        elseif (isset($this->longOptions[$optionName . '='])) {
          $handler = $this->longOptions[$optionName . '='];
        }
        if (isset($handler) && is_callable(array(
          $this,
          $handler,
        ))) {
          $this
            ->{$handler}($option[1]);
        }
    }
  }
  $this
    ->handleCustomTestSuite();
  if (!isset($this->arguments['test'])) {
    if (isset($this->options[1][0])) {
      $this->arguments['test'] = $this->options[1][0];
    }
    if (isset($this->options[1][1])) {
      $this->arguments['testFile'] = realpath($this->options[1][1]);
    }
    else {
      $this->arguments['testFile'] = '';
    }
    if (isset($this->arguments['test']) && is_file($this->arguments['test']) && substr($this->arguments['test'], -5, 5) != '.phpt') {
      $this->arguments['testFile'] = realpath($this->arguments['test']);
      $this->arguments['test'] = substr($this->arguments['test'], 0, strrpos($this->arguments['test'], '.'));
    }
  }
  if (!isset($this->arguments['testSuffixes'])) {
    $this->arguments['testSuffixes'] = array(
      'Test.php',
      '.phpt',
    );
  }
  if (isset($includePath)) {
    ini_set('include_path', $includePath . PATH_SEPARATOR . ini_get('include_path'));
  }
  if ($this->arguments['loader'] !== null) {
    $this->arguments['loader'] = $this
      ->handleLoader($this->arguments['loader']);
  }
  if (isset($this->arguments['configuration']) && is_dir($this->arguments['configuration'])) {
    $configurationFile = $this->arguments['configuration'] . '/phpunit.xml';
    if (file_exists($configurationFile)) {
      $this->arguments['configuration'] = realpath($configurationFile);
    }
    elseif (file_exists($configurationFile . '.dist')) {
      $this->arguments['configuration'] = realpath($configurationFile . '.dist');
    }
  }
  elseif (!isset($this->arguments['configuration']) && $this->arguments['useDefaultConfiguration']) {
    if (file_exists('phpunit.xml')) {
      $this->arguments['configuration'] = realpath('phpunit.xml');
    }
    elseif (file_exists('phpunit.xml.dist')) {
      $this->arguments['configuration'] = realpath('phpunit.xml.dist');
    }
  }
  if (isset($this->arguments['configuration'])) {
    try {
      $configuration = PHPUnit_Util_Configuration::getInstance($this->arguments['configuration']);
    } catch (Throwable $e) {
      print $e
        ->getMessage() . "\n";
      exit(PHPUnit_TextUI_TestRunner::FAILURE_EXIT);
    } catch (Exception $e) {
      print $e
        ->getMessage() . "\n";
      exit(PHPUnit_TextUI_TestRunner::FAILURE_EXIT);
    }
    $phpunit = $configuration
      ->getPHPUnitConfiguration();
    $configuration
      ->handlePHPConfiguration();

    /*
     * Issue #1216
     */
    if (isset($this->arguments['bootstrap'])) {
      $this
        ->handleBootstrap($this->arguments['bootstrap']);
    }
    elseif (isset($phpunit['bootstrap'])) {
      $this
        ->handleBootstrap($phpunit['bootstrap']);
    }

    /*
     * Issue #657
     */
    if (isset($phpunit['stderr']) && !isset($this->arguments['stderr'])) {
      $this->arguments['stderr'] = $phpunit['stderr'];
    }
    if (isset($phpunit['columns']) && !isset($this->arguments['columns'])) {
      $this->arguments['columns'] = $phpunit['columns'];
    }
    if (isset($phpunit['printerClass'])) {
      if (isset($phpunit['printerFile'])) {
        $file = $phpunit['printerFile'];
      }
      else {
        $file = '';
      }
      $this->arguments['printer'] = $this
        ->handlePrinter($phpunit['printerClass'], $file);
    }
    if (isset($phpunit['testSuiteLoaderClass'])) {
      if (isset($phpunit['testSuiteLoaderFile'])) {
        $file = $phpunit['testSuiteLoaderFile'];
      }
      else {
        $file = '';
      }
      $this->arguments['loader'] = $this
        ->handleLoader($phpunit['testSuiteLoaderClass'], $file);
    }
    $browsers = $configuration
      ->getSeleniumBrowserConfiguration();
    if (!empty($browsers)) {
      $this->arguments['deprecatedSeleniumConfiguration'] = true;
      if (class_exists('PHPUnit_Extensions_SeleniumTestCase')) {
        PHPUnit_Extensions_SeleniumTestCase::$browsers = $browsers;
      }
    }
    if (!isset($this->arguments['test'])) {
      $testSuite = $configuration
        ->getTestSuiteConfiguration(isset($this->arguments['testsuite']) ? $this->arguments['testsuite'] : null);
      if ($testSuite !== null) {
        $this->arguments['test'] = $testSuite;
      }
    }
  }
  elseif (isset($this->arguments['bootstrap'])) {
    $this
      ->handleBootstrap($this->arguments['bootstrap']);
  }
  if (isset($this->arguments['printer']) && is_string($this->arguments['printer'])) {
    $this->arguments['printer'] = $this
      ->handlePrinter($this->arguments['printer']);
  }
  if (isset($this->arguments['test']) && is_string($this->arguments['test']) && substr($this->arguments['test'], -5, 5) == '.phpt') {
    $test = new PHPUnit_Extensions_PhptTestCase($this->arguments['test']);
    $this->arguments['test'] = new PHPUnit_Framework_TestSuite();
    $this->arguments['test']
      ->addTest($test);
  }
  if (!isset($this->arguments['test']) || isset($this->arguments['testDatabaseLogRevision']) && !isset($this->arguments['testDatabaseDSN'])) {
    $this
      ->showHelp();
    exit(PHPUnit_TextUI_TestRunner::EXCEPTION_EXIT);
  }
}