<?php

namespace App\Tests\Repository;

use App\Entity\User;
use App\Repository\UserRepository;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;

class UserRepositoryTest extends KernelTestCase
{
    private UserRepository $repository;

    protected function setUp(): void
    {
        self::bootKernel();
        $this->repository = static::getContainer()->get(UserRepository::class);
    }

    public function testUpgradePasswordUpdatesUser(): void
    {
        $em = static::getContainer()->get('doctrine.orm.entity_manager');

        $user = new User();
        $user->setEmail('test-upgrade-'.uniqid().'@example.com');
        $user->setFirstName('Test');
        $user->setLastName('User');
        $user->setPassword('old-hash');
        $em->persist($user);
        $em->flush();

        $this->repository->upgradePassword($user, 'new-hash');

        $em->refresh($user);
        self::assertSame('new-hash', $user->getPassword());
    }

    public function testUpgradePasswordThrowsForUnsupportedUser(): void
    {
        $this->expectException(UnsupportedUserException::class);

        $fakeUser = new class implements PasswordAuthenticatedUserInterface {
            public function getPassword(): ?string
            {
                return null;
            }
        };

        $this->repository->upgradePassword($fakeUser, 'hash');
    }
}
