Skip to content

Commit

Permalink
Merge pull request #636 from stof/header-assertions
Browse files Browse the repository at this point in the history
Add support for header assertions
  • Loading branch information
stof committed Feb 4, 2015
2 parents 87a18d7 + e5a3632 commit f9b1497
Show file tree
Hide file tree
Showing 4 changed files with 270 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,27 @@ public function getResponseHeaders()
return $this->driver->getResponseHeaders();
}

/**
* Returns specific response header.
*
* @param string $name
*
* @return string|null
*/
public function getResponseHeader($name)
{
$headers = $this->driver->getResponseHeaders();

$name = strtolower($name);
$headers = array_change_key_case($headers, CASE_LOWER);

if (!isset($headers[$name])) {
return null;
}

return is_array($headers[$name]) ? $headers[$name][0] : $headers[$name];
}

/**
* Sets cookie.
*
Expand Down
100 changes: 100 additions & 0 deletions src/WebAssert.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,106 @@ public function statusCodeNotEquals($code)
$this->assert(intval($code) !== intval($actual), $message);
}

/**
* Checks that current response header equals value.
*
* @param string $name
* @param string $value
*
* @throws ExpectationException
*/
public function responseHeaderEquals($name, $value)
{
$actual = $this->session->getResponseHeader($name);
$message = sprintf('Current response header "%s" is "%s", but "%s" expected.', $name, $actual, $value);

$this->assert($value === $actual, $message);
}

/**
* Checks that current response header does not equal value.
*
* @param string $name
* @param string $value
*
* @throws ExpectationException
*/
public function responseHeaderNotEquals($name, $value)
{
$actual = $this->session->getResponseHeader($name);
$message = sprintf('Current response header "%s" is "%s", but should not be.', $name, $actual, $value);

$this->assert($value !== $actual, $message);
}

/**
* Checks that current response header contains value.
*
* @param string $name
* @param string $value
*
* @throws ExpectationException
*/
public function responseHeaderContains($name, $value)
{
$actual = $this->session->getResponseHeader($name);
$message = sprintf('The text "%s" was not found anywhere in the "%s" response header.', $value, $name);

$this->assert(false !== stripos($actual, $value), $message);
}

/**
* Checks that current response header does not contain value.
*
* @param string $name
* @param string $value
*
* @throws ExpectationException
*/
public function responseHeaderNotContains($name, $value)
{
$actual = $this->session->getResponseHeader($name);
$message = sprintf('The text "%s" was found in the "%s" response header, but it should not.', $value, $name);

$this->assert(false === stripos($actual, $value), $message);
}

/**
* Checks that current response header matches regex.
*
* @param string $name
* @param string $regex
*
* @throws ExpectationException
*/
public function responseHeaderMatches($name, $regex)
{
$actual = $this->session->getResponseHeader($name);
$message = sprintf('The pattern "%s" was not found anywhere in the "%s" response header.', $regex, $name);

$this->assert((bool) preg_match($regex, $actual), $message);
}

/**
* Checks that current response header does not match regex.
*
* @param string $name
* @param string $regex
*
* @throws ExpectationException
*/
public function responseHeaderNotMatches($name, $regex)
{
$actual = $this->session->getResponseHeader($name);
$message = sprintf(
'The pattern "%s" was found in the text of the "%s" response header, but it should not.',
$regex,
$name
);

$this->assert(!preg_match($regex, $actual), $message);
}

/**
* Checks that current page contains text.
*
Expand Down
23 changes: 23 additions & 0 deletions tests/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,29 @@ public function testGetResponseHeaders()
$this->assertEquals($ret, $this->session->getResponseHeaders());
}

/**
* @dataProvider provideResponseHeader
*/
public function testGetResponseHeader($expected, $name, array $headers)
{
$this->driver->expects($this->once())
->method('getResponseHeaders')
->willReturn($headers);

$this->assertSame($expected, $this->session->getResponseHeader($name));
}

public function provideResponseHeader()
{
return array(
array('test', 'Mink', array('Mink' => 'test')),
array('test', 'mink', array('Mink' => 'test')),
array('test', 'Mink', array('mink' => 'test')),
array('test', 'Mink', array('Mink' => array('test', 'test2'))),
array(null, 'other', array('Mink' => 'test')),
);
}

public function testSetCookie()
{
$this->driver->expects($this->once())
Expand Down
126 changes: 126 additions & 0 deletions tests/WebAssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,132 @@ public function testStatusCodeNotEquals()
);
}

public function testResponseHeaderEquals()
{
$this->session
->expects($this->any())
->method('getResponseHeader')
->will($this->returnValueMap(
array(
array('foo', 'bar'),
array('bar', 'baz'),
)
));

$this->assertCorrectAssertion('responseHeaderEquals', array('foo', 'bar'));
$this->assertWrongAssertion(
'responseHeaderEquals',
array('bar', 'foo'),
'Behat\\Mink\\Exception\\ExpectationException',
'Current response header "bar" is "baz", but "foo" expected.'
);
}

public function testResponseHeaderNotEquals()
{
$this->session
->expects($this->any())
->method('getResponseHeader')
->will($this->returnValueMap(
array(
array('foo', 'bar'),
array('bar', 'baz'),
)
));

$this->assertCorrectAssertion('responseHeaderNotEquals', array('foo', 'baz'));
$this->assertWrongAssertion(
'responseHeaderNotEquals',
array('bar', 'baz'),
'Behat\\Mink\\Exception\\ExpectationException',
'Current response header "bar" is "baz", but should not be.'
);
}

public function testResponseHeaderContains()
{
$this->session
->expects($this->any())
->method('getResponseHeader')
->will($this->returnValueMap(
array(
array('foo', 'bar'),
array('bar', 'baz'),
)
));

$this->assertCorrectAssertion('responseHeaderContains', array('foo', 'ba'));
$this->assertWrongAssertion(
'responseHeaderContains',
array('bar', 'bz'),
'Behat\\Mink\\Exception\\ExpectationException',
'The text "bz" was not found anywhere in the "bar" response header.'
);
}

public function testResponseHeaderNotContains()
{
$this->session
->expects($this->any())
->method('getResponseHeader')
->will($this->returnValueMap(
array(
array('foo', 'bar'),
array('bar', 'baz'),
)
));

$this->assertCorrectAssertion('responseHeaderNotContains', array('foo', 'bz'));
$this->assertWrongAssertion(
'responseHeaderNotContains',
array('bar', 'ba'),
'Behat\\Mink\\Exception\\ExpectationException',
'The text "ba" was found in the "bar" response header, but it should not.'
);
}

public function testResponseHeaderMatches()
{
$this->session
->expects($this->any())
->method('getResponseHeader')
->will($this->returnValueMap(
array(
array('foo', 'bar'),
array('bar', 'baz'),
)
));

$this->assertCorrectAssertion('responseHeaderMatches', array('foo', '/ba(.*)/'));
$this->assertWrongAssertion(
'responseHeaderMatches',
array('bar', '/b[^a]/'),
'Behat\\Mink\\Exception\\ExpectationException',
'The pattern "/b[^a]/" was not found anywhere in the "bar" response header.'
);
}

public function testResponseHeaderNotMatches()
{
$this->session
->expects($this->any())
->method('getResponseHeader')
->will($this->returnValueMap(
array(
array('foo', 'bar'),
array('bar', 'baz'),
)
));

$this->assertCorrectAssertion('responseHeaderNotMatches', array('foo', '/bz/'));
$this->assertWrongAssertion(
'responseHeaderNotMatches',
array('bar', '/b[ab]z/'),
'Behat\\Mink\\Exception\\ExpectationException',
'The pattern "/b[ab]z/" was found in the text of the "bar" response header, but it should not.'
);
}

public function testPageTextContains()
{
$page = $this->getMockBuilder('Behat\\Mink\\Element\\DocumentElement')
Expand Down

0 comments on commit f9b1497

Please sign in to comment.