Permalink
Browse files

Allow identity checking for non object types in TraversableContains a…

…ssertions.

This fixes #745.
The possibility to check identity for non object is done by adding a new optional parameter.
This comes in addition to the existing one that already allow to check object identity or not.
  • Loading branch information...
1 parent 90bf298 commit 255f5ccc26d548a9f9ffc67445be37cc46270afe Fred committed Dec 11, 2012
@@ -123,14 +123,15 @@ public static function assertArrayNotHasKey($key, $array, $message = '')
* @param string $message
* @param boolean $ignoreCase
* @param boolean $checkForObjectIdentity
+ * @param boolean $checkForNonObjectIdentity
* @since Method available since Release 2.1.0
*/
- public static function assertContains($needle, $haystack, $message = '', $ignoreCase = FALSE, $checkForObjectIdentity = TRUE)
+ public static function assertContains($needle, $haystack, $message = '', $ignoreCase = FALSE, $checkForObjectIdentity = TRUE, $checkForNonObjectIdentity = FALSE)
{
if (is_array($haystack) ||
is_object($haystack) && $haystack instanceof Traversable) {
$constraint = new PHPUnit_Framework_Constraint_TraversableContains(
- $needle, $checkForObjectIdentity
+ $needle, $checkForObjectIdentity, $checkForNonObjectIdentity
);
}
@@ -180,15 +181,16 @@ public static function assertAttributeContains($needle, $haystackAttributeName,
* @param string $message
* @param boolean $ignoreCase
* @param boolean $checkForObjectIdentity
+ * @param boolean $checkForNonObjectIdentity
* @since Method available since Release 2.1.0
*/
- public static function assertNotContains($needle, $haystack, $message = '', $ignoreCase = FALSE, $checkForObjectIdentity = TRUE)
+ public static function assertNotContains($needle, $haystack, $message = '', $ignoreCase = FALSE, $checkForObjectIdentity = TRUE, $checkForNonObjectIdentity = FALSE)
{
if (is_array($haystack) ||
is_object($haystack) && $haystack instanceof Traversable) {
$constraint = new PHPUnit_Framework_Constraint_Not(
new PHPUnit_Framework_Constraint_TraversableContains(
- $needle, $checkForObjectIdentity
+ $needle, $checkForObjectIdentity, $checkForNonObjectIdentity
)
);
}
@@ -2451,12 +2453,13 @@ public static function attribute(PHPUnit_Framework_Constraint $constraint, $attr
*
* @param mixed $value
* @param boolean $checkForObjectIdentity
+ * @param boolean $checkForNonObjectIdentity
* @return PHPUnit_Framework_Constraint_TraversableContains
* @since Method available since Release 3.0.0
*/
- public static function contains($value, $checkForObjectIdentity = TRUE)
+ public static function contains($value, $checkForObjectIdentity = TRUE, $checkForNonObjectIdentity = FALSE)
{
- return new PHPUnit_Framework_Constraint_TraversableContains($value, $checkForObjectIdentity);
+ return new PHPUnit_Framework_Constraint_TraversableContains($value, $checkForObjectIdentity, $checkForNonObjectIdentity);
}
/**
@@ -1541,12 +1541,13 @@ function classHasStaticAttribute($attributeName)
*
* @param mixed $value
* @param boolean $checkForObjectIdentity
+ * @param boolean $checkForNonObjectIdentity
* @return PHPUnit_Framework_Constraint_TraversableContains
* @since Method available since Release 3.0.0
*/
-function contains($value, $checkForObjectIdentity = TRUE)
+function contains($value, $checkForObjectIdentity = TRUE, $checkForNonObjectIdentity = FALSE)
{
- return PHPUnit_Framework_Assert::contains($value, $checkForObjectIdentity);
+ return PHPUnit_Framework_Assert::contains($value, $checkForObjectIdentity, $checkForNonObjectIdentity);
}
/**
@@ -64,26 +64,37 @@ class PHPUnit_Framework_Constraint_TraversableContains extends PHPUnit_Framework
*/
protected $checkForObjectIdentity;
+ /**
+ * @var boolean
+ */
+ protected $checkForNonObjectIdentity;
+
/**
* @var mixed
*/
protected $value;
/**
- * @param boolean $value
- * @param mixed $checkForObjectIdentity
+ * @param mixed $value
+ * @param boolean $checkForObjectIdentity
+ * @param boolean $checkForNonObjectIdentity
* @throws PHPUnit_Framework_Exception
*/
- public function __construct($value, $checkForObjectIdentity = TRUE)
+ public function __construct($value, $checkForObjectIdentity = TRUE, $checkForNonObjectIdentity = FALSE)
{
parent::__construct();
if (!is_bool($checkForObjectIdentity)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'boolean');
}
- $this->checkForObjectIdentity = $checkForObjectIdentity;
- $this->value = $value;
+ if (!is_bool($checkForNonObjectIdentity)) {
+ throw PHPUnit_Util_InvalidArgumentHelper::factory(3, 'boolean');
+ }
+
+ $this->checkForObjectIdentity = $checkForObjectIdentity;
+ $this->checkForNonObjectIdentity = $checkForNonObjectIdentity;
+ $this->value = $value;
}
/**
@@ -110,7 +121,10 @@ protected function matches($other)
}
} else {
foreach ($other as $element) {
- if ($element == $this->value) {
+ if (($this->checkForNonObjectIdentity &&
+ $element === $this->value) ||
+ (!$this->checkForNonObjectIdentity &&
+ $element == $this->value)) {
return TRUE;
}
}
@@ -3071,6 +3071,24 @@ public function testConstraintArrayContains2()
$this->fail();
}
+ /**
+ * @covers PHPUnit_Framework_Constraint_TraversableContains
+ */
+ public function testConstraintArrayContainsCheckForObjectIdentity()
+ {
+ // Check for primitive type.
+ $constraint = new PHPUnit_Framework_Constraint_TraversableContains('foo', TRUE, TRUE);
+
+ $this->assertFalse($constraint->evaluate(array(0), '', TRUE));
+ $this->assertFalse($constraint->evaluate(array(TRUE), '', TRUE));
+
+ // Default case.
+ $constraint = new PHPUnit_Framework_Constraint_TraversableContains('foo');
+
+ $this->assertTrue($constraint->evaluate(array(0), '', TRUE));
+ $this->assertTrue($constraint->evaluate(array(TRUE), '', TRUE));
+ }
+
/**
* @covers PHPUnit_Framework_Constraint_TraversableContains
* @covers PHPUnit_Framework_Constraint_Not

0 comments on commit 255f5cc

Please sign in to comment.