Skip to content

Commit 13894a3

Browse files
committed
Add fieldValueMatches / fieldValueNotMatches:
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" :)
1 parent 6d637f7 commit 13894a3

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

src/WebAssert.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,44 @@ public function fieldValueNotEquals($field, $value, TraversableElement $containe
695695
$this->assert(!preg_match($regex, $actual), $message);
696696
}
697697

698+
/**
699+
* Checks that specific field matches provided regex.
700+
*
701+
* @param string $field field id|name|label|value
702+
* @param string $regex pattern
703+
* @param TraversableElement $container document to check against
704+
*
705+
* @throws ExpectationException
706+
*/
707+
public function fieldValueMatches($field, $regex, TraversableElement $container = null)
708+
{
709+
$node = $this->fieldExists($field, $container);
710+
$actual = $node->getValue();
711+
712+
$message = sprintf('The pattern "%s" was not found in the value "%s" of field "%s".', $regex, $actual, $field);
713+
714+
$this->assert((bool) preg_match($regex, $actual), $message);
715+
}
716+
717+
/**
718+
* Checks that specific field have provided value.
719+
*
720+
* @param string $field field id|name|label|value
721+
* @param string $regex pattern
722+
* @param TraversableElement $container document to check against
723+
*
724+
* @throws ExpectationException
725+
*/
726+
public function fieldValueNotMatches($field, $regex, TraversableElement $container = null)
727+
{
728+
$node = $this->fieldExists($field, $container);
729+
$actual = $node->getValue();
730+
731+
$message = sprintf('The pattern "%s" was found in the value "%s" of field "%s", but it should not.', $regex, $actual, $field);
732+
733+
$this->assert(!(bool) preg_match($regex, $actual), $message);
734+
}
735+
698736
/**
699737
* Checks that specific checkbox is checked.
700738
*

tests/WebAssertTest.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,110 @@ public function testFieldValueNotEquals()
12931293
$this->assertCorrectAssertion('fieldValueNotEquals', array('username', ''));
12941294
}
12951295

1296+
public function testFieldValueMatches()
1297+
{
1298+
$page = $this->getMockBuilder('Behat\\Mink\\Element\\DocumentElement')
1299+
->disableOriginalConstructor()
1300+
->getMock()
1301+
;
1302+
1303+
$element = $this->getMockBuilder('Behat\\Mink\\Element\\NodeElement')
1304+
->disableOriginalConstructor()
1305+
->getMock()
1306+
;
1307+
1308+
$this->session
1309+
->expects($this->exactly(4))
1310+
->method('getPage')
1311+
->will($this->returnValue($page))
1312+
;
1313+
1314+
$page
1315+
->expects($this->exactly(4))
1316+
->method('findField')
1317+
->with('username')
1318+
->will($this->returnValue($element))
1319+
;
1320+
1321+
$element
1322+
->expects($this->exactly(4))
1323+
->method('getValue')
1324+
->will($this->returnValue(234))
1325+
;
1326+
1327+
$this->assertCorrectAssertion('fieldValueMatches', array('username', '/234/'));
1328+
$this->assertWrongAssertion(
1329+
'fieldValueMatches',
1330+
array('username', '/235/'),
1331+
'Behat\\Mink\\Exception\\ExpectationException',
1332+
'The pattern "/235/" was not found in the value "234" of field "username".'
1333+
);
1334+
$this->assertWrongAssertion(
1335+
'fieldValueMatches',
1336+
array('username', '/22/'),
1337+
'Behat\\Mink\\Exception\\ExpectationException',
1338+
'The pattern "/22/" was not found in the value "234" of field "username".'
1339+
);
1340+
$this->assertWrongAssertion(
1341+
'fieldValueMatches',
1342+
array('username', '/\D+/'),
1343+
'Behat\\Mink\\Exception\\ExpectationException',
1344+
'The pattern "/\D+/" was not found in the value "234" of field "username".'
1345+
);
1346+
}
1347+
1348+
public function testFieldValueNotMatches()
1349+
{
1350+
$page = $this->getMockBuilder('Behat\\Mink\\Element\\DocumentElement')
1351+
->disableOriginalConstructor()
1352+
->getMock()
1353+
;
1354+
1355+
$element = $this->getMockBuilder('Behat\\Mink\\Element\\NodeElement')
1356+
->disableOriginalConstructor()
1357+
->getMock()
1358+
;
1359+
1360+
$this->session
1361+
->expects($this->exactly(4))
1362+
->method('getPage')
1363+
->will($this->returnValue($page))
1364+
;
1365+
1366+
$page
1367+
->expects($this->exactly(4))
1368+
->method('findField')
1369+
->with('username')
1370+
->will($this->returnValue($element))
1371+
;
1372+
1373+
$element
1374+
->expects($this->exactly(4))
1375+
->method('getValue')
1376+
->will($this->returnValue(235))
1377+
;
1378+
1379+
$this->assertCorrectAssertion('fieldValueNotMatches', array('username', '/234/'));
1380+
$this->assertWrongAssertion(
1381+
'fieldValueNotMatches',
1382+
array('username', '/235/'),
1383+
'Behat\\Mink\\Exception\\ExpectationException',
1384+
'The pattern "/235/" was found in the value "235" of field "username", but it should not.'
1385+
);
1386+
$this->assertWrongAssertion(
1387+
'fieldValueNotMatches',
1388+
array('username', '/23/'),
1389+
'Behat\\Mink\\Exception\\ExpectationException',
1390+
'The pattern "/23/" was found in the value "235" of field "username", but it should not.'
1391+
);
1392+
$this->assertWrongAssertion(
1393+
'fieldValueNotMatches',
1394+
array('username', '/\d+/'),
1395+
'Behat\\Mink\\Exception\\ExpectationException',
1396+
'The pattern "/\d+/" was found in the value "235" of field "username", but it should not.'
1397+
);
1398+
}
1399+
12961400
public function testCheckboxChecked()
12971401
{
12981402
$page = $this->getMockBuilder('Behat\\Mink\\Element\\DocumentElement')

0 commit comments

Comments
 (0)