Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from elliotchance/feature/63-refactor-all-test…
…s-to-use-concise Feature/63 refactor all tests to use concise
- Loading branch information
Showing
94 changed files
with
1,720 additions
and
664 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] === ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]; | ||
} | ||
} |
Oops, something went wrong.