Test Setup Failed
Pull Request — master (#216)
by Viruthagiri
05:38
created

WP_PHPUnit_Util_Getopt::__construct()   D

Complexity

Conditions 17
Paths 260

Size

Total Lines 58
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 58
rs 4.738
cc 17
eloc 36
nc 260
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Installs WordPress for running the tests and loads WordPress and the test libraries
4
 */
5
6
7
$config_file_path = dirname( dirname( __FILE__ ) );
8
if ( ! file_exists( $config_file_path . '/wp-tests-config.php' ) ) {
9
	// Support the config file from the root of the develop repository.
10
	if ( basename( $config_file_path ) === 'phpunit' && basename( dirname( $config_file_path ) ) === 'tests' )
11
		$config_file_path = dirname( dirname( $config_file_path ) );
12
}
13
$config_file_path .= '/wp-tests-config.php';
14
15
/*
16
 * Globalize some WordPress variables, because PHPUnit loads this file inside a function
17
 * See: https://github.com/sebastianbergmann/phpunit/issues/325
18
 */
19
global $wpdb, $current_site, $current_blog, $wp_rewrite, $shortcode_tags, $wp, $phpmailer;
20
21
if ( !is_readable( $config_file_path ) ) {
22
	die( "ERROR: wp-tests-config.php is missing! Please use wp-tests-config-sample.php to create a config file.\n" );
23
}
24
require_once $config_file_path;
25
26
define( 'WP_TESTS_TABLE_PREFIX', $table_prefix );
27
define( 'DIR_TESTDATA', dirname( __FILE__ ) . '/../data' );
28
29
if ( ! defined( 'WP_TESTS_FORCE_KNOWN_BUGS' ) )
30
	define( 'WP_TESTS_FORCE_KNOWN_BUGS', false );
31
32
// Cron tries to make an HTTP request to the blog, which always fails, because tests are run in CLI mode only
33
define( 'DISABLE_WP_CRON', true );
34
35
define( 'WP_MEMORY_LIMIT', -1 );
36
define( 'WP_MAX_MEMORY_LIMIT', -1 );
37
38
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
39
$_SERVER['HTTP_HOST'] = WP_TESTS_DOMAIN;
40
$_SERVER['SERVER_NAME'] = WP_TESTS_DOMAIN;
41
$_SERVER['REQUEST_METHOD'] = 'GET';
42
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
43
44
$PHP_SELF = $GLOBALS['PHP_SELF'] = $_SERVER['PHP_SELF'] = '/index.php';
45
46
if ( "1" == getenv( 'WP_MULTISITE' ) ||
47
	( defined( 'WP_TESTS_MULTISITE') && WP_TESTS_MULTISITE ) ) {
48
	$multisite = true;
49
} else {
50
	$multisite = false;
51
}
52
53
// Override the PHPMailer
54
require_once( dirname( __FILE__ ) . '/mock-mailer.php' );
55
$phpmailer = new MockPHPMailer();
56
57
system( WP_PHP_BINARY . ' ' . escapeshellarg( dirname( __FILE__ ) . '/install.php' ) . ' ' . escapeshellarg( $config_file_path ) . ' ' . $multisite );
58
59
if ( $multisite ) {
60
	echo "Running as multisite..." . PHP_EOL;
61
	define( 'MULTISITE', true );
62
	define( 'SUBDOMAIN_INSTALL', false );
63
	$GLOBALS['base'] = '/';
64
} else {
65
	echo "Running as single site... To run multisite, use -c tests/phpunit/multisite.xml" . PHP_EOL;
66
}
67
unset( $multisite );
68
69
require_once dirname( __FILE__ ) . '/functions.php';
70
71
$GLOBALS['_wp_die_disabled'] = false;
72
// Allow tests to override wp_die
73
tests_add_filter( 'wp_die_handler', '_wp_die_handler_filter' );
74
75
// Preset WordPress options defined in bootstrap file.
76
// Used to activate themes, plugins, as well as  other settings.
77
if(isset($GLOBALS['wp_tests_options'])) {
78
	function wp_tests_options( $value ) {
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
		$key = substr( current_filter(), strlen( 'pre_option_' ) );
80
		return $GLOBALS['wp_tests_options'][$key];
81
	}
82
83
	foreach ( array_keys( $GLOBALS['wp_tests_options'] ) as $key ) {
84
		tests_add_filter( 'pre_option_'.$key, 'wp_tests_options' );
85
	}
86
}
87
88
// Load WordPress
89
require_once ABSPATH . '/wp-settings.php';
90
91
// Delete any default posts & related data
92
_delete_all_posts();
93
94
require dirname( __FILE__ ) . '/testcase.php';
95
require dirname( __FILE__ ) . '/testcase-rest-api.php';
96
require dirname( __FILE__ ) . '/testcase-xmlrpc.php';
97
require dirname( __FILE__ ) . '/testcase-ajax.php';
98
require dirname( __FILE__ ) . '/testcase-canonical.php';
99
require dirname( __FILE__ ) . '/exceptions.php';
100
require dirname( __FILE__ ) . '/utils.php';
101
require dirname( __FILE__ ) . '/spy-rest-server.php';
102
103
/**
104
 * A child class of the PHP test runner.
105
 *
106
 * Used to access the protected longOptions property, to parse the arguments
107
 * passed to the script.
108
 *
109
 * If it is determined that phpunit was called with a --group that corresponds
110
 * to an @ticket annotation (such as `phpunit --group 12345` for bugs marked
111
 * as #WP12345), then it is assumed that known bugs should not be skipped.
112
 *
113
 * If WP_TESTS_FORCE_KNOWN_BUGS is already set in wp-tests-config.php, then
114
 * how you call phpunit has no effect.
115
 */
116
class WP_PHPUnit_Util_Getopt extends PHPUnit_Util_Getopt {
117
	protected $longOptions = array(
118
	  'exclude-group=',
119
	  'group=',
120
	);
121
	function __construct( $argv ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
122
		array_shift( $argv );
123
		$options = array();
124
		while ( list( $i, $arg ) = each( $argv ) ) {
0 ignored issues
show
Unused Code introduced by
The assignment to $i is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
125
			try {
126
				if ( strlen( $arg ) > 1 && $arg[0] === '-' && $arg[1] === '-' ) {
127
					PHPUnit_Util_Getopt::parseLongOption( substr( $arg, 2 ), $this->longOptions, $options, $argv );
128
				}
129
			}
130
			catch ( PHPUnit_Framework_Exception $e ) {
131
				// Enforcing recognized arguments or correctly formed arguments is
132
				// not really the concern here.
133
				continue;
134
			}
135
		}
136
137
		$skipped_groups = array(
138
			'ajax' => true,
139
			'ms-files' => true,
140
			'external-http' => true,
141
		);
142
143
		foreach ( $options as $option ) {
144
			switch ( $option[0] ) {
145
				case '--exclude-group' :
146
					foreach ( $skipped_groups as $group_name => $skipped ) {
147
						$skipped_groups[ $group_name ] = false;
148
					}
149
					continue 2;
150
				case '--group' :
151
					$groups = explode( ',', $option[1] );
152
					foreach ( $groups as $group ) {
153
						if ( is_numeric( $group ) || preg_match( '/^(UT|Plugin)\d+$/', $group ) ) {
154
							WP_UnitTestCase::forceTicket( $group );
155
						}
156
					}
157
158
					foreach ( $skipped_groups as $group_name => $skipped ) {
159
						if ( in_array( $group_name, $groups ) ) {
160
							$skipped_groups[ $group_name ] = false;
161
						}
162
					}
163
					continue 2;
164
			}
165
		}
166
167
		$skipped_groups = array_filter( $skipped_groups );
168
		foreach ( $skipped_groups as $group_name => $skipped ) {
169
			echo sprintf( 'Not running %1$s tests. To execute these, use --group %1$s.', $group_name ) . PHP_EOL;
170
		}
171
172
		if ( ! isset( $skipped_groups['external-http'] ) ) {
173
			echo PHP_EOL;
174
			echo 'External HTTP skipped tests can be caused by timeouts.' . PHP_EOL;
175
			echo 'If this changeset includes changes to HTTP, make sure there are no timeouts.' . PHP_EOL;
176
			echo PHP_EOL;
177
		}
178
    }
179
}
180
new WP_PHPUnit_Util_Getopt( $_SERVER['argv'] );
181