/**
  * @param string $path
  * @param string|Position $position
  * @throws Exception\PathNotFoundException
  * @throws \InvalidArgumentException
  * @return int
  */
 protected function _handleOrder($path, $position)
 {
     if (is_string($position)) {
         $position = new Position($position);
     }
     switch ($position->getType()) {
         case Position::FIRST:
             $q = EntryQuery::create($this->entityType)->siblingsOf($path, true)->treeOrder();
             $this->_reorder($q, 2);
             return 1;
         case Position::BEFORE:
             if ($e = EntryPeer::retrieveByPath($this->entityType, $position->getPivot())) {
                 $orderNumber = $e->getOrderNumber();
                 $q = EntryQuery::create($this->entityType)->siblingsOf($path, true)->filterByOrderNumber($orderNumber, \Criteria::GREATER_EQUAL)->treeOrder();
                 $this->_reorder($q, $orderNumber + 1);
                 return $orderNumber;
             } else {
                 throw new PathNotFoundException($position->getPivot());
             }
         case Position::AFTER:
             if ($e = EntryPeer::retrieveByPath($this->entityType, $position->getPivot())) {
                 $orderNumber = $e->getOrderNumber() + 1;
                 $q = EntryQuery::create($this->entityType)->siblingsOf($path, true)->filterByOrderNumber($orderNumber, \Criteria::GREATER_EQUAL)->treeOrder();
                 $this->_reorder($q, $orderNumber + 1);
                 return $orderNumber;
             } else {
                 throw new PathNotFoundException($position->getPivot());
             }
         case Position::LAST:
             $max_taken = EntryQuery::create($this->entityType)->siblingsOf($path, true)->addAsColumn('max_taken', 'MAX(order_number)')->select(['max_taken'])->findOne();
             return $max_taken + 1;
         default:
             throw new \InvalidArgumentException('Unsupported position: ' . $position->getType());
     }
 }
 public function testMovePivot()
 {
     $x = ['/', '/home', '/home/io', '/home/zorro', '/home/ace', '/root', '/mount'];
     $tree = $this->createTreeManager();
     $docs = $this->createTree($tree, $x);
     $tree->move('/root', '/home/root', Position::before('/home/zorro'));
     $nodes = $tree->getChildren('/home');
     $paths = array_keys($nodes);
     $this->assertEquals(['/home/io/', '/home/root/', '/home/zorro/', '/home/ace/'], $paths);
     $tree->move('/home/root', '/home/root', Position::before('/home/ace'));
     $nodes = $tree->getChildren('/home');
     $paths = array_keys($nodes);
     $this->assertEquals(['/home/io/', '/home/zorro/', '/home/root/', '/home/ace/'], $paths);
 }