Created
November 2, 2020 23:12
-
-
Save TimothyBJacobs/f2ed982a92091a79401d72d8273ee086 to your computer and use it in GitHub Desktop.
WP-Browser/Codeception module to have WordPress version dependent tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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