From ae493a3e0ee4b8d169d314d808210180cec0e785 Mon Sep 17 00:00:00 2001 From: ischommer Date: Tue, 21 Sep 2010 20:29:25 +0000 Subject: [PATCH] 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/phpinstaller/branches/2.4@111054 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- dev/phpunit/PhpUnitWrapper.php | 147 +++++++++++++++++++++++++++++ dev/phpunit/PhpUnitWrapper_3_4.php | 51 ++++++++++ dev/phpunit/PhpUnitWrapper_3_5.php | 84 +++++++++++++++++ 3 files changed, 282 insertions(+) create mode 100644 dev/phpunit/PhpUnitWrapper.php create mode 100644 dev/phpunit/PhpUnitWrapper_3_4.php create mode 100644 dev/phpunit/PhpUnitWrapper_3_5.php diff --git a/dev/phpunit/PhpUnitWrapper.php b/dev/phpunit/PhpUnitWrapper.php new file mode 100644 index 0000000..0d263e8 --- /dev/null +++ b/dev/phpunit/PhpUnitWrapper.php @@ -0,0 +1,147 @@ +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 { + + } +} diff --git a/dev/phpunit/PhpUnitWrapper_3_4.php b/dev/phpunit/PhpUnitWrapper_3_4.php new file mode 100644 index 0000000..7775c83 --- /dev/null +++ b/dev/phpunit/PhpUnitWrapper_3_4.php @@ -0,0 +1,51 @@ +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 "

Coverage reports available here:

"; + } + } + + public function runTests() { + return parent::runTests(); + } + +} \ No newline at end of file diff --git a/dev/phpunit/PhpUnitWrapper_3_5.php b/dev/phpunit/PhpUnitWrapper_3_5.php new file mode 100644 index 0000000..f851b47 --- /dev/null +++ b/dev/phpunit/PhpUnitWrapper_3_5.php @@ -0,0 +1,84 @@ +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(); + } +} \ No newline at end of file