<?php

namespace Test\Integration;

require_once dirname(__DIR__) . '/Setup.php';

use Test;
use Test\Setup;
use Braintree;

class ApplePayTest extends Setup
{
    private static $gateway;

    public static function setUpBeforeClass(): void
    {
        self::$gateway = self::_buildMerchantGateway();
    }

    public static function tearDownAfterClass(): void
    {
        self::$gateway = null;
    }

    private static function _buildMerchantGateway()
    {
        $gateway = new Braintree\Gateway([
            'clientId' => 'client_id$development$integration_client_id',
            'clientSecret' => 'client_secret$development$integration_client_secret',
        ]);

        $result = $gateway->merchant()->create([
            'email' => 'name@email.com',
            'countryCodeAlpha3' => 'GBR',
            'paymentMethods' => ['credit_card', 'paypal'],
        ]);

        return new Braintree\Gateway([
            'accessToken' => $result->credentials->accessToken,
        ]);
    }

    public function testRegisterDomainWithExpectedStubbedResult()
    {
        $result = self::$gateway->applePay()->registerDomain('domain');
        $this->assertEquals(true, $result->success);
    }

    public function testValidationErrorWhenRegisteringNoDomain()
    {
        $result = self::$gateway->applePay()->registerDomain('');
        $this->assertEquals(false, $result->success);
        $this->assertEquals(1, preg_match('/Domain name is required\./', $result->message));
    }

    public function testUnregisterDomainWithExpectedStubbedResult()
    {
        $domain = 'example.com';
        $result = self::$gateway->applePay()->unregisterDomain($domain);
        $this->assertEquals(true, $result->success);
    }

    public function testUnregisterDomainWithSpecialCharactersWithExpectedStubbedResult()
    {
        $domain = 'ex&mple.com';
        $result = self::$gateway->applePay()->unregisterDomain($domain);
        $this->assertEquals(true, $result->success);
    }

    public function testUnregisterDomainWithSchemeWithExpectedStubbedResult()
    {
        $domain = 'http://example.com';
        $result = self::$gateway->applePay()->unregisterDomain($domain);
        $this->assertEquals(true, $result->success);
    }

    public function testRegisteredDomainsWithExpectedStubbedResult()
    {
        $result = self::$gateway->applePay()->registeredDomains();
        $this->assertEquals(true, $result->success);
        $registeredDomains = $result->applePayOptions->domains;
        $this->assertEmpty(array_diff(['www.example.com'], $registeredDomains));
    }

    public function testBinFieldsForApplePay()
    {
        $customer = Braintree\Customer::createNoValidate();
        $result = Braintree\PaymentMethod::create([
            'customerId' => $customer->id,
            'paymentMethodNonce' => Braintree\Test\Nonces::$applePayVisa,
            'cardholderName' => 'Jenny Block',
        ]);

        $this->assertTrue($result->success);
        $applePayCard = $result->paymentMethod;
        $this->assertNotNull($applePayCard->token);
        $this->assertNotNull($applePayCard->business);
        $this->assertNotNull($applePayCard->consumer);
        $this->assertNotNull($applePayCard->corporate);
        $this->assertNotNull($applePayCard->prepaidReloadable);
        $this->assertNotNull($applePayCard->purchase);
    }
}
