|
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; |
|
13 | 13 | use Behat\Behat\Hook\Scope\AfterFeatureScope; |
14 | 14 | use Behat\Behat\Hook\Scope\BeforeFeatureScope; |
15 | 15 | use Behat\Behat\Hook\Scope\BeforeStepScope; |
16 | | -use Behat\Testwork\Hook\Scope\HookScope; |
17 | 16 | use SebastianBergmann\CodeCoverage\Report\Clover; |
18 | 17 | use SebastianBergmann\CodeCoverage\Driver\Selector; |
19 | 18 | use SebastianBergmann\CodeCoverage\Driver\Xdebug; |
20 | 19 | use SebastianBergmann\CodeCoverage\Filter; |
21 | 20 | use SebastianBergmann\CodeCoverage\CodeCoverage; |
22 | 21 | use SebastianBergmann\Environment\Runtime; |
23 | 22 | use RuntimeException; |
24 | | -use WP_CLI; |
25 | 23 | use DirectoryIterator; |
26 | 24 | use WP_CLI\Process; |
27 | 25 | use WP_CLI\ProcessRun; |
|
30 | 28 |
|
31 | 29 | /** |
32 | 30 | * Features context. |
33 | | - * |
34 | | - * @phpstan-ignore class.implementsDeprecatedInterface |
35 | 31 | */ |
36 | | -class FeatureContext implements SnippetAcceptingContext { |
| 32 | +class FeatureContext implements Context { |
37 | 33 |
|
38 | 34 | use GivenStepDefinitions; |
39 | 35 | use ThenStepDefinitions; |
@@ -282,9 +278,6 @@ private static function running_with_code_coverage() { |
282 | 278 | return \in_array( $with_code_coverage, [ 'true', '1' ], true ); |
283 | 279 | } |
284 | 280 |
|
285 | | - /** |
286 | | - * @AfterSuite |
287 | | - */ |
288 | 281 | public static function merge_coverage_reports(): void { |
289 | 282 | if ( ! self::running_with_code_coverage() ) { |
290 | 283 | return; |
@@ -430,14 +423,14 @@ private static function get_process_env_variables(): array { |
430 | 423 |
|
431 | 424 | // Ensure we're using the expected `wp` binary. |
432 | 425 | $bin_path = self::get_bin_path(); |
433 | | - wp_cli_behat_env_debug( "WP-CLI binary path: {$bin_path}" ); |
| 426 | + self::debug( "WP-CLI binary path: {$bin_path}" ); |
434 | 427 |
|
435 | 428 | if ( ! file_exists( "{$bin_path}/wp" ) ) { |
436 | | - wp_cli_behat_env_debug( "WARNING: No file named 'wp' found in the provided/detected binary path." ); |
| 429 | + self::debug( "WARNING: No file named 'wp' found in the provided/detected binary path." ); |
437 | 430 | } |
438 | 431 |
|
439 | 432 | 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." ); |
| 433 | + self::debug( "WARNING: File named 'wp' found in the provided/detected binary path is not executable." ); |
441 | 434 | } |
442 | 435 |
|
443 | 436 | $path_separator = Utils\is_windows() ? ';' : ':'; |
@@ -502,9 +495,9 @@ private static function get_process_env_variables(): array { |
502 | 495 | } |
503 | 496 |
|
504 | 497 | // Dump environment for debugging purposes, but before adding the GitHub token. |
505 | | - wp_cli_behat_env_debug( 'Environment:' ); |
| 498 | + self::debug( 'Environment:' ); |
506 | 499 | foreach ( $env as $key => $value ) { |
507 | | - wp_cli_behat_env_debug( " [{$key}] => {$value}" ); |
| 500 | + self::debug( " [{$key}] => {$value}" ); |
508 | 501 | } |
509 | 502 |
|
510 | 503 | $github_token = getenv( 'GITHUB_TOKEN' ); |
@@ -651,6 +644,8 @@ private static function cache_wp_files( $version = '' ): void { |
651 | 644 | * @BeforeSuite |
652 | 645 | */ |
653 | 646 | public static function prepare( BeforeSuiteScope $scope ): void { |
| 647 | + self::bootstrap_feature_context(); |
| 648 | + |
654 | 649 | // Test performance statistics - useful for detecting slow tests. |
655 | 650 | self::$log_run_times = getenv( 'WP_CLI_TEST_LOG_RUN_TIMES' ); |
656 | 651 | if ( false !== self::$log_run_times ) { |
@@ -687,6 +682,8 @@ public static function afterSuite( AfterSuiteScope $scope ): void { |
687 | 682 | if ( self::$log_run_times ) { |
688 | 683 | self::log_run_times_after_suite( $scope ); |
689 | 684 | } |
| 685 | + |
| 686 | + self::merge_coverage_reports(); |
690 | 687 | } |
691 | 688 |
|
692 | 689 | /** |
@@ -866,6 +863,74 @@ public function __construct() { |
866 | 863 | $this->set_cache_dir(); |
867 | 864 | } |
868 | 865 |
|
| 866 | + /** |
| 867 | + * @param string $message |
| 868 | + */ |
| 869 | + protected static function debug( $message ): void { // phpcs:ignore Universal.Files.SeparateFunctionsFromOO.Mixed |
| 870 | + if ( ! getenv( 'WP_CLI_TEST_DEBUG_BEHAT_ENV' ) ) { |
| 871 | + return; |
| 872 | + } |
| 873 | + |
| 874 | + echo "{$message}\n"; |
| 875 | + } |
| 876 | + |
| 877 | + /** |
| 878 | + * Load required support files as needed before heading into the Behat context. |
| 879 | + */ |
| 880 | + protected static function bootstrap_feature_context(): void { |
| 881 | + $vendor_folder = self::get_vendor_dir(); |
| 882 | + self::debug( "Vendor folder location: {$vendor_folder}" ); |
| 883 | + |
| 884 | + // Didn't manage to detect a valid vendor folder. |
| 885 | + if ( empty( $vendor_folder ) ) { |
| 886 | + return; |
| 887 | + } |
| 888 | + |
| 889 | + // We assume the vendor folder is located in the project root folder. |
| 890 | + $project_folder = dirname( $vendor_folder ); |
| 891 | + |
| 892 | + $framework_folder = self::get_framework_dir(); |
| 893 | + self::debug( "Framework folder location: {$framework_folder}" ); |
| 894 | + |
| 895 | + // Load helper functionality that is needed for the tests. |
| 896 | + require_once "{$framework_folder}/php/utils.php"; |
| 897 | + require_once "{$framework_folder}/php/WP_CLI/Process.php"; |
| 898 | + require_once "{$framework_folder}/php/WP_CLI/ProcessRun.php"; |
| 899 | + |
| 900 | + // Manually load Composer file includes by generating a config with require: |
| 901 | + // statements for each file. |
| 902 | + $project_composer = "{$project_folder}/composer.json"; |
| 903 | + if ( ! file_exists( $project_composer ) ) { |
| 904 | + return; |
| 905 | + } |
| 906 | + |
| 907 | + $composer = json_decode( file_get_contents( $project_composer ) ); |
| 908 | + if ( empty( $composer->autoload->files ) ) { |
| 909 | + return; |
| 910 | + } |
| 911 | + |
| 912 | + $contents = "require:\n"; |
| 913 | + foreach ( $composer->autoload->files as $file ) { |
| 914 | + $contents .= " - {$project_folder}/{$file}\n"; |
| 915 | + } |
| 916 | + |
| 917 | + $temp_folder = sys_get_temp_dir() . '/wp-cli-package-test'; |
| 918 | + if ( |
| 919 | + ! is_dir( $temp_folder ) |
| 920 | + && ! mkdir( $temp_folder ) |
| 921 | + && ! is_dir( $temp_folder ) |
| 922 | + ) { |
| 923 | + return; |
| 924 | + } |
| 925 | + |
| 926 | + $project_config = "{$temp_folder}/config.yml"; |
| 927 | + file_put_contents( $project_config, $contents ); |
| 928 | + putenv( 'WP_CLI_CONFIG_PATH=' . $project_config ); |
| 929 | + |
| 930 | + self::debug( "Project config file location: {$project_config}" ); |
| 931 | + self::debug( "Project config:\n{$contents}" ); |
| 932 | + } |
| 933 | + |
869 | 934 | /** |
870 | 935 | * Replace standard {VARIABLE_NAME} variables and the special {INVOKE_WP_CLI_WITH_PHP_ARGS-args} and {WP_VERSION-version-latest} variables. |
871 | 936 | * Note that standard variable names can only contain uppercase letters, digits and underscores and cannot begin with a digit. |
@@ -1766,74 +1831,3 @@ private static function log_proc_method_run_time( $key, $start_time ): void { |
1766 | 1831 | ++self::$proc_method_run_times[ $key ][1]; |
1767 | 1832 | } |
1768 | 1833 | } |
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