WPCLI
WPCLI module
Use WP-CLI to interact with the WordPress installation under test and issue commands.
This module is used in the context of end-to-end testing, together with, or as a replacement for the WPDb module to manipulate the database and the WPFilesystem module to manipulate the site file structure.
This module should be with Cest and Cept test cases.
Configuration
path- required; the path to the WordPress installation under test. This can be a relative path to the codeception root directory, or an absolute path to the WordPress installation directory. The WordPress installation directory is the directory that contains thewp-load.phpfile.url- the URL of the WordPress installation under test. Equivalent to the--urloption of thewpcommand.user- the user to use to run thewpcommand. Equivalent to the--useroption of thewpcommand.skip-plugins- a boolean value to indicate if thewpcommand should skip loading plugins. Equivalent to the--skip-pluginsoption of thewpcommand.skip-themes- a boolean value to indicate if thewpcommand should skip loading themes. Equivalent to the--skip-themesoption of thewpcommand.skip-packages- a boolean value to indicate if thewpcommand should skip loading packages. Equivalent to the--skip-packagesoption of thewpcommand.require- a list of PHP files to require before running thewpcommand. Equivalent to the--requireoption of thewpcommand.exec- PHP code to execute before running thewpcommand. Equivalent to the--execoption of thewpcommand.context- the context to use when running thewpcommand. Equivalent to the--contextoption of thewpcommand.color- a boolean value to indicate if thewpcommand should output in color. Equivalent to the--coloroption of thewpcommand.no-color- a boolean value to indicate if thewpcommand should not output in color. Equivalent to the--no-coloroption of thewpcommand.debug- a boolean value to indicate if thewpcommand should output debug information. Equivalent to the--debugoption of thewpcommand.quiet- a boolean value to indicate if thewpcommand should suppress informational messages. Equivalent to the--quietoption of thewpcommand.throw- a boolean value to indicate if thewpcommand should throw an exception if the command fails.timeout- the timeout to use when running thewpcommand. When the timeout is reached the command will be terminated as a failure.cache-dir- the directory to use to cache the files WPCLI might downloads. Equivalent to setting theWP_CLI_CACHE_DIRenvironment variable.config-path- the path to thewp-cli.ymlfile to use. Equivalent to setting theWP_CLI_CONFIG_PATHenvironment variable.custom-shell- the shell to use to run thewpcommand. Equivalent to setting theWP_CLI_SHELLenvironment variable.packages-dir- the directory to use to store the packages downloaded by thewp packagecommand. Equivalent to setting theWP_CLI_PACKAGES_DIRenvironment variable.bin- the path to a custom WP-CLI binary.allow-root- a boolean value to indicate if thewpcommand should be run with the--allow-rootflag. Equivalent to the--allow-rootoption of thewpcommand. This is useful when running wp-cli commands as the root user.
The following is an example of the module configuration to run WPCLI commands on the /var/wordpress directory:
The following configuration uses dynamic configuration parameters to set the module configuration:
The following configuration uses a custom WP-CLI binary:
Methods
The module provides the following methods:
changeWpcliPath
Signature: changeWpcliPath(string $path) : void
Changes the path to the WordPress installation that WPCLI should use.
This is the equivalent of the --path option.
<?php
// Operate on the installation specified in the `path` config parameter.
$I->cli(['core','version']);
// Change to another installation and run a command there.
$I->changeWpcliPath('var/wordpress-installation-two');
$I->cli(['core','version']);
cli
Signature: cli([array|string $command], [?array $env], [mixed $input]) : int
Executes a wp-cli command targeting the test WordPress installation.
<?php
// Activate a plugin via wp-cli in the test WordPress site.
$I->cli(['plugin', 'activate', 'my-plugin']);
// Change a user password.
$I->cli(['user', 'update', 'luca', '--user_pass=newpassword']);
cliToArray
Signature: cliToArray(array $command, [?callable $splitCallback], [?array $env], [mixed $input]) : array
Returns the output of a wp-cli command as an array optionally allowing a callback to process the output.
<?php
// Return a list of inactive themes, like ['twentyfourteen', 'twentyfifteen'].
$inactiveThemes = $I->cliToArray(['theme', 'list', '--status=inactive', '--field=name']);
// Get the list of installed plugins and only keep the ones starting with "foo".
$fooPlugins = $I->cliToArray(['plugin', 'list', '--field=name'], function($output){
return array_filter(explode(PHP_EOL, $output), function($name){
return strpos(trim($name), 'foo') === 0;
});
});
cliToString
Signature: cliToString(array $command, [?array $env], [mixed $input]) : string
Returns the output of a wp-cli command as a string.
<?php
// Return the current site administrator email, using string command format.
$adminEmail = $I->cliToString('option get admin_email');
// Get the list of active plugins in JSON format, two ways.
$activePlugins = $I->cliToString(['plugin', 'list','--status=active', '--format=json']);
$activePlugins = $I->cliToString(['option', 'get', 'active_plugins' ,'--format=json']);
dontSeeInShellOutput
Signature: dontSeeInShellOutput(string $text) : void
Checks that output from last command doesn't contain text.
<?php
// Return the current site administrator email, using string command format.
$I->cli('plugin list --status=active');
$I->dontSeeInShellOutput('my-inactive/plugin.php');
dontSeeShellOutputMatches
Signature: dontSeeShellOutputMatches(string $regex) : void
Checks that output from the last command doesn't match a given regular expression.
<?php
// Return the current site administrator email, using string command format.
$I->cli('option get siteurl');
$I->dontSeeShellOutputMatches('/^http/');
grabLastCliProcess
Signature: grabLastCliProcess() : lucatume\WPBrowser\WordPress\CliProcess
grabLastShellErrorOutput
Signature: grabLastShellErrorOutput() : string
Returns the shell error output of the last command.
grabLastShellOutput
Signature: grabLastShellOutput() : string
Returns the shell output of the last command.
seeInShellOutput
Signature: seeInShellOutput(string $text) : void
Checks that output from last command contains text.
<?php
// Return the current site administrator email, using string command format.
$I->cli('option get admin_email');
$I->seeInShellOutput('admin@example.org');
seeResultCodeIs
Signature: seeResultCodeIs(int $code) : void
Checks the result code from the last command.
<?php
// Return the current site administrator email, using string command format.
$I->cli('option get admin_email');
$I->seeResultCodeIs(0);
seeResultCodeIsNot
Signature: seeResultCodeIsNot(int $code) : void
Checks the result code from the last command.
<?php
// Return the current site administrator email, using string command format.
$I->cli('invalid command');
$I->seeResultCodeIsNot(0);
seeShellOutputMatches
Signature: seeShellOutputMatches(string $regex) : void
Checks that output from the last command matches a given regular expression.
<?php
// Return the current site administrator email, using string command format.
$I->cli('option get admin_email');
$I->seeShellOutputMatches('/^\S+@\S+$/');
Explore the WP-CLI documentation for more information on the available commands.