<commit_before><?php
namespace beatrix\tests\unit;

use beatrix\helpers\TreeBuilder;
use beatrix\iblock\IblockSectionsMenu;
use beatrix\tests\mock\CIBlock;
use beatrix\tests\mock\CIBlockSection;
use beatrix\tests\mock\CModule;
use beatrix\tests\mock\CPageOption;
use Mockery;

class MenuTest extends \PHPUnit_Framework_TestCase
{
    /** @var IblockSectionsMenu */
    private $menu;

    public function setUp()
    {
        class_alias(CIBlock::_cl(), 'CIBlock');
        class_alias(CModule::_cl(), 'CModule');
        $ibMockResult = Mockery::mock('resultMock');
        $ibMockResult->shouldReceive('GetNext')
            ->andReturn(
                array('NAME' => 'Foo', 'ID' => 1),
                null
            );
        \beatrix\tests\mock\CIBlock::returnList($ibMockResult);

        $ibsMockResult = Mockery::mock('mockSectionsResult');
        $ibsMockResult
            ->shouldReceive('GetNext')
            ->andReturn(
                array('NAME' => 'FooSection', 'ID' => 1, 'CODE' => 'foo-section'),
                null
            );
        $ibsMockResult->shouldReceive(array(
            'NavStart' => null,
            'SelectedRowsCount' => 1,
        ));
        \beatrix\tests\mock\CIBlockSection::returnList($ibsMockResult);
        class_alias(CIBlockSection::_cl(), 'CIBlockSection');
        class_alias(CPageOption::_cl(), 'CPageOption');
        $this->menu = new IblockSectionsMenu('foo');
    }


    public function testBuildTree()
    {
        $items = array(
            array('DEPTH_LEVEL' => 1, 'NAME' => 'Level1-1',),
            array('DEPTH_LEVEL' => 2, 'NAME' => 'Level2-1',),
            array('DEPTH_LEVEL' => 1, 'NAME' => 'Level1-2',),
            array('DEPTH_LEVEL' => 2, 'NAME' => 'Level2-2',),
            array('DEPTH_LEVEL' => 2, 'NAME' => 'Level2-3',),
            array('DEPTH_LEVEL' => 3, 'NAME' => 'Level3-1',),
            array('DEPTH_LEVEL' => 3, 'NAME' => 'Level3-2',),
            array('DEPTH_LEVEL' => 3, 'NAME' => 'Level3-3',),
            array('DEPTH_LEVEL' => 1, 'NAME' => 'Level1-3',),
        );
        $expect = <<<TREE
|-0 Array
| |-DEPTH_LEVEL 1
| |-NAME Level1-1
| \-#children Array
|   \-0 Array
|     |-DEPTH_LEVEL 2
|     \-NAME Level2-1
|-1 Array
| |-DEPTH_LEVEL 1
| |-NAME Level1-2
| \-#children Array
|   |-0 Array
|   | |-DEPTH_LEVEL 2
|   | \-NAME Level2-2
|   \-1 Array
|     |-DEPTH_LEVEL 2
|     |-NAME Level2-3
|     \-#children Array
|       |-0 Array
|       | |-DEPTH_LEVEL 3
|       | \-NAME Level3-1
|       |-1 Array
|       | |-DEPTH_LEVEL 3
|       | \-NAME Level3-2
|       \-2 Array
|         |-DEPTH_LEVEL 3
|         \-NAME Level3-3
\-2 Array
  |-DEPTH_LEVEL 1
  \-NAME Level1-3
TREE;
        $tree = $this->menu->getTree($items)->getTreeArray();
        $it = new \RecursiveTreeIterator(new \RecursiveArrayIterator($tree), \RecursiveTreeIterator::BYPASS_CURRENT,
            null);
        $treeDebug = '';
        foreach ($it as $k => $line) {
            $line = "$k {$line}" . PHP_EOL;
            $treeDebug .= $line;
            print $line;
        }
        $this->assertEquals($expect, rtrim($treeDebug), 'Tree must be built correctly');

    }

    public function testBuildTreeWithCallback()
    {
        $items = array(
            array('DEPTH_LEVEL' => 1, 'NAME' => 'Level1-1',),
            array('DEPTH_LEVEL' => 1, 'NAME' => 'Level1-2',),
            array('DEPTH_LEVEL' => 2, 'NAME' => 'Level2-1',),
            array('DEPTH_LEVEL' => 1, 'NAME' => 'Level1-3',),
        );
        $builder = new TreeBuilder($items, 'DEPTH_LEVEL', function ($item) {
            $item['#children'][] = 'foo';
            return $item;
        });
        foreach ($builder->getTreeArray() as $item) {
            $this->assertArrayHasKey('#children', $item, 'Item not modified');
            $this->assertNotFalse(array_search('foo', $item['#children']), 'Item not modified');
        }
    }

    public function tearDown()
    {
        Mockery::close();
	}
}<commit_msg>Fix broken tests<commit_after><?php
namespace beatrix\tests\unit;

use beatrix\helpers\TreeBuilder;
use beatrix\iblock\IblockSectionsMenu;
use beatrix\tests\mock\CIBlock;
use beatrix\tests\mock\CIBlockSection;
use beatrix\tests\mock\CModule;
use beatrix\tests\mock\CPageOption;
use Mockery;

class MenuTest extends \PHPUnit_Framework_TestCase
{
    /** @var IblockSectionsMenu */
    private $menu;

    public function setUp()
    {
        if (!class_exists('CIBlock')) {
            class_alias(CIBlock::_cl(), 'CIBlock');
            class_alias(CModule::_cl(), 'CModule');
            class_alias(CIBlockSection::_cl(), 'CIBlockSection');
            class_alias(CPageOption::_cl(), 'CPageOption');
        }
        $ibMockResult = Mockery::mock('resultMock');
        $ibMockResult->shouldReceive('GetNext')
            ->andReturn(
                array('NAME' => 'Foo', 'ID' => 1),
                null
            );
        \beatrix\tests\mock\CIBlock::returnList($ibMockResult);

        $ibsMockResult = Mockery::mock('mockSectionsResult');
        $ibsMockResult
            ->shouldReceive('GetNext')
            ->andReturn(
                array('NAME' => 'FooSection', 'ID' => 1, 'CODE' => 'foo-section'),
                null
            );
        $ibsMockResult->shouldReceive(array(
            'NavStart' => null,
            'SelectedRowsCount' => 1,
        ));
        \beatrix\tests\mock\CIBlockSection::returnList($ibsMockResult);

        $this->menu = new IblockSectionsMenu('foo');
    }


    public function testBuildTree()
    {
        $items = array(
            array('DEPTH_LEVEL' => 1, 'NAME' => 'Level1-1',),
            array('DEPTH_LEVEL' => 2, 'NAME' => 'Level2-1',),
            array('DEPTH_LEVEL' => 1, 'NAME' => 'Level1-2',),
            array('DEPTH_LEVEL' => 2, 'NAME' => 'Level2-2',),
            array('DEPTH_LEVEL' => 2, 'NAME' => 'Level2-3',),
            array('DEPTH_LEVEL' => 3, 'NAME' => 'Level3-1',),
            array('DEPTH_LEVEL' => 3, 'NAME' => 'Level3-2',),
            array('DEPTH_LEVEL' => 3, 'NAME' => 'Level3-3',),
            array('DEPTH_LEVEL' => 1, 'NAME' => 'Level1-3',),
        );
        $expect = <<<TREE
|-0 Array
| |-DEPTH_LEVEL 1
| |-NAME Level1-1
| |-#has-active-child 0
| \-#children Array
|   \-0 Array
|     |-DEPTH_LEVEL 2
|     \-NAME Level2-1
|-1 Array
| |-DEPTH_LEVEL 1
| |-NAME Level1-2
| |-#has-active-child 0
| \-#children Array
|   |-0 Array
|   | |-DEPTH_LEVEL 2
|   | \-NAME Level2-2
|   \-1 Array
|     |-DEPTH_LEVEL 2
|     |-NAME Level2-3
|     |-#has-active-child 0
|     \-#children Array
|       |-0 Array
|       | |-DEPTH_LEVEL 3
|       | \-NAME Level3-1
|       |-1 Array
|       | |-DEPTH_LEVEL 3
|       | \-NAME Level3-2
|       \-2 Array
|         |-DEPTH_LEVEL 3
|         \-NAME Level3-3
\-2 Array
  |-DEPTH_LEVEL 1
  \-NAME Level1-3
TREE;
        $tree = $this->menu->getTree($items)->getTreeArray();
        $it = new \RecursiveTreeIterator(
            new \RecursiveArrayIterator($tree),
            \RecursiveTreeIterator::BYPASS_CURRENT,
            null
        );
        $treeDebug = '';
        foreach ($it as $k => $line) {
            if (is_bool($line)) {
                $line = (int)$line;
            }
            $line = "$k {$line}" . PHP_EOL;
            $treeDebug .= $line;
            print $line;
        }
        $this->assertEquals($expect, rtrim($treeDebug), 'Tree must be built correctly');
    }

    public function testBuildTreeWithCallback()
    {
        $items = array(
            array('DEPTH_LEVEL' => 1, 'NAME' => 'Level1-1',),
            array('DEPTH_LEVEL' => 1, 'NAME' => 'Level1-2',),
            array('DEPTH_LEVEL' => 2, 'NAME' => 'Level2-1',),
            array('DEPTH_LEVEL' => 1, 'NAME' => 'Level1-3',),
        );
        $builder = new TreeBuilder($items, 'DEPTH_LEVEL', function ($item) {
            $item['#children'][] = 'foo';
            return $item;
        });
        foreach ($builder->getTreeArray() as $item) {
            $this->assertArrayHasKey('#children', $item, 'Item not modified');
            $this->assertNotFalse(array_search('foo', $item['#children']), 'Item not modified');
        }
    }

    public function tearDown()
    {
        Mockery::close();
	}
}
{'fraction_non_alphanumeric': 0.17572001458257383, 'fraction_numerical': 0.01932191031717098, 'mean_word_length': 2.1150643451930358, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 97, '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': '6055615', 'n_tokens_mistral': 2836, 'n_tokens_neox': 2690, 'n_words': 513}