Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #746 from farnoux/master

Allow identity checking for non-object types in `assertContains` and all asserts that depend on `TraversableContains`.
  • Loading branch information...
commit 2ec5398fafbdac1634ebac8c3f060a54798ac0ec 2 parents 90bf298 + b0b0eda
@whatthejeff whatthejeff authored
View
27 PHPUnit/Framework/Assert.php
@@ -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
);
}
@@ -159,16 +160,18 @@ public static function assertContains($needle, $haystack, $message = '', $ignore
* @param string $message
* @param boolean $ignoreCase
* @param boolean $checkForObjectIdentity
+ * @param boolean $checkForNonObjectIdentity
* @since Method available since Release 3.0.0
*/
- public static function assertAttributeContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = '', $ignoreCase = FALSE, $checkForObjectIdentity = TRUE)
+ public static function assertAttributeContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = '', $ignoreCase = FALSE, $checkForObjectIdentity = TRUE, $checkForNonObjectIdentity = FALSE)
{
self::assertContains(
$needle,
self::readAttribute($haystackClassOrObject, $haystackAttributeName),
$message,
$ignoreCase,
- $checkForObjectIdentity
+ $checkForObjectIdentity,
+ $checkForNonObjectIdentity
);
}
@@ -180,15 +183,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
)
);
}
@@ -220,16 +224,18 @@ public static function assertNotContains($needle, $haystack, $message = '', $ign
* @param string $message
* @param boolean $ignoreCase
* @param boolean $checkForObjectIdentity
+ * @param boolean $checkForNonObjectIdentity
* @since Method available since Release 3.0.0
*/
- public static function assertAttributeNotContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = '', $ignoreCase = FALSE, $checkForObjectIdentity = TRUE)
+ public static function assertAttributeNotContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = '', $ignoreCase = FALSE, $checkForObjectIdentity = TRUE, $checkForNonObjectIdentity = FALSE)
{
self::assertNotContains(
$needle,
self::readAttribute($haystackClassOrObject, $haystackAttributeName),
$message,
$ignoreCase,
- $checkForObjectIdentity
+ $checkForObjectIdentity,
+ $checkForNonObjectIdentity
);
}
@@ -2451,12 +2457,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);
}
/**
View
25 PHPUnit/Framework/Assert/Functions.php
@@ -114,11 +114,12 @@ function assertArrayNotHasKey($key, array $array, $message = '')
* @param string $message
* @param boolean $ignoreCase
* @param boolean $checkForObjectIdentity
+ * @param boolean $checkForNonObjectIdentity
* @since Method available since Release 3.0.0
*/
-function assertAttributeContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = '', $ignoreCase = FALSE, $checkForObjectIdentity = TRUE)
+function assertAttributeContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = '', $ignoreCase = FALSE, $checkForObjectIdentity = TRUE, $checkForNonObjectIdentity = FALSE)
{
- return PHPUnit_Framework_Assert::assertAttributeContains($needle, $haystackAttributeName, $haystackClassOrObject, $message, $ignoreCase, $checkForObjectIdentity);
+ return PHPUnit_Framework_Assert::assertAttributeContains($needle, $haystackAttributeName, $haystackClassOrObject, $message, $ignoreCase, $checkForObjectIdentity, $checkForNonObjectIdentity);
}
/**
@@ -277,11 +278,12 @@ function assertAttributeLessThanOrEqual($expected, $actualAttributeName, $actual
* @param string $message
* @param boolean $ignoreCase
* @param boolean $checkForObjectIdentity
+ * @param boolean $checkForNonObjectIdentity
* @since Method available since Release 3.0.0
*/
-function assertAttributeNotContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = '', $ignoreCase = FALSE, $checkForObjectIdentity = TRUE)
+function assertAttributeNotContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = '', $ignoreCase = FALSE, $checkForObjectIdentity = TRUE, $checkForNonObjectIdentity = FALSE)
{
- return PHPUnit_Framework_Assert::assertAttributeNotContains($needle, $haystackAttributeName, $haystackClassOrObject, $message, $ignoreCase, $checkForObjectIdentity);
+ return PHPUnit_Framework_Assert::assertAttributeNotContains($needle, $haystackAttributeName, $haystackClassOrObject, $message, $ignoreCase, $checkForObjectIdentity, $checkForNonObjectIdentity);
}
/**
@@ -463,11 +465,12 @@ function assertClassNotHasStaticAttribute($attributeName, $className, $message =
* @param string $message
* @param boolean $ignoreCase
* @param boolean $checkForObjectIdentity
+ * @param boolean $checkForNonObjectIdentity
* @since Method available since Release 2.1.0
*/
-function assertContains($needle, $haystack, $message = '', $ignoreCase = FALSE, $checkForObjectIdentity = TRUE)
+function assertContains($needle, $haystack, $message = '', $ignoreCase = FALSE, $checkForObjectIdentity = TRUE, $checkForNonObjectIdentity = FALSE)
{
- return PHPUnit_Framework_Assert::assertContains($needle, $haystack, $message, $ignoreCase, $checkForObjectIdentity);
+ return PHPUnit_Framework_Assert::assertContains($needle, $haystack, $message, $ignoreCase, $checkForObjectIdentity, $checkForNonObjectIdentity);
}
/**
@@ -771,11 +774,12 @@ function assertLessThanOrEqual($expected, $actual, $message = '')
* @param string $message
* @param boolean $ignoreCase
* @param boolean $checkForObjectIdentity
+ * @param boolean $checkForNonObjectIdentity
* @since Method available since Release 2.1.0
*/
-function assertNotContains($needle, $haystack, $message = '', $ignoreCase = FALSE, $checkForObjectIdentity = TRUE)
+function assertNotContains($needle, $haystack, $message = '', $ignoreCase = FALSE, $checkForObjectIdentity = TRUE, $checkForNonObjectIdentity = FALSE)
{
- return PHPUnit_Framework_Assert::assertNotContains($needle, $haystack, $message, $ignoreCase, $checkForObjectIdentity);
+ return PHPUnit_Framework_Assert::assertNotContains($needle, $haystack, $message, $ignoreCase, $checkForObjectIdentity, $checkForNonObjectIdentity);
}
/**
@@ -1541,12 +1545,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);
}
/**
View
26 PHPUnit/Framework/Constraint/TraversableContains.php
@@ -65,16 +65,22 @@ 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();
@@ -82,8 +88,13 @@ public function __construct($value, $checkForObjectIdentity = TRUE)
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;
}
}
View
77 Tests/Framework/AssertTest.php
@@ -152,6 +152,25 @@ public function testAssertArrayContainsString()
$this->fail();
}
+
+ /**
+ * @covers PHPUnit_Framework_Assert::assertContains
+ */
+ public function testAssertArrayContainsNonObject()
+ {
+ $this->assertContains('foo', array(TRUE));
+
+ try {
+ $this->assertContains('foo', array(TRUE), '', FALSE, TRUE, TRUE);
+ }
+
+ catch (PHPUnit_Framework_AssertionFailedError $e) {
+ return;
+ }
+
+ $this->fail();
+ }
+
/**
* @covers PHPUnit_Framework_Assert::assertContainsOnlyInstancesOf
*/
@@ -446,6 +465,24 @@ public function testAssertArrayNotContainsString()
/**
* @covers PHPUnit_Framework_Assert::assertNotContains
*/
+ public function testAssertArrayNotContainsNonObject()
+ {
+ $this->assertNotContains('foo', array(TRUE), '', FALSE, TRUE, TRUE);
+
+ try {
+ $this->assertNotContains('foo', array(TRUE));
+ }
+
+ catch (PHPUnit_Framework_AssertionFailedError $e) {
+ return;
+ }
+
+ $this->fail();
+ }
+
+ /**
+ * @covers PHPUnit_Framework_Assert::assertNotContains
+ */
public function testAssertStringNotContainsString()
{
$this->assertNotContains('foo', 'bar');
@@ -1882,6 +1919,46 @@ public function testAssertPrivateAttributeNotContains()
}
/**
+ * @covers PHPUnit_Framework_Assert::assertAttributeContains
+ */
+ public function testAssertAttributeContainsNonObject()
+ {
+ $obj = new ClassWithNonPublicAttributes;
+
+ $this->assertAttributeContains(TRUE, 'privateArray', $obj);
+
+ try {
+ $this->assertAttributeContains(TRUE, 'privateArray', $obj, '', FALSE, TRUE, TRUE);
+ }
+
+ catch (PHPUnit_Framework_AssertionFailedError $e) {
+ return;
+ }
+
+ $this->fail();
+ }
+
+ /**
+ * @covers PHPUnit_Framework_Assert::assertAttributeNotContains
+ */
+ public function testAssertAttributeNotContainsNonObject()
+ {
+ $obj = new ClassWithNonPublicAttributes;
+
+ $this->assertAttributeNotContains(TRUE, 'privateArray', $obj, '', FALSE, TRUE, TRUE);
+
+ try {
+ $this->assertAttributeNotContains(TRUE, 'privateArray', $obj);
+ }
+
+ catch (PHPUnit_Framework_AssertionFailedError $e) {
+ return;
+ }
+
+ $this->fail();
+ }
+
+ /**
* @covers PHPUnit_Framework_Assert::assertAttributeEquals
*/
public function testAssertPublicAttributeEquals()
View
18 Tests/Framework/ConstraintTest.php
@@ -3073,6 +3073,24 @@ public function testConstraintArrayContains2()
/**
* @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
* @covers PHPUnit_Framework_Assert::logicalNot
* @covers PHPUnit_Framework_TestFailure::exceptionToString
Please sign in to comment.
Something went wrong with that request. Please try again.