<?php

namespace MintyPHP\Tests\Service\Auth;

use MintyPHP\Service\Auth\AuthCryptoGateway;
use PHPUnit\Framework\TestCase;

final class AuthCryptoGatewayTest extends TestCase
{
    protected function setUp(): void
    {
        if (!defined('APP_CRYPTO_KEY')) {
            define('APP_CRYPTO_KEY', str_repeat('ab', 32));
        }
    }

    public function testIsConfiguredDelegatesToCrypto(): void
    {
        $gateway = new AuthCryptoGateway();

        $this->assertTrue($gateway->isConfigured());
    }

    public function testEncryptDecryptRoundTripThroughGateway(): void
    {
        $gateway = new AuthCryptoGateway();
        $plaintext = 'sso-client-secret';

        $encrypted = $gateway->encryptString($plaintext);

        $this->assertNotSame($plaintext, $encrypted);
        $this->assertSame($plaintext, $gateway->decryptString($encrypted));
    }

    public function testDecryptStringThrowsOnMalformedInput(): void
    {
        $gateway = new AuthCryptoGateway();

        $this->expectException(\RuntimeException::class);
        $gateway->decryptString('not-a-valid-payload');
    }

    public function testEncryptProducesDifferentCiphertextEachCall(): void
    {
        $gateway = new AuthCryptoGateway();

        $this->assertNotSame(
            $gateway->encryptString('same-value'),
            $gateway->encryptString('same-value'),
        );
    }
}
