123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?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);
- }
- }
|