Skip to content

Commit

Permalink
Add fieldValueMatches / fieldValueNotMatches:
Browse files Browse the repository at this point in the history
Squash:
* Added tests.
* Fixed inversion of condition.
* Add fieldValueMatches / fieldValueNotMatches

Would be hand to allow pattern assertions on field values like on the page text.
This was mentioned here #696 but: "PR got lost in the sands of time" :)
  • Loading branch information
das-peter committed Jun 2, 2019
1 parent 6d637f7 commit 13894a3
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
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

0 comments on commit 13894a3

Please sign in to comment.