<?php

declare(strict_types=1);

namespace Apexl\Tests\User\UnitTests;

use apexl\Config\ConfigurationMock;
use apexl\Config\Exceptions\MissingConfigException;
use apexl\Io\modules\user\services\JwtKeyFromConfigService;
use apexl\Io\TestInfrastructure\UnitTest;
use DI\DependencyException;
use DI\NotFoundException;
use Firebase\JWT\Key;
use PHPUnit\Framework\Attributes\Test;
use function apexl\Io\container;

final class JwtKeyFromConfigTest extends UnitTest
{
    /**
     * @throws DependencyException
     * @throws NotFoundException
     * @throws MissingConfigException
     */
    #[Test]
    public function key(): void
    {
        $secretKey = $this->faker->lexify(implode('?', array_fill(0, 512, '?')));
        $algorithm = $this->faker->randomElement([
            'HS256',
            'HS384',
            'HS512',
        ]);

        $config = new ConfigurationMock([
            'auth.jwt.secret_key' => $secretKey,
            'auth.jwt.algorithm' => $algorithm,
        ]);


        $key = container()->make(JwtKeyFromConfigService::class, [
            'config' => $config,
        ])->key();

        $this->assertInstanceOf(Key::class, $key);
        $this->assertSame($secretKey, $key->getKeyMaterial());
        $this->assertSame($algorithm, $key->getAlgorithm());
    }
}