Skip to content
Browse files

Fix removeFriends() method by adding tests

  • Loading branch information...
1 parent f1000bc commit 9fac02ccfa452d233ee8a86e60ff3b29827ce959 @willdurand willdurand committed
Showing with 36 additions and 1 deletion.
  1. +5 −1 src/templates/parent/addRemoveAllRelations.php
  2. +31 −0 tests/EqualNestBehaviorTest.php
View
6 src/templates/parent/addRemoveAllRelations.php
@@ -7,6 +7,10 @@
*/
public function remove<?php echo $pluralRefTableName ?>()
{
+ foreach ($this->get<?php echo $pluralRefTableName ?>() as $obj) {
+ $obj->remove<?php echo $refTableName ?>($this);
+ }
+
// this sets the collection to an empty Propel object collection; upon save, all relations will be removed
- $this-><?php echo $pluralRefTableName ?>();
+ $this->set<?php echo $pluralRefTableName ?>(array());
}
View
31 tests/EqualNestBehaviorTest.php
@@ -268,4 +268,35 @@ public function testAddFriendsWithPropelCollection()
$this->assertEquals(4, count($john->getFriends()));
}
+
+ public function testRemoveFriendsWithArray()
+ {
+ $john = new Person();
+ $john->setName('john');
+ $jean = new Person();
+ $jean->setName('jean');
+ $phil = new Person();
+ $phil->setName('phil');
+
+ $john->setFriends(array($jean, $phil));
+ $john->save();
+
+ $this->assertEquals(3, PersonQuery::create()->count());
+
+ $this->assertTrue($john->hasFriend($jean));
+ $this->assertTrue($john->hasFriend($phil));
+ $this->assertTrue($phil->hasFriend($john));
+ $this->assertTrue($jean->hasFriend($john));
+
+ $john->removeFriends();
+ $john->save();
+
+ $this->assertEquals(0, count($john->getFriends()));
+ $this->assertEquals(3, PersonQuery::create()->count());
+
+ $this->assertFalse($john->hasFriend($phil));
+ $this->assertFalse($john->hasFriend($jean));
+ $this->assertFalse($phil->hasFriend($john));
+ $this->assertFalse($jean->hasFriend($john));
+ }
}

0 comments on commit 9fac02c

Please sign in to comment.
Something went wrong with that request. Please try again.