Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix #92 "HaveKey" Matcher has "$expected" and "$actual" switched

  • Loading branch information...
commit 241b68f9481a79607cf00f2a0bce25d126fc104d 1 parent b3e6ad8
@MarcelloDuarte MarcelloDuarte authored
Showing with 39 additions and 39 deletions.
  1. +30 −30 spec/Matcher/HaveKeySpec.php
  2. +9 −9 src/PHPSpec/Matcher/HaveKey.php
View
60 spec/Matcher/HaveKeySpec.php
@@ -6,41 +6,41 @@
class DescribeHaveKey extends \PHPSpec\Context {
private $matcher;
-
+
function before()
{
- $this->matcher = $this->spec(new HaveKey(array('foo' => 42)));
- $this->matcher->matches('foo');
+ $this->matcher = $this->spec(new HaveKey('foo'));
+ $this->matcher->matches(array('foo' => 42));
+ }
+
+ function itShouldReturnADescriptionWithExpectedValue()
+ {
+ $this->matcher->getDescription()->should->be('have key \'foo\'');
+ }
+
+ function itShouldReturnAMeaningfulFailureMessageIfRequested()
+ {
+ $this->matcher->matches(array('bar' => 42));
+ $this->matcher->getFailureMessage()->should->be(
+ 'expected to have key \'foo\', got key does not exist (using haveKey())'
+ );
}
- function itShouldReturnADescriptionWithExpectedValue()
+ function itShouldReturnAMeaningulNegativeFailureMessageIfRequired()
{
- $this->matcher->getDescription()->should->be('have key \'foo\'');
- }
-
- function itShouldReturnAMeaningfulFailureMessageIfRequested()
+ $this->matcher->matches(array('bar' => 42));
+ $this->matcher->getNegativeFailureMessage()->should->be(
+ 'expected key \'foo\' not to exist (using haveKey())'
+ );
+ }
+
+ function itReturnsTrueIfKeyExists()
{
- $this->matcher->matches('bar');
- $this->matcher->getFailureMessage()->should->be(
- 'expected to have key \'bar\', got key does not exist (using haveKey())'
- );
- }
-
- function itShouldReturnAMeaningulNegativeFailureMessageIfRequired()
+ $this->matcher->matches(array('foo' => 42))->should->beTrue();
+ }
+
+ function itReturnsFalseIfKeyDoesNotExist()
{
- $this->matcher->matches('foo');
- $this->matcher->getNegativeFailureMessage()->should->be(
- 'expected key \'foo\' not to exist (using haveKey())'
- );
- }
-
- function itReturnsTrueIfKeyExists()
- {
- $this->matcher->matches('foo')->should->beTrue();
- }
-
- function itReturnsFalseIfKeyDoesNotExist()
- {
- $this->matcher->matches('zoo')->should->beFalse();
- }
+ $this->matcher->matches(array('zoo' => 42))->should->beFalse();
+ }
}
View
18 src/PHPSpec/Matcher/HaveKey.php
@@ -35,11 +35,6 @@ class HaveKey implements Matcher
*/
public function __construct($expected)
{
- if (!is_array($expected)) {
- throw new \InvalidArgumentException(
- 'Expected value for have key matcher should be array'
- );
- }
$this->_expected = $expected;
}
@@ -52,7 +47,12 @@ public function __construct($expected)
public function matches($actual)
{
$this->_actual = $actual;
- return array_key_exists($actual, $this->_expected);
+ if (!is_array($actual)) {
+ throw new \InvalidArgumentException(
+ 'Actual value for have key matcher should be array'
+ );
+ }
+ return array_key_exists($this->_expected, $actual);
}
/**
@@ -62,7 +62,7 @@ public function matches($actual)
*/
public function getFailureMessage()
{
- return 'expected to have key ' . var_export($this->_actual, true) .
+ return 'expected to have key ' . var_export($this->_expected, true) .
', got key does not exist (using haveKey())';
}
@@ -73,7 +73,7 @@ public function getFailureMessage()
*/
public function getNegativeFailureMessage()
{
- return 'expected key ' . var_export($this->_actual, true) .
+ return 'expected key ' . var_export($this->_expected, true) .
' not to exist (using haveKey())';
}
@@ -84,6 +84,6 @@ public function getNegativeFailureMessage()
*/
public function getDescription()
{
- return 'have key ' . var_export($this->_actual, true);
+ return 'have key ' . var_export($this->_expected, true);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.