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

use Ferienpass\CoreBundle\Payments\PaymentSystem;
use Ferienpass\CoreBundle\Payments\Provider\PaymentProviderInterface;
use Ferienpass\CoreBundle\Settings\SystemSettingsManager;
use PHPUnit\Framework\TestCase;

/**
 * Two independent ways for a municipality to be taking payments, and the
 * setting is the one that covers cash and bank transfer — the case an online
 * provider says nothing about.
 */
class PaymentSystemTest extends TestCase
{
    public function testIsOnWhenTheMunicipalitySaysSo(): void
    {
        $this->assertTrue($this->system(setting: true, providerConfigured: false)->isEnabled());
    }

    public function testIsOnWhenAnOnlineProviderIsConfigured(): void
    {
        $this->assertTrue($this->system(setting: false, providerConfigured: true)->isEnabled());
    }

    public function testIsOffWhenNeither(): void
    {
        $this->assertFalse($this->system(setting: false, providerConfigured: false)->isEnabled());
    }

    /**
     * The setting covers cash and bank transfer, so it must not imply that an
     * online provider exists — the admin hides the online-payment surfaces on
     * this answer, not on isEnabled().
     */
    public function testTheSettingAloneIsNotAnOnlineProvider(): void
    {
        $this->assertFalse($this->system(setting: true, providerConfigured: false)->hasOnlineProvider());
        $this->assertTrue($this->system(setting: false, providerConfigured: true)->hasOnlineProvider());
    }

    private function system(bool $setting, bool $providerConfigured): PaymentSystem
    {
        $settings = $this->createStub(SystemSettingsManager::class);
        $settings->method('isPaymentsEnabled')->willReturn($setting);

        $provider = $this->createStub(PaymentProviderInterface::class);
        $provider->method('isConfigured')->willReturn($providerConfigured);

        return new PaymentSystem($provider, $settings);
    }
}
