From ca62f7333000b56687598f5add70ebcc836fa809 Mon Sep 17 00:00:00 2001 From: Will Morgan Date: Thu, 17 Apr 2014 14:14:51 +0100 Subject: [PATCH] BUG Allow PHPUnit installation with composer / Fix travis --- .travis.yml | 2 +- composer.json | 3 ++ dev/phpunit/PhpUnitWrapper.php | 28 ++++++++---- dev/phpunit/PhpUnitWrapper_3_4.php | 10 ++-- dev/phpunit/PhpUnitWrapper_3_5.php | 61 ++++--------------------- dev/phpunit/PhpUnitWrapper_Generic.php | 63 ++++++++++++++++++++++++++ 6 files changed, 100 insertions(+), 67 deletions(-) create mode 100644 dev/phpunit/PhpUnitWrapper_Generic.php diff --git a/.travis.yml b/.travis.yml index c97ef435c..c40b8e809 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,7 +13,7 @@ before_script: - cd ~/builds/ss script: - - phpunit sapphire/tests + - vendor/bin/phpunit sapphire/tests branches: except: diff --git a/composer.json b/composer.json index 0baf15aca..459a64fe3 100644 --- a/composer.json +++ b/composer.json @@ -18,5 +18,8 @@ "require": { "php": ">=5.2.4", "composer/installers": "*" + }, + "require-dev": { + "phpunit/PHPUnit": "~3.7" } } diff --git a/dev/phpunit/PhpUnitWrapper.php b/dev/phpunit/PhpUnitWrapper.php index 7c98107a1..c7fdc06ab 100644 --- a/dev/phpunit/PhpUnitWrapper.php +++ b/dev/phpunit/PhpUnitWrapper.php @@ -135,17 +135,25 @@ class PhpUnitWrapper implements IPhpUnitWrapper { * * @return PhpUnitWrapper Instance of the php-wrapper class */ - static function inst() { + public static function inst() { if (self::$phpunit_wrapper == null) { - if (fileExistsInIncludePath("/PHPUnit/Autoload.php")) { - self::$phpunit_wrapper = new PhpUnitWrapper_3_5(); - } else - if (fileExistsInIncludePath("/PHPUnit/Framework.php")) { - self::$phpunit_wrapper = new PhpUnitWrapper_3_4(); - } else { - self::$phpunit_wrapper = new PhpUnitWrapper(); - } + // 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")) { + self::$phpunit_wrapper = new PhpUnitWrapper_3_5(); + } + // 3.4 detection + else if (fileExistsInIncludePath("/PHPUnit/Framework.php")) { + self::$phpunit_wrapper = new PhpUnitWrapper_3_4(); + } + // No version found - will lead to an error + else { + self::$phpunit_wrapper = new PhpUnitWrapper(); + } self::$phpunit_wrapper->init(); } @@ -209,7 +217,7 @@ class PhpUnitWrapper implements IPhpUnitWrapper { $this->beforeRunTests(); $this->getSuite()->run($this->getFrameworkTestResults()); - $this->aferRunTests(); + $this->afterRunTests(); } } diff --git a/dev/phpunit/PhpUnitWrapper_3_4.php b/dev/phpunit/PhpUnitWrapper_3_4.php index 476fd0fbd..8ee218467 100644 --- a/dev/phpunit/PhpUnitWrapper_3_4.php +++ b/dev/phpunit/PhpUnitWrapper_3_4.php @@ -9,7 +9,9 @@ */ class PhpUnitWrapper_3_4 extends PhpUnitWrapper { - protected $version = 'PhpUnit V3.4'; + public function getVersion() { + return 'PhpUnit V3.4'; + } /** * Initialise the wrapper class. @@ -37,10 +39,10 @@ class PhpUnitWrapper_3_4 extends PhpUnitWrapper { } /** - * Overwrites aferRunTests. Creates coverage report and clover report + * Overwrites afterRunTests. Creates coverage report and clover report * if required. */ - protected function aferRunTests() { + protected function afterRunTests() { if($this->getCoverageStatus()) { @@ -60,4 +62,4 @@ class PhpUnitWrapper_3_4 extends PhpUnitWrapper { } } -} \ No newline at end of file +} diff --git a/dev/phpunit/PhpUnitWrapper_3_5.php b/dev/phpunit/PhpUnitWrapper_3_5.php index f6376965c..65b7abba4 100644 --- a/dev/phpunit/PhpUnitWrapper_3_5.php +++ b/dev/phpunit/PhpUnitWrapper_3_5.php @@ -4,65 +4,22 @@ * @subpackage dev */ -class PhpUnitWrapper_3_5 extends PhpUnitWrapper { +class PhpUnitWrapper_3_5 extends PhpUnitWrapper_Generic { - protected $version = 'PhpUnit V3.5'; - - protected $coverage = null; - - protected static $test_name = 'SapphireTest'; - - static function get_test_name() { - return self::$test_name; + public function getVersion() { + return 'PhpUnit V3.5'; } /** * Initialise the wrapper class. */ public function init() { - require_once 'PHP/CodeCoverage.php'; - require_once 'PHP/CodeCoverage/Report/HTML.php'; - - require_once 'PHPUnit/Autoload.php'; - - require_once 'PHP/CodeCoverage/Filter.php'; - } - - /** - * Overwrites beforeRunTests. Initiates coverage-report generation if - * $coverage has been set to true (@see setCoverageStatus). - */ - 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); - } - - $filter->addFileToBlacklist(__FILE__, 'PHPUNIT'); - - $coverage->start(self::get_test_name()); + if(!class_exists('PHPUnit_Framework_TestCase')) { + require_once 'PHP/CodeCoverage.php'; + require_once 'PHP/CodeCoverage/Report/HTML.php'; + require_once 'PHPUnit/Autoload.php'; + require_once 'PHP/CodeCoverage/Filter.php'; } } - /** - * Overwrites aferRunTests. Creates coverage report and clover report - * if required. - */ - protected function aferRunTests() { - - if($this->getCoverageStatus()) { - $coverage = $this->coverage; - $coverage->stop(); - - $writer = new PHP_CodeCoverage_Report_HTML(); - $writer->process($coverage, ASSETS_PATH.'/code-coverage-report'); - } - } - -} \ No newline at end of file +} diff --git a/dev/phpunit/PhpUnitWrapper_Generic.php b/dev/phpunit/PhpUnitWrapper_Generic.php new file mode 100644 index 000000000..5b8a1fc13 --- /dev/null +++ b/dev/phpunit/PhpUnitWrapper_Generic.php @@ -0,0 +1,63 @@ +getCoverageStatus()) { + $this->coverage = new PHP_CodeCoverage(); + $coverage = $this->coverage; + + $filter = $coverage->filter(); + + foreach(TestRunner::$coverage_filter_dirs as $dir) { + $filter->addDirectoryToBlacklist(BASE_PATH . '/' . $dir); + } + + $filter->addFileToBlacklist(__FILE__, 'PHPUNIT'); + + $coverage->start(self::get_test_name()); + } + } + + /** + * Overwrites afterRunTests. Creates coverage report and clover report + * if required. + */ + protected function afterRunTests() { + + if($this->getCoverageStatus()) { + $coverage = $this->coverage; + $coverage->stop(); + + $writer = new PHP_CodeCoverage_Report_HTML(); + $writer->process($coverage, ASSETS_PATH.'/code-coverage-report'); + } + } + +}