|
2 | 2 |
|
3 | 3 | namespace WP_CLI\Tests\Context; |
4 | 4 |
|
5 | | -use Behat\Behat\Context\SnippetAcceptingContext; |
| 5 | +use Behat\Behat\Context\Context; |
6 | 6 | use Behat\Behat\EventDispatcher\Event\OutlineTested; |
7 | 7 | use Behat\Behat\Hook\Scope\AfterScenarioScope; |
8 | 8 | use Behat\Behat\Hook\Scope\BeforeScenarioScope; |
|
28 | 28 |
|
29 | 29 | /** |
30 | 30 | * Features context. |
31 | | - * |
32 | | - * @phpstan-ignore class.implementsDeprecatedInterface |
33 | 31 | */ |
34 | | -class FeatureContext implements SnippetAcceptingContext { |
| 32 | +class FeatureContext implements Context { |
35 | 33 |
|
36 | 34 | use GivenStepDefinitions; |
37 | 35 | use ThenStepDefinitions; |
@@ -280,9 +278,6 @@ private static function running_with_code_coverage() { |
280 | 278 | return \in_array( $with_code_coverage, [ 'true', '1' ], true ); |
281 | 279 | } |
282 | 280 |
|
283 | | - /** |
284 | | - * @AfterSuite |
285 | | - */ |
286 | 281 | public static function merge_coverage_reports(): void { |
287 | 282 | if ( ! self::running_with_code_coverage() ) { |
288 | 283 | return; |
@@ -435,13 +430,13 @@ private static function get_process_env_variables(): array { |
435 | 430 | throw new RuntimeException( 'Could not find WP-CLI binary path.' ); |
436 | 431 | } |
437 | 432 |
|
438 | | - wp_cli_behat_env_debug( "WP-CLI binary path: {$bin_path}" ); |
| 433 | + self::debug( "WP-CLI binary path: {$bin_path}" ); |
439 | 434 |
|
440 | 435 | $bin = Utils\is_windows() ? 'wp.bat' : 'wp'; |
441 | 436 | $full_bin_path = $bin_path . DIRECTORY_SEPARATOR . $bin; |
442 | 437 |
|
443 | 438 | if ( ! is_executable( $full_bin_path ) ) { |
444 | | - wp_cli_behat_env_debug( "WARNING: File named '{$bin}' found in the provided/detected binary path is not executable." ); |
| 439 | + self::debug( "WARNING: File named '{$bin}' found in the provided/detected binary path is not executable." ); |
445 | 440 | } |
446 | 441 |
|
447 | 442 | $path_separator = Utils\is_windows() ? ';' : ':'; |
@@ -510,9 +505,9 @@ private static function get_process_env_variables(): array { |
510 | 505 | } |
511 | 506 |
|
512 | 507 | // Dump environment for debugging purposes, but before adding the GitHub token. |
513 | | - wp_cli_behat_env_debug( 'Environment:' ); |
| 508 | + self::debug( 'Environment:' ); |
514 | 509 | foreach ( $env as $key => $value ) { |
515 | | - wp_cli_behat_env_debug( " [{$key}] => {$value}" ); |
| 510 | + self::debug( " [{$key}] => {$value}" ); |
516 | 511 | } |
517 | 512 |
|
518 | 513 | $github_token = getenv( 'GITHUB_TOKEN' ); |
@@ -663,6 +658,8 @@ private static function cache_wp_files( $version = '' ): void { |
663 | 658 | * @BeforeSuite |
664 | 659 | */ |
665 | 660 | public static function prepare( BeforeSuiteScope $scope ): void { |
| 661 | + self::bootstrap_feature_context(); |
| 662 | + |
666 | 663 | // Test performance statistics - useful for detecting slow tests. |
667 | 664 | self::$log_run_times = getenv( 'WP_CLI_TEST_LOG_RUN_TIMES' ); |
668 | 665 | if ( false !== self::$log_run_times ) { |
@@ -705,6 +702,8 @@ public static function afterSuite( AfterSuiteScope $scope ): void { |
705 | 702 | if ( self::$log_run_times ) { |
706 | 703 | self::log_run_times_after_suite( $scope ); |
707 | 704 | } |
| 705 | + |
| 706 | + self::merge_coverage_reports(); |
708 | 707 | } |
709 | 708 |
|
710 | 709 | /** |
@@ -893,6 +892,74 @@ public function __construct() { |
893 | 892 | $this->set_cache_dir(); |
894 | 893 | } |
895 | 894 |
|
| 895 | + /** |
| 896 | + * @param string $message |
| 897 | + */ |
| 898 | + protected static function debug( $message ): void { // phpcs:ignore Universal.Files.SeparateFunctionsFromOO.Mixed |
| 899 | + if ( ! getenv( 'WP_CLI_TEST_DEBUG_BEHAT_ENV' ) ) { |
| 900 | + return; |
| 901 | + } |
| 902 | + |
| 903 | + echo "{$message}\n"; |
| 904 | + } |
| 905 | + |
| 906 | + /** |
| 907 | + * Load required support files as needed before heading into the Behat context. |
| 908 | + */ |
| 909 | + protected static function bootstrap_feature_context(): void { |
| 910 | + $vendor_folder = self::get_vendor_dir(); |
| 911 | + self::debug( "Vendor folder location: {$vendor_folder}" ); |
| 912 | + |
| 913 | + // Didn't manage to detect a valid vendor folder. |
| 914 | + if ( empty( $vendor_folder ) ) { |
| 915 | + return; |
| 916 | + } |
| 917 | + |
| 918 | + // We assume the vendor folder is located in the project root folder. |
| 919 | + $project_folder = dirname( $vendor_folder ); |
| 920 | + |
| 921 | + $framework_folder = self::get_framework_dir(); |
| 922 | + self::debug( "Framework folder location: {$framework_folder}" ); |
| 923 | + |
| 924 | + // Load helper functionality that is needed for the tests. |
| 925 | + require_once "{$framework_folder}/php/utils.php"; |
| 926 | + require_once "{$framework_folder}/php/WP_CLI/Process.php"; |
| 927 | + require_once "{$framework_folder}/php/WP_CLI/ProcessRun.php"; |
| 928 | + |
| 929 | + // Manually load Composer file includes by generating a config with require: |
| 930 | + // statements for each file. |
| 931 | + $project_composer = "{$project_folder}/composer.json"; |
| 932 | + if ( ! file_exists( $project_composer ) ) { |
| 933 | + return; |
| 934 | + } |
| 935 | + |
| 936 | + $composer = json_decode( (string) file_get_contents( $project_composer ) ); |
| 937 | + if ( empty( $composer->autoload->files ) ) { |
| 938 | + return; |
| 939 | + } |
| 940 | + |
| 941 | + $contents = "require:\n"; |
| 942 | + foreach ( $composer->autoload->files as $file ) { |
| 943 | + $contents .= " - {$project_folder}/{$file}\n"; |
| 944 | + } |
| 945 | + |
| 946 | + $temp_folder = sys_get_temp_dir() . '/wp-cli-package-test'; |
| 947 | + if ( |
| 948 | + ! is_dir( $temp_folder ) |
| 949 | + && ! mkdir( $temp_folder ) |
| 950 | + && ! is_dir( $temp_folder ) |
| 951 | + ) { |
| 952 | + return; |
| 953 | + } |
| 954 | + |
| 955 | + $project_config = "{$temp_folder}/config.yml"; |
| 956 | + file_put_contents( $project_config, $contents ); |
| 957 | + putenv( 'WP_CLI_CONFIG_PATH=' . $project_config ); |
| 958 | + |
| 959 | + self::debug( "Project config file location: {$project_config}" ); |
| 960 | + self::debug( "Project config:\n{$contents}" ); |
| 961 | + } |
| 962 | + |
896 | 963 | /** |
897 | 964 | * Replace standard {VARIABLE_NAME} variables and the special {INVOKE_WP_CLI_WITH_PHP_ARGS-args} and {WP_VERSION-version-latest} variables. |
898 | 965 | * Note that standard variable names can only contain uppercase letters, digits and underscores and cannot begin with a digit. |
@@ -1853,73 +1920,3 @@ private static function log_proc_method_run_time( $key, $start_time ): void { |
1853 | 1920 | ++self::$proc_method_run_times[ $key ][1]; |
1854 | 1921 | } |
1855 | 1922 | } |
1856 | | - |
1857 | | - |
1858 | | -/** |
1859 | | - * @param string $message |
1860 | | - */ |
1861 | | -function wp_cli_behat_env_debug( $message ): void { // phpcs:ignore Universal.Files.SeparateFunctionsFromOO.Mixed |
1862 | | - if ( ! getenv( 'WP_CLI_TEST_DEBUG_BEHAT_ENV' ) ) { |
1863 | | - return; |
1864 | | - } |
1865 | | - |
1866 | | - echo "{$message}\n"; |
1867 | | -} |
1868 | | - |
1869 | | -/** |
1870 | | - * Load required support files as needed before heading into the Behat context. |
1871 | | - */ |
1872 | | -function wpcli_bootstrap_behat_feature_context(): void { |
1873 | | - $vendor_folder = FeatureContext::get_vendor_dir(); |
1874 | | - wp_cli_behat_env_debug( "Vendor folder location: {$vendor_folder}" ); |
1875 | | - |
1876 | | - // Didn't manage to detect a valid vendor folder. |
1877 | | - if ( empty( $vendor_folder ) ) { |
1878 | | - return; |
1879 | | - } |
1880 | | - |
1881 | | - // We assume the vendor folder is located in the project root folder. |
1882 | | - $project_folder = dirname( $vendor_folder ); |
1883 | | - |
1884 | | - $framework_folder = FeatureContext::get_framework_dir(); |
1885 | | - wp_cli_behat_env_debug( "Framework folder location: {$framework_folder}" ); |
1886 | | - |
1887 | | - // Load helper functionality that is needed for the tests. |
1888 | | - require_once "{$framework_folder}/php/utils.php"; |
1889 | | - require_once "{$framework_folder}/php/WP_CLI/ProcessRun.php"; |
1890 | | - |
1891 | | - // Manually load Composer file includes by generating a config with require: |
1892 | | - // statements for each file. |
1893 | | - $project_composer = "{$project_folder}/composer.json"; |
1894 | | - if ( ! file_exists( $project_composer ) ) { |
1895 | | - return; |
1896 | | - } |
1897 | | - |
1898 | | - $composer = json_decode( (string) file_get_contents( $project_composer ) ); |
1899 | | - if ( empty( $composer->autoload->files ) ) { |
1900 | | - return; |
1901 | | - } |
1902 | | - |
1903 | | - $contents = "require:\n"; |
1904 | | - foreach ( $composer->autoload->files as $file ) { |
1905 | | - $contents .= " - {$project_folder}/{$file}\n"; |
1906 | | - } |
1907 | | - |
1908 | | - $temp_folder = sys_get_temp_dir() . '/wp-cli-package-test'; |
1909 | | - if ( |
1910 | | - ! is_dir( $temp_folder ) |
1911 | | - && ! mkdir( $temp_folder ) |
1912 | | - && ! is_dir( $temp_folder ) |
1913 | | - ) { |
1914 | | - return; |
1915 | | - } |
1916 | | - |
1917 | | - $project_config = "{$temp_folder}/config.yml"; |
1918 | | - file_put_contents( $project_config, $contents ); |
1919 | | - putenv( 'WP_CLI_CONFIG_PATH=' . $project_config ); |
1920 | | - |
1921 | | - wp_cli_behat_env_debug( "Project config file location: {$project_config}" ); |
1922 | | - wp_cli_behat_env_debug( "Project config:\n{$contents}" ); |
1923 | | -} |
1924 | | - |
1925 | | -wpcli_bootstrap_behat_feature_context(); |
0 commit comments