Skip to content

Commit

Permalink
Merge pull request #64 from elliotchance/feature/63-refactor-all-test…
Browse files Browse the repository at this point in the history
…s-to-use-concise

Feature/63 refactor all tests to use concise
  • Loading branch information
elliotchance committed Jul 20, 2014
2 parents 00eb71a + 93d2230 commit 216bf66
Show file tree
Hide file tree
Showing 94 changed files with 1,720 additions and 664 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -23,8 +23,8 @@ class AttributeTest extends TestCase
// or you can create your assertion by chaining
$this->assert($result, exactly_equals, 123);

// assertThat for convenience
assertThat($answer, is_an_associative_array);
// you may prefer assert_that
assert_that($answer, is_an_associative_array);

// while generally not recommended, you can use code blocks (notice $self instead of $this)
$this->assert('`$self->calc->add(3, 5)` equals 8');
Expand Down
143 changes: 143 additions & 0 deletions concise
@@ -0,0 +1,143 @@
#!/usr/bin/env php
<?php

require_once('vendor/autoload.php');

echo "Concise 1.0 by Elliot Chance.\n\n";

class Concise_ResultPrinter extends PHPUnit_TextUI_ResultPrinter
{
protected $width = 79;

protected $numTestsFailed = 0;

public function __construct()
{
parent::__construct();
$this->width = exec('tput cols');
}

public function write($buffer)
{
if(substr($buffer, 0, 7) !== 'PHPUnit') {
return parent::write($buffer);
}
}

public function endTest(PHPUnit_Framework_Test $test, $time)
{
if ($test instanceof PHPUnit_Framework_TestCase) {
$this->numAssertions += $test->getNumAssertions();
}
else if ($test instanceof PHPUnit_Extensions_PhptTestCase) {
$this->numAssertions++;
}

++$this->numTestsRun;
$this->update();
}

protected function printHeader()
{
echo "\n";
}

protected function printFooter(PHPUnit_Framework_TestResult $result)
{
if (count($result) === 0) {
$message = 'No tests executed!';
echo "\033[2A\033[0;30m\033[42m{$message}\033[0m";
}
else if ($result->wasSuccessful() &&
$result->allHarmless() &&
$result->allCompletelyImplemented() &&
$result->noneSkipped()) {
$message = 'OK';
echo "\033[2A\033[0;30m\033[42m{$message}\033[0m";
}
else if ((!$result->allCompletelyImplemented() ||
!$result->allHarmless() ||
!$result->noneSkipped()) &&
$result->wasSuccessful()) {
$message = 'OK, but incomplete, skipped, or risky tests!';
echo "\033[2A\033[0;30m\033[42m{$message}\033[0m";
}

parent::printHeader();
}

protected function repeat($string, $times)
{
if($times < 0) {
return '';
}
return str_repeat($string, $times);
}

protected function update()
{
if($this->numTestsRun > 1) {
echo "\033[2A";
}

$assertionString = $this->numAssertions . ' assertion' . ($this->numAssertions == 1 ? '' : 's');
echo $assertionString . $this->repeat(' ', $this->width - (2 * $this->numTestsWidth) - 11 - strlen($assertionString));
$done = $this->numTestsRun / $this->numTests;
printf(' %' . $this->numTestsWidth . 'd / %' . $this->numTestsWidth . "d (%3s%%)\n",
$this->numTestsRun, $this->numTests, floor($done * 100));

$failures = 0;
if($this->numTestsFailed) {
$failures = ceil($this->width * $this->numTestsFailed / $this->numTests);
}
$successes = floor($this->width * $done) - $failures;
echo "\033[42m" . $this->repeat(' ', $successes);
echo "\033[41m" . $this->repeat(' ', $failures);
echo "\033[0m" . $this->repeat('_', $this->width - $successes - $failures) . "\n";
}

protected function printDefect(PHPUnit_Framework_TestFailure $defect, $count)
{
$this->printDefectHeader($defect, $count);
$this->printDefectTrace($defect);
}

public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
{
++$this->numTestsFailed;
}

public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
{
++$this->numTestsFailed;
}

public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
{
}

public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time)
{
}

public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
{
}
}

class Concise_TestRunner extends PHPUnit_TextUI_TestRunner
{
}

class Concise_Command extends PHPUnit_TextUI_Command
{
protected function createRunner()
{
$runner = new Concise_TestRunner($this->arguments['loader']);
$runner->setPrinter(new Concise_ResultPrinter());
return $runner;
}
}

$command = new Concise_Command();
$command->run($_SERVER['argv'], true);
4 changes: 2 additions & 2 deletions src/Concise/Assertion.php
Expand Up @@ -120,11 +120,11 @@ protected function executeAssertion()
$args = $this->checkDataTypes($args);
}

$answer = $this->getMatcher()->match($result['syntax'], $args);
$answer = $this->getMatcher()->match($this->originalSyntax, $args);
if(true === $answer || null === $answer) {
return;
}
$message = $this->getMatcher()->renderFailureMessage($result['syntax'], $result['arguments']);
$message = $this->getMatcher()->renderFailureMessage($result['syntax'], $args);
throw new \PHPUnit_Framework_AssertionFailedError($message);
}

Expand Down
5 changes: 0 additions & 5 deletions src/Concise/Matcher/AbstractMatcher.php
Expand Up @@ -27,9 +27,4 @@ public function renderFailureMessage($syntax, array $data = array())
$renderer = new SyntaxRenderer();
return $renderer->render($syntax, $data);
}

protected function getComparer()
{
return new Comparer();
}
}
21 changes: 21 additions & 0 deletions src/Concise/Matcher/Between.php
@@ -0,0 +1,21 @@
<?php

namespace Concise\Matcher;

class Between extends AbstractMatcher
{
const DESCRIPTION = 'A number must be between two values (inclusive).';

public function supportedSyntaxes()
{
return array(
'?:number is between ?:number and ?:number' => self::DESCRIPTION,
'?:number between ?:number and ?:number' => self::DESCRIPTION,
);
}

public function match($syntax, array $data = array())
{
return $data[0] >= $data[1] && $data[0] <= $data[2];
}
}
12 changes: 5 additions & 7 deletions src/Concise/Matcher/DoesNotMatchRegularExpression.php
Expand Up @@ -2,24 +2,22 @@

namespace Concise\Matcher;

use \Concise\Services\ToStringConverter;

class DoesNotMatchRegularExpression extends MatchesRegularExpression
{
const DESCRIPTION = 'Assert a string does not match a regular expression.';

public function supportedSyntaxes()
{
return array(
'? does not match regular expression ?:regex' => self::DESCRIPTION,
'? doesnt match regular expression ?:regex' => self::DESCRIPTION,
'? does not match regex ?:regex' => self::DESCRIPTION,
'? doesnt match regex ?:regex' => self::DESCRIPTION,
'?:string does not match regular expression ?:regex' => self::DESCRIPTION,
'?:string doesnt match regular expression ?:regex' => self::DESCRIPTION,
'?:string does not match regex ?:regex' => self::DESCRIPTION,
'?:string doesnt match regex ?:regex' => self::DESCRIPTION,
);
}

public function match($syntax, array $data = array())
{
return !parent::match('? matches regular expression ?:regex', $data);
return !parent::match(null, $data);
}
}
2 changes: 1 addition & 1 deletion src/Concise/Matcher/Equals.php
Expand Up @@ -18,6 +18,6 @@ public function supportedSyntaxes()

public function match($syntax, array $data = array())
{
return $this->getComparer()->compare($data[0], $data[1]);
return $data[0] == $data[1];
}
}
31 changes: 31 additions & 0 deletions src/Concise/Matcher/HasItem.php
@@ -0,0 +1,31 @@
<?php

namespace Concise\Matcher;

class HasItem extends AbstractMatcher
{
const DESCRIPTION = 'Assert an array has key and value item.';

const SPLIT_SYNTAX = '?:array has key ?:string with value ?';

public function supportedSyntaxes()
{
return array(
self::SPLIT_SYNTAX => self::DESCRIPTION,
'?:array has item ?:array' => self::DESCRIPTION,
);
}

public function match($syntax, array $data = array())
{
if($syntax === self::SPLIT_SYNTAX) {
return $this->match(null, array($data[0], array($data[1] => $data[2])));
}
foreach($data[0] as $key => $value) {
if(array($key => $value) == $data[1]) {
return true;
}
}
return false;
}
}
28 changes: 28 additions & 0 deletions src/Concise/Matcher/HasItems.php
@@ -0,0 +1,28 @@
<?php

namespace Concise\Matcher;

class HasItems extends HasItem
{
const DESCRIPTION = 'Assert an array has all key and value items.';

public function supportedSyntaxes()
{
return array(
'?:array has items ?:array' => self::DESCRIPTION,
);
}

public function match($syntax, array $data = array())
{
if(count($data[1]) === 0) {
return true;
}
foreach($data[1] as $key => $value) {
if(!parent::match($data[0], array($data[0], array($key => $value)))) {
return false;
}
}
return true;
}
}
26 changes: 26 additions & 0 deletions src/Concise/Matcher/HasValues.php
@@ -0,0 +1,26 @@
<?php

namespace Concise\Matcher;

class HasValues extends AbstractMatcher
{
const DESCRIPTION = 'Assert an array has several values in any order.';

public function supportedSyntaxes()
{
return array(
'?:array has values ?:array' => self::DESCRIPTION,
);
}

public function match($syntax, array $data = array())
{
$keys = array_values($data[0]);
foreach($data[1] as $key) {
if(!in_array($key, $keys)) {
return false;
}
}
return true;
}
}
21 changes: 21 additions & 0 deletions src/Concise/Matcher/IsAnEmptyArray.php
@@ -0,0 +1,21 @@
<?php

namespace Concise\Matcher;

class IsAnEmptyArray extends AbstractMatcher
{
const DESCRIPTION = 'Assert an array is empty (no elements).';

public function supportedSyntaxes()
{
return array(
'?:array is empty array' => self::DESCRIPTION,
'?:array is an empty array' => self::DESCRIPTION,
);
}

public function match($syntax, array $data = array())
{
return count($data[0]) === 0;
}
}
18 changes: 18 additions & 0 deletions src/Concise/Matcher/IsBlank.php
@@ -0,0 +1,18 @@
<?php

namespace Concise\Matcher;

class IsBlank extends AbstractMatcher
{
public function supportedSyntaxes()
{
return array(
'?:string is blank' => 'Assert a string is zero length.',
);
}

public function match($syntax, array $data = array())
{
return $data[0] === '';
}
}
2 changes: 1 addition & 1 deletion src/Concise/Matcher/IsFalse.php
Expand Up @@ -13,6 +13,6 @@ public function supportedSyntaxes()

public function match($syntax, array $data = array())
{
return $this->getComparer()->compare($data[0], false);
return $data[0] === false;
}
}
22 changes: 22 additions & 0 deletions src/Concise/Matcher/IsGreaterThan.php
@@ -0,0 +1,22 @@
<?php

namespace Concise\Matcher;

class IsGreaterThan extends AbstractMatcher
{
const DESCRIPTION = 'A number is greater than another number.';

public function supportedSyntaxes()
{
return array(
'?:number is greater than ?:number' => self::DESCRIPTION,
'?:number greater than ?:number' => self::DESCRIPTION,
'?:number gt ?:number' => self::DESCRIPTION,
);
}

public function match($syntax, array $data = array())
{
return $data[0] > $data[1];
}
}

0 comments on commit 216bf66

Please sign in to comment.