Skip to content

Commit 190a68c

Browse files
committed
Avoid side effects in FeatureContext file
1 parent 500516c commit 190a68c

File tree

1 file changed

+81
-79
lines changed

1 file changed

+81
-79
lines changed

src/Context/FeatureContext.php

Lines changed: 81 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,6 @@ private static function running_with_code_coverage() {
282282
return \in_array( $with_code_coverage, [ 'true', '1' ], true );
283283
}
284284

285-
/**
286-
* @AfterSuite
287-
*/
288285
public static function merge_coverage_reports(): void {
289286
if ( ! self::running_with_code_coverage() ) {
290287
return;
@@ -430,14 +427,14 @@ private static function get_process_env_variables(): array {
430427

431428
// Ensure we're using the expected `wp` binary.
432429
$bin_path = self::get_bin_path();
433-
wp_cli_behat_env_debug( "WP-CLI binary path: {$bin_path}" );
430+
self::debug( "WP-CLI binary path: {$bin_path}" );
434431

435432
if ( ! file_exists( "{$bin_path}/wp" ) ) {
436-
wp_cli_behat_env_debug( "WARNING: No file named 'wp' found in the provided/detected binary path." );
433+
self::debug( "WARNING: No file named 'wp' found in the provided/detected binary path." );
437434
}
438435

439436
if ( ! is_executable( "{$bin_path}/wp" ) ) {
440-
wp_cli_behat_env_debug( "WARNING: File named 'wp' found in the provided/detected binary path is not executable." );
437+
self::debug( "WARNING: File named 'wp' found in the provided/detected binary path is not executable." );
441438
}
442439

443440
$path_separator = Utils\is_windows() ? ';' : ':';
@@ -502,9 +499,9 @@ private static function get_process_env_variables(): array {
502499
}
503500

504501
// Dump environment for debugging purposes, but before adding the GitHub token.
505-
wp_cli_behat_env_debug( 'Environment:' );
502+
self::debug( 'Environment:' );
506503
foreach ( $env as $key => $value ) {
507-
wp_cli_behat_env_debug( " [{$key}] => {$value}" );
504+
self::debug( " [{$key}] => {$value}" );
508505
}
509506

510507
$github_token = getenv( 'GITHUB_TOKEN' );
@@ -651,6 +648,8 @@ private static function cache_wp_files( $version = '' ): void {
651648
* @BeforeSuite
652649
*/
653650
public static function prepare( BeforeSuiteScope $scope ): void {
651+
self::bootstrap_feature_context();
652+
654653
// Test performance statistics - useful for detecting slow tests.
655654
self::$log_run_times = getenv( 'WP_CLI_TEST_LOG_RUN_TIMES' );
656655
if ( false !== self::$log_run_times ) {
@@ -679,6 +678,8 @@ public static function prepare( BeforeSuiteScope $scope ): void {
679678
* @AfterSuite
680679
*/
681680
public static function afterSuite( AfterSuiteScope $scope ): void {
681+
self::bootstrap_feature_context();
682+
682683
if ( self::$composer_local_repository ) {
683684
self::remove_dir( self::$composer_local_repository );
684685
self::$composer_local_repository = null;
@@ -687,6 +688,8 @@ public static function afterSuite( AfterSuiteScope $scope ): void {
687688
if ( self::$log_run_times ) {
688689
self::log_run_times_after_suite( $scope );
689690
}
691+
692+
self::merge_coverage_reports();
690693
}
691694

692695
/**
@@ -802,6 +805,8 @@ public static function create_cache_dir(): string {
802805
* Every scenario gets its own context object.
803806
*/
804807
public function __construct() {
808+
$this->bootstrap_feature_context();
809+
805810
if ( getenv( 'WP_CLI_TEST_DBROOTUSER' ) ) {
806811
$this->variables['DB_ROOT_USER'] = getenv( 'WP_CLI_TEST_DBROOTUSER' );
807812
}
@@ -866,6 +871,74 @@ public function __construct() {
866871
$this->set_cache_dir();
867872
}
868873

874+
/**
875+
* @param string $message
876+
*/
877+
protected static function debug( $message ): void { // phpcs:ignore Universal.Files.SeparateFunctionsFromOO.Mixed
878+
if ( ! getenv( 'WP_CLI_TEST_DEBUG_BEHAT_ENV' ) ) {
879+
return;
880+
}
881+
882+
echo "{$message}\n";
883+
}
884+
885+
/**
886+
* Load required support files as needed before heading into the Behat context.
887+
*/
888+
protected static function bootstrap_feature_context(): void {
889+
$vendor_folder = self::get_vendor_dir();
890+
self::debug( "Vendor folder location: {$vendor_folder}" );
891+
892+
// Didn't manage to detect a valid vendor folder.
893+
if ( empty( $vendor_folder ) ) {
894+
return;
895+
}
896+
897+
// We assume the vendor folder is located in the project root folder.
898+
$project_folder = dirname( $vendor_folder );
899+
900+
$framework_folder = self::get_framework_dir();
901+
self::debug( "Framework folder location: {$framework_folder}" );
902+
903+
// Load helper functionality that is needed for the tests.
904+
require_once "{$framework_folder}/php/utils.php";
905+
require_once "{$framework_folder}/php/WP_CLI/Process.php";
906+
require_once "{$framework_folder}/php/WP_CLI/ProcessRun.php";
907+
908+
// Manually load Composer file includes by generating a config with require:
909+
// statements for each file.
910+
$project_composer = "{$project_folder}/composer.json";
911+
if ( ! file_exists( $project_composer ) ) {
912+
return;
913+
}
914+
915+
$composer = json_decode( file_get_contents( $project_composer ) );
916+
if ( empty( $composer->autoload->files ) ) {
917+
return;
918+
}
919+
920+
$contents = "require:\n";
921+
foreach ( $composer->autoload->files as $file ) {
922+
$contents .= " - {$project_folder}/{$file}\n";
923+
}
924+
925+
$temp_folder = sys_get_temp_dir() . '/wp-cli-package-test';
926+
if (
927+
! is_dir( $temp_folder )
928+
&& ! mkdir( $temp_folder )
929+
&& ! is_dir( $temp_folder )
930+
) {
931+
return;
932+
}
933+
934+
$project_config = "{$temp_folder}/config.yml";
935+
file_put_contents( $project_config, $contents );
936+
putenv( 'WP_CLI_CONFIG_PATH=' . $project_config );
937+
938+
self::debug( "Project config file location: {$project_config}" );
939+
self::debug( "Project config:\n{$contents}" );
940+
}
941+
869942
/**
870943
* Replace standard {VARIABLE_NAME} variables and the special {INVOKE_WP_CLI_WITH_PHP_ARGS-args} and {WP_VERSION-version-latest} variables.
871944
* Note that standard variable names can only contain uppercase letters, digits and underscores and cannot begin with a digit.
@@ -1766,74 +1839,3 @@ private static function log_proc_method_run_time( $key, $start_time ): void {
17661839
++self::$proc_method_run_times[ $key ][1];
17671840
}
17681841
}
1769-
1770-
1771-
/**
1772-
* @param string $message
1773-
*/
1774-
function wp_cli_behat_env_debug( $message ): void { // phpcs:ignore Universal.Files.SeparateFunctionsFromOO.Mixed
1775-
if ( ! getenv( 'WP_CLI_TEST_DEBUG_BEHAT_ENV' ) ) {
1776-
return;
1777-
}
1778-
1779-
echo "{$message}\n";
1780-
}
1781-
1782-
/**
1783-
* Load required support files as needed before heading into the Behat context.
1784-
*/
1785-
function wpcli_bootstrap_behat_feature_context(): void {
1786-
$vendor_folder = FeatureContext::get_vendor_dir();
1787-
wp_cli_behat_env_debug( "Vendor folder location: {$vendor_folder}" );
1788-
1789-
// Didn't manage to detect a valid vendor folder.
1790-
if ( empty( $vendor_folder ) ) {
1791-
return;
1792-
}
1793-
1794-
// We assume the vendor folder is located in the project root folder.
1795-
$project_folder = dirname( $vendor_folder );
1796-
1797-
$framework_folder = FeatureContext::get_framework_dir();
1798-
wp_cli_behat_env_debug( "Framework folder location: {$framework_folder}" );
1799-
1800-
// Load helper functionality that is needed for the tests.
1801-
require_once "{$framework_folder}/php/utils.php";
1802-
require_once "{$framework_folder}/php/WP_CLI/Process.php";
1803-
require_once "{$framework_folder}/php/WP_CLI/ProcessRun.php";
1804-
1805-
// Manually load Composer file includes by generating a config with require:
1806-
// statements for each file.
1807-
$project_composer = "{$project_folder}/composer.json";
1808-
if ( ! file_exists( $project_composer ) ) {
1809-
return;
1810-
}
1811-
1812-
$composer = json_decode( file_get_contents( $project_composer ) );
1813-
if ( empty( $composer->autoload->files ) ) {
1814-
return;
1815-
}
1816-
1817-
$contents = "require:\n";
1818-
foreach ( $composer->autoload->files as $file ) {
1819-
$contents .= " - {$project_folder}/{$file}\n";
1820-
}
1821-
1822-
$temp_folder = sys_get_temp_dir() . '/wp-cli-package-test';
1823-
if (
1824-
! is_dir( $temp_folder )
1825-
&& ! mkdir( $temp_folder )
1826-
&& ! is_dir( $temp_folder )
1827-
) {
1828-
return;
1829-
}
1830-
1831-
$project_config = "{$temp_folder}/config.yml";
1832-
file_put_contents( $project_config, $contents );
1833-
putenv( 'WP_CLI_CONFIG_PATH=' . $project_config );
1834-
1835-
wp_cli_behat_env_debug( "Project config file location: {$project_config}" );
1836-
wp_cli_behat_env_debug( "Project config:\n{$contents}" );
1837-
}
1838-
1839-
wpcli_bootstrap_behat_feature_context();

0 commit comments

Comments
 (0)