public function testIssue273() { $meta = $this->em->getClassMetadata(self::CATEGORY_UUID); $root = new CategoryUuid(); $root->setTitle("Root"); $this->assertTrue($root instanceof Node); $this->em->persist($root); $rootId = $root->getId(); $this->em->flush(); $this->em->clear(); $root = $this->em->getRepository(self::CATEGORY_UUID)->find($rootId); $left = $meta->getReflectionProperty('lft')->getValue($root); $right = $meta->getReflectionProperty('rgt')->getValue($root); $this->assertEquals(1, $left); $this->assertEquals(2, $right); $child = new CategoryUuid(); $child->setTitle("child"); $child->setParent($root); $this->em->persist($child); $childId = $child->getId(); $this->em->flush(); $this->em->clear(); $root = $this->em->getRepository(self::CATEGORY_UUID)->find($rootId); $left = $meta->getReflectionProperty('lft')->getValue($root); $right = $meta->getReflectionProperty('rgt')->getValue($root); $level = $meta->getReflectionProperty('level')->getValue($root); $this->assertEquals(1, $left); $this->assertEquals(4, $right); $this->assertEquals(0, $level); $child = $this->em->getRepository(self::CATEGORY_UUID)->find($childId); $left = $meta->getReflectionProperty('lft')->getValue($child); $right = $meta->getReflectionProperty('rgt')->getValue($child); $level = $meta->getReflectionProperty('level')->getValue($child); $this->assertEquals(2, $left); $this->assertEquals(3, $right); $this->assertEquals(1, $level); $child2 = new CategoryUuid(); $child2->setTitle("child2"); $child2->setParent($root); $this->em->persist($child2); $child2Id = $child2->getId(); $this->em->flush(); $this->em->clear(); $root = $this->em->getRepository(self::CATEGORY_UUID)->find($rootId); $left = $meta->getReflectionProperty('lft')->getValue($root); $right = $meta->getReflectionProperty('rgt')->getValue($root); $level = $meta->getReflectionProperty('level')->getValue($root); $this->assertEquals(1, $left); $this->assertEquals(6, $right); $this->assertEquals(0, $level); $child2 = $this->em->getRepository(self::CATEGORY_UUID)->find($child2Id); $left = $meta->getReflectionProperty('lft')->getValue($child2); $right = $meta->getReflectionProperty('rgt')->getValue($child2); $level = $meta->getReflectionProperty('level')->getValue($child2); $this->assertEquals(4, $left); $this->assertEquals(5, $right); $this->assertEquals(1, $level); $childsChild = new CategoryUuid(); $childsChild->setTitle("childs2_child"); $childsChild->setParent($child2); $this->em->persist($childsChild); $childsChildId = $childsChild->getId(); $this->em->flush(); $this->em->clear(); $child2 = $this->em->getRepository(self::CATEGORY_UUID)->find($child2Id); $left = $meta->getReflectionProperty('lft')->getValue($child2); $right = $meta->getReflectionProperty('rgt')->getValue($child2); $level = $meta->getReflectionProperty('level')->getValue($child2); $this->assertEquals(4, $left); $this->assertEquals(7, $right); $this->assertEquals(1, $level); $level = $meta->getReflectionProperty('level')->getValue($childsChild); $this->assertEquals(2, $level); // test updates to nodes, parent changes $childsChild = $this->em->getRepository(self::CATEGORY_UUID)->find($childsChildId); $child = $this->em->getRepository(self::CATEGORY_UUID)->find($childId); $childsChild->setTitle('childs_child'); $childsChild->setParent($child); $this->em->persist($childsChild); $this->em->flush(); $this->em->clear(); $child = $this->em->getRepository(self::CATEGORY_UUID)->find($childId); $left = $meta->getReflectionProperty('lft')->getValue($child); $right = $meta->getReflectionProperty('rgt')->getValue($child); $level = $meta->getReflectionProperty('level')->getValue($child); $this->assertEquals(2, $left); $this->assertEquals(5, $right); $this->assertEquals(1, $level); // test deletion $this->em->remove($child); $this->em->flush(); $this->em->clear(); $root = $this->em->getRepository(self::CATEGORY_UUID)->find($rootId); $left = $meta->getReflectionProperty('lft')->getValue($root); $right = $meta->getReflectionProperty('rgt')->getValue($root); $this->assertEquals(1, $left); $this->assertEquals(4, $right); // test persisting in any time $yetAnotherChild = new CategoryUuid(); $this->em->persist($yetAnotherChild); $yetAnotherChild->setTitle("yetanotherchild"); $yetAnotherChild->setParent($root); //$this->em->persist($yetAnotherChild); $this->em->flush(); $this->em->clear(); $left = $meta->getReflectionProperty('lft')->getValue($yetAnotherChild); $right = $meta->getReflectionProperty('rgt')->getValue($yetAnotherChild); $level = $meta->getReflectionProperty('level')->getValue($yetAnotherChild); $this->assertEquals(4, $left); $this->assertEquals(5, $right); $this->assertEquals(1, $level); }
private function populateUuid() { $root = new CategoryUuid(); $root->setTitle("Food"); $root2 = new CategoryUuid(); $root2->setTitle("Sports"); $child = new CategoryUuid(); $child->setTitle("Fruits"); $child->setParent($root); $child2 = new CategoryUuid(); $child2->setTitle("Vegitables"); $child2->setParent($root); $childsChild = new CategoryUuid(); $childsChild->setTitle("Carrots"); $childsChild->setParent($child2); $potatoes = new CategoryUuid(); $potatoes->setTitle("Potatoes"); $potatoes->setParent($child2); $this->em->persist($root); $this->em->persist($root2); $this->em->persist($child); $this->em->persist($child2); $this->em->persist($childsChild); $this->em->persist($potatoes); $this->em->flush(); $this->em->clear(); }