<?php

namespace Autogedal\TbiBank\Test\Unit;

use Autogedal\TbiBank\Model\FailureRedirect;
use Magento\Framework\UrlInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use PHPUnit\Framework\TestCase;

class FailureRedirectTest extends TestCase
{
    public function testFailureUrlCarriesSignedOrderIdentifier(): void
    {
        $config = $this->createMock(ScopeConfigInterface::class);
        $config->method('getValue')->willReturn('tbi/failure');
        $urlBuilder = $this->createMock(UrlInterface::class);
        $urlBuilder->expects($this->once())->method('getUrl')->with(
            'tbi/failure',
            $this->callback(function (array $params): bool {
                return $params['_secure'] === true
                    && $params['_query']['order_id'] === '100000123'
                    && is_string($params['_query']['signature']);
            })
        )->willReturn('https://example.test/tbi/failure/?order_id=100000123');

        $redirect = new FailureRedirect($config, $urlBuilder);
        $redirect->getUrl(null, '100000123', 'test-key');

        $this->assertTrue($redirect->isValidSignature(
            '100000123',
            hash_hmac('sha256', '100000123', 'test-key'),
            'test-key'
        ));
        $this->assertFalse($redirect->isValidSignature('100000123', 'invalid', 'test-key'));
    }
}
