GetClientTokenTest.php 5.23 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Braintree\Controller\Adminhtml\Payment;

use Braintree\Configuration;
use Magento\Backend\Model\Session\Quote;
use Magento\Braintree\Gateway\Config\Config;
use Magento\Braintree\Model\Adapter\BraintreeAdapter;
use Magento\Braintree\Model\Adapter\BraintreeAdapterFactory;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Store\Api\StoreRepositoryInterface;
use Magento\TestFramework\TestCase\AbstractBackendController;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

/**
 * Tests \Magento\Braintree\Controller\Adminhtml\Payment\GetClientToken
 */
class GetClientTokenTest extends AbstractBackendController
{
    /**
     * @var Quote
     */
    private $quoteSession;

    /**
     * @var ObjectManager|MockObject $stubObjectManager
     */
    private $stubObjectManager;

    /**
     * @inheritdoc
     */
    protected function setUp()
    {
        parent::setUp();

        $this->quoteSession = $this->_objectManager->get(Quote::class);

        $this->stubObjectManager = $this->getMockBuilder(ObjectManager::class)
            ->disableOriginalConstructor()
            ->getMock();

        $adapterFactory = new BraintreeAdapterFactory(
            $this->stubObjectManager,
            $this->_objectManager->get(Config::class)
        );

        $this->_objectManager->addSharedInstance($adapterFactory, BraintreeAdapterFactory::class);
    }

    /**
     * @inheritdoc
     */
    protected function tearDown()
    {
        $this->_objectManager->removeSharedInstance(BraintreeAdapterFactory::class);
        parent::tearDown();
    }

    /**
     * Checks if client token will retrieved from Braintree initialized with default scope.
     *
     * @magentoDataFixture Magento/Braintree/_files/payment_configuration.php
     * @magentoAppArea adminhtml
     */
    public function testExecute()
    {
        $this->perform(
            'def_merchant_id',
            'def_public_key',
            'def_private_key'
        );
    }

    /**
     * Checks if client token will be retrieved from Braintree initialized per store.
     *
     * @magentoDataFixture Magento/Braintree/_files/payment_configuration.php
     * @magentoAppArea adminhtml
     */
    public function testExecuteWithStoreConfiguration()
    {
        /** @var StoreRepositoryInterface $storeRepository */
        $storeRepository = $this->_objectManager->get(StoreRepositoryInterface::class);
        $store = $storeRepository->get('test');
        $this->quoteSession->setStoreId($store->getId());

        $this->perform(
            'store_merchant_id',
            'store_public_key',
            'def_private_key' // should be read from default scope
        );
    }

    /**
     * Checks if client token will be retrieved from Braintree initialized per website.
     *
     * @magentoDataFixture Magento/Braintree/_files/payment_configuration.php
     * @magentoAppArea adminhtml
     */
    public function testExecuteWithWebsiteConfiguration()
    {
        /** @var StoreRepositoryInterface $storeRepository */
        $storeRepository = $this->_objectManager->get(StoreRepositoryInterface::class);
        $store = $storeRepository->get('fixture_second_store');
        $this->quoteSession->setStoreId($store->getId());

        $this->perform(
            'website_merchant_id',
            'def_public_key', // should be read from default scope
            'website_private_key'
        );
    }

    /**
     * Perform test.
     *
     * @param string $merchantId
     * @param string $publicKey
     * @param string $privateKey
     * @return void
     */
    private function perform($merchantId, $publicKey, $privateKey)
    {
        $args = [
            'merchantId' => $merchantId,
            'publicKey' => $publicKey,
            'privateKey' => $privateKey,
            'environment' => 'sandbox',
        ];

        $adapter = $this->getMockBuilder(BraintreeAdapter::class)
            ->setConstructorArgs($args)
            ->setMethods(['generate'])
            ->getMock();
        $adapter->method('generate')
            ->willReturn('client_token');

        $this->stubObjectManager->method('create')
            ->with(BraintreeAdapter::class, $args)
            ->willReturn($adapter);

        $this->dispatch('backend/braintree/payment/getClientToken');

        /** @var SerializerInterface $serializer */
        $serializer = $this->_objectManager->get(SerializerInterface::class);
        $decoded = $serializer->unserialize($this->getResponse()->getBody());
        $this->performAsserts($decoded['clientToken'], $merchantId, $publicKey, $privateKey);
    }

    /**
     * Perform Asserts.
     *
     * @param string $clientToken
     * @param string $merchantId
     * @param string $publicKey
     * @param string $privateKey
     * @return void
     */
    private function performAsserts($clientToken, $merchantId, $publicKey, $privateKey)
    {
        self::assertEquals('client_token', $clientToken);
        self::assertEquals(Configuration::merchantId(), $merchantId);
        self::assertEquals(Configuration::publicKey(), $publicKey);
        self::assertEquals(Configuration::privateKey(), $privateKey);
    }
}