Skip to content

Commit

Permalink
BUG Allow PHPUnit installation with composer / Fix travis
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Morgan authored and Damian Mooyman committed May 2, 2014
1 parent 48f6566 commit bec8927
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 66 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Expand Up @@ -19,6 +19,9 @@
"php": ">=5.3.2",
"composer/installers": "*"
},
"require-dev": {
"phpunit/PHPUnit": "~3.7"
},
"autoload": {
"classmap": ["tests/behat/features/bootstrap"]
},
Expand Down
20 changes: 14 additions & 6 deletions dev/phpunit/PhpUnitWrapper.php
Expand Up @@ -138,14 +138,22 @@ public function getVersion() {
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();

}
Expand Down Expand Up @@ -209,7 +217,7 @@ public function runTests() {

$this->beforeRunTests();
$this->getSuite()->run($this->getFrameworkTestResults());
$this->aferRunTests();
$this->afterRunTests();
}

/**
Expand Down
8 changes: 5 additions & 3 deletions dev/phpunit/PhpUnitWrapper_3_4.php
Expand Up @@ -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.
Expand Down Expand Up @@ -46,10 +48,10 @@ protected function beforeRunTests() {
}

/**
* 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()) {

Expand Down
59 changes: 3 additions & 56 deletions dev/phpunit/PhpUnitWrapper_3_5.php
Expand Up @@ -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';
}

/**
Expand All @@ -23,56 +17,9 @@ public function init() {
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');
}
}

}
71 changes: 71 additions & 0 deletions dev/phpunit/PhpUnitWrapper_Generic.php
@@ -0,0 +1,71 @@
<?php

/**
* Generic PhpUnitWrapper.
* Originally intended for use with Composer based installations, but will work
* with any fully functional autoloader.
*/
class PhpUnitWrapper_Generic extends PhpUnitWrapper {

/**
* Returns a version string, like 3.7.34 or 4.2-dev.
* @return string
*/
public function getVersion() {
return PHPUnit_Runner_Version::id();
}

protected $coverage = null;

protected static $test_name = 'SapphireTest';

public static function get_test_name() {
return static::$test_name;
}

/**
* 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 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');
}
}

}

0 comments on commit bec8927

Please sign in to comment.