Permalink
Browse files

- Merge [4659].

  • Loading branch information...
1 parent 528860d commit dcde3a1fe229b211c8725f140f242d0f5172239a @sebastianbergmann committed Feb 23, 2009
View
20 PHPUnit/Framework/Constraint/IsEqual.php
@@ -204,6 +204,26 @@ protected function recursiveComparison($a, $b, $depth = 0)
return FALSE;
}
+ if ($a instanceof SplObjectStorage XOR $b instanceof SplObjectStorage) {
+ return FALSE;
+ }
+
+ if ($a instanceof SplObjectStorage) {
+ foreach ($a as $object) {
+ if (!$b->contains($object)) {
+ return FALSE;
+ }
+ }
+
+ foreach ($b as $object) {
+ if (!$a->contains($object)) {
+ return FALSE;
+ }
+ }
+
+ return TRUE;
+ }
+
if ($a instanceof DOMDocument || $b instanceof DOMDocument) {
if (!$a instanceof DOMDocument) {
$_a = new DOMDocument;
View
4 PHPUnit/Framework/Constraint/TraversableContains.php
@@ -81,6 +81,10 @@ public function __construct($value)
*/
public function evaluate($other)
{
+ if ($other instanceof SplObjectStorage) {
+ return $other->contains($this->value);
+ }
+
foreach ($other as $straw) {
if ($straw === $this->value) {
return TRUE;
View
100 PHPUnit/Tests/Framework/AssertTest.php
@@ -97,6 +97,29 @@ public function testFail()
/**
* @covers PHPUnit_Framework_Assert::assertContains
*/
+ public function testAssertSplObjectStorageContainsObject()
+ {
+ $a = new stdClass;
+ $b = new stdClass;
+ $c = new SplObjectStorage;
+ $c->attach($a);
+
+ $this->assertContains($a, $c);
+
+ try {
+ $this->assertContains($b, $c);
+ }
+
+ catch (PHPUnit_Framework_AssertionFailedError $e) {
+ return;
+ }
+
+ $this->fail();
+ }
+
+ /**
+ * @covers PHPUnit_Framework_Assert::assertContains
+ */
public function testAssertArrayContainsObject()
{
$a = new stdClass;
@@ -264,6 +287,29 @@ public function testAssertStringContainsString()
/**
* @covers PHPUnit_Framework_Assert::assertNotContains
*/
+ public function testAssertSplObjectStorageNotContainsObject()
+ {
+ $a = new stdClass;
+ $b = new stdClass;
+ $c = new SplObjectStorage;
+ $c->attach($a);
+
+ $this->assertNotContains($b, $c);
+
+ try {
+ $this->assertNotContains($a, $c);
+ }
+
+ catch (PHPUnit_Framework_AssertionFailedError $e) {
+ return;
+ }
+
+ $this->fail();
+ }
+
+ /**
+ * @covers PHPUnit_Framework_Assert::assertNotContains
+ */
public function testAssertArrayNotContainsObject()
{
$a = new stdClass;
@@ -723,6 +769,60 @@ public function testAssertNotEqualsObject()
/**
* @covers PHPUnit_Framework_Assert::assertEquals
*/
+ public function testAssertEqualsSplObjectStorage()
+ {
+ $a = new SampleClass( 4, 8, 15);
+ $b = new SampleClass(16, 23, 42);
+
+ $c = new SplObjectStorage;
+ $c->attach($a);
+
+ $d = new SplObjectStorage;
+ $d->attach($b);
+
+ $this->assertEquals($c, $c);
+
+ try {
+ $this->assertEquals($c, $d);
+ }
+
+ catch (PHPUnit_Framework_AssertionFailedError $e) {
+ return;
+ }
+
+ $this->fail();
+ }
+
+ /**
+ * @covers PHPUnit_Framework_Assert::assertNotEquals
+ */
+ public function testAssertNotEqualsSplObjectStorage()
+ {
+ $a = new SampleClass( 4, 8, 15);
+ $b = new SampleClass(16, 23, 42);
+
+ $c = new SplObjectStorage;
+ $c->attach($a);
+
+ $d = new SplObjectStorage;
+ $d->attach($b);
+
+ $this->assertNotEquals($c, $d);
+
+ try {
+ $this->assertNotEquals($c, $c);
+ }
+
+ catch (PHPUnit_Framework_AssertionFailedError $e) {
+ return;
+ }
+
+ $this->fail();
+ }
+
+ /**
+ * @covers PHPUnit_Framework_Assert::assertEquals
+ */
public function testAssertEqualsString()
{
$this->assertEquals('ab', 'ab');
View
63 PHPUnit/Tests/Framework/ConstraintTest.php
@@ -1556,7 +1556,7 @@ public function testConstraintStringEndsNotWith2()
/**
* @covers PHPUnit_Framework_Constraint_TraversableContains
*/
- public function testConstraintTraversableContains()
+ public function testConstraintArrayContains()
{
$constraint = new PHPUnit_Framework_Constraint_TraversableContains('foo');
@@ -1583,7 +1583,7 @@ public function testConstraintTraversableContains()
/**
* @covers PHPUnit_Framework_Constraint_TraversableContains
*/
- public function testConstraintTraversableContains2()
+ public function testConstraintArrayContains2()
{
$constraint = new PHPUnit_Framework_Constraint_TraversableContains('foo');
@@ -1608,7 +1608,7 @@ public function testConstraintTraversableContains2()
* @covers PHPUnit_Framework_Constraint_Not
* @covers PHPUnit_Framework_Assert::logicalNot
*/
- public function testConstraintTraversableNotContains()
+ public function testConstraintArrayNotContains()
{
$constraint = PHPUnit_Framework_Assert::logicalNot(
new PHPUnit_Framework_Constraint_TraversableContains('foo')
@@ -1639,7 +1639,7 @@ public function testConstraintTraversableNotContains()
* @covers PHPUnit_Framework_Constraint_Not
* @covers PHPUnit_Framework_Assert::logicalNot
*/
- public function testConstraintTraversableNotContains2()
+ public function testConstraintArrayNotContains2()
{
$constraint = PHPUnit_Framework_Assert::logicalNot(
new PHPUnit_Framework_Constraint_TraversableContains('foo')
@@ -1660,5 +1660,60 @@ public function testConstraintTraversableNotContains2()
$this->fail();
}
+
+ /**
+ * @covers PHPUnit_Framework_Constraint_TraversableContains
+ */
+ public function testConstraintSplObjectStorageContains()
+ {
+ $object = new StdClass;
+ $constraint = new PHPUnit_Framework_Constraint_TraversableContains($object);
+ $this->assertEquals("contains \nstdClass Object\n(\n)\n", $constraint->toString());
+
+ $storage = new SplObjectStorage;
+ $this->assertFalse($constraint->evaluate($storage));
+
+ $storage->attach($object);
+ $this->assertTrue($constraint->evaluate($storage));
+
+ try {
+ $constraint->fail(new SplObjectStorage, '');
+ }
+
+ catch (PHPUnit_Framework_ExpectationFailedException $e) {
+ $this->assertEquals(
+ "Failed asserting that an iterator contains \nstdClass Object\n(\n)\n.",
+ $e->getDescription()
+ );
+
+ return;
+ }
+
+ $this->fail();
+ }
+
+ /**
+ * @covers PHPUnit_Framework_Constraint_TraversableContains
+ */
+ public function testConstraintSplObjectStorageContains2()
+ {
+ $object = new StdClass;
+ $constraint = new PHPUnit_Framework_Constraint_TraversableContains($object);
+
+ try {
+ $constraint->fail(new SplObjectStorage, 'custom message');
+ }
+
+ catch (PHPUnit_Framework_ExpectationFailedException $e) {
+ $this->assertEquals(
+ "custom message\nFailed asserting that an iterator contains \nstdClass Object\n(\n)\n.",
+ $e->getDescription()
+ );
+
+ return;
+ }
+
+ $this->fail();
+ }
}
?>

0 comments on commit dcde3a1

Please sign in to comment.