WP-Browser/Codeception module to have WordPress version dependent tests
<?php | |
namespace Helper; | |
class WpVersion extends \Codeception\Module { | |
use \tad\WPBrowser\Traits\WithWpCli; | |
private $version; | |
public function _initialize() { | |
parent::_initialize(); | |
$this->setUpWpCli( $this->config['path'] ); | |
} | |
public function _beforeSuite( $settings = [] ) { | |
if ( isset( $GLOBALS['wp_version'] ) ) { | |
$this->version = $GLOBALS['wp_version']; | |
} else { | |
$this->version = trim( $this->runCommand( 'core', 'version' )->getOutput() ); | |
} | |
} | |
public function _before( \Codeception\TestInterface $test ) { | |
if ( ! $requires = $test->getMetadata()->getParam( 'requires' ) ) { | |
return; | |
} | |
foreach ( $requires as $require ) { | |
if ( ! preg_match( '/^WP\s+(?P<operator>[<>=!]{0,2})\s*(?P<version>[\d\.-]+)$/', $require, $matches ) ) { | |
throw new \PHPUnit_Framework_RiskyTestError( 'Invalid @requires annotation.' ); | |
} | |
$operator = $matches['operator']; | |
$version = $matches['version']; | |
list( $wp_version ) = explode( '-', $this->version ); | |
if ( ! version_compare( $wp_version, $version, $operator ) ) { | |
throw new \PHPUnit_Framework_SkippedTestError( sprintf( 'WP %s %s is required.', $operator, $version ) ); | |
} | |
} | |
} | |
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