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

Fix fieldValueEquals() for checking multi-valued fields. Closes #704 #739

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
26 changes: 19 additions & 7 deletions src/WebAssert.php
Original file line number Diff line number Diff line change
Expand Up @@ -670,15 +670,21 @@ public function fieldValueEquals($field, $value, TraversableElement $container =
{
$node = $this->fieldExists($field, $container);
$actual = $node->getValue();
$regex = '/^'.preg_quote($value, '/').'$/ui';

$message = sprintf('The field "%s" value is "%s", but "%s" expected.', $field, $actual, $value);
if (is_array($actual)) {
$condition = $actual == (array) $value;
} else {
$regex = '/^'.preg_quote($value, '/').'$/ui';
$condition = (bool) preg_match($regex, $actual);
}

$this->assert((bool) preg_match($regex, $actual), $message);
$message = sprintf('The field "%s" value is "%s", but "%s" expected.', $field, implode(', ', (array) $actual), implode(', ', (array) $value));

$this->assert($condition, $message);
}

/**
* Checks that specific field have provided value.
* Checks that specific field does not have provided value.
*
* @param string $field field id|name|label|value
* @param string $value field value
Expand All @@ -690,11 +696,17 @@ public function fieldValueNotEquals($field, $value, TraversableElement $containe
{
$node = $this->fieldExists($field, $container);
$actual = $node->getValue();
$regex = '/^'.preg_quote($value, '/').'$/ui';

$message = sprintf('The field "%s" value is "%s", but it should not be.', $field, $actual);
if (is_array($actual)) {
$condition = $actual != (array) $value;
} else {
$regex = '/^'.preg_quote($value, '/').'$/ui';
$condition = !preg_match($regex, $actual);
}

$this->assert(!preg_match($regex, $actual), $message);
$message = sprintf('The field "%s" value is "%s", but it should not be.', $field, implode(', ', (array) $actual));

$this->assert($condition, $message);
}

/**
Expand Down
53 changes: 45 additions & 8 deletions tests/WebAssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1110,22 +1110,22 @@ public function testFieldValueEquals()
;

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

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

$element
->expects($this->exactly(4))
->expects($this->exactly(9))
->method('getValue')
->will($this->returnValue(234))
->will($this->onConsecutiveCalls(234, 234, 234, 234, array(234), array(234, 432), array(234), array(234, 432), array(234, 432)))
;

$this->assertCorrectAssertion('fieldValueEquals', array('username', 234));
Expand All @@ -1147,6 +1147,27 @@ public function testFieldValueEquals()
'Behat\\Mink\\Exception\\ExpectationException',
'The field "username" value is "234", but "" expected.'
);

$this->assertCorrectAssertion('fieldValueEquals', array('username', array(234)));
$this->assertCorrectAssertion('fieldValueEquals', array('username', array(234, 432)));
$this->assertWrongAssertion(
'fieldValueEquals',
array('username', array(235)),
'Behat\\Mink\\Exception\\ExpectationException',
'The field "username" value is "234", but "235" expected.'
);
$this->assertWrongAssertion(
'fieldValueEquals',
array('username', array(235, 431)),
'Behat\\Mink\\Exception\\ExpectationException',
'The field "username" value is "234, 432", but "235, 431" expected.'
);
$this->assertWrongAssertion(
'fieldValueEquals',
array('username', array()),
'Behat\\Mink\\Exception\\ExpectationException',
'The field "username" value is "234, 432", but "" expected.'
);
}

public function testFieldValueNotEquals()
Expand All @@ -1162,22 +1183,22 @@ public function testFieldValueNotEquals()
;

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

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

$element
->expects($this->exactly(4))
->expects($this->exactly(9))
->method('getValue')
->will($this->returnValue(235))
->will($this->onConsecutiveCalls(235, 235, 235, 235, array(235), array(235, 431), array(235), array(235, 431), array(235, 431)))
;

$this->assertCorrectAssertion('fieldValueNotEquals', array('username', 234));
Expand All @@ -1189,6 +1210,22 @@ public function testFieldValueNotEquals()
);
$this->assertCorrectAssertion('fieldValueNotEquals', array('username', 23));
$this->assertCorrectAssertion('fieldValueNotEquals', array('username', ''));

$this->assertCorrectAssertion('fieldValueNotEquals', array('username', array(234)));
$this->assertWrongAssertion(
'fieldValueNotEquals',
array('username', array(235, 431)),
'Behat\\Mink\\Exception\\ExpectationException',
'The field "username" value is "235, 431", but it should not be.'
);
$this->assertWrongAssertion(
'fieldValueNotEquals',
array('username', array(235)),
'Behat\\Mink\\Exception\\ExpectationException',
'The field "username" value is "235", but it should not be.'
);
$this->assertCorrectAssertion('fieldValueNotEquals', array('username', array(234, 431)));
$this->assertCorrectAssertion('fieldValueNotEquals', array('username', array()));
}

public function testCheckboxChecked()
Expand Down