Skip to content

Instantly share code, notes, and snippets.

@TimothyBJacobs
Last active November 2, 2020 21:58
Show Gist options
  • Save TimothyBJacobs/17b9037d97883fee0a575eb4968deb5d to your computer and use it in GitHub Desktop.
Save TimothyBJacobs/17b9037d97883fee0a575eb4968deb5d to your computer and use it in GitHub Desktop.
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