GetClientTokenTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Controller\Adminhtml\Payment;
  7. use Braintree\Configuration;
  8. use Magento\Backend\Model\Session\Quote;
  9. use Magento\Braintree\Gateway\Config\Config;
  10. use Magento\Braintree\Model\Adapter\BraintreeAdapter;
  11. use Magento\Braintree\Model\Adapter\BraintreeAdapterFactory;
  12. use Magento\Framework\App\ObjectManager;
  13. use Magento\Framework\Serialize\SerializerInterface;
  14. use Magento\Store\Api\StoreRepositoryInterface;
  15. use Magento\TestFramework\TestCase\AbstractBackendController;
  16. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  17. /**
  18. * Tests \Magento\Braintree\Controller\Adminhtml\Payment\GetClientToken
  19. */
  20. class GetClientTokenTest extends AbstractBackendController
  21. {
  22. /**
  23. * @var Quote
  24. */
  25. private $quoteSession;
  26. /**
  27. * @var ObjectManager|MockObject $stubObjectManager
  28. */
  29. private $stubObjectManager;
  30. /**
  31. * @inheritdoc
  32. */
  33. protected function setUp()
  34. {
  35. parent::setUp();
  36. $this->quoteSession = $this->_objectManager->get(Quote::class);
  37. $this->stubObjectManager = $this->getMockBuilder(ObjectManager::class)
  38. ->disableOriginalConstructor()
  39. ->getMock();
  40. $adapterFactory = new BraintreeAdapterFactory(
  41. $this->stubObjectManager,
  42. $this->_objectManager->get(Config::class)
  43. );
  44. $this->_objectManager->addSharedInstance($adapterFactory, BraintreeAdapterFactory::class);
  45. }
  46. /**
  47. * @inheritdoc
  48. */
  49. protected function tearDown()
  50. {
  51. $this->_objectManager->removeSharedInstance(BraintreeAdapterFactory::class);
  52. parent::tearDown();
  53. }
  54. /**
  55. * Checks if client token will retrieved from Braintree initialized with default scope.
  56. *
  57. * @magentoDataFixture Magento/Braintree/_files/payment_configuration.php
  58. * @magentoAppArea adminhtml
  59. */
  60. public function testExecute()
  61. {
  62. $this->perform(
  63. 'def_merchant_id',
  64. 'def_public_key',
  65. 'def_private_key'
  66. );
  67. }
  68. /**
  69. * Checks if client token will be retrieved from Braintree initialized per store.
  70. *
  71. * @magentoDataFixture Magento/Braintree/_files/payment_configuration.php
  72. * @magentoAppArea adminhtml
  73. */
  74. public function testExecuteWithStoreConfiguration()
  75. {
  76. /** @var StoreRepositoryInterface $storeRepository */
  77. $storeRepository = $this->_objectManager->get(StoreRepositoryInterface::class);
  78. $store = $storeRepository->get('test');
  79. $this->quoteSession->setStoreId($store->getId());
  80. $this->perform(
  81. 'store_merchant_id',
  82. 'store_public_key',
  83. 'def_private_key' // should be read from default scope
  84. );
  85. }
  86. /**
  87. * Checks if client token will be retrieved from Braintree initialized per website.
  88. *
  89. * @magentoDataFixture Magento/Braintree/_files/payment_configuration.php
  90. * @magentoAppArea adminhtml
  91. */
  92. public function testExecuteWithWebsiteConfiguration()
  93. {
  94. /** @var StoreRepositoryInterface $storeRepository */
  95. $storeRepository = $this->_objectManager->get(StoreRepositoryInterface::class);
  96. $store = $storeRepository->get('fixture_second_store');
  97. $this->quoteSession->setStoreId($store->getId());
  98. $this->perform(
  99. 'website_merchant_id',
  100. 'def_public_key', // should be read from default scope
  101. 'website_private_key'
  102. );
  103. }
  104. /**
  105. * Perform test.
  106. *
  107. * @param string $merchantId
  108. * @param string $publicKey
  109. * @param string $privateKey
  110. * @return void
  111. */
  112. private function perform($merchantId, $publicKey, $privateKey)
  113. {
  114. $args = [
  115. 'merchantId' => $merchantId,
  116. 'publicKey' => $publicKey,
  117. 'privateKey' => $privateKey,
  118. 'environment' => 'sandbox',
  119. ];
  120. $adapter = $this->getMockBuilder(BraintreeAdapter::class)
  121. ->setConstructorArgs($args)
  122. ->setMethods(['generate'])
  123. ->getMock();
  124. $adapter->method('generate')
  125. ->willReturn('client_token');
  126. $this->stubObjectManager->method('create')
  127. ->with(BraintreeAdapter::class, $args)
  128. ->willReturn($adapter);
  129. $this->dispatch('backend/braintree/payment/getClientToken');
  130. /** @var SerializerInterface $serializer */
  131. $serializer = $this->_objectManager->get(SerializerInterface::class);
  132. $decoded = $serializer->unserialize($this->getResponse()->getBody());
  133. $this->performAsserts($decoded['clientToken'], $merchantId, $publicKey, $privateKey);
  134. }
  135. /**
  136. * Perform Asserts.
  137. *
  138. * @param string $clientToken
  139. * @param string $merchantId
  140. * @param string $publicKey
  141. * @param string $privateKey
  142. * @return void
  143. */
  144. private function performAsserts($clientToken, $merchantId, $publicKey, $privateKey)
  145. {
  146. self::assertEquals('client_token', $clientToken);
  147. self::assertEquals(Configuration::merchantId(), $merchantId);
  148. self::assertEquals(Configuration::publicKey(), $publicKey);
  149. self::assertEquals(Configuration::privateKey(), $privateKey);
  150. }
  151. }