Feature: Context handling via --context global flag Scenario: CLI context can be selected, but is same as default Given a WP install When I run `wp eval 'var_export( is_admin() );'` Then the return code should be 0 And STDOUT should be: """ false """ When I run `wp --context=cli eval 'var_export( is_admin() );'` Then the return code should be 0 And STDOUT should be: """ false """ When I run `wp eval 'var_export( function_exists( "media_handle_upload" ) );'` Then the return code should be 0 And STDOUT should be: """ true """ When I run `wp --context=cli eval 'var_export( function_exists( "media_handle_upload" ) );'` Then the return code should be 0 And STDOUT should be: """ true """ When I run `wp eval 'add_action( "admin_init", static function () { WP_CLI::warning( "admin_init was triggered." ); } );'` Then the return code should be 0 And STDERR should not contain: """ admin_init was triggered. """ When I run `wp --context=cli eval 'add_action( "admin_init", static function () { WP_CLI::warning( "admin_init was triggered." ); } );'` Then the return code should be 0 And STDERR should not contain: """ admin_init was triggered. """ Scenario: Admin context can be selected Given a WP install When I run `wp --context=admin eval 'var_export( is_admin() );'` Then the return code should be 0 And STDOUT should be: """ true """ When I run `wp --context=admin eval 'var_export( function_exists( "media_handle_upload" ) );'` Then the return code should be 0 And STDOUT should be: """ true """ When I run `wp eval --context=admin 'add_action( "admin_init", static function () { WP_CLI::warning( "admin_init was triggered." ); } );'` Then the return code should be 0 And STDERR should not contain: """ admin_init was triggered. """ Scenario: Frontend context can be selected (and does nothing yet...) Given a WP install When I run `wp --context=frontend eval 'var_export( is_admin() );'` Then the return code should be 0 And STDOUT should be: """ false """ When I run `wp --context=frontend eval 'var_export( function_exists( "media_handle_upload" ) );'` Then the return code should be 0 And STDOUT should be: """ true """ When I run `wp --context=frontend eval 'add_action( "admin_init", static function () { WP_CLI::warning( "admin_init was triggered." ); } );'` Then the return code should be 0 And STDERR should not contain: """ admin_init was triggered. """ Scenario: Auto context can be selected and changes environment based on command Given a WP install And a context-logger.php file: """ context_manager->get_context(); WP_CLI::log( "Current context: {$context}" ); } ); """ When I run `wp --require=context-logger.php --context=auto post list` Then the return code should be 0 And STDOUT should contain: """ Current context: cli """ When I run `wp --require=context-logger.php --context=auto plugin list` Then the return code should be 0 And STDOUT should contain: """ Current context: admin """ Scenario: Unknown contexts throw an exception Given a WP install When I try `wp --context=nonsense post list` Then the return code should be 1 And STDOUT should be empty And STDERR should contain: """ Error: Unknown context 'nonsense' """ Scenario: Bundled contexts can be filtered Given a WP install And a custom-contexts.php file: """