-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Enable matching array keys against constraints #312
Conversation
Array keys may now be matched against all string (starts/ends with, contains, PCRE) and numeric matchers (is null, greater than, less than).
@@ -86,7 +86,18 @@ class PHPUnit_Framework_Constraint_ArrayHasKey extends PHPUnit_Framework_Constra | |||
*/ | |||
protected function matches($other) | |||
{ | |||
return array_key_exists($this->key, $other); | |||
if (is_object($this->key)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use the instanceof
operator instead of is_object()
and is_a()
.
Fixed the issues you mentioned, Sebastian. |
And just stumbled across this... IsNull is not a numeric constraint IMHO, so I reverted that change. |
Any news on this? I'd like to see that feature, too. |
Any news on this? I'd like to see this feature as well. |
👍 This would be a really handy feature. |
👍 |
RSpec::Mocks has had |
@damncabbage, This PR does not add the equivalent of $array = array(
9 => 'foobar',
'name6' => 'Jeff'
);
$this->assertArrayHasKey(
new PHPUnit_Framework_Constraint_PCREMatch('/name\d/'),
$array
);
$this->assertArrayHasKey(
new PHPUnit_Framework_Constraint_StringStartsWith('name'),
$array
);
$this->assertArrayHasKey(
new PHPUnit_Framework_Constraint_StringEndsWith('6'),
$array
);
$this->assertArrayHasKey(
new PHPUnit_Framework_Constraint_StringContains('ame'),
$array
);
$this->assertArrayHasKey(
new PHPUnit_Framework_Constraint_GreaterThan(8),
$array
);
$this->assertArrayHasKey(
new PHPUnit_Framework_Constraint_LessThan(10),
$array
); |
Would someone be willing to provide some use cases for this? |
What does this PR need in order to be merged? @whatthejeff use cases are something like parsing a string and expecting an array which contains certain keys. This would be more elegant and meaningful than the current way which would be: $this->assertTrue(array_key_exists('key', $array)); Edit: Ha, ignore me. I just saw that |
What this pull request needs is use cases. |
I think it's time to close this for a couple of reasons:
|
Array keys may now be matched against all string (starts/ends with, contains, PCRE) and numeric matchers (is null, greater than, less than).
This introduces two new interfaces (StringConstraint and NumericConstraint), which could be replaced with one interface ArrayKeyConstraint if that is more feasible.