<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\CoreBundle\Tests\Session;

use Ferienpass\CoreBundle\Session\Flash;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpFoundation\Session\FlashBagAwareSessionInterface;

final class FlashTest extends TestCase
{
    public function testAddConfirmationAddsFlashMessage(): void
    {
        [$flashBag, $flash] = $this->createFlash();

        $flashBag->expects(self::once())->method('add')->with('confirmation', self::isArray());

        $flash->addConfirmation('Headline', 'Text');
    }

    public function testAddErrorAddsFlashMessage(): void
    {
        [$flashBag, $flash] = $this->createFlash();

        $flashBag->expects(self::once())->method('add')->with('error', self::isArray());

        $flash->addError('Error headline');
    }

    public function testAddInfoBannerAddsFlashMessage(): void
    {
        [$flashBag, $flash] = $this->createFlash();

        $flashBag->expects(self::once())->method('add')->with('banner-info', self::isArray());

        $flash->addInfoBanner();
    }

    public function testAddConfirmationModalAddsFlashMessage(): void
    {
        [$flashBag, $flash] = $this->createFlash();

        $flashBag->expects(self::once())->method('add')->with('modal-confirm', self::isArray());

        $flash->addConfirmationModal();
    }

    public function testAddErrorModalAddsFlashMessage(): void
    {
        [$flashBag, $flash] = $this->createFlash();

        $flashBag->expects(self::once())->method('add')->with('modal-error', self::isArray());

        $flash->addErrorModal();
    }

    public function testThrowsLogicExceptionWhenSessionNotFound(): void
    {
        $this->expectException(\LogicException::class);

        $requestStack = $this->createMock(RequestStack::class);
        $requestStack->method('getSession')->willThrowException(new SessionNotFoundException());

        new Flash($requestStack)->addConfirmation('Test');
    }

    private function createFlash(): array
    {
        $flashBag = $this->createMock(FlashBagInterface::class);
        $session = $this->createMock(FlashBagAwareSessionInterface::class);
        $session->method('getFlashBag')->willReturn($flashBag);

        $requestStack = $this->createMock(RequestStack::class);
        $requestStack->method('getSession')->willReturn($session);

        return [$flashBag, new Flash($requestStack)];
    }
}
