| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 | <?php
namespace Gedmo\Tree;
use Doctrine\Common\EventManager;
use Tool\BaseTestCaseORM;
use Tree\Fixture\Category;
use Tree\Fixture\RootCategory;
/**
 * These are tests for Tree behavior
 *
 * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
 * @package Gedmo.Tree
 * @link http://www.gediminasm.org
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
class NestedTreePositionTest extends BaseTestCaseORM
{
    const CATEGORY = "Tree\\Fixture\\Category";
    const ROOT_CATEGORY = "Tree\\Fixture\\RootCategory";
    protected function setUp()
    {
        parent::setUp();
        $evm = new EventManager;
        $evm->addEventSubscriber(new TreeListener);
        $this->getMockSqliteEntityManager($evm);
    }
    /**
    * @test
    * @expectedException UnexpectedValueException
    */
    function shouldFailToPersistRootSibling()
    {
        $food = new Category;
        $food->setTitle('Food');
        $sport = new Category;
        $sport->setTitle('Sport');
        $repo = $this->em->getRepository(self::CATEGORY);
        $repo->persistAsFirstChild($food);
        $repo->persistAsNextSiblingOf($sport, $food);
        $this->em->flush();
    }
    /**
     * @test
     * @expectedException UnexpectedValueException
     */
    function shouldFailToPersistRootAsSiblingForRootBasedTree()
    {
        $food = new RootCategory;
        $food->setTitle('Food');
        $sport = new RootCategory;
        $sport->setTitle('Sport');
        $repo = $this->em->getRepository(self::ROOT_CATEGORY);
        $repo->persistAsFirstChild($food);
        $repo->persistAsNextSiblingOf($sport, $food);
        $this->em->flush();
    }
    public function testTreeChildPositionMove2()
    {
        $this->populate();
        $repo = $this->em->getRepository(self::ROOT_CATEGORY);
        $oranges = $repo->findOneByTitle('Oranges');
        $meat = $repo->findOneByTitle('Meat');
        $this->assertEquals(2, $oranges->getLevel());
        $this->assertEquals(7, $oranges->getLeft());
        $this->assertEquals(8, $oranges->getRight());
        $repo->persistAsNextSiblingOf($meat, $oranges);
        $this->em->flush();
        $oranges = $repo->findOneByTitle('Oranges');
        $meat = $repo->findOneByTitle('Meat');
        $this->assertEquals(7, $oranges->getLeft());
        $this->assertEquals(8, $oranges->getRight());
        //Normal test that pass
        $this->assertEquals(9, $meat->getLeft());
        $this->assertEquals(10, $meat->getRight());
        // Raw query to show the issue #108 with wrong left value by Doctrine
        $dql = 'SELECT c FROM ' . self::ROOT_CATEGORY . ' c';
        $dql .= ' WHERE c.id = 5'; //5 == meat
        $meat_array = $this->em->createQuery($dql)->getScalarResult();
        $this->assertEquals(9, $meat_array[0]['c_lft']);
        $this->assertEquals(10, $meat_array[0]['c_rgt']);
        $this->assertEquals(2, $meat_array[0]['c_level']);
    }
    public function testTreeChildPositionMove3()
    {
        $this->populate();
        $repo = $this->em->getRepository(self::ROOT_CATEGORY);
        $oranges = $repo->findOneByTitle('Oranges');
        $milk = $repo->findOneByTitle('Milk');
        $this->assertEquals(2, $oranges->getLevel());
        $this->assertEquals(7, $oranges->getLeft());
        $this->assertEquals(8, $oranges->getRight());
        $repo->persistAsNextSiblingOf($milk, $oranges);
        $this->em->flush();
        $this->assertEquals(7, $oranges->getLeft());
        $this->assertEquals(8, $oranges->getRight());
        //Normal test that pass
        $this->assertEquals(9, $milk->getLeft());
        $this->assertEquals(10, $milk->getRight());
        // Raw query to show the issue #108 with wrong left value by Doctrine
        $dql = 'SELECT c FROM ' . self::ROOT_CATEGORY . ' c';
        $dql .= ' WHERE c.id = 4 '; //4 == Milk
        $milk_array = $this->em->createQuery($dql)->getScalarResult();
        $this->assertEquals(9, $milk_array[0]['c_lft']);
        $this->assertEquals(10, $milk_array[0]['c_rgt']);
        $this->assertEquals(2, $milk_array[0]['c_level']);
    }
    public function testPositionedUpdates()
    {
        $this->populate();
        $repo = $this->em->getRepository(self::ROOT_CATEGORY);
        $citrons = $repo->findOneByTitle('Citrons');
        $vegitables = $repo->findOneByTitle('Vegitables');
        $repo->persistAsNextSiblingOf($vegitables, $citrons);
        $this->em->flush();
        $this->assertEquals(5, $vegitables->getLeft());
        $this->assertEquals(6, $vegitables->getRight());
        $this->assertEquals(2, $vegitables->getParent()->getId());
        $fruits = $repo->findOneByTitle('Fruits');
        $this->assertEquals(2, $fruits->getLeft());
        $this->assertEquals(9, $fruits->getRight());
        $milk = $repo->findOneByTitle('Milk');
        $repo->persistAsFirstChildOf($milk, $fruits);
        $this->em->flush();
        $this->assertEquals(3, $milk->getLeft());
        $this->assertEquals(4, $milk->getRight());
        $this->assertEquals(2, $fruits->getLeft());
        $this->assertEquals(11, $fruits->getRight());
    }
    public function testTreeChildPositionMove()
    {
        $this->populate();
        $repo = $this->em->getRepository(self::ROOT_CATEGORY);
        $oranges = $repo->findOneByTitle('Oranges');
        $fruits = $repo->findOneByTitle('Fruits');
        $this->assertEquals(2, $oranges->getLevel());
        $repo->persistAsNextSiblingOf($oranges, $fruits);
        $this->em->flush();
        $this->assertEquals(1, $oranges->getLevel());
        $this->assertCount(1, $repo->children($fruits, true));
        $vegies = $repo->findOneByTitle('Vegitables');
        $this->assertEquals(2, $vegies->getLeft());
        $repo->persistAsNextSiblingOf($vegies, $fruits);
        $this->em->flush();
        $this->assertEquals(6, $vegies->getLeft());
        $this->em->flush();
        $this->assertEquals(6, $vegies->getLeft());
    }
    public function testOnRootCategory()
    {
        // need to check if this does not produce errors
        $repo = $this->em->getRepository(self::ROOT_CATEGORY);
        $fruits = new RootCategory;
        $fruits->setTitle('Fruits');
        $vegitables = new RootCategory;
        $vegitables->setTitle('Vegitables');
        $milk = new RootCategory;
        $milk->setTitle('Milk');
        $meat = new RootCategory;
        $meat->setTitle('Meat');
        $repo
            ->persistAsFirstChild($fruits)
            ->persistAsFirstChild($vegitables)
            ->persistAsLastChild($milk)
            ->persistAsLastChild($meat);
        $cookies = new RootCategory;
        $cookies->setTitle('Cookies');
        $drinks = new RootCategory;
        $drinks->setTitle('Drinks');
        $repo
            ->persistAsNextSibling($cookies)
            ->persistAsPrevSibling($drinks);
        $this->em->flush();
        $dql = 'SELECT COUNT(c) FROM ' . self::ROOT_CATEGORY . ' c';
        $dql .= ' WHERE c.lft = 1 AND c.rgt = 2 AND c.parent IS NULL AND c.level = 0';
        $count = $this->em->createQuery($dql)->getSingleScalarResult();
        $this->assertEquals(6, $count);
        $repo = $this->em->getRepository(self::CATEGORY);
        $fruits = new Category;
        $fruits->setTitle('Fruits');
        $vegitables = new Category;
        $vegitables->setTitle('Vegitables');
        $milk = new Category;
        $milk->setTitle('Milk');
        $meat = new Category;
        $meat->setTitle('Meat');
        $repo
            ->persistAsFirstChild($fruits)
            ->persistAsFirstChild($vegitables)
            ->persistAsLastChild($milk)
            ->persistAsLastChild($meat);
        $cookies = new Category;
        $cookies->setTitle('Cookies');
        $drinks = new Category;
        $drinks->setTitle('Drinks');
        $repo
            ->persistAsNextSibling($cookies)
            ->persistAsPrevSibling($drinks);
        $this->em->flush();
        $dql = 'SELECT COUNT(c) FROM ' . self::CATEGORY . ' c';
        $dql .= ' WHERE c.parentId IS NULL AND c.level = 0';
        $dql .= ' AND c.lft BETWEEN 1 AND 11';
        $count = $this->em->createQuery($dql)->getSingleScalarResult();
        $this->assertEquals(6, $count);
    }
    public function testRootTreePositionedInserts()
    {
        $repo = $this->em->getRepository(self::ROOT_CATEGORY);
        // test child positioned inserts
        $food = new RootCategory;
        $food->setTitle('Food');
        $fruits = new RootCategory;
        $fruits->setTitle('Fruits');
        $vegitables = new RootCategory;
        $vegitables->setTitle('Vegitables');
        $milk = new RootCategory;
        $milk->setTitle('Milk');
        $meat = new RootCategory;
        $meat->setTitle('Meat');
        $repo
            ->persistAsFirstChild($food)
            ->persistAsFirstChildOf($fruits, $food)
            ->persistAsFirstChildOf($vegitables, $food)
            ->persistAsLastChildOf($milk, $food)
            ->persistAsLastChildOf($meat, $food);
        $this->em->flush();
        $this->assertEquals(4, $fruits->getLeft());
        $this->assertEquals(5, $fruits->getRight());
        $this->assertEquals(2, $vegitables->getLeft());
        $this->assertEquals(3, $vegitables->getRight());
        $this->assertEquals(6, $milk->getLeft());
        $this->assertEquals(7, $milk->getRight());
        $this->assertEquals(8, $meat->getLeft());
        $this->assertEquals(9, $meat->getRight());
        // test sibling positioned inserts
        $cookies = new RootCategory;
        $cookies->setTitle('Cookies');
        $drinks = new RootCategory;
        $drinks->setTitle('Drinks');
        $repo
            ->persistAsNextSiblingOf($cookies, $milk)
            ->persistAsPrevSiblingOf($drinks, $milk);
        $this->em->flush();
        $this->assertEquals(6, $drinks->getLeft());
        $this->assertEquals(7, $drinks->getRight());
        $this->assertEquals(10, $cookies->getLeft());
        $this->assertEquals(11, $cookies->getRight());
        $this->assertTrue($repo->verify());
    }
    public function testSimpleTreePositionedInserts()
    {
        $repo = $this->em->getRepository(self::CATEGORY);
        // test child positioned inserts
        $food = new Category;
        $food->setTitle('Food');
        $repo->persistAsFirstChild($food);
        $fruits = new Category;
        $fruits->setTitle('Fruits');
        $fruits->setParent($food);
        $repo->persistAsFirstChild($fruits);
        $vegitables = new Category;
        $vegitables->setTitle('Vegitables');
        $vegitables->setParent($food);
        $repo->persistAsFirstChild($vegitables);
        $milk = new Category;
        $milk->setTitle('Milk');
        $milk->setParent($food);
        $repo->persistAsLastChild($milk);
        $meat = new Category;
        $meat->setTitle('Meat');
        $meat->setParent($food);
        $repo->persistAsLastChild($meat);
        $this->em->flush();
        $this->assertEquals(4, $fruits->getLeft());
        $this->assertEquals(5, $fruits->getRight());
        $this->assertEquals(2, $vegitables->getLeft());
        $this->assertEquals(3, $vegitables->getRight());
        $this->assertEquals(6, $milk->getLeft());
        $this->assertEquals(7, $milk->getRight());
        $this->assertEquals(8, $meat->getLeft());
        $this->assertEquals(9, $meat->getRight());
        // test sibling positioned inserts
        $cookies = new Category;
        $cookies->setTitle('Cookies');
        $cookies->setParent($milk);
        $repo->persistAsNextSibling($cookies);
        $drinks = new Category;
        $drinks->setTitle('Drinks');
        $drinks->setParent($milk);
        $repo->persistAsPrevSibling($drinks);
        $this->em->flush();
        $this->assertEquals(6, $drinks->getLeft());
        $this->assertEquals(7, $drinks->getRight());
        $this->assertEquals(10, $cookies->getLeft());
        $this->assertEquals(11, $cookies->getRight());
        $this->assertTrue($repo->verify());
    }
    private function populate()
    {
        $repo = $this->em->getRepository(self::ROOT_CATEGORY);
        $food = new RootCategory;
        $food->setTitle('Food');
        $fruits = new RootCategory;
        $fruits->setTitle('Fruits');
        $vegitables = new RootCategory;
        $vegitables->setTitle('Vegitables');
        $milk = new RootCategory;
        $milk->setTitle('Milk');
        $meat = new RootCategory;
        $meat->setTitle('Meat');
        $oranges = new RootCategory;
        $oranges->setTitle('Oranges');
        $citrons = new RootCategory;
        $citrons->setTitle('Citrons');
        $repo
            ->persistAsFirstChild($food)
            ->persistAsFirstChildOf($fruits, $food)
            ->persistAsFirstChildOf($vegitables, $food)
            ->persistAsLastChildOf($milk, $food)
            ->persistAsLastChildOf($meat, $food)
            ->persistAsFirstChildOf($oranges, $fruits)
            ->persistAsFirstChildOf($citrons, $fruits);
        $this->em->flush();
    }
    protected function getUsedEntityFixtures()
    {
        return array(
            self::CATEGORY,
            self::ROOT_CATEGORY
        );
    }
}
 |