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

Enable matching array keys against constraints #312

Closed
wants to merge 3 commits into from

Conversation

andreaswolf
Copy link
Contributor

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.

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)) {

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().

@andreaswolf
Copy link
Contributor Author

Fixed the issues you mentioned, Sebastian.

@andreaswolf
Copy link
Contributor Author

And just stumbled across this... IsNull is not a numeric constraint IMHO, so I reverted that change.

@cweiske
Copy link

cweiske commented Dec 12, 2011

Any news on this? I'd like to see that feature, too.

@damncabbage
Copy link

Any news on this? I'd like to see this feature as well.

@craigmarvelley
Copy link

👍 This would be a really handy feature.

@catchamonkey
Copy link

👍

@damncabbage
Copy link

RSpec::Mocks has had hash_including since 2008. Please consider merging this pull request so we can try catching up a little bit.

@whatthejeff
Copy link
Contributor

@damncabbage, This PR does not add the equivalent of hash_including (see: argument_matchers.rb). This PR adds support for matching an array key against some of the existing PHPUnit constraints. For instance:

$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
);

@whatthejeff
Copy link
Contributor

Would someone be willing to provide some use cases for this?

@wildlyinaccurate
Copy link

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 assertArrayHasKey() exists!

@sebastianbergmann
Copy link
Owner

What this pull request needs is use cases.

@whatthejeff
Copy link
Contributor

I think it's time to close this for a couple of reasons:

  1. This PR is very old and can no longer be automatically merged.
  2. No one has provided uses cases since we asked for them 3 months ago.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants