<?php

declare(strict_types=1);

namespace Ferienpass\AdminBundle\Tests\Controller\Page;

use Ferienpass\AdminBundle\Breadcrumb\Breadcrumb;
use Ferienpass\AdminBundle\Controller\Page\HomeController;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\Request;
use Twig\Environment;

final class HomeControllerTest extends TestCase
{
    public function testInvokePassesWidgetClassNames(): void
    {
        $widget1 = new class {};
        $widget2 = new class {};

        $breadcrumb = $this->createMock(Breadcrumb::class);
        $breadcrumb->method('generate')->willReturn(
            (new \Knp\Menu\MenuFactory())->createItem('root')
        );

        $twig = $this->createMock(Environment::class);
        $twig->expects(self::once())
            ->method('render')
            ->with(
                '@FerienpassAdmin/page/home.html.twig',
                self::callback(function (array $params) use ($widget1, $widget2): bool {
                    return \is_array($params['widgets'])
                        && 2 === \count($params['widgets'])
                        && $params['widgets'][0] === $widget1::class
                        && $params['widgets'][1] === $widget2::class;
                }),
            )
            ->willReturn('Dashboard');

        $container = new Container();
        $container->set('twig', $twig);

        $controller = new HomeController([$widget1, $widget2]);
        $controller->setContainer($container);

        $response = $controller(Request::create('/admin'), $breadcrumb);

        self::assertSame(200, $response->getStatusCode());
    }

    public function testInvokeWithNoWidgets(): void
    {
        $breadcrumb = $this->createMock(Breadcrumb::class);
        $breadcrumb->method('generate')->willReturn(
            (new \Knp\Menu\MenuFactory())->createItem('root')
        );

        $twig = $this->createMock(Environment::class);
        $twig->expects(self::once())
            ->method('render')
            ->with(
                '@FerienpassAdmin/page/home.html.twig',
                self::callback(fn (array $params): bool => [] === $params['widgets']),
            )
            ->willReturn('Dashboard');

        $container = new Container();
        $container->set('twig', $twig);

        $controller = new HomeController([]);
        $controller->setContainer($container);

        $response = $controller(Request::create('/admin'), $breadcrumb);

        self::assertSame(200, $response->getStatusCode());
    }
}
