123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511 |
- <?php
- namespace Gedmo\Tree;
- use Doctrine\Common\EventManager;
- use Tool\BaseTestCaseORM;
- use Doctrine\Common\Util\Debug,
- Tree\Fixture\Category,
- 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 RepositoryTest 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);
- $this->populate();
- }
- public function testBasicFunctions()
- {
- $vegies = $this->em->getRepository(self::CATEGORY)
- ->findOneByTitle('Vegitables');
- $food = $this->em->getRepository(self::CATEGORY)
- ->findOneByTitle('Food');
- // test childCount
- $childCount = $this->em->getRepository(self::CATEGORY)
- ->childCount($vegies);
- $this->assertEquals(2, $childCount);
- $childCount = $this->em->getRepository(self::CATEGORY)
- ->childCount($food);
- $this->assertEquals(4, $childCount);
- $childCount = $this->em->getRepository(self::CATEGORY)
- ->childCount($food, true);
- $this->assertEquals(2, $childCount);
- $childCount = $this->em->getRepository(self::CATEGORY)
- ->childCount();
- $this->assertEquals(6, $childCount);
- // test children
- $children = $this->em->getRepository(self::CATEGORY)
- ->children($vegies);
- $this->assertCount(2, $children);
- $this->assertEquals('Carrots', $children[0]->getTitle());
- $this->assertEquals('Potatoes', $children[1]->getTitle());
- $children = $this->em->getRepository(self::CATEGORY)
- ->children($food);
- $this->assertCount(4, $children);
- $this->assertEquals('Fruits', $children[0]->getTitle());
- $this->assertEquals('Vegitables', $children[1]->getTitle());
- $this->assertEquals('Carrots', $children[2]->getTitle());
- $this->assertEquals('Potatoes', $children[3]->getTitle());
- $children = $this->em->getRepository(self::CATEGORY)
- ->children($food, true);
- $this->assertCount(2, $children);
- $this->assertEquals('Fruits', $children[0]->getTitle());
- $this->assertEquals('Vegitables', $children[1]->getTitle());
- $children = $this->em->getRepository(self::CATEGORY)
- ->children();
- $this->assertCount(6, $children);
- // path
- $path = $this->em->getRepository(self::CATEGORY)
- ->getPath($vegies);
- $this->assertCount(2, $path);
- $this->assertEquals('Food', $path[0]->getTitle());
- $this->assertEquals('Vegitables', $path[1]->getTitle());
- $carrots = $this->em->getRepository(self::CATEGORY)
- ->findOneByTitle('Carrots');
- $path = $this->em->getRepository(self::CATEGORY)
- ->getPath($carrots);
- $this->assertCount(3, $path);
- $this->assertEquals('Food', $path[0]->getTitle());
- $this->assertEquals('Vegitables', $path[1]->getTitle());
- $this->assertEquals('Carrots', $path[2]->getTitle());
- // leafs
- $leafs = $this->em->getRepository(self::CATEGORY)
- ->getLeafs();
- $this->assertCount(4, $leafs);
- $this->assertEquals('Fruits', $leafs[0]->getTitle());
- $this->assertEquals('Carrots', $leafs[1]->getTitle());
- $this->assertEquals('Potatoes', $leafs[2]->getTitle());
- $this->assertEquals('Sports', $leafs[3]->getTitle());
- }
- public function testAdvancedFunctions()
- {
- $this->populateMore();
- $onions = $this->em->getRepository(self::CATEGORY)
- ->findOneByTitle('Onions');
- $repo = $this->em->getRepository(self::CATEGORY);
- $meta = $this->em->getClassMetadata(self::CATEGORY);
- $left = $meta->getReflectionProperty('lft')->getValue($onions);
- $right = $meta->getReflectionProperty('rgt')->getValue($onions);
- $this->assertEquals(11, $left);
- $this->assertEquals(12, $right);
- // move up onions by one position
- $repo->moveUp($onions, 1);
- $left = $meta->getReflectionProperty('lft')->getValue($onions);
- $right = $meta->getReflectionProperty('rgt')->getValue($onions);
- $this->assertEquals(9, $left);
- $this->assertEquals(10, $right);
- // move down onions by one position
- $repo->moveDown($onions, 1);
- $left = $meta->getReflectionProperty('lft')->getValue($onions);
- $right = $meta->getReflectionProperty('rgt')->getValue($onions);
- $this->assertEquals(11, $left);
- $this->assertEquals(12, $right);
- // move to the up onions on this level
- $repo->moveUp($onions, true);
- $left = $meta->getReflectionProperty('lft')->getValue($onions);
- $right = $meta->getReflectionProperty('rgt')->getValue($onions);
- $this->assertEquals(5, $left);
- $this->assertEquals(6, $right);
- // test tree reordering
- // reorder tree by title
- $food = $repo->findOneByTitle('Food');
- $repo->reorder($food, 'title');
- $node = $this->em->getRepository(self::CATEGORY)
- ->findOneByTitle('Cabbages');
- $left = $meta->getReflectionProperty('lft')->getValue($node);
- $right = $meta->getReflectionProperty('rgt')->getValue($node);
- $this->assertEquals(5, $left);
- $this->assertEquals(6, $right);
- $node = $this->em->getRepository(self::CATEGORY)
- ->findOneByTitle('Carrots');
- $left = $meta->getReflectionProperty('lft')->getValue($node);
- $right = $meta->getReflectionProperty('rgt')->getValue($node);
- $this->assertEquals(7, $left);
- $this->assertEquals(8, $right);
- $node = $this->em->getRepository(self::CATEGORY)
- ->findOneByTitle('Onions');
- $left = $meta->getReflectionProperty('lft')->getValue($node);
- $right = $meta->getReflectionProperty('rgt')->getValue($node);
- $this->assertEquals(9, $left);
- $this->assertEquals(10, $right);
- $node = $this->em->getRepository(self::CATEGORY)
- ->findOneByTitle('Potatoes');
- $left = $meta->getReflectionProperty('lft')->getValue($node);
- $right = $meta->getReflectionProperty('rgt')->getValue($node);
- $this->assertEquals(11, $left);
- $this->assertEquals(12, $right);
- // test removal with reparenting
- $vegies = $this->em->getRepository(self::CATEGORY)
- ->findOneByTitle('Vegitables');
- $repo->removeFromTree($vegies);
- $this->em->clear(); // clear all cached nodes
- $vegies = $this->em->getRepository(self::CATEGORY)
- ->findOneByTitle('Vegitables');
- $this->assertNull($vegies);
- $node = $this->em->getRepository(self::CATEGORY)
- ->findOneByTitle('Fruits');
- $left = $meta->getReflectionProperty('lft')->getValue($node);
- $right = $meta->getReflectionProperty('rgt')->getValue($node);
- $this->assertEquals(2, $left);
- $this->assertEquals(3, $right);
- $this->assertEquals('Food', $node->getParent()->getTitle());
- $node = $this->em->getRepository(self::CATEGORY)
- ->findOneByTitle('Cabbages');
- $left = $meta->getReflectionProperty('lft')->getValue($node);
- $right = $meta->getReflectionProperty('rgt')->getValue($node);
- $this->assertEquals(4, $left);
- $this->assertEquals(5, $right);
- $this->assertEquals('Food', $node->getParent()->getTitle());
- }
- public function testRootRemoval()
- {
- $repo = $this->em->getRepository(self::CATEGORY);
- $meta = $this->em->getClassMetadata(self::CATEGORY);
- $this->populateMore();
- $food = $repo->findOneByTitle('Food');
- $repo->removeFromTree($food);
- $this->em->clear();
- $food = $repo->findOneByTitle('Food');
- $this->assertNull($food);
- $node = $repo->findOneByTitle('Fruits');
- $left = $meta->getReflectionProperty('lft')->getValue($node);
- $right = $meta->getReflectionProperty('rgt')->getValue($node);
- $this->assertEquals(1, $left);
- $this->assertEquals(2, $right);
- $this->assertNull($node->getParent());
- $node = $repo->findOneByTitle('Vegitables');
- $left = $meta->getReflectionProperty('lft')->getValue($node);
- $right = $meta->getReflectionProperty('rgt')->getValue($node);
- $this->assertEquals(3, $left);
- $this->assertEquals(12, $right);
- $this->assertNull($node->getParent());
- }
- public function testVerificationAndRecover()
- {
- $repo = $this->em->getRepository(self::CATEGORY);
- $meta = $this->em->getClassMetadata(self::CATEGORY);
- $this->populateMore();
- // test verification of tree
- $this->assertTrue($repo->verify());
- // now lets brake something
- $dql = 'UPDATE ' . self::CATEGORY . ' node';
- $dql .= ' SET node.lft = 1';
- $dql .= ' WHERE node.id = 8';
- $q = $this->em->createQuery($dql);
- $q->getSingleScalarResult();
- $this->em->clear(); // must clear cached entities
- // verify again
- $result = $repo->verify();
- $this->assertTrue(is_array($result));
- $this->assertArrayHasKey(0, $result);
- $this->assertArrayHasKey(1, $result);
- $this->assertArrayHasKey(2, $result);
- $duplicate = $result[0];
- $missing = $result[1];
- $invalidLeft = $result[2];
- $this->assertEquals('index [1], duplicate', $duplicate);
- $this->assertEquals('index [11], missing', $missing);
- $this->assertEquals('node [8] left is less than parent`s [4] left value', $invalidLeft);
- // test recover functionality
- $repo->recover();
- $this->em->flush();
- $this->assertTrue($repo->verify());
- }
- public function testMoveRootNode()
- {
- $repo = $this->em->getRepository(self::CATEGORY);
- $food = $repo->findOneByTitle('Food');
- $repo->moveDown($food, 1);
- $meta = $this->em->getClassMetadata(self::CATEGORY);
- $left = $meta->getReflectionProperty('lft')->getValue($food);
- $right = $meta->getReflectionProperty('rgt')->getValue($food);
- $this->assertEquals(3, $left);
- $this->assertEquals(12, $right);
- $this->assertNull($food->getParent());
- $this->assertTrue($repo->verify());
- }
- public function testIssue273()
- {
- $this->populateUuid();
- $vegies = $this->em->getRepository(self::CATEGORY_UUID)
- ->findOneByTitle('Vegitables');
- $food = $this->em->getRepository(self::CATEGORY_UUID)
- ->findOneByTitle('Food');
- // test childCount
- $childCount = $this->em->getRepository(self::CATEGORY_UUID)
- ->childCount($vegies);
- $this->assertEquals(2, $childCount);
- $childCount = $this->em->getRepository(self::CATEGORY_UUID)
- ->childCount($food);
- $this->assertEquals(4, $childCount);
- $childCount = $this->em->getRepository(self::CATEGORY_UUID)
- ->childCount($food, true);
- $this->assertEquals(2, $childCount);
- $childCount = $this->em->getRepository(self::CATEGORY_UUID)
- ->childCount();
- $this->assertEquals(6, $childCount);
- // test children
- $children = $this->em->getRepository(self::CATEGORY_UUID)
- ->children($vegies);
- $this->assertCount(2, $children);
- $this->assertEquals('Carrots', $children[0]->getTitle());
- $this->assertEquals('Potatoes', $children[1]->getTitle());
- $children = $this->em->getRepository(self::CATEGORY_UUID)
- ->children($food);
- $this->assertCount(4, $children);
- $this->assertEquals('Fruits', $children[0]->getTitle());
- $this->assertEquals('Vegitables', $children[1]->getTitle());
- $this->assertEquals('Carrots', $children[2]->getTitle());
- $this->assertEquals('Potatoes', $children[3]->getTitle());
- $children = $this->em->getRepository(self::CATEGORY_UUID)
- ->children($food, true);
- $this->assertCount(2, $children);
- $this->assertEquals('Fruits', $children[0]->getTitle());
- $this->assertEquals('Vegitables', $children[1]->getTitle());
- $children = $this->em->getRepository(self::CATEGORY_UUID)
- ->children();
- $this->assertCount(6, $children);
- // path
- $path = $this->em->getRepository(self::CATEGORY_UUID)
- ->getPath($vegies);
- $this->assertCount(2, $path);
- $this->assertEquals('Food', $path[0]->getTitle());
- $this->assertEquals('Vegitables', $path[1]->getTitle());
- $carrots = $this->em->getRepository(self::CATEGORY_UUID)
- ->findOneByTitle('Carrots');
- $path = $this->em->getRepository(self::CATEGORY_UUID)
- ->getPath($carrots);
- $this->assertCount(3, $path);
- $this->assertEquals('Food', $path[0]->getTitle());
- $this->assertEquals('Vegitables', $path[1]->getTitle());
- $this->assertEquals('Carrots', $path[2]->getTitle());
- // leafs
- $leafs = $this->em->getRepository(self::CATEGORY_UUID)
- ->getLeafs($path[0]);
- $this->assertCount(3, $leafs);
- $this->assertEquals('Fruits', $leafs[0]->getTitle());
- $this->assertEquals('Carrots', $leafs[1]->getTitle());
- $this->assertEquals('Potatoes', $leafs[2]->getTitle());
- }
- protected function getUsedEntityFixtures()
- {
- return array(
- self::CATEGORY,
- self::CATEGORY_UUID,
- );
- }
- private function populateMore()
- {
- $vegies = $this->em->getRepository(self::CATEGORY)
- ->findOneByTitle('Vegitables');
- $cabbages = new Category();
- $cabbages->setParent($vegies);
- $cabbages->setTitle('Cabbages');
- $onions = new Category();
- $onions->setParent($vegies);
- $onions->setTitle('Onions');
- $this->em->persist($cabbages);
- $this->em->persist($onions);
- $this->em->flush();
- $this->em->clear();
- }
- private function populate()
- {
- $root = new Category();
- $root->setTitle("Food");
- $root2 = new Category();
- $root2->setTitle("Sports");
- $child = new Category();
- $child->setTitle("Fruits");
- $child->setParent($root);
- $child2 = new Category();
- $child2->setTitle("Vegitables");
- $child2->setParent($root);
- $childsChild = new Category();
- $childsChild->setTitle("Carrots");
- $childsChild->setParent($child2);
- $potatoes = new Category();
- $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();
- }
- 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();
- }
- }
|