From bec8927a08bf82139020fc3db9f3e6eb25ac0efc 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 | 20 +++++--- dev/phpunit/PhpUnitWrapper_3_4.php | 8 +-- dev/phpunit/PhpUnitWrapper_3_5.php | 59 ++------------------- dev/phpunit/PhpUnitWrapper_Generic.php | 71 ++++++++++++++++++++++++++ 6 files changed, 97 insertions(+), 66 deletions(-) create mode 100644 dev/phpunit/PhpUnitWrapper_Generic.php diff --git a/.travis.yml b/.travis.yml index 14e32996e..5db2c5ec2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -41,7 +41,7 @@ before_script: - php ~/travis-support/travis_setup_php54_webserver.php --if-env BEHAT_TEST script: - - "if [ \"$BEHAT_TEST\" = \"\" ]; then phpunit framework/tests; fi" + - "if [ \"$BEHAT_TEST\" = \"\" ]; then vendor/bin/phpunit framework/tests; fi" - "if [ \"$BEHAT_TEST\" = \"1\" ]; then vendor/bin/behat @framework; fi" after_failure: diff --git a/composer.json b/composer.json index 0114eca1c..16559bd74 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,9 @@ "php": ">=5.3.2", "composer/installers": "*" }, + "require-dev": { + "phpunit/PHPUnit": "~3.7" + }, "autoload": { "classmap": ["tests/behat/features/bootstrap"] }, diff --git a/dev/phpunit/PhpUnitWrapper.php b/dev/phpunit/PhpUnitWrapper.php index 194339ea7..3765d6478 100644 --- a/dev/phpunit/PhpUnitWrapper.php +++ b/dev/phpunit/PhpUnitWrapper.php @@ -138,14 +138,22 @@ class PhpUnitWrapper implements IPhpUnitWrapper { public static function inst() { if (self::$phpunit_wrapper == null) { - if (fileExistsInIncludePath("/PHPUnit/Autoload.php")) { + // 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(); - } else - if (fileExistsInIncludePath("/PHPUnit/Framework.php")) { + } + // 3.4 detection + else if (fileExistsInIncludePath("/PHPUnit/Framework.php")) { self::$phpunit_wrapper = new PhpUnitWrapper_3_4(); - } else { + } + // 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 6a0d5f67b..3e259df2c 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. @@ -46,10 +48,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()) { diff --git a/dev/phpunit/PhpUnitWrapper_3_5.php b/dev/phpunit/PhpUnitWrapper_3_5.php index 4f244e866..d0bf48efe 100644 --- a/dev/phpunit/PhpUnitWrapper_3_5.php +++ b/dev/phpunit/PhpUnitWrapper_3_5.php @@ -4,16 +4,10 @@ * @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'; - - public static function get_test_name() { - return self::$test_name; + public function getVersion() { + return 'PhpUnit V3.5'; } /** @@ -23,56 +17,9 @@ class PhpUnitWrapper_3_5 extends PhpUnitWrapper { 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 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(); - $modules = $this->moduleDirectories(); - - foreach(TestRunner::config()->coverage_filter_dirs as $dir) { - if($dir[0] == '*') { - $dir = substr($dir, 1); - foreach ($modules as $module) { - $filter->addDirectoryToBlacklist(BASE_PATH . "/$module/$dir"); - } - } else { - $filter->addDirectoryToBlacklist(BASE_PATH . '/' . $dir); - } - } - - $filter->addFileToBlacklist(__FILE__, 'PHPUNIT'); - - $coverage->start(self::get_test_name()); - } - } - - /** - * 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'); - } - } } diff --git a/dev/phpunit/PhpUnitWrapper_Generic.php b/dev/phpunit/PhpUnitWrapper_Generic.php new file mode 100644 index 000000000..502c98f88 --- /dev/null +++ b/dev/phpunit/PhpUnitWrapper_Generic.php @@ -0,0 +1,71 @@ +getCoverageStatus()) { + $this->coverage = new PHP_CodeCoverage(); + $coverage = $this->coverage; + + $filter = $coverage->filter(); + $modules = $this->moduleDirectories(); + + foreach(TestRunner::config()->coverage_filter_dirs as $dir) { + if($dir[0] == '*') { + $dir = substr($dir, 1); + foreach ($modules as $module) { + $filter->addDirectoryToBlacklist(BASE_PATH . "/$module/$dir"); + } + } else { + $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'); + } + } + +}