Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WP-Browser/Codeception module to install a requested WordPress version
<?php
namespace Helper;
class WpVersion extends \Codeception\Module {
use \tad\WPBrowser\Traits\WithWpCli;
public function _initialize() {
parent::_initialize();
$this->setUpWpCli( $this->config['path'] );
}
public function _beforeSuite( $settings = [] ) {
if ( ! empty( $this->config['version'] ) ) {
$this->install( trim( $this->config['version'], " \t\n\r\0\x0B'\"" ) );
}
}
protected function install( $version ) {
$this->debugSection( 'WpVersion', sprintf( 'Installing WordPress %s', $version ) );
try {
$process = $this->runCommand( 'core', 'update', '--version=' . $version );
if ( ! $process->isSuccessful() ) {
throw new \Codeception\Exception\ModuleException( $this, sprintf(
'Failed to install WordPress %s: %s',
$version,
$process->getErrorOutput() ?: $process->getOutput()
) );
}
echo sprintf( 'Installed WordPress: %s', $version ) . PHP_EOL;
} catch ( \Exception $e ) {
throw new \Codeception\Exception\ModuleException( $this, $e->getMessage() );
}
}
protected function runCommand( ...$user_command ) {
$options = [];
if ( ! empty( $this->config['allow-root'] ) ) {
$options[] = '--allow-root';
}
$command = array_merge( $options, $user_command );
$this->debugSection( 'WpVersion', $command );
$process = $this->executeWpCliCommand( $command, null );
$out = $process->getOutput() ?: $process->getErrorOutput();
$this->debugSection( 'WpVersion', $out );
return $process;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment