Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to define options to display colors #1527

Merged
merged 1 commit into from
Dec 15, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/TextUI/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class PHPUnit_TextUI_Command
* @var array
*/
protected $longOptions = array(
'colors' => null,
'colors==' => null,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbladen, if you use a single = then you make a value required.
As an example take a look on --coverage-html and --coverage-text.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tested that locally, fair enough.

'bootstrap=' => null,
'columns=' => null,
'configuration=' => null,
Expand Down Expand Up @@ -280,7 +280,7 @@ protected function handleArguments(array $argv)
foreach ($this->options[0] as $option) {
switch ($option[0]) {
case '--colors': {
$this->arguments['colors'] = true;
$this->arguments['colors'] = $option[1] ?: PHPUnit_TextUI_ResultPrinter::COLOR_AUTO;
}
break;

Expand Down Expand Up @@ -948,7 +948,7 @@ protected function showHelp()
--no-globals-backup Do not backup and restore \$GLOBALS for each test.
--static-backup Backup and restore static attributes for each test.

--colors Use colors in output.
--colors=<flag> Use colors in output ("never", "auto" or "always").
--columns <n> Number of columns to use for progress outout.
--columns max Use maximum number of columns for progress outout.
--stderr Write to STDERR instead of STDOUT.
Expand Down
24 changes: 19 additions & 5 deletions src/TextUI/ResultPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ class PHPUnit_TextUI_ResultPrinter extends PHPUnit_Util_Printer implements PHPUn
const EVENT_TESTSUITE_START = 2;
const EVENT_TESTSUITE_END = 3;

const COLOR_NEVER = 'never';
const COLOR_AUTO = 'auto';
const COLOR_ALWAYS = 'always';
const COLOR_DEFAULT = self::COLOR_NEVER;

/**
* @var array
*/
Expand Down Expand Up @@ -138,22 +143,26 @@ class PHPUnit_TextUI_ResultPrinter extends PHPUnit_Util_Printer implements PHPUn
*
* @param mixed $out
* @param boolean $verbose
* @param boolean $colors
* @param string $colors
* @param boolean $debug
* @param integer|string $numberOfColumns
* @throws PHPUnit_Framework_Exception
* @since Method available since Release 3.0.0
*/
public function __construct($out = null, $verbose = false, $colors = false, $debug = false, $numberOfColumns = 80)
public function __construct($out = null, $verbose = false, $colors = self::COLOR_DEFAULT, $debug = false, $numberOfColumns = 80)
{
parent::__construct($out);

if (!is_bool($verbose)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'boolean');
}

if (!is_bool($colors)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(3, 'boolean');
$availableColors = array(self::COLOR_NEVER, self::COLOR_AUTO, self::COLOR_ALWAYS);
if (!in_array($colors, $availableColors)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(
3,
vsprintf('value from "%s", "%s" or "%s"', $availableColors)
);
}

if (!is_bool($debug)) {
Expand All @@ -174,8 +183,13 @@ public function __construct($out = null, $verbose = false, $colors = false, $deb

$this->numberOfColumns = $numberOfColumns;
$this->verbose = $verbose;
$this->colors = $colors && $console->hasColorSupport();
$this->debug = $debug;

if ($colors === self::COLOR_AUTO && $console->hasColorSupport()) {
$this->colors = true;
} else {
$this->colors = (self::COLOR_ALWAYS === $colors);
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/TextUI/TestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ class_exists($arguments['printer'], false)) {
if (isset($arguments['coverageText'])) {
if ($arguments['coverageText'] == 'php://stdout') {
$outputStream = $this->printer;
$colors = (bool) $arguments['colors'];
$colors = $arguments['colors'];
} else {
$outputStream = new PHPUnit_Util_Printer($arguments['coverageText']);
$colors = false;
Expand Down Expand Up @@ -905,7 +905,7 @@ protected function handleConfiguration(array &$arguments)
$arguments['backupStaticAttributes'] = isset($arguments['backupStaticAttributes']) ? $arguments['backupStaticAttributes'] : null;
$arguments['cacheTokens'] = isset($arguments['cacheTokens']) ? $arguments['cacheTokens'] : false;
$arguments['columns'] = isset($arguments['columns']) ? $arguments['columns'] : 80;
$arguments['colors'] = isset($arguments['colors']) ? $arguments['colors'] : false;
$arguments['colors'] = isset($arguments['colors']) ? $arguments['colors'] : PHPUnit_TextUI_ResultPrinter::COLOR_DEFAULT;
$arguments['convertErrorsToExceptions'] = isset($arguments['convertErrorsToExceptions']) ? $arguments['convertErrorsToExceptions'] : true;
$arguments['convertNoticesToExceptions'] = isset($arguments['convertNoticesToExceptions']) ? $arguments['convertNoticesToExceptions'] : true;
$arguments['convertWarningsToExceptions'] = isset($arguments['convertWarningsToExceptions']) ? $arguments['convertWarningsToExceptions'] : true;
Expand Down
29 changes: 27 additions & 2 deletions src/Util/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,9 @@ public function getPHPUnitConfiguration()
}

if ($root->hasAttribute('colors')) {
$result['colors'] = $this->getBoolean(
(string) $root->getAttribute('colors'), false
$result['colors'] = $this->getColorFlag(
(string) $root->getAttribute('colors'),
PHPUnit_TextUI_ResultPrinter::COLOR_DEFAULT
);
}

Expand Down Expand Up @@ -979,6 +980,30 @@ protected function getTestSuite(DOMElement $testSuiteNode, $testSuiteFilter = nu
return $suite;
}

/**
* @param string $value
* @param boolean $default
* @return boolean
*/
protected function getColorFlag($value, $default)
{
$currentValue = strtolower($value);

if (in_array($currentValue, array('true', 'auto'))) {
return PHPUnit_TextUI_ResultPrinter::COLOR_AUTO;
}

if (in_array($currentValue, array('false', 'never'))) {
return PHPUnit_TextUI_ResultPrinter::COLOR_NEVER;
}

if (in_array($currentValue, array('always'))) {
return PHPUnit_TextUI_ResultPrinter::COLOR_ALWAYS;
}

return $default;
}

/**
* @param string $value
* @param boolean $default
Expand Down
19 changes: 19 additions & 0 deletions tests/TextUI/colors-always.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
phpunit --colors=always BankAccountTest ../_files/BankAccountTest.php
--FILE--
<?php
$_SERVER['argv'][1] = '--no-configuration';
$_SERVER['argv'][2] = '--colors=always';
$_SERVER['argv'][3] = __DIR__.'/../_files/BankAccountTest.php';

require __DIR__ . '/../bootstrap.php';
PHPUnit_TextUI_Command::main();
?>
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

...

Time: %s, Memory: %sMb

%s[30;42mOK (3 tests, 3 assertions)%s[0m
2 changes: 1 addition & 1 deletion tests/TextUI/help.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Test Execution Options:
--no-globals-backup Do not backup and restore $GLOBALS for each test.
--static-backup Backup and restore static attributes for each test.

--colors Use colors in output.
--colors=<flag> Use colors in output ("never", "auto" or "always").
--columns <n> Number of columns to use for progress outout.
--columns max Use maximum number of columns for progress outout.
--stderr Write to STDERR instead of STDOUT.
Expand Down
2 changes: 1 addition & 1 deletion tests/TextUI/help2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Test Execution Options:
--no-globals-backup Do not backup and restore $GLOBALS for each test.
--static-backup Backup and restore static attributes for each test.

--colors Use colors in output.
--colors=<flag> Use colors in output ("never", "auto" or "always").
--columns <n> Number of columns to use for progress outout.
--columns max Use maximum number of columns for progress outout.
--stderr Write to STDERR instead of STDOUT.
Expand Down
2 changes: 1 addition & 1 deletion tests/Util/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public function testPHPUnitConfigurationIsReadCorrectly()
'bootstrap' => '/path/to/bootstrap.php',
'cacheTokens' => false,
'columns' => 80,
'colors' => false,
'colors' => 'never',
'stderr' => false,
'convertErrorsToExceptions' => true,
'convertNoticesToExceptions' => true,
Expand Down