-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathphpunit-bootstrap.php
84 lines (70 loc) · 2.4 KB
/
phpunit-bootstrap.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
<?php
/**
* YoastCS: Bootstrap file for running the tests.
*
* - Load the PHPCS PHPUnit bootstrap file providing cross-version PHPUnit support.
* {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1384}
* - Load the Composer autoload file.
* - Automatically limit the testing to the YoastCS tests.
*
* @package Yoast\YoastCS
* @since 3.0.0
*/
use PHP_CodeSniffer\Util\Standards;
if ( defined( 'PHP_CODESNIFFER_IN_TESTS' ) === false ) {
define( 'PHP_CODESNIFFER_IN_TESTS', true );
}
/*
* Load the necessary PHPCS files.
*/
// Get the PHPCS dir from an environment variable.
$phpcs_dir = getenv( 'PHPCS_DIR' );
$composer_phpcs_path = __DIR__ . '/vendor/squizlabs/php_codesniffer';
if ( $phpcs_dir === false && is_dir( $composer_phpcs_path ) ) {
// PHPCS installed via Composer.
$phpcs_dir = $composer_phpcs_path;
}
elseif ( $phpcs_dir !== false ) {
/*
* PHPCS in a custom directory.
* For this to work, the `PHPCS_DIR` needs to be set in a custom `phpunit.xml` file.
*/
$phpcs_dir = realpath( $phpcs_dir );
}
// Try and load the PHPCS autoloader.
if ( $phpcs_dir !== false
&& file_exists( $phpcs_dir . '/autoload.php' )
&& file_exists( $phpcs_dir . '/tests/bootstrap.php' )
) {
require_once $phpcs_dir . '/autoload.php';
require_once $phpcs_dir . '/tests/bootstrap.php'; // PHPUnit 6.x+ support.
}
else {
echo 'Uh oh... can\'t find PHPCS.
If you use Composer, please run `composer install`.
Otherwise, make sure you set a `PHPCS_DIR` environment variable in your phpunit.xml file
pointing to the PHPCS directory and that PHPCSUtils is included in the `installed_paths`
for that PHPCS install.
';
exit( 1 );
}
/*
* Set the PHPCS_IGNORE_TEST environment variable to ignore tests from other standards.
*/
$yoast_standards = [
'Yoast' => true,
];
$all_standards = Standards::getInstalledStandards();
$all_standards[] = 'Generic';
$standards_to_ignore = [];
foreach ( $all_standards as $standard ) {
if ( isset( $yoast_standards[ $standard ] ) === true ) {
continue;
}
$standards_to_ignore[] = $standard;
}
$standards_to_ignore_string = implode( ',', $standards_to_ignore );
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_putenv -- This is not production, but test code.
putenv( "PHPCS_IGNORE_TESTS={$standards_to_ignore_string}" );
// Clean up.
unset( $phpcs_dir, $composer_phpcs_path, $all_standards, $standards_to_ignore, $standard, $standards_to_ignore_string );