<?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\CmsBundle\Tests\UserAccount;

use Ferienpass\CmsBundle\UserAccount\UserAccountFragments;
use Ferienpass\CmsBundle\UserAccount\UserAccountFragmentValueHolder;
use PHPUnit\Framework\TestCase;

final class UserAccountFragmentsTest extends TestCase
{
    public function testAllReturnsEmptyArrayByDefault(): void
    {
        $fragments = new UserAccountFragments();

        self::assertSame([], $fragments->all());
    }

    public function testKeysReturnsEmptyArrayByDefault(): void
    {
        $fragments = new UserAccountFragments();

        self::assertSame([], $fragments->keys());
    }

    public function testAddFragmentAddsEntry(): void
    {
        $fragments = new UserAccountFragments();
        $fragments->addFragment('profile', 'user-profile', 'user', 'Profile', 10);

        self::assertCount(1, $fragments->all());
        self::assertArrayHasKey('profile', $fragments->all());
    }

    public function testKeysReturnsFragmentKeys(): void
    {
        $fragments = new UserAccountFragments();
        $fragments->addFragment('profile', 'user-profile', 'user');
        $fragments->addFragment('settings', 'user-settings', 'cog');

        self::assertSame(['profile', 'settings'], $fragments->keys());
    }

    public function testFragmentsAreSortedByPriorityDescending(): void
    {
        $fragments = new UserAccountFragments();
        $fragments->addFragment('low', 'low', 'icon', null, 1);
        $fragments->addFragment('high', 'high', 'icon', null, 10);
        $fragments->addFragment('medium', 'medium', 'icon', null, 5);

        $keys = $fragments->keys();
        self::assertSame(['high', 'medium', 'low'], $keys);
    }

    public function testValueHolderGettersReturnCorrectValues(): void
    {
        $holder = new UserAccountFragmentValueHolder('my-key', 'my-alias', 'my-icon', 'My Label', 42, true);

        self::assertSame('my-key', $holder->getKey());
        self::assertSame('my-alias', $holder->getAlias());
        self::assertSame('my-icon', $holder->getIcon());
        self::assertSame('My Label', $holder->getLabel());
        self::assertSame(42, $holder->getPriority());
        self::assertTrue($holder->isHideFromNavigation());
    }

    public function testValueHolderDefaultValues(): void
    {
        $holder = new UserAccountFragmentValueHolder('key', 'alias', 'icon');

        self::assertNull($holder->getLabel());
        self::assertSame(0, $holder->getPriority());
        self::assertFalse($holder->isHideFromNavigation());
    }
}
