-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBootstrapManager.php
More file actions
191 lines (166 loc) · 4.68 KB
/
Copy pathBootstrapManager.php
File metadata and controls
191 lines (166 loc) · 4.68 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
namespace WPMedia\PHPUnit;
class BootstrapManager {
/**
* Builds up the command-line arguments that PHPUnit needs and then loads phpunit.
*
* @since 1.0.0
*
* @param string $which_testsuite Which test suite to run: "unit" or "integration".
*/
public static function runTestSuite( $which_testsuite ) {
$_SERVER['argv'] = $GLOBALS['argv'] = self::getConfigArgv( $which_testsuite );
$_SERVER['argc'] = $GLOBALS['argc'] = count( $GLOBALS['argv'] );
// Find and load PHPUnit.
foreach ( [ dirname( dirname( __DIR__ ) ), __DIR__ . '/vendor' ] as $root ) {
if ( is_readable( "{$root}/bin/phpunit" ) ) {
require_once "{$root}/bin/phpunit";
return;
}
}
}
/**
* Sets up the constants for the test suite.
*
* @since 1.0.0
*
* @param string $which_testsuite Which test suite to run: "unit" or "integration".
*/
public static function setupConstants( $which_testsuite ) {
define( 'WPMEDIA_PHPUNIT_ROOT_DIR', self::getRootDir( self::getArg( 'WPMEDIA_PHPUNIT_ROOT_DIR' ) ) );
$path = self::getArg( 'path' );
if ( false !== $path ) {
$path = rtrim( $path['path'], '/\\' );
} else {
$path = 'Tests/' . ucfirst( $which_testsuite );
}
define( 'WPMEDIA_PHPUNIT_ROOT_TEST_DIR', WPMEDIA_PHPUNIT_ROOT_DIR . DIRECTORY_SEPARATOR . $path );
}
/**
* Checks if the --group exists and matches the given name.
*
* @since 1.0.0
*
* @param string $group_name Group name to check.
*
* @return bool true when --group exists and name matches; else false.
*/
public static function isGroup( $group_name ) {
$group = self::getArg( '--group' );
if ( false === $group ) {
return false;
}
$group_name_index = ++$group['index'];
// Bail out if no groupName given.
if ( ! isset( $_SERVER['argv'][ $group_name_index ] ) ) {
return false;
}
// The next arg after the `--group` is the group name. Check if it matches.
return ( $group_name === $_SERVER['argv'][ $group_name_index ] );
}
/**
* Gets the absolute path to the phpunit.xml.dist file.
*
* @since 1.0.0
*
* @param string $test_dir Name of the test directory, i.e. Unit, unit, Integration, integration.
*
* @return string the absolute path to the phpunit.xml.dist file.
*/
protected static function getPhpunitXml( $test_dir ) {
if ( is_readable( WPMEDIA_PHPUNIT_ROOT_TEST_DIR . '/phpunit.xml.dist' ) ) {
return WPMEDIA_PHPUNIT_ROOT_TEST_DIR . '/phpunit.xml.dist';
}
return __DIR__ . "/{$test_dir}/phpunit.xml.dist";
}
/**
* Gets the configuration to build the command line arguments for which test to run.
*
* @since 1.0.0
*
* @param string $test unit or integration.
*
* @return array command line arguments for $argv.
*/
protected static function getConfigArgv( $test ) {
$script = [];
if ( 'unit' === $test ) {
$script = [
'vendor/bin/phpunit',
'--testsuite',
'unit',
'--colors=always',
'--configuration',
self::getPhpunitXml( 'Unit' ),
];
} elseif ( 'integration' === $test ) {
$script = [
'vendor/bin/phpunit',
'--testsuite',
'integration',
'--colors=always',
'--configuration',
self::getPhpunitXml( 'Integration' ),
];
}
self::removeCliArg( 'path' );
self::removeCliArg( 'WPMEDIA_PHPUNIT_ROOT_DIR' );
if ( 2 === $_SERVER['argc'] ) {
return $script;
}
// Add in additional command line arguments.
foreach ( array_slice( $_SERVER['argv'], 2 ) as $arg ) {
$script[] = $arg;
}
return $script;
}
/**
* Removes the given argument from the command line arguments.
*
* @since 1.0.0
*
* @param string $key The command line argument "key" to be stripped out.
*/
protected static function removeCliArg( $key ) {
$arg = self::getArg( $key );
if ( false === $arg ) {
return;
}
unset( $_SERVER['argv'][ $arg['index'] ] );
}
/**
* Gets the requested command line argument value, if it exists.
*
* @since 1.0.0
*
* @param string $key Command-line argument.
*
* @return array|bool
*/
protected static function getArg( $key ) {
foreach ( $_SERVER['argv'] as $index => $arg ) {
if ( $key === substr( $arg, 0, strlen( $key ) ) ) {
return [ 'index' => $index, $key => str_replace( "{$key}=", '', $arg ) ];
}
}
return false;
}
/**
* Gets the absolute path to the root (the parent package, plugin, or repo using this package) directory.
*
* @since 1.0
*
* @param string $root The given starting root, if any.
*
* @return string absolute path to the root directory.
*/
protected static function getRootDir( $root ) {
if ( false === $root ) {
return dirname( dirname( dirname( __DIR__ ) ) );
}
if ( '.' === $root['WPMEDIA_PHPUNIT_ROOT_DIR'] ) {
return __DIR__;
}
return ltrim( $root['WPMEDIA_PHPUNIT_ROOT_DIR'], '/\\' );
}
}