Skip to content

Commit

Permalink
Merge pull request #151 from elliotchance/1.3.3/151-understand-verbosity
Browse files Browse the repository at this point in the history
Understand verbosity
  • Loading branch information
elliotchance committed Sep 13, 2014
2 parents e726987 + af17b4c commit 4629013
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 13 deletions.
7 changes: 6 additions & 1 deletion src/Concise/Console/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@

use Concise\Console\TestRunner\DefaultTestRunner;
use Concise\Console\ResultPrinter\ResultPrinterProxy;
use Concise\Console\ResultPrinter\DefaultResultPrinter;

class Command extends \PHPUnit_TextUI_Command
{
protected function createRunner()
{
$resultPrinter = new DefaultResultPrinter();
if (array_key_exists('verbose', $this->arguments) && $this->arguments['verbose']) {
$resultPrinter->setVerbose(true);
}
$testRunner = new DefaultTestRunner();
$testRunner->setPrinter(new ResultPrinterProxy());
$testRunner->setPrinter(new ResultPrinterProxy($resultPrinter));

return $testRunner;
}
Expand Down
12 changes: 12 additions & 0 deletions src/Concise/Console/ResultPrinter/AbstractResultPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ abstract class AbstractResultPrinter implements TestResultDelegateInterface, Sta

public $assertionCount = 0;

protected $verbose = false;

public function getSuccessCount()
{
return $this->getTestCount() - $this->getFailureCount() - $this->getErrorCount()
Expand Down Expand Up @@ -91,4 +93,14 @@ public function write($string)
public function end()
{
}

public function setVerbose($verbose)
{
$this->verbose = $verbose;
}

public function isVerbose()
{
return $this->verbose;
}
}
9 changes: 9 additions & 0 deletions src/Concise/Console/ResultPrinter/DefaultResultPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ public function update()

protected function add($status, PHPUnit_Framework_Test $test, Exception $e)
{
switch ($status) {
case PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED:
case PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE:
case PHPUnit_Runner_BaseTestRunner::STATUS_RISKY:
if (!$this->isVerbose()) {
return;
}
}

$renderIssue = new RenderIssue();
$message = $renderIssue->render($status, $this->issueNumber, $test, $e);
$this->appendTextAbove("$message\n\n");
Expand Down
8 changes: 2 additions & 6 deletions src/Concise/Console/ResultPrinter/ResultPrinterProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@ class ResultPrinterProxy extends \PHPUnit_TextUI_ResultPrinter

protected $totalSuccesses = 0;

public function __construct(TestResultDelegateInterface $resultPrinter = null)
public function __construct(TestResultDelegateInterface $resultPrinter)
{
parent::__construct();
if ($resultPrinter) {
$this->resultPrinter = $resultPrinter;
} else {
$this->resultPrinter = new DefaultResultPrinter();
}
$this->resultPrinter = $resultPrinter;
}

public function getResultPrinter()
Expand Down
43 changes: 37 additions & 6 deletions tests/Concise/Console/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,57 @@

use \Concise\TestCase;

class CommandStub extends Command
{
public function setArgument($name, $value)
{
$this->arguments[$name] = $value;
}
}

class CommandTest extends TestCase
{
public function testCommandExtendsPHPUnit()
{
$this->assert(new Command(), instance_of, 'PHPUnit_TextUI_Command');
}

protected function getCommandMock()
{
return $this->niceMock('Concise\Console\CommandStub')
->expose('createRunner')
->done();
}

public function testCreateRunnerReturnsAConciseRunner()
{
$command = $this->niceMock('Concise\Console\Command')
->expose('createRunner')
->done();
$command = $this->getCommandMock();
$this->assert($command->createRunner(), instance_of, 'Concise\Console\TestRunner\DefaultTestRunner');
}

public function testPrinterUsesProxy()
{
$command = $this->niceMock('Concise\Console\Command')
->expose('createRunner')
->done();
$command = $this->getCommandMock();
$this->assert($command->createRunner()->getPrinter(), instance_of, 'Concise\Console\ResultPrinter\ResultPrinterProxy');
}

public function testVerboseIsFalseByDefault()
{
$command = $this->getCommandMock();
$this->assert($command->createRunner()->getPrinter()->getResultPrinter()->isVerbose(), is_false);
}

public function testVerboseIsTurnedOnIfItExistsAndHasBeenSet()
{
$command = $this->getCommandMock();
$command->setArgument('verbose', true);
$this->assert($command->createRunner()->getPrinter()->getResultPrinter()->isVerbose(), is_true);
}

public function testVerboseIsNotTurnedOnIfItExistsButIfNotTrue()
{
$command = $this->getCommandMock();
$command->setArgument('verbose', false);
$this->assert($command->createRunner()->getPrinter()->getResultPrinter()->isVerbose(), is_false);
}
}
16 changes: 16 additions & 0 deletions tests/Concise/Console/ResultPrinter/AbstractResultPrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,20 @@ public function testEndReturnsNull()
{
$this->assert($this->resultPrinter->end(), is_null);
}

public function testVerbosityCanBeSet()
{
$this->assert($this->resultPrinter->setVerbose(false), is_null);
}

public function testDefaultVerboseIsOff()
{
$this->assert($this->resultPrinter->isVerbose(), is_false);
}

public function testGetVerbose()
{
$this->resultPrinter->setVerbose(true);
$this->assert($this->resultPrinter->isVerbose(), is_true);
}
}
36 changes: 36 additions & 0 deletions tests/Concise/Console/ResultPrinter/DefaultResultPrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Concise\TestCase;
use PHPUnit_Runner_BaseTestRunner;
use Exception;

class DefaultResultPrinterStub extends DefaultResultPrinter
{
Expand Down Expand Up @@ -102,4 +103,39 @@ public function testEndWillUpdateProgress()
->done();
$resultPrinter->end();
}

public function verboseDataSet()
{
$show = 1;
$hide = 0;

return array(
array(PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED, true, $show),
array(PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED, false, $hide),
array(PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE, true, $show),
array(PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE, false, $hide),
array(PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE, true, $show),
array(PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE, false, $show),
array(PHPUnit_Runner_BaseTestRunner::STATUS_ERROR, true, $show),
array(PHPUnit_Runner_BaseTestRunner::STATUS_ERROR, false, $show),
array(PHPUnit_Runner_BaseTestRunner::STATUS_RISKY, true, $show),
array(PHPUnit_Runner_BaseTestRunner::STATUS_RISKY, false, $hide),
);
}

/**
* @dataProvider verboseDataSet
*/
public function testVerbosePrintsIssues($status, $isVerbose, $willBePrinted)
{
$resultPrinter = $this->niceMock('Concise\Console\ResultPrinter\DefaultResultPrinter')
->expect('appendTextAbove')->exactly($willBePrinted)
->expose('add')
->done();
$resultPrinter->setVerbose($isVerbose);
$test = $this->niceMock('PHPUnit_Framework_TestCase')
->stub(array('getName' => ''))
->done();
$resultPrinter->add($status, $test, new Exception());
}
}

0 comments on commit 4629013

Please sign in to comment.