Skip to content

Commit

Permalink
add context that checks the resonse headers
Browse files Browse the repository at this point in the history
  • Loading branch information
miya0001 committed Dec 23, 2016
1 parent 7d13393 commit eb1a79e
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 30 deletions.
18 changes: 18 additions & 0 deletions features/http-response.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Feature: HTTP response

Scenario: Check http status code

When I am on "/"
Then the HTTP status should be 200

When I am on "/the-page-not-found"
Then the HTTP status should be 404

Scenario: Check http response headers

When I am on "/"
Then the HTTP headers should be:
| header | value |
| Content-Type | text/html; charset=UTF-8 |
| Connection | close |
| Host | 127.0.0.1:8080 |
10 changes: 0 additions & 10 deletions features/http-status.feature

This file was deleted.

14 changes: 7 additions & 7 deletions features/plugins.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ Feature: Get statuses of plugins
Scenario: Get plugins

When I login as the "administrator" role
Then plugins should be:
Then the plugins should be:
| slug | status |
| akismet | inactive |
| hello-dolly | inactive |
| wordpress-importer | active |

Scenario Outline: Plugins should be installed.
Scenario Outline: Plugin should be installed.

When I login as the "administrator" role
Then the "<slug>" plugins should be installed
Then the "<slug>" plugin should be installed

Examples:
| slug |
Expand All @@ -23,21 +23,21 @@ Feature: Get statuses of plugins
Scenario Outline: Plugins should be activated.

When I login as the "administrator" role
Then the "<slug>" plugins should be activated
Then the "<slug>" plugin should be activated

Examples:
| slug |
| wordpress-importer |

Scenario: Plugins should not be installed.
Scenario: Plugin should not be installed.

When I login as the "administrator" role
Then the "my-plugin" plugins should not be installed
Then the "my-plugin" plugin should not be installed

Scenario Outline: Plugins should not be activated.

When I login as the "administrator" role
Then the "<slug>" plugins should not be activated
Then the "<slug>" plugin should not be activated

Examples:
| slug |
Expand Down
11 changes: 11 additions & 0 deletions src/Context/RawWordPressContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ protected function get_http_status()
return intval( $session->getStatusCode() );
}

/**
* Get http response headers from the current page.
*
* @return array HTTP response headers.
*/
protected function get_http_headers()
{
$session = $this->get_goutte_session();
return $session->getResponseHeaders();
}

/**
* Log in into the WordPress
*
Expand Down
60 changes: 47 additions & 13 deletions src/Context/WordPressContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class WordPressContext extends RawWordPressContext
{
/**
* Save env to variable
* 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_]+)\}$/
Expand All @@ -39,22 +39,56 @@ public function the_http_status_should_be( $expect )
) );
}

/**
* Check HTTP response headers.
* Example:
* Then the HTTP headers should be:
* | header | value |
* | Content-Type | text/html; charset=UTF-8 |
* | Connection | close |
* | Host | 127.0.0.1:8080 |
*
* @then /^the HTTP headers should be:$/
*/
public function the_http_headers_should_be( TableNode $table )
{
$headers = $this->get_http_headers();

foreach ( $table->getHash() as $row ) {
if ( ! empty( $headers[ $row['header'] ] ) ) {
$value = $headers[ $row['header'] ][0];
$this->assertSame( $row['value'], $value, sprintf(
'The value of "%1$s" header is "%1$s", but it should be "%3$s".',
$row['header'],
$value,
$row['value']
) );
} else {
throw new \Exception( sprintf(
'The value of "%1$s" header is empty, but it should be "%2$s".',
$row['header'],
$row['value']
) );
}
}
}

/**
* Check status of plugins
* Example:
* Then there are plugins:
* Then the plugins should be:
* | slug | status |
* | akismet | inactive |
* | hello-dolly | inactive |
* | wordpress-importer | active |
*
* @then /^plugins should be:$/
* @then /^the plugins should be:$/
*/
public function there_are_plugins( TableNode $table )
public function the_plugins_should_be( TableNode $table )
{
$plugins = $this->get_plugins();

foreach ($table->getHash() as $row) {
foreach ( $table->getHash() as $row ) {
if ( ! empty( $plugins[ $row['slug'] ] ) ) {
$status = $plugins[ $row['slug'] ]['status'];
$this->assertSame( $row['status'], $status, sprintf(
Expand All @@ -72,9 +106,9 @@ public function there_are_plugins( TableNode $table )
}

/**
* Check status of plugins
* Check status of plugins.
*
* @then /^the "(?P<slug>[^"]*)" plugins should be (?P<expect>(installed|activated))$/
* @then /^the "(?P<slug>[^"]*)" plugin should be (?P<expect>(installed|activated))$/
*/
public function the_plugin_should_be( $slug, $expect )
{
Expand All @@ -97,9 +131,9 @@ public function the_plugin_should_be( $slug, $expect )
}

/**
* Check status of plugins
* Check status of plugins.
*
* @then /^the "(?P<slug>[^"]*)" plugins should not be (?P<expect>(installed|activated))$/
* @then /^the "(?P<slug>[^"]*)" plugin should not be (?P<expect>(installed|activated))$/
*/
public function the_plugin_should_not_be( $slug, $expect )
{
Expand All @@ -122,7 +156,7 @@ public function the_plugin_should_not_be( $slug, $expect )
}

/**
* Check the theme is activated
* Check the theme is activated.
* Example: Given the "twentysixteen" theme should be activated
*
* @Then /^the "(?P<theme>[^"]*)" theme should be activated$/
Expand All @@ -140,7 +174,7 @@ public function theme_should_be_activated( $theme )
}

/**
* Check WordPress version
* Check WordPress version.
* Example: Given the WordPress version should be "4.6"
*
* @Given /^the WordPress version should be "(?P<version>[^"]*)"$/
Expand Down Expand Up @@ -174,7 +208,7 @@ public function wordpress_version_should_be( $version )
}

/**
* Return exception if user is not logged in
* Return exception if user is not logged-in.
* Example: Then I should be logged in
*
* @Then I should be logged in
Expand All @@ -187,7 +221,7 @@ public function i_should_be_logged_in()
}

/**
* Return exception if user is logged in
* Return exception if user is logged-in.
* Example: I should not be logged in
*
* @Then I should not be logged in
Expand Down

0 comments on commit eb1a79e

Please sign in to comment.