mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENHANCEMENT: Created a phpunit wrapper class to ensure that Sapphire's test framework is capable of running unit tests, coverage report and retrieve clover-statistics for PHPUnit 3.4 and PHPUnit 3.5
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@111039 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
94f03523b5
commit
73824f3a24
147
dev/phpunit/PhpUnitWrapper.php
Normal file
147
dev/phpunit/PhpUnitWrapper.php
Normal file
@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This method checks if a given filename exists in the include path (defined
|
||||
* in php.ini.
|
||||
*
|
||||
* @return boolean when the file has been found in the include path.
|
||||
*/
|
||||
function fileExistsInIncludePath($filename) {
|
||||
$paths = explode(PATH_SEPARATOR, ini_get('include_path'));
|
||||
foreach($paths as $path) {
|
||||
if(substr($path,-1) == DIRECTORY_SEPARATOR) $path = substr($path,0,-1);
|
||||
if(@file_exists($path."/".$filename)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
class PhpUnitWrapper implements IPhpUnitWrapper {
|
||||
|
||||
private $coverage = false;
|
||||
|
||||
private $suite = null;
|
||||
|
||||
private $results = null;
|
||||
|
||||
protected $version = 'none';
|
||||
|
||||
private $reporter = null;
|
||||
|
||||
public function getVersion() {
|
||||
return $this->version;
|
||||
}
|
||||
|
||||
public function getFrameworkTestResults() {
|
||||
return $this->results;
|
||||
}
|
||||
|
||||
public function setFrameworkTestResults($value) {
|
||||
$this->results = $value;
|
||||
}
|
||||
|
||||
public function getCoverageStatus() {
|
||||
return $this->coverage;
|
||||
}
|
||||
|
||||
public function setCoverageStatus($value) {
|
||||
$this->coverage = $value;
|
||||
}
|
||||
|
||||
public function getSuite() {
|
||||
return $this->suite;
|
||||
}
|
||||
|
||||
public function setReporter($value) {
|
||||
$this->reporter = $value;
|
||||
}
|
||||
|
||||
public function getReporter() {
|
||||
return $this->reporter;
|
||||
}
|
||||
|
||||
public function setSuite($value) {
|
||||
$this->suite = $value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
static function getPhpUnit_Version() {
|
||||
$result = 'none';
|
||||
|
||||
if (TestRunner::get_phpunit_wrapper() == null) {
|
||||
if (fileExistsInIncludePath("/PHPUnit/Autoload.php")) {
|
||||
TestRunner::set_phpunit_wrapper(new PhpUnitWrapper_3_5());
|
||||
} else
|
||||
if (fileExistsInIncludePath("/PHPUnit/Framework.php")) {
|
||||
TestRunner::set_phpunit_wrapper(new PhpUnitWrapper_3_4());
|
||||
} else {
|
||||
TestRunner::set_phpunit_wrapper(new PhpUnitWrapper());
|
||||
}
|
||||
TestRunner::get_phpunit_wrapper()->init();
|
||||
|
||||
}
|
||||
$result = TestRunner::get_phpunit_wrapper()->getVersion();
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if one of the two supported PHPUNIT versions is installed.
|
||||
*/
|
||||
static function hasPhpUnit() {
|
||||
return (self::getPhpUnit_Version() != 'none');
|
||||
}
|
||||
|
||||
public function init() {
|
||||
}
|
||||
|
||||
protected function beforeRunTests() {
|
||||
// throw new PhpUnitWrapper_Excption('Method \'beforeRunTests\' not implemented in PhpUnitWrapper.');
|
||||
}
|
||||
|
||||
protected function afterRunTests() {
|
||||
// throw new PhpUnitWrapper_Excption('Method \'afterRunTests\' not implemented in PhpUnitWrapper.');
|
||||
}
|
||||
|
||||
public function runTests() {
|
||||
|
||||
if(Director::is_cli()) {
|
||||
$this->setReporter( new CliTestReporter() );
|
||||
} else {
|
||||
$this->setReporter( new SapphireTestReporter() );
|
||||
}
|
||||
|
||||
if ($this->getFrameworkTestResults() == null) {
|
||||
$this->setFrameworkTestResults(new PHPUnit_Framework_TestResult());
|
||||
}
|
||||
$this->getFrameworkTestResults()->addListener( $this->getReporter() );
|
||||
|
||||
$this->beforeRunTests();
|
||||
$this->getSuite()->run($this->getFrameworkTestResults());
|
||||
$this->aferRunTests();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
interface IPhpUnitWrapper {
|
||||
|
||||
public function init();
|
||||
|
||||
public function runTests();
|
||||
|
||||
}
|
||||
|
||||
// This class is here to help with documentation.
|
||||
if(!PhpUnitWrapper::hasPhpUnit()) {
|
||||
/**
|
||||
* PHPUnit is a testing framework that can be installed using PEAR.
|
||||
* It's not bundled with Sapphire, you will need to install it yourself.
|
||||
*
|
||||
* @package sapphire
|
||||
* @subpackage testing
|
||||
*/
|
||||
class PHPUnit_Framework_TestCase {
|
||||
|
||||
}
|
||||
}
|
51
dev/phpunit/PhpUnitWrapper_3_4.php
Normal file
51
dev/phpunit/PhpUnitWrapper_3_4.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
class PhpUnitWrapper_3_4 extends PhpUnitWrapper {
|
||||
|
||||
protected $version = 'PhpUnit V3.4';
|
||||
|
||||
public function init() {
|
||||
require_once 'PHPUnit/Framework.php';
|
||||
require_once 'PHPUnit/Util/Report.php';
|
||||
require_once 'PHPUnit/TextUI/TestRunner.php';
|
||||
}
|
||||
|
||||
protected function beforeRunTests() {
|
||||
|
||||
if($this->getCoverageStatus()) {
|
||||
// blacklist selected folders from coverage report
|
||||
foreach(TestRunner::$coverage_filter_dirs as $dir) {
|
||||
PHPUnit_Util_Filter::addDirectoryToFilter(BASE_PATH . '/' . $dir);
|
||||
}
|
||||
$this->getFrameworkTestResults()->collectCodeCoverageInformation(true);
|
||||
}
|
||||
}
|
||||
|
||||
protected function aferRunTests() {
|
||||
|
||||
if($this->getCoverageStatus()) {
|
||||
require_once 'PHPUnit/Util/Log/CodeCoverage/XML/Clover.php';
|
||||
$writer = new PHPUnit_Util_Log_CodeCoverage_XML_Clover('clover.xml');
|
||||
$writer->process($this->getFrameworkTestResults());
|
||||
|
||||
if(!file_exists(ASSETS_PATH . '/coverage-report')) {
|
||||
mkdir(ASSETS_PATH . '/coverage-report');
|
||||
}
|
||||
|
||||
PHPUnit_Util_Report::render($this->getFrameworkTestResults(), ASSETS_PATH . '/coverage-report/');
|
||||
|
||||
$coverageApp = ASSETS_PATH . '/coverage-report/' . preg_replace('/[^A-Za-z0-9]/','_',preg_replace('/(\/$)|(^\/)/','',Director::baseFolder())) . '.html';
|
||||
$coverageTemplates = ASSETS_PATH . '/coverage-report/' . preg_replace('/[^A-Za-z0-9]/','_',preg_replace('/(\/$)|(^\/)/','',realpath(TEMP_FOLDER))) . '.html';
|
||||
|
||||
echo "<p>Coverage reports available here:<ul>
|
||||
<li><a href=\"$coverageApp\">Coverage report of the application</a></li>
|
||||
<li><a href=\"$coverageTemplates\">Coverage report of the templates</a></li>
|
||||
</ul>";
|
||||
}
|
||||
}
|
||||
|
||||
public function runTests() {
|
||||
return parent::runTests();
|
||||
}
|
||||
|
||||
}
|
84
dev/phpunit/PhpUnitWrapper_3_5.php
Normal file
84
dev/phpunit/PhpUnitWrapper_3_5.php
Normal file
@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
class PhpUnitWrapper_3_5 extends PhpUnitWrapper {
|
||||
|
||||
protected $version = 'PhpUnit V3.5';
|
||||
|
||||
protected $coverage = null;
|
||||
|
||||
protected static $test_name = 'SapphireTest';
|
||||
|
||||
protected static $generate_clover = false;
|
||||
|
||||
protected static $clover_filename = 'clover.xml';
|
||||
|
||||
static function get_test_name() {
|
||||
return self::$test_name;
|
||||
}
|
||||
|
||||
static function get_generate_clover() {
|
||||
return self::$generate_clover;
|
||||
}
|
||||
|
||||
static function set_generate_clover($value) {
|
||||
self::$generate_clover = $value;
|
||||
}
|
||||
|
||||
static function get_clover_filename() {
|
||||
return self::$clover_filename;
|
||||
}
|
||||
|
||||
static function set_clover_filename($value) {
|
||||
self::$clover_filename = $value;
|
||||
}
|
||||
|
||||
public function init() {
|
||||
require_once 'PHP/CodeCoverage.php';
|
||||
require_once 'PHP/CodeCoverage/Report/Clover.php';
|
||||
require_once 'PHP/CodeCoverage/Report/HTML.php';
|
||||
|
||||
require_once 'PHPUnit/Autoload.php';
|
||||
|
||||
require_once 'PHP/CodeCoverage/Filter.php';
|
||||
PHP_CodeCoverage_Filter::getInstance()->addFileToBlacklist(__FILE__, 'PHPUNIT');
|
||||
|
||||
}
|
||||
|
||||
protected function beforeRunTests() {
|
||||
|
||||
if($this->getCoverageStatus()) {
|
||||
$this->coverage = new PHP_CodeCoverage;
|
||||
$coverage = $this->coverage;
|
||||
|
||||
$filter = $coverage->filter();
|
||||
|
||||
foreach(TestRunner::$coverage_filter_dirs as $dir) {
|
||||
$filter->addDirectoryToBlacklist(BASE_PATH . '/' . $dir);
|
||||
}
|
||||
|
||||
$coverage->start(self::get_test_name());
|
||||
}
|
||||
}
|
||||
|
||||
protected function aferRunTests() {
|
||||
|
||||
if($this->getCoverageStatus()) {
|
||||
$coverage = $this->coverage;
|
||||
$coverage->stop();
|
||||
|
||||
if (self::get_generate_clover() == true) {
|
||||
|
||||
$filename = self::get_clover_filename();
|
||||
$writer = new PHP_CodeCoverage_Report_Clover;
|
||||
$writer->process($coverage, ASSETS_PATH."/".$filename);
|
||||
}
|
||||
|
||||
$writer = new PHP_CodeCoverage_Report_HTML;
|
||||
$writer->process($coverage, ASSETS_PATH.'/code-coverage-report');
|
||||
}
|
||||
}
|
||||
|
||||
public function runTests() {
|
||||
return parent::runTests();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user