<?php
namespace ProVM\Test\Service;

use PHPUnit\Framework\TestCase;
use Incoviba\Common\Define;
use Incoviba\Model;
use Incoviba\Repository;
use Incoviba\Service;

class MenuTest extends TestCase
{
    public function testBuild(): void
    {
        $tests = mt_rand(10, 100);
        for ($i = 0; $i < $tests; $i ++) {
            list($expected, $menu) = $this->generateBuildTest();
            $this->assertEquals($expected, $menu->build(1));
        }
    }
    protected function generateBuildTest(): array
    {
        $modelCount = mt_rand(3, 100);
        $expected = [];
        $models = [];
        for ($j = 0; $j < $modelCount; $j ++) {
            $model = $this->generateModel();
            $models []= $model;
            $expected []= "<a class=\"item\" href=\"http://localhost/url{$model->id}\">title{$model->id}</a>";
        }
        $expected = implode(PHP_EOL, $expected);
        $connection = $this->getMockBuilder(Define\Connection::class)->getMock();
        $repository = $this->getMockBuilder(Repository\Menu::class)->setConstructorArgs(compact('connection'))->getMock();
        $permissionsRepository = $this->getMockBuilder(Repository\Permission::class)->setConstructorArgs(compact('connection'))->getMock();
        $permissions = $this->getMockBuilder(Service\Permission::class)->setConstructorArgs([$permissionsRepository])->getMock();
        $repository->method('fetchByUser')->willReturn($models);
        $menu = new Service\Menu($repository, $permissions, (object) ['base' => 'http://localhost']);

        return [$expected, $menu];
    }
    protected function generateModel(): Model\Menu
    {
        $id = mt_rand(1, 100000);
        $model = $this->getMockBuilder(Model\Menu::class)->getMock();
        $model->id = $id;
        $model->url = "url{$id}";
        $model->title = "title{$id}";

        return $model;
    }
}
