Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/run-behat-tests
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,4 @@ if [[ "${WP_CLI_TEST_COVERAGE}" == "true" ]] && vendor/bin/behat --help 2>/dev/n
fi

# Run the functional tests.
vendor/bin/behat --format progress "$BEHAT_TAGS" --strict "${BEHAT_EXTRA_ARGS[@]}" "$@"
vendor/bin/behat --snippets-for="WP_CLI\Tests\Context\FeatureContext" --format progress "$BEHAT_TAGS" --strict "${BEHAT_EXTRA_ARGS[@]}" "$@"
164 changes: 79 additions & 85 deletions src/Context/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace WP_CLI\Tests\Context;

use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Behat\Context\Context;
use Behat\Behat\EventDispatcher\Event\OutlineTested;
use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
Expand All @@ -13,15 +13,13 @@
use Behat\Behat\Hook\Scope\AfterFeatureScope;
use Behat\Behat\Hook\Scope\BeforeFeatureScope;
use Behat\Behat\Hook\Scope\BeforeStepScope;
use Behat\Testwork\Hook\Scope\HookScope;
use SebastianBergmann\CodeCoverage\Report\Clover;
use SebastianBergmann\CodeCoverage\Driver\Selector;
use SebastianBergmann\CodeCoverage\Driver\Xdebug;
use SebastianBergmann\CodeCoverage\Filter;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\Environment\Runtime;
use RuntimeException;
use WP_CLI;
use DirectoryIterator;
use WP_CLI\Process;
use WP_CLI\ProcessRun;
Expand All @@ -30,10 +28,8 @@

/**
* Features context.
*
* @phpstan-ignore class.implementsDeprecatedInterface
*/
class FeatureContext implements SnippetAcceptingContext {
class FeatureContext implements Context {

use GivenStepDefinitions;
use ThenStepDefinitions;
Expand Down Expand Up @@ -282,9 +278,6 @@ private static function running_with_code_coverage() {
return \in_array( $with_code_coverage, [ 'true', '1' ], true );
}

/**
* @AfterSuite
*/
public static function merge_coverage_reports(): void {
if ( ! self::running_with_code_coverage() ) {
return;
Expand Down Expand Up @@ -430,14 +423,14 @@ private static function get_process_env_variables(): array {

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

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

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

$path_separator = Utils\is_windows() ? ';' : ':';
Expand Down Expand Up @@ -502,9 +495,9 @@ private static function get_process_env_variables(): array {
}

// Dump environment for debugging purposes, but before adding the GitHub token.
wp_cli_behat_env_debug( 'Environment:' );
self::debug( 'Environment:' );
foreach ( $env as $key => $value ) {
wp_cli_behat_env_debug( " [{$key}] => {$value}" );
self::debug( " [{$key}] => {$value}" );
}

$github_token = getenv( 'GITHUB_TOKEN' );
Expand Down Expand Up @@ -651,6 +644,8 @@ private static function cache_wp_files( $version = '' ): void {
* @BeforeSuite
*/
public static function prepare( BeforeSuiteScope $scope ): void {
self::bootstrap_feature_context();

// Test performance statistics - useful for detecting slow tests.
self::$log_run_times = getenv( 'WP_CLI_TEST_LOG_RUN_TIMES' );
if ( false !== self::$log_run_times ) {
Expand Down Expand Up @@ -687,6 +682,8 @@ public static function afterSuite( AfterSuiteScope $scope ): void {
if ( self::$log_run_times ) {
self::log_run_times_after_suite( $scope );
}

self::merge_coverage_reports();
}

/**
Expand Down Expand Up @@ -866,6 +863,74 @@ public function __construct() {
$this->set_cache_dir();
}

/**
* @param string $message
*/
protected static function debug( $message ): void { // phpcs:ignore Universal.Files.SeparateFunctionsFromOO.Mixed
if ( ! getenv( 'WP_CLI_TEST_DEBUG_BEHAT_ENV' ) ) {
return;
}

echo "{$message}\n";
}

/**
* Load required support files as needed before heading into the Behat context.
*/
protected static function bootstrap_feature_context(): void {
$vendor_folder = self::get_vendor_dir();
self::debug( "Vendor folder location: {$vendor_folder}" );

// Didn't manage to detect a valid vendor folder.
if ( empty( $vendor_folder ) ) {
return;
}

// We assume the vendor folder is located in the project root folder.
$project_folder = dirname( $vendor_folder );

$framework_folder = self::get_framework_dir();
self::debug( "Framework folder location: {$framework_folder}" );

// Load helper functionality that is needed for the tests.
require_once "{$framework_folder}/php/utils.php";
require_once "{$framework_folder}/php/WP_CLI/Process.php";
require_once "{$framework_folder}/php/WP_CLI/ProcessRun.php";

// Manually load Composer file includes by generating a config with require:
// statements for each file.
$project_composer = "{$project_folder}/composer.json";
if ( ! file_exists( $project_composer ) ) {
return;
}

$composer = json_decode( file_get_contents( $project_composer ) );
if ( empty( $composer->autoload->files ) ) {
return;
}

$contents = "require:\n";
foreach ( $composer->autoload->files as $file ) {
$contents .= " - {$project_folder}/{$file}\n";
}

$temp_folder = sys_get_temp_dir() . '/wp-cli-package-test';
if (
! is_dir( $temp_folder )
&& ! mkdir( $temp_folder )
&& ! is_dir( $temp_folder )
) {
return;
}

$project_config = "{$temp_folder}/config.yml";
file_put_contents( $project_config, $contents );
putenv( 'WP_CLI_CONFIG_PATH=' . $project_config );

self::debug( "Project config file location: {$project_config}" );
self::debug( "Project config:\n{$contents}" );
}

/**
* Replace standard {VARIABLE_NAME} variables and the special {INVOKE_WP_CLI_WITH_PHP_ARGS-args} and {WP_VERSION-version-latest} variables.
* Note that standard variable names can only contain uppercase letters, digits and underscores and cannot begin with a digit.
Expand Down Expand Up @@ -1766,74 +1831,3 @@ private static function log_proc_method_run_time( $key, $start_time ): void {
++self::$proc_method_run_times[ $key ][1];
}
}


/**
* @param string $message
*/
function wp_cli_behat_env_debug( $message ): void { // phpcs:ignore Universal.Files.SeparateFunctionsFromOO.Mixed
if ( ! getenv( 'WP_CLI_TEST_DEBUG_BEHAT_ENV' ) ) {
return;
}

echo "{$message}\n";
}

/**
* Load required support files as needed before heading into the Behat context.
*/
function wpcli_bootstrap_behat_feature_context(): void {
$vendor_folder = FeatureContext::get_vendor_dir();
wp_cli_behat_env_debug( "Vendor folder location: {$vendor_folder}" );

// Didn't manage to detect a valid vendor folder.
if ( empty( $vendor_folder ) ) {
return;
}

// We assume the vendor folder is located in the project root folder.
$project_folder = dirname( $vendor_folder );

$framework_folder = FeatureContext::get_framework_dir();
wp_cli_behat_env_debug( "Framework folder location: {$framework_folder}" );

// Load helper functionality that is needed for the tests.
require_once "{$framework_folder}/php/utils.php";
require_once "{$framework_folder}/php/WP_CLI/Process.php";
require_once "{$framework_folder}/php/WP_CLI/ProcessRun.php";

// Manually load Composer file includes by generating a config with require:
// statements for each file.
$project_composer = "{$project_folder}/composer.json";
if ( ! file_exists( $project_composer ) ) {
return;
}

$composer = json_decode( file_get_contents( $project_composer ) );
if ( empty( $composer->autoload->files ) ) {
return;
}

$contents = "require:\n";
foreach ( $composer->autoload->files as $file ) {
$contents .= " - {$project_folder}/{$file}\n";
}

$temp_folder = sys_get_temp_dir() . '/wp-cli-package-test';
if (
! is_dir( $temp_folder )
&& ! mkdir( $temp_folder )
&& ! is_dir( $temp_folder )
) {
return;
}

$project_config = "{$temp_folder}/config.yml";
file_put_contents( $project_config, $contents );
putenv( 'WP_CLI_CONFIG_PATH=' . $project_config );

wp_cli_behat_env_debug( "Project config file location: {$project_config}" );
wp_cli_behat_env_debug( "Project config:\n{$contents}" );
}

wpcli_bootstrap_behat_feature_context();