|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Vault\Test\Unit\Plugin; |
| 9 | + |
| 10 | +use Magento\Checkout\Api\PaymentInformationManagementInterface; |
| 11 | +use Magento\Store\Api\Data\StoreInterface; |
| 12 | +use Magento\Store\Model\StoreManagerInterface; |
| 13 | +use Magento\Vault\Api\PaymentMethodListInterface; |
| 14 | +use Magento\Vault\Plugin\PaymentVaultInformationManagement; |
| 15 | +use Magento\Quote\Api\Data\PaymentInterface; |
| 16 | +use PHPUnit\Framework\MockObject\MockObject; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | + |
| 19 | +/** |
| 20 | + * Test for payment vault information management plugin |
| 21 | + */ |
| 22 | +class PaymentVaultInformationManagementTest extends TestCase |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var StoreManagerInterface|MockObject |
| 26 | + */ |
| 27 | + private $storeManager; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var StoreInterface|MockObject |
| 31 | + */ |
| 32 | + private $store; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var PaymentMethodListInterface|MockObject |
| 36 | + */ |
| 37 | + private $paymentMethodList; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var PaymentVaultInformationManagement |
| 41 | + */ |
| 42 | + private $plugin; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var PaymentInformationManagementInterface|MockObject |
| 46 | + */ |
| 47 | + private $paymentInformationManagement; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var PaymentInterface|MockObject |
| 51 | + */ |
| 52 | + private $payment; |
| 53 | + |
| 54 | + /** |
| 55 | + * @inheritDoc |
| 56 | + */ |
| 57 | + protected function setUp(): void |
| 58 | + { |
| 59 | + $this->storeManager = $this->getMockBuilder(StoreManagerInterface::class) |
| 60 | + ->disableOriginalConstructor() |
| 61 | + ->onlyMethods(['getStore']) |
| 62 | + ->getMockForAbstractClass(); |
| 63 | + $this->store = $this->getMockBuilder(StoreInterface::class) |
| 64 | + ->disableOriginalConstructor() |
| 65 | + ->onlyMethods(['getId']) |
| 66 | + ->getMockForAbstractClass(); |
| 67 | + $this->paymentMethodList = $this->getMockBuilder(PaymentMethodListInterface::class) |
| 68 | + ->disableOriginalConstructor() |
| 69 | + ->onlyMethods(['getActiveList']) |
| 70 | + ->getMockForAbstractClass(); |
| 71 | + $this->paymentInformationManagement = $this |
| 72 | + ->getMockBuilder(PaymentInformationManagementInterface::class) |
| 73 | + ->disableOriginalConstructor() |
| 74 | + ->getMockForAbstractClass(); |
| 75 | + $this->payment = $this->getMockBuilder(PaymentInterface::class) |
| 76 | + ->onlyMethods(['setMethod']) |
| 77 | + ->disableOriginalConstructor() |
| 78 | + ->getMockForAbstractClass(); |
| 79 | + $this->plugin = new PaymentVaultInformationManagement($this->paymentMethodList, $this->storeManager); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Test payment method for vault before saving payment information |
| 84 | + * |
| 85 | + * @param string $requestPaymentMethodCode |
| 86 | + * @param string $methodCode |
| 87 | + * @dataProvider vaultPaymentMethodDataProvider |
| 88 | + * |
| 89 | + * @return void |
| 90 | + */ |
| 91 | + public function testBeforeSavePaymentInformation($requestPaymentMethodCode, $methodCode): void |
| 92 | + { |
| 93 | + $this->store->method('getId') |
| 94 | + ->willReturn(1); |
| 95 | + $this->storeManager->method('getStore') |
| 96 | + ->willReturn($this->store); |
| 97 | + $activeVaultMethod = $this->getMockBuilder(PaymentInterface::class) |
| 98 | + ->disableOriginalConstructor() |
| 99 | + ->addMethods(['getCode', 'getProviderCode']) |
| 100 | + ->getMockForAbstractClass(); |
| 101 | + $activeVaultMethod->method('getCode') |
| 102 | + ->willReturn($methodCode); |
| 103 | + $this->paymentMethodList->method('getActiveList') |
| 104 | + ->willReturn([$activeVaultMethod]); |
| 105 | + $this->payment->method('getMethod') |
| 106 | + ->willReturn($requestPaymentMethodCode); |
| 107 | + $this->payment->expects($this->once()) |
| 108 | + ->method('setMethod') |
| 109 | + ->with($methodCode); |
| 110 | + |
| 111 | + $this->plugin->beforeSavePaymentInformation( |
| 112 | + $this->paymentInformationManagement, |
| 113 | + '1', |
| 114 | + $this->payment, |
| 115 | + null |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Data provider for BeforeSavePaymentInformation. |
| 121 | + * |
| 122 | + * @return array |
| 123 | + */ |
| 124 | + public function vaultPaymentMethodDataProvider(): array |
| 125 | + { |
| 126 | + return [ |
| 127 | + ['braintree_cc_vault_01', 'braintree_cc_vault'], |
| 128 | + ]; |
| 129 | + } |
| 130 | +} |
0 commit comments