Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
miya0001 committed Nov 7, 2016
2 parents b1a4824 + cb1d707 commit 0ab7632
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ $ composer install
Start a WordPress site.

```
$ npm run wp-install
$ npm run wp-start
$ npm run install-wp
$ npm run wp
```

Run the test!
Expand Down
44 changes: 44 additions & 0 deletions src/Context/RawWordPressContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ class RawWordPressContext extends RawMinkContext
{
protected $timeout = 60;

protected $parameters; // parameters from the `behat.yml`.

public function set_params( $params )
{
$this->parameters = $params;
}

public function get_params()
{
return $this->parameters;
}

/**
* Log in into the WordPress
*
Expand Down Expand Up @@ -130,4 +142,36 @@ protected function wait_the_element( $selector )

throw new \Exception( "No html element found for the selector ('$selector')" );
}

/**
* Get the WordPress version from meta.
*
* @return string WordPress versin number.
*/
protected function get_wp_version()
{
$this->getSession()->visit( $this->locatePath( '/' ) );
$page = $this->getSession()->getPage();
$meta = $page->find( 'css', "meta[name=generator]" );
if ( $meta ) {
$version = $meta->getAttribute( "content" );
if ( $version ) {
return str_replace( "WordPress ", "", $version );
}
}

throw new \Exception( "No version number found" );
}

protected function replace_variables( $str ) {
return preg_replace_callback( '/\{([A-Z_]+)\}/', array( $this, '_replace_var' ), $str );
}

private function _replace_var( $matches ) {
$cmd = $matches[0];
foreach ( array_slice( $matches, 1 ) as $key ) {
$cmd = str_replace( '{' . $key . '}', $this->variables[ $key ], $cmd );
}
return $cmd;
}
}
59 changes: 53 additions & 6 deletions src/Context/WordPressContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,49 @@
*/
class WordPressContext extends RawWordPressContext
{
private $parameters; // parameters from the `behat.yml`.

public function set_params( $params )
/**
* Save env to variable
* Example: Given save env $WP_VERSION as {WP_VERSION}
*
* @Given /^save env \$(?P<env>[A-Z_]+) as \{(?P<var>[A-Z_]+)\}$/
*/
public function save_env_as_var( $env, $var )
{
$this->parameters = $params;
$this->variables[ $var ] = getenv( $env );
}

public function get_params()
/**
* Check WordPress version
* Example: Given the WordPress version should be "4.6"
*
* @Given /^the WordPress version should be "(?P<version>[^"]*)"$/
*/
public function wordpress_version_should_be( $version )
{
return $this->parameters;
$version = $this->replace_variables( $version );

if ( "latest" === $version || "nightly" === $version ) {
$api = file_get_contents( "https://api.wordpress.org/core/version-check/1.7/" );
$versions = json_decode( $api );
$latest = $versions->offers[0]->current;
}

if ( "latest" === $version ) {
$version = $latest;
}

$the_version = $this->get_wp_version();
if ( 0 === strpos( $the_version, $version ) ) {
return true;
} elseif ( "nightly" === $version && version_compare( $the_version, $latest, ">=" ) ) {
return true;
} else {
throw new \Exception( sprintf(
"The WordPress version number is %s, but it should be %s",
$the_version,
$version
) );
}
}

/**
Expand All @@ -46,6 +79,9 @@ public function i_have_loggend_in()
*/
public function login_as_user_password( $username, $password )
{
$username = $this->replace_variables( $username );
$password = $this->replace_variables( $password );

$this->login( $username, $password );
}

Expand All @@ -58,6 +94,8 @@ public function login_as_user_password( $username, $password )
*/
public function login_as_the_role( $role )
{
$role = $this->replace_variables( $role );

$p = $this->get_params();

if ( empty( $p['roles'][ $role ] ) ) {
Expand All @@ -81,6 +119,8 @@ public function login_as_the_role( $role )
*/
public function hover_over_the_element( $selector )
{
$selector = $this->replace_variables( $selector );

$session = $this->getSession();
$element = $session->getPage()->find( 'css', $selector );

Expand All @@ -105,6 +145,7 @@ public function hover_over_the_element( $selector )
*/
public function wait_for_second( $second = 1 )
{
$second = $this->replace_variables( $second );
$this->getSession()->wait( $second * 1000 );
}

Expand All @@ -116,6 +157,7 @@ public function wait_for_second( $second = 1 )
*/
public function wait_the_element_be_loaded( $selector )
{
$selector = $this->replace_variables( $selector );
return $this->wait_the_element( $selector );
}

Expand All @@ -129,6 +171,9 @@ public function wait_the_element_be_loaded( $selector )
*/
public function set_window_size( $width, $height )
{
$width = $this->replace_variables( $width );
$height = $this->replace_variables( $height );

$this->getSession()->getDriver()->resizeWindow( $width, $height, 'current' );
}

Expand All @@ -153,6 +198,8 @@ public function i_logout()
*/
public function take_a_screenshot( $path )
{
$path = $this->replace_variables( $path );

$path = str_replace( "~", posix_getpwuid(posix_geteuid())['dir'], $path );
$image = $this->getSession()->getDriver()->getScreenshot();
$result = file_put_contents( $path, $image );
Expand Down

0 comments on commit 0ab7632

Please sign in to comment.