Skip to content

Commit

Permalink
Merge pull request #224 from elliotchance/1.4/193-set-color-scheme
Browse files Browse the repository at this point in the history
1.4/193 set color scheme
  • Loading branch information
elliotchance committed Nov 24, 2014
2 parents 30ba490 + 03c4c4e commit f75d62a
Show file tree
Hide file tree
Showing 9 changed files with 289 additions and 57 deletions.
47 changes: 45 additions & 2 deletions src/Concise/Console/Command.php
Expand Up @@ -5,11 +5,14 @@
use Concise\Console\TestRunner\DefaultTestRunner;
use Concise\Console\ResultPrinter\ResultPrinterProxy;
use Concise\Console\ResultPrinter\DefaultResultPrinter;
use Concise\Console\ResultPrinter\CIResultPrinter;
use Concise\Console\Theme\DefaultTheme;
use Exception;
use Concise\Console\ResultPrinter\CIResultPrinter;

class Command extends \PHPUnit_TextUI_Command
{
protected $colorScheme = null;

protected $ci = false;

protected function createRunner()
Expand All @@ -24,6 +27,36 @@ protected function createRunner()
return $testRunner;
}

protected function getThemeForClass($class)
{
if (class_exists($class)) {
return new $class();
}

return null;
}

protected function findTheme()
{
$candidates = array($this->colorScheme, "Concise\\Console\\Theme\\{$this->colorScheme}Theme");
foreach ($candidates as $class) {
if ($r = $this->getThemeForClass($class)) {
return $r;
}
}

throw new Exception("No such color scheme '{$this->colorScheme}'.");
}

public function getColorScheme()
{
if ($this->colorScheme) {
return $this->findTheme();
}

return new DefaultTheme();
}

public function getResultPrinter()
{
if ($this->ci || `tput colors` < 2) {
Expand All @@ -39,17 +72,27 @@ public function getResultPrinter()
protected function handleArguments(array $argv)
{
$this->longOptions['test-colors'] = null;
$this->longOptions['color-scheme='] = null;
$this->longOptions['list-color-schemes'] = null;
$this->longOptions['ci'] = null;
parent::handleArguments($argv);

foreach ($this->options[0] as $option) {
switch ($option[0]) {
case '--test-colors':
$testColors = new TestColors(new DefaultTheme());
$testColors = new TestColors($this->getColorScheme());
echo $testColors->renderAll();
exit(0);
break;

case '--color-scheme':
$this->colorTheme = $option[1];
break;

case '--list-color-schemes':
echo "Color Schemes:\n default\n\n";
exit(0);

case '--ci':
$this->ci = true;
break;
Expand Down
2 changes: 1 addition & 1 deletion src/Concise/Console/TestColors.php
Expand Up @@ -19,6 +19,6 @@ public function renderAll()
$renderer->render('? is instance of ?', array(function () {}, 'Closure')),
);

return "Some assertion examples:\n " . implode("\n ", $lines) . "\n\n";
return "Some assertion examples:\n\n" . implode("\n\n", $lines) . "\n\n";
}
}
89 changes: 89 additions & 0 deletions src/Concise/Console/Theme/AbstractTheme.php
@@ -0,0 +1,89 @@
<?php

namespace Concise\Console\Theme;

use PHPUnit_Runner_BaseTestRunner;

abstract class AbstractTheme implements ThemeInterface
{
/**
* @return array
*/
public function getTheme()
{
return array(
PHPUnit_Runner_BaseTestRunner::STATUS_PASSED => $this->getStatusPassedColor(),
PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE => $this->getStatusFailureColor(),
PHPUnit_Runner_BaseTestRunner::STATUS_ERROR => $this->getStatusErrorColor(),
PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED => $this->getStatusSkippedColor(),
PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE => $this->getStatusIncompleteColor(),
PHPUnit_Runner_BaseTestRunner::STATUS_RISKY => $this->getStatusRiskyColor(),
'value.integer' => $this->getValueIntegerColor(),
'value.float' => $this->getValueFloatColor(),
'value.string' => $this->getValueStringColor(),
'value.closure' => $this->getValueClosureColor(),
'value.null' => $this->getValueNullColor(),
'value.boolean' => $this->getValueBooleanColor(),
);
}

public function getValueIntegerColor()
{
return 'red';
}

public function getValueFloatColor()
{
return 'magenta';
}

public function getValueStringColor()
{
return 'yellow';
}

public function getValueClosureColor()
{
return 'cyan';
}

public function getValueNullColor()
{
return 'blue';
}

public function getValueBooleanColor()
{
return 'green';
}

public function getStatusPassedColor()
{
return 'green';
}

public function getStatusFailureColor()
{
return 'red';
}

public function getStatusErrorColor()
{
return 'red';
}

public function getStatusSkippedColor()
{
return 'blue';
}

public function getStatusIncompleteColor()
{
return 'yellow';
}

public function getStatusRiskyColor()
{
return 'yellow';
}
}
24 changes: 1 addition & 23 deletions src/Concise/Console/Theme/DefaultTheme.php
Expand Up @@ -2,28 +2,6 @@

namespace Concise\Console\Theme;

use PHPUnit_Runner_BaseTestRunner;

class DefaultTheme
class DefaultTheme extends AbstractTheme
{
/**
* @return array
*/
public function getTheme()
{
return array(
PHPUnit_Runner_BaseTestRunner::STATUS_PASSED => 'green',
PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE => 'red',
PHPUnit_Runner_BaseTestRunner::STATUS_ERROR => 'red',
PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED => 'blue',
PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE => 'yellow',
PHPUnit_Runner_BaseTestRunner::STATUS_RISKY => 'yellow',
'value.integer' => 'red',
'value.float' => 'magenta',
'value.string' => 'yellow',
'value.closure' => 'cyan',
'value.null' => 'blue',
'value.boolean' => 'green',
);
}
}
35 changes: 35 additions & 0 deletions src/Concise/Console/Theme/ThemeInterface.php
@@ -0,0 +1,35 @@
<?php

namespace Concise\Console\Theme;

interface ThemeInterface
{
/**
* @return array
*/
public function getTheme();

public function getValueIntegerColor();

public function getValueFloatColor();

public function getValueStringColor();

public function getValueClosureColor();

public function getValueNullColor();

public function getValueBooleanColor();

public function getStatusPassedColor();

public function getStatusFailureColor();

public function getStatusErrorColor();

public function getStatusSkippedColor();

public function getStatusIncompleteColor();

public function getStatusRiskyColor();
}
50 changes: 50 additions & 0 deletions tests/Concise/Console/CommandColorSchemeTest.php
@@ -0,0 +1,50 @@
<?php

namespace Concise\Console;

use \Concise\TestCase;

class CommandColorSchemeTest extends TestCase
{
protected $command;

public function setUp()
{
parent::setUp();
$this->command = new Command();
}

public function testDefaultColorSchemeIsSet()
{
$this->assert($this->command->getColorScheme(), instance_of, 'Concise\Console\Theme\DefaultTheme');
}

public function testColorSchemeCanBeAClassName()
{
$theme = $this->mock('Concise\Console\Theme\DefaultTheme')->get();
$this->setProperty($this->command, 'colorScheme', get_class($theme));
$this->assert(get_class($this->command->getColorScheme()), equals, get_class($theme));
}

/**
* @expectedException \Exception
* @expectedExceptionMessage No such color scheme 'Foo\Bar'.
*/
public function testColorSchemeWillThrowExceptionIfColorSchemeClassDoesNotExist()
{
$this->setProperty($this->command, 'colorScheme', 'Foo\Bar');
$this->command->getColorScheme();
}

public function testColorSchemeCanBeAClassNameFoundInTheDefaultNamespace()
{
$this->setProperty($this->command, 'colorScheme', 'Default');
$this->assert($this->command->getColorScheme(), instance_of, 'Concise\Console\Theme\DefaultTheme');
}

public function testColorSchemeWithLowerCase()
{
$this->setProperty($this->command, 'colorScheme', 'default');
$this->assert($this->command->getColorScheme(), instance_of, 'Concise\Console\Theme\DefaultTheme');
}
}
31 changes: 1 addition & 30 deletions tests/Concise/Console/Theme/DefaultThemeTest.php
Expand Up @@ -2,10 +2,7 @@

namespace Concise\Console\Theme;

use Concise\TestCase;
use PHPUnit_Runner_BaseTestRunner;

class DefaultThemeTest extends TestCase
class DefaultThemeTest extends ThemeTestCase
{
protected $theme;

Expand All @@ -14,30 +11,4 @@ public function setUp()
parent::setUp();
$this->theme = new DefaultTheme();
}

public function keysProvider()
{
return array(
array(PHPUnit_Runner_BaseTestRunner::STATUS_PASSED),
array(PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE),
array(PHPUnit_Runner_BaseTestRunner::STATUS_ERROR),
array(PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED),
array(PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE),
array(PHPUnit_Runner_BaseTestRunner::STATUS_RISKY),
array('value.integer'),
array('value.float'),
array('value.string'),
array('value.closure'),
array('value.null'),
array('value.boolean'),
);
}

/**
* @dataProvider keysProvider
*/
public function testThemeHasSuccess($expectedKey)
{
$this->assert($this->theme->getTheme(), has_key, $expectedKey);
}
}

0 comments on commit f75d62a

Please sign in to comment.