TokensConfigProviderTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Vault\Test\Unit\Model\Ui;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. use Magento\Store\Api\Data\StoreInterface;
  9. use Magento\Store\Model\StoreManagerInterface;
  10. use Magento\Vault\Api\Data\PaymentTokenInterface;
  11. use Magento\Vault\Api\PaymentMethodListInterface;
  12. use Magento\Vault\Model\CustomerTokenManagement;
  13. use Magento\Vault\Model\Ui\TokensConfigProvider;
  14. use Magento\Vault\Model\Ui\TokenUiComponentInterface;
  15. use Magento\Vault\Model\Ui\TokenUiComponentProviderInterface;
  16. use Magento\Vault\Model\VaultPaymentInterface;
  17. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  18. /**
  19. * Class ConfigProviderTest
  20. *
  21. * @see \Magento\Vault\Model\Ui\TokensConfigProvider
  22. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  23. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  24. */
  25. class TokensConfigProviderTest extends \PHPUnit\Framework\TestCase
  26. {
  27. /**
  28. * @var StoreManagerInterface|MockObject
  29. */
  30. private $storeManager;
  31. /**
  32. * @var VaultPaymentInterface|MockObject
  33. */
  34. private $vaultPayment;
  35. /**
  36. * @var StoreInterface|MockObject
  37. */
  38. private $store;
  39. /**
  40. * @var CustomerTokenManagement|MockObject
  41. */
  42. private $customerTokenManagement;
  43. /**
  44. * @var PaymentMethodListInterface|MockObject
  45. */
  46. private $vaultPaymentList;
  47. /**
  48. * @var ObjectManager
  49. */
  50. private $objectManager;
  51. protected function setUp()
  52. {
  53. $this->objectManager = new ObjectManager($this);
  54. $this->vaultPaymentList = $this->createMock(PaymentMethodListInterface::class);
  55. $this->vaultPayment = $this->getMockForAbstractClass(VaultPaymentInterface::class);
  56. $this->storeManager = $this->createMock(StoreManagerInterface::class);
  57. $this->store = $this->createMock(StoreInterface::class);
  58. $this->customerTokenManagement = $this->getMockBuilder(CustomerTokenManagement::class)
  59. ->disableOriginalConstructor()
  60. ->getMock();
  61. }
  62. public function testGetConfig()
  63. {
  64. $storeId = 1;
  65. $vaultProviderCode = 'vault_provider_code';
  66. $expectedConfig = [
  67. 'payment' => [
  68. 'vault' => [
  69. $vaultProviderCode . '_' . '0' => [
  70. 'config' => ['token_code' => 'code'],
  71. 'component' => 'Vendor_Module/js/vault_component'
  72. ]
  73. ]
  74. ]
  75. ];
  76. $token = $this->getMockForAbstractClass(PaymentTokenInterface::class);
  77. $tokenUiComponentProvider = $this->getMockForAbstractClass(TokenUiComponentProviderInterface::class);
  78. $tokenUiComponent = $this->getMockForAbstractClass(TokenUiComponentInterface::class);
  79. $this->storeManager->expects(static::once())
  80. ->method('getStore')
  81. ->willReturn($this->store);
  82. $this->store->expects(static::once())
  83. ->method('getId')
  84. ->willReturn($storeId);
  85. $this->vaultPaymentList->expects(static::once())
  86. ->method('getActiveList')
  87. ->with($storeId)
  88. ->willReturn([$this->vaultPayment]);
  89. $this->vaultPayment->expects(static::once())
  90. ->method('getProviderCode')
  91. ->willReturn($vaultProviderCode);
  92. $this->customerTokenManagement->expects(static::once())
  93. ->method('getCustomerSessionTokens')
  94. ->willReturn([$token]);
  95. $token->expects(static::once())
  96. ->method('getPaymentMethodCode')
  97. ->willReturn($vaultProviderCode);
  98. $tokenUiComponentProvider->expects(static::once())
  99. ->method('getComponentForToken')
  100. ->with($token)
  101. ->willReturn($tokenUiComponent);
  102. $tokenUiComponent->expects(static::once())
  103. ->method('getConfig')
  104. ->willReturn(['token_code' => 'code']);
  105. $tokenUiComponent->expects(static::once())
  106. ->method('getName')
  107. ->willReturn('Vendor_Module/js/vault_component');
  108. $configProvider = new TokensConfigProvider(
  109. $this->storeManager,
  110. $this->customerTokenManagement,
  111. [
  112. $vaultProviderCode => $tokenUiComponentProvider
  113. ]
  114. );
  115. $this->objectManager->setBackwardCompatibleProperty(
  116. $configProvider,
  117. 'vaultPaymentList',
  118. $this->vaultPaymentList
  119. );
  120. static::assertEquals($expectedConfig, $configProvider->getConfig());
  121. }
  122. }