Skip to content

Instantly share code, notes, and snippets.

@akovalyov
Created July 23, 2014 23:07
Show Gist options
  • Save akovalyov/5281620607952784f6dd to your computer and use it in GitHub Desktop.
Save akovalyov/5281620607952784f6dd to your computer and use it in GitHub Desktop.
Behat redirects
<?php
namespace Context;
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Knp\FriendlyContexts\Context\RawPageContext;
class FeatureContext extends RawPageContext implements Context, SnippetAcceptingContext
{
/**
* @Then I should be redirected to :page page
*/
public function iAmRedirected($page, $arguments = null)
{
$headers = $this->getSession()->getResponseHeaders();
if (empty($headers['Location']) && empty($headers['location'])) {
throw new \RuntimeException('The response should contain a "Location" header');
}
$header = empty($headers['Location']) ? $headers['location'] : $headers['Location'];
if (is_array($header)) {
$header = current($header);
self::assertEquals($header, $this->locatePath($this->getPagePath($page, $arguments)), 'The "Location" header points to the correct URI');
}
$client = $this->getClient();
$client->followRedirects(true);
$client->followRedirect();
}
public static function assertEquals($actual, $expected, $exceptionMessage = null)
{
if ($expected !== $actual) {
throw new \RuntimeException($exceptionMessage? $exceptionMessage : "$expected is not equal to $actual.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment