Author: ornicar2
Date: 2010-02-12 15:04:11 +0100 (Fri, 12 Feb 2010)
New Revision: 27941

Modified:
   plugins/diemPlugin/trunk/dmCorePlugin/lib/menu/dmMenu.php
   plugins/diemPlugin/trunk/dmCorePlugin/test/unit/dmMenuTest.php
Log:
[Diem]
- added dmMenu->moveToFirst() & dmMenu->moveToLast() methods
- added unit tests for dmMenu

Modified: plugins/diemPlugin/trunk/dmCorePlugin/lib/menu/dmMenu.php
===================================================================
--- plugins/diemPlugin/trunk/dmCorePlugin/lib/menu/dmMenu.php   2010-02-12 
13:31:30 UTC (rev 27940)
+++ plugins/diemPlugin/trunk/dmCorePlugin/lib/menu/dmMenu.php   2010-02-12 
14:04:11 UTC (rev 27941)
@@ -154,6 +154,9 @@
     return null === $this->label ? $this->getName() : $this->label;
   }
 
+  /**
+   * @return dmBaseLinkTag the link tag
+   */
   public function getLink()
   {
     if(!$this->link instanceof dmBaseLinkTag && !empty($this->link))
@@ -164,6 +167,9 @@
     return $this->link;
   }
 
+  /**
+   * @return dmMenu the parent menu
+   */
   public function end()
   {
     return $this->parent;
@@ -179,12 +185,18 @@
     return $this->level;
   }
 
+  /**
+   * @return dmMenu the root menu
+   */
   public function getRoot()
   {
     return $this->parent ? $this->parent->getRoot() : $this;
   }
 
-  public function getParent(dmMenu $parent = null)
+  /**
+   * @return dmMenu the parent menu
+   */
+  public function getParent()
   {
     return $this->parent;
   }
@@ -224,6 +236,25 @@
     return $this->children[$name];
   }
 
+  public function getSiblings($includeMe = false)
+  {
+    if(!$this->getParent())
+    {
+      throw new dmException('Root menu has no siblings');
+    }
+
+    $siblings = array();
+    foreach($this->getParent()->getChildren() as $key => $child)
+    {
+      if($includeMe || $child != $this)
+      {
+        $siblings[$key] = $child;
+      }
+    }
+
+    return $siblings;
+  }
+
   /**
    * Checks
    */
@@ -266,6 +297,36 @@
   }
 
   /**
+   * Move
+   */
+  public function moveToFirst()
+  {
+    $siblings = array($this->getName() => $this) + $this->getSiblings();
+
+    $this->getParent()->setChildren($siblings)->reAssignChildrenNums();
+
+    return $this;
+  }
+
+  public function moveToLast()
+  {
+    $siblings = $this->getSiblings() + array($this->getName() => $this);
+
+    $this->getParent()->setChildren($siblings)->reAssignChildrenNums();
+
+    return $this;
+  }
+
+  public function reAssignChildrenNums()
+  {
+    $it = 0;
+    foreach($this->getChildren() as $child)
+    {
+      $child->num(++$it);
+    }
+  }
+
+  /**
    * Manipulation
    */
 
@@ -305,6 +366,13 @@
     return $this;
   }
 
+  public function setChildren(array $children)
+  {
+    $this->children = $children;
+
+    return $this;
+  }
+
   /**
    * Recursively add children based on the page structure
    */

Modified: plugins/diemPlugin/trunk/dmCorePlugin/test/unit/dmMenuTest.php
===================================================================
--- plugins/diemPlugin/trunk/dmCorePlugin/test/unit/dmMenuTest.php      
2010-02-12 13:31:30 UTC (rev 27940)
+++ plugins/diemPlugin/trunk/dmCorePlugin/test/unit/dmMenuTest.php      
2010-02-12 14:04:11 UTC (rev 27941)
@@ -8,7 +8,7 @@
 $helper = new dmUnitTestHelper();
 $helper->boot('front');
 
-$t = new lime_test(46);
+$t = new lime_test();
 
 dm::loadHelpers(array('Dm', 'I18N'));
 
@@ -177,4 +177,49 @@
 $menu = $helper->get('menu')->addChild('Home', '@homepage')->end();
 $html = _tag('ul', _tag('li.first.last.dm_parent', 
_link()->text($helper->get('i18n')->__('Home'))));
 
-$t->is($menu->render(), $html, 'Parent li has the dm_parent class');
\ No newline at end of file
+$t->is($menu->render(), $html, 'Parent li has the dm_parent class');
+
+$menu = $helper->get('menu')
+->addChild('Home')->end()
+->addChild('Sites')
+  ->addChild('Diem')->end()
+  ->addChild('Symfony')->end()
+->end();
+
+$html = _tag('ul',
+  _tag('li.first', 'Home').
+  _tag('li.last',
+    'Sites'.
+    _tag('ul',
+      _tag('li.first', 'Diem').
+      _tag('li.last', 'Symfony')
+    )
+  )
+);
+
+$t->is($menu->render(), $html, $html);
+
+$t->comment('->getSiblings()');
+
+$t->is_deeply($menu['Home']->getSiblings(), array('Sites' => $menu['Sites']), 
'->getSiblings() works');
+
+$t->is_deeply($menu['Home']->getSiblings(true), array('Home' => $menu['Home'], 
'Sites' => $menu['Sites']), '->getSiblings(true) works');
+
+$t->comment('Move menus');
+
+$menu['Home']->moveToLast();
+
+$menu['Sites']['Symfony']->moveToFirst();
+
+$html = _tag('ul',
+  _tag('li.first',
+    'Sites'.
+    _tag('ul',
+      _tag('li.first', 'Symfony').
+      _tag('li.last', 'Diem')
+    )
+  ).
+  _tag('li.last', 'Home')
+);
+
+$t->is($menu->render(), $html, $html);
\ No newline at end of file

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.

Reply via email to