<commit_before><?php

namespace Bundle\MenuBundle\Tests;
use Bundle\MenuBundle\MenuItem;

class MenuItemRenderTest extends \PHPUnit_Framework_TestCase
{
    public function setUp()
    {
        MenuItem::setRenderCompressed(true);
    }

    public function testRenderWholeMenu()
    {
        extract($this->getSampleTree());
        $rendered = '<ul class="root"><li class="first">Parent 1<ul class="menu_level_1"><li class="first">Child 1</li><li>Child 2</li><li class="last">Child 3</li></ul></li><li class="last">Parent 2<ul class="menu_level_1"><li class="first last">Child 4<ul class="menu_level_2"><li class="first last">Grandchild 1</li></ul></li></ul></li></ul>';
        $this->assertEquals($rendered, $menu->render());
    }

    public function testToString()
    {
        extract($this->getSampleTree());
        $rendered = '<ul class="root"><li class="first">Parent 1<ul class="menu_level_1"><li class="first">Child 1</li><li>Child 2</li><li class="last">Child 3</li></ul></li><li class="last">Parent 2<ul class="menu_level_1"><li class="first last">Child 4<ul class="menu_level_2"><li class="first last">Grandchild 1</li></ul></li></ul></li></ul>';
        $this->assertEquals($rendered, (string) $menu);
    }

    public function testRenderWithClassAndTitle()
    {
        extract($this->getSampleTree());
        $pt2->setAttribute('class', 'parent2_class');
        $pt2->setAttribute('title', 'parent2 title');
        $rendered = '<ul class="root"><li class="first">Parent 1<ul class="menu_level_1"><li class="first">Child 1</li><li>Child 2</li><li class="last">Child 3</li></ul></li><li class="parent2_class last" title="parent2 title">Parent 2<ul class="menu_level_1"><li class="first last">Child 4<ul class="menu_level_2"><li class="first last">Grandchild 1</li></ul></li></ul></li></ul>';
        $this->assertEquals($rendered, $menu->render());
    }

    public function testRenderWithCurrentItem()
    {
        extract($this->getSampleTree());
        $ch2->setIsCurrent(true);
        $rendered = '<ul class="root"><li class="current_ancestor first">Parent 1<ul class="menu_level_1"><li class="first">Child 1</li><li class="current">Child 2</li><li class="last">Child 3</li></ul></li><li class="last">Parent 2<ul class="menu_level_1"><li class="first last">Child 4<ul class="menu_level_2"><li class="first last">Grandchild 1</li></ul></li></ul></li></ul>';
        $this->assertEquals($rendered, $menu->render());
    }

    public function testRenderSubMenuPortionWithClassAndTitle()
    {
        extract($this->getSampleTree());
        $pt2->setAttribute('class', 'parent2_class');
        $pt2->setAttribute('title', 'parent2 title');
        $rendered = '<ul class="parent2_class" title="parent2 title"><li class="first last">Child 4<ul class="menu_level_2"><li class="first last">Grandchild 1</li></ul></li></ul>';
        $this->assertEquals($rendered, $menu['Parent 2']->render());
    }

    public function testDoNotShowChildrenRendersNothing()
    {
        extract($this->getSampleTree());
        $menu->setShowChildren(false);
        $rendered = '';
        $this->assertEquals($rendered, $menu->render());
    }

    public function testDoNotShowChildChildrenRendersPartialMenu()
    {
        extract($this->getSampleTree());
        $menu['Parent 1']->setShowChildren(false);
        $rendered = '<ul class="root"><li class="first">Parent 1</li><li class="last">Parent 2<ul class="menu_level_1"><li class="first last">Child 4<ul class="menu_level_2"><li class="first last">Grandchild 1</li></ul></li></ul></li></ul>';
        $this->assertEquals($rendered, $menu->render());
    }

    public function testDoNotShowChildRendersPartialMenu()
    {
        extract($this->getSampleTree());
        $menu['Parent 1']->setShow(false);
        $rendered = '<ul class="root"><li class="first last">Parent 2<ul class="menu_level_1"><li class="first last">Child 4<ul class="menu_level_2"><li class="first last">Grandchild 1</li></ul></li></ul></li></ul>';
        $this->assertEquals($rendered, $menu->render());
    }

    public function testDepth0()
    {
        extract($this->getSampleTree());
        $rendered = '';
        $this->assertEquals($rendered, $menu->render(0));
    }

    public function testDepth1()
    {
        extract($this->getSampleTree());
        $rendered = '<ul class="root"><li class="first">Parent 1</li><li class="last">Parent 2</li></ul>';
        $this->assertEquals($rendered, $menu->render(1));
    }

    public function testDepth2()
    {
        extract($this->getSampleTree());
        $rendered = '<ul class="root"><li class="first">Parent 1<ul class="menu_level_1"><li class="first">Child 1</li><li>Child 2</li><li class="last">Child 3</li></ul></li><li class="last">Parent 2<ul class="menu_level_1"><li class="first last">Child 4</li></ul></li></ul>';
        $this->assertEquals($rendered, $menu->render(2));
    }

    /**
     * @return array the tree items
     */
    protected function getSampleTree($class = 'Bundle\MenuBundle\MenuItem')
    {
        $menu = new $class('Root li', null, array('class' => 'root'));
        $pt1 = $menu->getChild('Parent 1');
        $ch1 = $pt1->addChild('Child 1');
        $ch2 = $pt1->addChild('Child 2');

        // add the 3rd child via addChild with an object
        $ch3 = new $class('Child 3');
        $pt1->addChild($ch3);

        $pt2 = $menu->getChild('Parent 2');<commit_msg>Test rendering with fixed depth and not showing all children<commit_after><?php

namespace Bundle\MenuBundle\Tests;
use Bundle\MenuBundle\MenuItem;

class MenuItemRenderTest extends \PHPUnit_Framework_TestCase
{
    public function setUp()
    {
        MenuItem::setRenderCompressed(true);
    }

    public function testRenderWholeMenu()
    {
        extract($this->getSampleTree());
        $rendered = '<ul class="root"><li class="first">Parent 1<ul class="menu_level_1"><li class="first">Child 1</li><li>Child 2</li><li class="last">Child 3</li></ul></li><li class="last">Parent 2<ul class="menu_level_1"><li class="first last">Child 4<ul class="menu_level_2"><li class="first last">Grandchild 1</li></ul></li></ul></li></ul>';
        $this->assertEquals($rendered, $menu->render());
    }

    public function testToString()
    {
        extract($this->getSampleTree());
        $rendered = '<ul class="root"><li class="first">Parent 1<ul class="menu_level_1"><li class="first">Child 1</li><li>Child 2</li><li class="last">Child 3</li></ul></li><li class="last">Parent 2<ul class="menu_level_1"><li class="first last">Child 4<ul class="menu_level_2"><li class="first last">Grandchild 1</li></ul></li></ul></li></ul>';
        $this->assertEquals($rendered, (string) $menu);
    }

    public function testRenderWithClassAndTitle()
    {
        extract($this->getSampleTree());
        $pt2->setAttribute('class', 'parent2_class');
        $pt2->setAttribute('title', 'parent2 title');
        $rendered = '<ul class="root"><li class="first">Parent 1<ul class="menu_level_1"><li class="first">Child 1</li><li>Child 2</li><li class="last">Child 3</li></ul></li><li class="parent2_class last" title="parent2 title">Parent 2<ul class="menu_level_1"><li class="first last">Child 4<ul class="menu_level_2"><li class="first last">Grandchild 1</li></ul></li></ul></li></ul>';
        $this->assertEquals($rendered, $menu->render());
    }

    public function testRenderWithCurrentItem()
    {
        extract($this->getSampleTree());
        $ch2->setIsCurrent(true);
        $rendered = '<ul class="root"><li class="current_ancestor first">Parent 1<ul class="menu_level_1"><li class="first">Child 1</li><li class="current">Child 2</li><li class="last">Child 3</li></ul></li><li class="last">Parent 2<ul class="menu_level_1"><li class="first last">Child 4<ul class="menu_level_2"><li class="first last">Grandchild 1</li></ul></li></ul></li></ul>';
        $this->assertEquals($rendered, $menu->render());
    }

    public function testRenderSubMenuPortionWithClassAndTitle()
    {
        extract($this->getSampleTree());
        $pt2->setAttribute('class', 'parent2_class');
        $pt2->setAttribute('title', 'parent2 title');
        $rendered = '<ul class="parent2_class" title="parent2 title"><li class="first last">Child 4<ul class="menu_level_2"><li class="first last">Grandchild 1</li></ul></li></ul>';
        $this->assertEquals($rendered, $menu['Parent 2']->render());
    }

    public function testDoNotShowChildrenRendersNothing()
    {
        extract($this->getSampleTree());
        $menu->setShowChildren(false);
        $rendered = '';
        $this->assertEquals($rendered, $menu->render());
    }

    public function testDoNotShowChildChildrenRendersPartialMenu()
    {
        extract($this->getSampleTree());
        $menu['Parent 1']->setShowChildren(false);
        $rendered = '<ul class="root"><li class="first">Parent 1</li><li class="last">Parent 2<ul class="menu_level_1"><li class="first last">Child 4<ul class="menu_level_2"><li class="first last">Grandchild 1</li></ul></li></ul></li></ul>';
        $this->assertEquals($rendered, $menu->render());
    }

    public function testDoNotShowChildRendersPartialMenu()
    {
        extract($this->getSampleTree());
        $menu['Parent 1']->setShow(false);
        $rendered = '<ul class="root"><li class="first last">Parent 2<ul class="menu_level_1"><li class="first last">Child 4<ul class="menu_level_2"><li class="first last">Grandchild 1</li></ul></li></ul></li></ul>';
        $this->assertEquals($rendered, $menu->render());
    }

    public function testDepth0()
    {
        extract($this->getSampleTree());
        $rendered = '';
        $this->assertEquals($rendered, $menu->render(0));
    }

    public function testDepth1()
    {
        extract($this->getSampleTree());
        $rendered = '<ul class="root"><li class="first">Parent 1</li><li class="last">Parent 2</li></ul>';
        $this->assertEquals($rendered, $menu->render(1));
    }

    public function testDepth2()
    {
        extract($this->getSampleTree());
        $rendered = '<ul class="root"><li class="first">Parent 1<ul class="menu_level_1"><li class="first">Child 1</li><li>Child 2</li><li class="last">Child 3</li></ul></li><li class="last">Parent 2<ul class="menu_level_1"><li class="first last">Child 4</li></ul></li></ul>';
        $this->assertEquals($rendered, $menu->render(2));
    }

    public function testDepth2WithNotShowChildChildren()
    {
        extract($this->getSampleTree());
        $menu['Parent 1']->setShowChildren(false);
        $rendered = '<ul class="root"><li class="first">Parent 1</li><li class="last">Parent 2<ul class="menu_level_1"><li class="first last">Child 4</li></ul></li></ul>';
        $this->assertEquals($rendered, $menu->render(2));
    }

    /**
     * @return array the tree items
     */
    protected function getSampleTree($class = 'Bundle\MenuBundle\MenuItem')
    {
        $menu = new $class('Root li', null, array('class' => 'root'));
        $pt1 = $menu->getChild('Parent 1');
        $ch1 = $pt1->addChild('Child 1');
        $ch2 = $pt1->addChild('Child 2');

        // add the 3rd child via addChild with an object
        $ch3 = new $class('Child 3');
        $pt1->addChild($ch3);

        $pt2 = $menu->getChild('Parent 2');
{'fraction_non_alphanumeric': 0.2018251085319394, 'fraction_numerical': 0.01851687782404536, 'mean_word_length': 4.13557779799818, 'pattern_counts': {'":': 0, '<': 307, '<?xml version=': 0, '>': 401, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1179963', 'n_tokens_mistral': 3582, 'n_tokens_neox': 3382, 'n_words': 617}