123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- <?php
- namespace Gedmo\Tree;
- use Doctrine\Common\EventManager;
- use Tool\BaseTestCaseORM;
- use Doctrine\Common\Util\Debug;
- use Tree\Fixture\Category;
- use Tree\Fixture\CategoryUuid;
- /**
- * These are tests for Tree behavior
- *
- * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
- * @link http://www.gediminasm.org
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
- class TreeTest extends BaseTestCaseORM
- {
- const CATEGORY = "Tree\\Fixture\\Category";
- const CATEGORY_UUID = "Tree\\Fixture\\CategoryUuid";
- protected function setUp()
- {
- parent::setUp();
- $evm = new EventManager;
- $evm->addEventSubscriber(new TreeListener);
- $this->getMockSqliteEntityManager($evm);
- }
- public function testTheTree()
- {
- $meta = $this->em->getClassMetadata(self::CATEGORY);
- $root = new Category();
- $root->setTitle("Root");
- $this->assertTrue($root instanceof Node);
- $this->em->persist($root);
- $this->em->flush();
- $this->em->clear();
- $root = $this->em->getRepository(self::CATEGORY)->find(1);
- $left = $meta->getReflectionProperty('lft')->getValue($root);
- $right = $meta->getReflectionProperty('rgt')->getValue($root);
- $this->assertEquals(1, $left);
- $this->assertEquals(2, $right);
- $child = new Category();
- $child->setTitle("child");
- $child->setParent($root);
- $this->em->persist($child);
- $this->em->flush();
- $this->em->clear();
- $root = $this->em->getRepository(self::CATEGORY)->find(1);
- $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)->find(2);
- $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 Category();
- $child2->setTitle("child2");
- $child2->setParent($root);
- $this->em->persist($child2);
- $this->em->flush();
- $this->em->clear();
- $root = $this->em->getRepository(self::CATEGORY)->find(1);
- $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)->find(3);
- $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 Category();
- $childsChild->setTitle("childs2_child");
- $childsChild->setParent($child2);
- $this->em->persist($childsChild);
- $this->em->flush();
- $this->em->clear();
- $child2 = $this->em->getRepository(self::CATEGORY)->find(3);
- $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)->find(4);
- $child = $this->em->getRepository(self::CATEGORY)->find(2);
- $childsChild->setTitle('childs_child');
- $childsChild->setParent($child);
- $this->em->persist($childsChild);
- $this->em->flush();
- $this->em->clear();
- $child = $this->em->getRepository(self::CATEGORY)->find(2);
- $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)->find(1);
- $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 Category();
- $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);
- }
- public function testIssue33()
- {
- $repo = $this->em->getRepository(self::CATEGORY);
- $root = new Category;
- $root->setTitle('root');
- $node1 = new Category;
- $node1->setTitle('node1');
- $node1->setParent($root);
- $node2 = new Category;
- $node2->setTitle('node2');
- $node2->setParent($root);
- $subNode = new Category;
- $subNode->setTitle('sub-node');
- $subNode->setParent($node2);
- $this->em->persist($root);
- $this->em->persist($node1);
- $this->em->persist($node2);
- $this->em->persist($subNode);
- $this->em->flush();
- $this->em->clear();
- $subNode = $repo->findOneByTitle('sub-node');
- $node1 = $repo->findOneByTitle('node1');
- $subNode->setParent($node1);
- $this->em->persist($subNode);
- $this->em->flush();
- $this->em->clear();
- $meta = $this->em->getClassMetadata(self::CATEGORY);
- $subNode = $repo->findOneByTitle('sub-node');
- $left = $meta->getReflectionProperty('lft')->getValue($subNode);
- $right = $meta->getReflectionProperty('rgt')->getValue($subNode);
- $this->assertEquals(3, $left);
- $this->assertEquals(4, $right);
- $node1 = $repo->findOneByTitle('node1');
- $left = $meta->getReflectionProperty('lft')->getValue($node1);
- $right = $meta->getReflectionProperty('rgt')->getValue($node1);
- $this->assertEquals(2, $left);
- $this->assertEquals(5, $right);
- }
- 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);
- }
- protected function getUsedEntityFixtures()
- {
- return array(
- self::CATEGORY,
- self::CATEGORY_UUID,
- );
- }
- }
|