2010-10-19 05:06:55 +02:00
|
|
|
<?php
|
2010-10-19 05:35:14 +02:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2010-10-19 05:35:14 +02:00
|
|
|
* @subpackage dev
|
|
|
|
*/
|
2010-10-19 05:06:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method checks if a given filename exists in the include path (defined
|
2014-08-15 08:53:05 +02:00
|
|
|
* in php.ini.
|
2010-10-19 05:06:55 +02:00
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2010-10-19 05:35:14 +02:00
|
|
|
/**
|
|
|
|
* PHPUnit Wrapper class.
|
|
|
|
* Base class for PHPUnit wrapper classes to support different PHPUnit versions.
|
2011-03-11 02:45:49 +01:00
|
|
|
* The current implementation supports PHPUnit 3.4 and PHPUnit 3.5.
|
2010-10-19 05:35:14 +02:00
|
|
|
*/
|
2010-10-19 05:06:55 +02:00
|
|
|
class PhpUnitWrapper implements IPhpUnitWrapper {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 05:35:14 +02:00
|
|
|
/**
|
|
|
|
* Flag if coverage report shall be generated or not.
|
|
|
|
* @var boolean
|
2014-08-15 08:53:05 +02:00
|
|
|
*/
|
2010-10-19 05:06:55 +02:00
|
|
|
private $coverage = false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 05:35:14 +02:00
|
|
|
/**
|
|
|
|
* PHPUnit-TestSuite class. The tests, added to this suite are performed
|
|
|
|
* in this test-run.
|
|
|
|
* @var PHPUnit_Framework_TestSuite
|
2014-08-15 08:53:05 +02:00
|
|
|
*/
|
2010-10-19 05:06:55 +02:00
|
|
|
private $suite = null;
|
|
|
|
|
2010-10-19 05:35:14 +02:00
|
|
|
/**
|
|
|
|
* @var PHPUnit_Framework_TestResult
|
|
|
|
*/
|
2010-10-19 05:06:55 +02:00
|
|
|
private $results = null;
|
|
|
|
|
2010-10-19 05:35:14 +02:00
|
|
|
/**
|
|
|
|
* @var PHPUnit_Framework_TestListener
|
2014-08-15 08:53:05 +02:00
|
|
|
*/
|
2010-10-19 05:35:14 +02:00
|
|
|
private $reporter = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-11 02:45:49 +01:00
|
|
|
/**
|
|
|
|
* Shows the version, implemented by the phpunit-wrapper class instance.
|
|
|
|
* This instance implements no phpunit, the version is null.
|
|
|
|
* @var String
|
2014-08-15 08:53:05 +02:00
|
|
|
*/
|
2011-03-11 02:45:49 +01:00
|
|
|
protected $version = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 05:35:14 +02:00
|
|
|
private static $phpunit_wrapper = null;
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
/**
|
2010-10-19 05:35:14 +02:00
|
|
|
* Getter for $coverage (@see $coverage).
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2010-10-19 05:06:55 +02:00
|
|
|
public function getCoverageStatus() {
|
|
|
|
return $this->coverage;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
/**
|
2010-10-19 05:35:14 +02:00
|
|
|
* Setter for $coverage (@see $coverage).
|
|
|
|
* @parameter $value Boolean
|
2014-08-15 08:53:05 +02:00
|
|
|
*/
|
2010-10-19 05:06:55 +02:00
|
|
|
public function setCoverageStatus($value) {
|
|
|
|
$this->coverage = $value;
|
|
|
|
}
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
/**
|
2010-10-19 05:35:14 +02:00
|
|
|
* Getter for $suite (@see $suite).
|
|
|
|
* @return PHPUnit_Framework_TestSuite
|
2014-08-15 08:53:05 +02:00
|
|
|
*/
|
2010-10-19 05:06:55 +02:00
|
|
|
public function getSuite() {
|
|
|
|
return $this->suite;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
/**
|
2010-10-19 05:35:14 +02:00
|
|
|
* Setter for $suite (@see $suite).
|
|
|
|
* @param $value PHPUnit_Framework_TestSuite
|
|
|
|
*/
|
|
|
|
public function setSuite($value) {
|
|
|
|
$this->suite = $value;
|
2010-10-19 05:06:55 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
/**
|
2010-10-19 05:35:14 +02:00
|
|
|
* Getter for $reporter (@see $reporter).
|
|
|
|
* @return PHPUnit_Framework_TestListener
|
|
|
|
*/
|
2010-10-19 05:06:55 +02:00
|
|
|
public function getReporter() {
|
|
|
|
return $this->reporter;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
/**
|
2010-10-19 05:35:14 +02:00
|
|
|
* Setter for $reporter (@see $reporter).
|
|
|
|
* @param $value PHPUnit_Framework_TestListener
|
|
|
|
*/
|
|
|
|
public function setReporter($value) {
|
|
|
|
$this->reporter = $value;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
/**
|
2010-10-19 05:35:14 +02:00
|
|
|
* Getter for $results (@see $results).
|
|
|
|
* @return PHPUnit_Framework_TestResult
|
|
|
|
*/
|
|
|
|
public function getFrameworkTestResults() {
|
|
|
|
return $this->results;
|
2010-10-19 05:06:55 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
/**
|
2010-10-19 05:35:14 +02:00
|
|
|
* Setter for $results (@see $results).
|
|
|
|
* @param $value PHPUnit_Framework_TestResult
|
|
|
|
*/
|
|
|
|
public function setFrameworkTestResults($value) {
|
|
|
|
$this->results = $value;
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2010-10-19 05:35:14 +02:00
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
/**
|
2011-03-11 02:45:49 +01:00
|
|
|
* Getter for $version (@see $version).
|
2010-10-19 05:35:14 +02:00
|
|
|
* @return String
|
2014-08-15 08:53:05 +02:00
|
|
|
*/
|
2010-10-19 05:35:14 +02:00
|
|
|
public function getVersion() {
|
2011-03-11 02:45:49 +01:00
|
|
|
return $this->version;
|
2010-10-19 05:35:14 +02:00
|
|
|
}
|
2010-10-19 05:06:55 +02:00
|
|
|
|
|
|
|
/**
|
2010-10-19 05:35:14 +02:00
|
|
|
* Loads and initiates phpunit, based on the available phpunit version.
|
2010-10-19 05:06:55 +02:00
|
|
|
*
|
2010-10-19 05:35:14 +02:00
|
|
|
* @return PhpUnitWrapper Instance of the php-wrapper class
|
2010-10-19 05:06:55 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function inst() {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 05:35:14 +02:00
|
|
|
if (self::$phpunit_wrapper == null) {
|
2014-04-17 15:14:51 +02:00
|
|
|
// Loaded via autoloader, composer or other generic
|
|
|
|
if (class_exists('PHPUnit_Runner_Version')) {
|
|
|
|
self::$phpunit_wrapper = new PhpUnitWrapper_Generic();
|
|
|
|
}
|
|
|
|
// 3.5 detection
|
|
|
|
else if (fileExistsInIncludePath("/PHPUnit/Autoload.php")) {
|
2012-12-08 12:20:20 +01:00
|
|
|
self::$phpunit_wrapper = new PhpUnitWrapper_3_5();
|
2014-04-17 15:14:51 +02:00
|
|
|
}
|
|
|
|
// 3.4 detection
|
|
|
|
else if (fileExistsInIncludePath("/PHPUnit/Framework.php")) {
|
2012-12-08 12:20:20 +01:00
|
|
|
self::$phpunit_wrapper = new PhpUnitWrapper_3_4();
|
2014-04-17 15:14:51 +02:00
|
|
|
}
|
|
|
|
// No version found - will lead to an error
|
|
|
|
else {
|
2012-12-08 12:20:20 +01:00
|
|
|
self::$phpunit_wrapper = new PhpUnitWrapper();
|
2014-04-17 15:14:51 +02:00
|
|
|
}
|
2010-10-19 05:35:14 +02:00
|
|
|
self::$phpunit_wrapper->init();
|
2011-03-11 02:45:49 +01:00
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2010-10-19 05:35:14 +02:00
|
|
|
return self::$phpunit_wrapper;
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2010-10-19 05:06:55 +02:00
|
|
|
|
|
|
|
/**
|
2011-03-11 02:45:49 +01:00
|
|
|
* Returns true if one of the two supported PHPUNIT versions is installed.
|
|
|
|
*
|
2010-10-19 05:35:14 +02:00
|
|
|
* @return boolean true if PHPUnit has been installed on the environment.
|
2010-10-19 05:06:55 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function has_php_unit() {
|
2011-03-11 02:45:49 +01:00
|
|
|
return (Bool) self::inst()->getVersion();
|
2010-10-19 05:06:55 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
/**
|
2010-10-19 05:35:14 +02:00
|
|
|
* Implements method, defined in the interface IPhpUnitWrapper:init (@see IPhpUnitWrapper).
|
|
|
|
* This wrapper class doesn't require any initialisation.
|
2014-08-15 08:53:05 +02:00
|
|
|
*/
|
2010-10-19 05:06:55 +02:00
|
|
|
public function init() {
|
|
|
|
}
|
|
|
|
|
2010-10-19 05:35:14 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* This method is called before the unittests are performed.
|
2010-10-19 05:35:14 +02:00
|
|
|
* This wrapper implements the non-PHPUnit version which means that unit tests
|
2014-08-15 08:53:05 +02:00
|
|
|
* can not be performed.
|
2010-10-19 05:35:14 +02:00
|
|
|
* @throws PhpUnitWrapper_Excption
|
|
|
|
*/
|
2010-10-19 05:06:55 +02:00
|
|
|
protected function beforeRunTests() {
|
2010-10-19 05:35:14 +02:00
|
|
|
throw new PhpUnitWrapper_Exception('Method \'beforeRunTests\' not implemented in PhpUnitWrapper.');
|
2010-10-19 05:06:55 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 05:35:14 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* This method is called after the unittests are performed.
|
2010-10-19 05:35:14 +02:00
|
|
|
* This wrapper implements the non-PHPUnit version which means that unit tests
|
2014-08-15 08:53:05 +02:00
|
|
|
* can not be performed.
|
2010-10-19 05:35:14 +02:00
|
|
|
* @throws PhpUnitWrapper_Excption
|
|
|
|
*/
|
2010-10-19 05:06:55 +02:00
|
|
|
protected function afterRunTests() {
|
2010-10-19 05:35:14 +02:00
|
|
|
throw new PhpUnitWrapper_Exception('Method \'afterRunTests\' not implemented in PhpUnitWrapper.');
|
2010-10-19 05:06:55 +02:00
|
|
|
}
|
|
|
|
|
2010-10-19 05:35:14 +02:00
|
|
|
/**
|
2012-03-24 04:38:57 +01:00
|
|
|
* Perform all tests, added to the suite and initialises SilverStripe to collect
|
2010-10-19 05:35:14 +02:00
|
|
|
* the results of the unit tests.
|
|
|
|
*
|
|
|
|
* This method calls @see beforeRunTests and @see afterRunTests.
|
|
|
|
*/
|
2010-10-19 05:06:55 +02:00
|
|
|
public function runTests() {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 05:06:55 +02:00
|
|
|
if(Director::is_cli()) {
|
|
|
|
$this->setReporter( new CliTestReporter() );
|
|
|
|
} else {
|
|
|
|
$this->setReporter( new SapphireTestReporter() );
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 05:06:55 +02:00
|
|
|
if ($this->getFrameworkTestResults() == null) {
|
|
|
|
$this->setFrameworkTestResults(new PHPUnit_Framework_TestResult());
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2010-10-19 05:06:55 +02:00
|
|
|
$this->getFrameworkTestResults()->addListener( $this->getReporter() );
|
|
|
|
|
|
|
|
$this->beforeRunTests();
|
|
|
|
$this->getSuite()->run($this->getFrameworkTestResults());
|
2014-04-17 15:14:51 +02:00
|
|
|
$this->afterRunTests();
|
2010-10-19 05:06:55 +02:00
|
|
|
}
|
2012-03-24 04:38:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array containing all the module folders in the base dir.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function moduleDirectories() {
|
|
|
|
$files = scandir(BASE_PATH);
|
|
|
|
$modules = array();
|
|
|
|
foreach($files as $file) {
|
|
|
|
if(is_dir(BASE_PATH . "/$file") && file_exists(BASE_PATH . "/$file/_config.php")) {
|
|
|
|
$modules[] = $file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $modules;
|
|
|
|
}
|
2010-10-19 05:06:55 +02:00
|
|
|
}
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
/**
|
2010-10-19 05:35:14 +02:00
|
|
|
* Interface, implementing the general PHPUnit wrapper API.
|
|
|
|
*/
|
2010-10-19 05:06:55 +02:00
|
|
|
interface IPhpUnitWrapper {
|
|
|
|
|
|
|
|
public function init();
|
|
|
|
|
|
|
|
public function runTests();
|
|
|
|
}
|
|
|
|
|
2010-10-19 05:35:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PHPUnitWrapper Exception class
|
2014-08-15 08:53:05 +02:00
|
|
|
*/
|
2010-10-19 05:35:14 +02:00
|
|
|
class PhpUnitWrapper_Exception extends Exception {}
|
|
|
|
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
// If PHPUnit is not installed on the local environment, declare the class to
|
2010-10-19 05:35:14 +02:00
|
|
|
// ensure that missing class declarations are available to avoind any PHP fatal
|
|
|
|
// errors.
|
|
|
|
//
|
|
|
|
if(!PhpUnitWrapper::has_php_unit()) {
|
2010-10-19 05:06:55 +02:00
|
|
|
/**
|
2014-09-08 07:46:09 +02:00
|
|
|
* PHPUnit is a testing framework that can be installed using Composer.
|
2012-03-24 04:38:57 +01:00
|
|
|
* It's not bundled with SilverStripe, you will need to install it yourself.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2010-10-19 05:06:55 +02:00
|
|
|
* @subpackage testing
|
|
|
|
*/
|
|
|
|
class PHPUnit_Framework_TestCase {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|