forked from darnellefunk/komodo-phpunit-harness
/
HarnessSelector.php
157 lines (105 loc) · 5.15 KB
/
HarnessSelector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* @author Michael Heim
* @link http://www.zeilenwechsel.de/
* @version 1.1.0
* @license http://www.zeilenwechsel.de/it/code/browse/komodo-phpunit-harness/prod/license.txt
*/
require_once('phpunit.phar');
/**
* Change to support PHPUnit 4+
* This library is loaded in via phpunit.phar above
*/
//require_once( 'PHPUnit/Runner/Version.php' );
require_once( dirname( dirname( __file__ ) ) . '/Utils/Loader.php' );
PHPUnitXt_Utils_Loader::load( 'PHPUnitXt_Base_PrinterOptions' );
class PHPUnitXt_Base_HarnessSelector {
protected $ver_phpunit, $root;
/* @var PHPUnitXt_Base_PrinterOptions */
protected $defaults;
/* @var array for PHPUnit 3.6 */
protected $blacklist = array();
public function __construct ( PHPUnitXt_Base_PrinterOptions $defaults ) {
$this->defaults = $defaults;
$this->ver_phpunit = $this->get_phpunit_version();
$this->root = PHPUnitXt_Utils_Loader::root();
}
public function run_harness( $argv, $argc ) {
$this->prepare_harness();
PHPUnitXt_Utils_Loader::load( 'PHPUnitXt_Base_Command' );
PHPUnitXt_Base_Command::exec( $argv, $this->defaults, $this->blacklist );
}
public function get_phpunit_version( $as_string = false ) {
$ver = PHPUnit_Runner_Version::id();
if ( ! $as_string ) {
$ver_array = explode( '.', $ver );
$ver_array[0] .= '.';
$ver = (float) implode( '', $ver_array );
}
return $ver;
}
protected function prepare_harness() {
if ( $this->ver_phpunit < 3.5 ) {
require_once( 'PHPUnit/Framework.php' );
require_once( 'PHPUnit/TextUI/ResultPrinter.php' );
require_once( 'PHPUnit/TextUI/Command.php' );
PHPUnit_Util_Filter::addDirectoryToFilter( $this->root, '.php', 'PHPUNIT', '' );
} elseif ( $this->ver_phpunit < 3.6 ) {
require_once( 'PHPUnit/Autoload.php' );
require_once( 'PHP/CodeCoverage.php' );
PHP_CodeCoverage::getInstance()->filter()->addDirectoryToBlacklist( $this->root, '.php', '', 'PHPUNIT' );
} else {
// PHPUnit 3.6+
/**
* Change to support PHPUnit 4+
* These libraries are loaded in via phpunit.phar or don't exist in PHPUnit 4+
*/
//require_once( 'PHPUnit/Autoload.php' );
//require_once( 'PHP/CodeCoverage.php' );
$this->add_directory_to_blacklist( $this->root, '.php' );
}
}
/**
* Adds a directory to the blacklist (recursively). For use with PHPUnit 3.6.
*
* NB: There is a way to modify the internal blacklist directly. It would
* involve creating a PHPUnit_Util_GlobalState reflector and calling
* setAccessible() on it. This is suggested on StackOverflow, http://goo.gl/HP2el.
* But it requires PHP 5.3.
*
* @param string $directory
* @param string $suffix
* @return PHPUnitXt_Base_HarnessSelector
*/
protected function add_directory_to_blacklist ( $directory, $suffix = '.php' ) {
$dirpath = realpath( $directory );
$filepaths = array();
if ( is_dir( $dirpath ) ) {
$dir_iterator = new RecursiveDirectoryIterator( $dirpath );
/* @var $file DirectoryIterator */
foreach ( new RecursiveIteratorIterator( $dir_iterator ) as $file ) {
$path = $file->getPathname();
if ( $suffix === '' or substr( $path, - strlen( $suffix ) ) == $suffix ) $this->add_file_to_blacklist( $path );
}
}
return $this;
}
/**
* Adds a file to the blacklist. For use with PHPUnit 3.6.
*
* NB: realpath() is invoked here.
*
* @param string $filepath
* @param boolean $avoid_duplicates
* @return PHPUnitXt_Base_HarnessSelector
*/
protected function add_file_to_blacklist ( $filepath, $avoid_duplicates = false ) {
$path = realpath( $filepath );
if ( $avoid_duplicates ) {
if ( in_array( $path, $this->blacklist ) ) $path = false;
}
if ( $path !== false ) $this->blacklist[] = $path;
return $this;
}
}
?>