<?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\ApiPlatform;

use ApiPlatform\State\SerializerContextBuilderInterface;
use Ferienpass\CoreBundle\ApiPlatform\AuthenticatedGroupsContextBuilder;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request;

final class AuthenticatedGroupsContextBuilderTest extends TestCase
{
    public function testContextWithoutGroupsIsReturnedUnchanged(): void
    {
        $request = new Request();
        $context = ['some_key' => 'value'];

        $decorated = $this->createMock(SerializerContextBuilderInterface::class);
        $decorated->method('createFromRequest')->willReturn($context);

        $security = $this->createMock(Security::class);
        $security->method('isGranted')->willReturn(true);

        $builder = new AuthenticatedGroupsContextBuilder($decorated, $security);
        $result = $builder->createFromRequest($request, true);

        self::assertSame($context, $result);
    }

    public function testContextGroupsNotExtendedForAnonymousUser(): void
    {
        $request = new Request();
        $context = ['groups' => ['existing_group']];

        $decorated = $this->createMock(SerializerContextBuilderInterface::class);
        $decorated->method('createFromRequest')->willReturn($context);

        $security = $this->createMock(Security::class);
        $security->method('isGranted')->with('ROLE_USER')->willReturn(false);

        $builder = new AuthenticatedGroupsContextBuilder($decorated, $security);
        $result = $builder->createFromRequest($request, true);

        self::assertSame(['existing_group'], $result['groups']);
    }

    public function testNormalizationAddsReadGroupForAuthenticatedUser(): void
    {
        $request = new Request();
        $context = ['groups' => ['existing_group']];

        $decorated = $this->createMock(SerializerContextBuilderInterface::class);
        $decorated->method('createFromRequest')->willReturn($context);

        $security = $this->createMock(Security::class);
        $security->method('isGranted')->with('ROLE_USER')->willReturn(true);

        $builder = new AuthenticatedGroupsContextBuilder($decorated, $security);
        $result = $builder->createFromRequest($request, true);

        self::assertContains('api:offer:read', $result['groups']);
    }

    public function testDenormalizationAddsWriteGroupForAuthenticatedUser(): void
    {
        $request = new Request();
        $context = ['groups' => ['existing_group']];

        $decorated = $this->createMock(SerializerContextBuilderInterface::class);
        $decorated->method('createFromRequest')->willReturn($context);

        $security = $this->createMock(Security::class);
        $security->method('isGranted')->with('ROLE_USER')->willReturn(true);

        $builder = new AuthenticatedGroupsContextBuilder($decorated, $security);
        $result = $builder->createFromRequest($request, false);

        self::assertContains('api:offer:write', $result['groups']);
        self::assertNotContains('api:offer:read', $result['groups']);
    }
}
