Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fieldValueMatches / fieldValueNotMatches #775

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/WebAssert.php
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,44 @@ public function fieldValueNotEquals($field, $value, TraversableElement $containe
$this->assert(!preg_match($regex, $actual), $message);
}

/**
* Checks that specific field matches provided regex.
*
* @param string $field field id|name|label|value
* @param string $regex pattern
* @param TraversableElement $container document to check against
*
* @throws ExpectationException
*/
public function fieldValueMatches($field, $regex, TraversableElement $container = null)
{
$node = $this->fieldExists($field, $container);
$actual = $node->getValue();

$message = sprintf('The pattern "%s" was not found in the value "%s" of field "%s".', $regex, $actual, $field);

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

/**
* Checks that specific field have provided value.
*
* @param string $field field id|name|label|value
* @param string $regex pattern
* @param TraversableElement $container document to check against
*
* @throws ExpectationException
*/
public function fieldValueNotMatches($field, $regex, TraversableElement $container = null)
{
$node = $this->fieldExists($field, $container);
$actual = $node->getValue();

$message = sprintf('The pattern "%s" was found in the value "%s" of field "%s", but it should not.', $regex, $actual, $field);

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

/**
* Checks that specific checkbox is checked.
*
Expand Down
104 changes: 104 additions & 0 deletions tests/WebAssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,110 @@ public function testFieldValueNotEquals()
$this->assertCorrectAssertion('fieldValueNotEquals', array('username', ''));
}

public function testFieldValueMatches()
{
$page = $this->getMockBuilder('Behat\\Mink\\Element\\DocumentElement')
->disableOriginalConstructor()
->getMock()
;

$element = $this->getMockBuilder('Behat\\Mink\\Element\\NodeElement')
->disableOriginalConstructor()
->getMock()
;

$this->session
->expects($this->exactly(4))
->method('getPage')
->will($this->returnValue($page))
;

$page
->expects($this->exactly(4))
->method('findField')
->with('username')
->will($this->returnValue($element))
;

$element
->expects($this->exactly(4))
->method('getValue')
->will($this->returnValue(234))
;

$this->assertCorrectAssertion('fieldValueMatches', array('username', '/234/'));
$this->assertWrongAssertion(
'fieldValueMatches',
array('username', '/235/'),
'Behat\\Mink\\Exception\\ExpectationException',
'The pattern "/235/" was not found in the value "234" of field "username".'
);
$this->assertWrongAssertion(
'fieldValueMatches',
array('username', '/22/'),
'Behat\\Mink\\Exception\\ExpectationException',
'The pattern "/22/" was not found in the value "234" of field "username".'
);
$this->assertWrongAssertion(
'fieldValueMatches',
array('username', '/\D+/'),
'Behat\\Mink\\Exception\\ExpectationException',
'The pattern "/\D+/" was not found in the value "234" of field "username".'
);
}

public function testFieldValueNotMatches()
{
$page = $this->getMockBuilder('Behat\\Mink\\Element\\DocumentElement')
->disableOriginalConstructor()
->getMock()
;

$element = $this->getMockBuilder('Behat\\Mink\\Element\\NodeElement')
->disableOriginalConstructor()
->getMock()
;

$this->session
->expects($this->exactly(4))
->method('getPage')
->will($this->returnValue($page))
;

$page
->expects($this->exactly(4))
->method('findField')
->with('username')
->will($this->returnValue($element))
;

$element
->expects($this->exactly(4))
->method('getValue')
->will($this->returnValue(235))
;

$this->assertCorrectAssertion('fieldValueNotMatches', array('username', '/234/'));
$this->assertWrongAssertion(
'fieldValueNotMatches',
array('username', '/235/'),
'Behat\\Mink\\Exception\\ExpectationException',
'The pattern "/235/" was found in the value "235" of field "username", but it should not.'
);
$this->assertWrongAssertion(
'fieldValueNotMatches',
array('username', '/23/'),
'Behat\\Mink\\Exception\\ExpectationException',
'The pattern "/23/" was found in the value "235" of field "username", but it should not.'
);
$this->assertWrongAssertion(
'fieldValueNotMatches',
array('username', '/\d+/'),
'Behat\\Mink\\Exception\\ExpectationException',
'The pattern "/\d+/" was found in the value "235" of field "username", but it should not.'
);
}

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