Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add more tests to test the ActiveQuery part, refactored some parts to…

… avoid dirty states
  • Loading branch information...
commit bdc9ff6713d18218f05a5661b0853e3e92cbde8d 1 parent ec6ec3a
@willdurand willdurand authored
View
7 src/EqualNestParentBehavior.php
@@ -45,4 +45,11 @@ public function getQueryBuilderModifier()
return $this->queryBuilderModifier;
}
+
+ public function getFirstPrimaryKey()
+ {
+ $pks = $this->table->getPrimaryKey();
+
+ return $pks[0];
+ }
}
View
1  src/EqualNestParentBehaviorQueryBuilderModifier.php
@@ -51,6 +51,7 @@ public function addCountRelatedObjects($builder)
'pluralRefTableName' => $builder->getPluralizer()->getPluralForm($this->middleTable->getPhpName()),
'objectClassname' => $builder->getStubObjectBuilder()->getClassname(),
'objectName' => '$' . $this->table->getStudlyPhpName(),
+ 'pk' => $this->behavior->getFirstPrimaryKey(),
), '/templates/parent/');
}
}
View
8 src/templates/parent/addCountRelatedObjects.php
@@ -6,11 +6,7 @@
* @param PropelPDO $con
* @return integer
*/
-public function count<?php echo $pluralRefTableName ?>Of(<?php echo $objectClassname ?> <?php echo $objectName ?>, $con = null)
+public function count<?php echo $pluralRefTableName ?>Of(<?php echo $objectClassname ?> <?php echo $objectName ?>, PropelPDO $con = null)
{
- $obj = clone <?php echo $objectName ?>;
- $obj->clearList<?php echo $pluralRefTableName ?>PKs();
- $obj->clear<?php echo $pluralRefTableName ?>();
-
- return $obj->count<?php echo $pluralRefTableName ?>($this, $con);
+ return <?php echo $objectName ?>->get<?php echo $pluralRefTableName ?>()->count();
}
View
3  src/templates/parent/addRemoveAllRelations.php
@@ -10,7 +10,4 @@ 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->set<?php echo $pluralRefTableName ?>(array());
}
View
87 tests/EqualNestBehaviorTest.php
@@ -299,8 +299,9 @@ public function testRemoveFriends()
$john->save();
- $this->assertEquals(0, count($john->getFriends()));
$this->assertEquals(3, PersonQuery::create()->count());
+ $this->assertEquals(0, FriendQuery::create()->count());
+ $this->assertEquals(0, count($john->getFriends()));
$this->assertFalse($john->hasFriend($phil));
$this->assertFalse($john->hasFriend($jean));
@@ -336,4 +337,88 @@ public function testCountFriends()
$john->save();
$this->assertEquals(1, $john->countFriends());
}
+
+ public function testCountFriendsOf()
+ {
+ $john = new Person();
+ $john->setName('john');
+ $john->save();
+
+ $jean = new Person();
+ $jean->setName('jean');
+ $jean->save();
+
+ $phil = new Person();
+ $phil->setName('phil');
+ $phil->save();
+
+ $this->assertEquals(0, FriendQuery::create()->count());
+
+ $this->assertEquals(0, PersonQuery::create()->countFriendsOf($phil));
+ $this->assertEquals(0, PersonQuery::create()->countFriendsOf($jean));
+ $this->assertEquals(0, PersonQuery::create()->countFriendsOf($john));
+
+ $jean->addFriend($phil);
+
+ $this->assertEquals(0, FriendQuery::create()->count());
+ $this->assertEquals(1, PersonQuery::create()->countFriendsOf($phil));
+ $this->assertEquals(1, PersonQuery::create()->countFriendsOf($jean));
+
+ $jean->save();
+ $this->assertEquals(1, PersonQuery::create()->countFriendsOf($phil));
+ $this->assertEquals(1, PersonQuery::create()->countFriendsOf($jean));
+
+ $this->assertEquals(1, FriendQuery::create()->count());
+
+ $jean->removeFriends();
+ $jean->save();
+
+ $this->assertEquals(0, PersonQuery::create()->countFriendsOf($jean));
+ $this->assertEquals(0, PersonQuery::create()->countFriendsOf($phil));
+ }
+
+ public function testFindFriendsOf()
+ {
+ $john = new Person();
+ $john->setName('john');
+ $john->save();
+
+ $jean = new Person();
+ $jean->setName('jean');
+ $jean->save();
+
+ $phil = new Person();
+ $phil->setName('phil');
+ $phil->save();
+
+ $this->assertEquals(0, PersonQuery::create()->findFriendsOf($phil)->count());
+ $this->assertEquals(0, PersonQuery::create()->findFriendsOf($jean)->count());
+ $this->assertEquals(0, PersonQuery::create()->findFriendsOf($john)->count());
+
+ $jean->addFriend($phil);
+ $this->assertEquals(0, PersonQuery::create()->findFriendsOf($phil)->count());
+ $this->assertEquals(0, PersonQuery::create()->findFriendsOf($jean)->count());
+
+ $jean->save();
+ $this->assertEquals(1, PersonQuery::create()->findFriendsOf($phil)->count());
+ $this->assertEquals(1, PersonQuery::create()->findFriendsOf($jean)->count());
+
+ $coll = PersonQuery::create()->findFriendsOf($phil);
+ $this->assertInstanceOf('PropelObjectCollection', $coll);
+ $this->assertInstanceOf('Person', $coll[0]);
+ $this->assertEquals('jean', $coll[0]->getName());
+
+ $coll = PersonQuery::create()->findFriendsOf($jean);
+ $this->assertInstanceOf('PropelObjectCollection', $coll);
+ $this->assertInstanceOf('Person', $coll[0]);
+ $this->assertEquals('phil', $coll[0]->getName());
+
+ $jean->removeFriends();
+ $jean->save();
+
+ $this->assertEquals(0, FriendQuery::create()->count());
+
+ $this->assertEquals(0, PersonQuery::create()->findFriendsOf($phil)->count());
+ $this->assertEquals(0, PersonQuery::create()->findFriendsOf($jean)->count());
+ }
}

0 comments on commit bdc9ff6

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