Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FormTest.php
Go to the documentation of this file.
1 <?php
7 
10 use Magento\Braintree\Gateway\Config\Config as GatewayConfig;
17 use PHPUnit_Framework_MockObject_MockObject as MockObject;
18 
22 class FormTest extends \PHPUnit\Framework\TestCase
23 {
24  public static $baseCardTypes = [
25  'AE' => 'American Express',
26  'VI' => 'Visa',
27  'MC' => 'MasterCard',
28  'DI' => 'Discover',
29  'JBC' => 'JBC',
30  'CUP' => 'China Union Pay',
31  'MI' => 'Maestro',
32  ];
33 
34  public static $configCardTypes = [
35  'AE', 'VI', 'MC', 'DI', 'JBC'
36  ];
37 
41  private $block;
42 
46  private $sessionQuoteMock;
47 
51  private $gatewayConfigMock;
52 
56  private $ccTypeMock;
57 
61  private $paymentDataHelperMock;
62 
66  private $storeId = '1';
67 
68  protected function setUp()
69  {
70  $this->initCcTypeMock();
71  $this->initSessionQuoteMock();
72  $this->initGatewayConfigMock();
73 
74  $this->paymentDataHelperMock = $this->getMockBuilder(Data::class)
75  ->disableOriginalConstructor()
76  ->setMethods(['getMethodInstance'])
77  ->getMock();
78 
79  $managerHelper = new ObjectManager($this);
80  $this->block = $managerHelper->getObject(Form::class, [
81  'paymentConfig' => $managerHelper->getObject(Config::class),
82  'sessionQuote' => $this->sessionQuoteMock,
83  'gatewayConfig' => $this->gatewayConfigMock,
84  'ccType' => $this->ccTypeMock,
85  'paymentDataHelper' =>$this->paymentDataHelperMock,
86  ]);
87  }
88 
96  public function testGetCcAvailableTypes($countryId, array $availableTypes, array $expected)
97  {
98  $this->sessionQuoteMock->expects(static::once())
99  ->method('getCountryId')
100  ->willReturn($countryId);
101 
102  $this->gatewayConfigMock->expects(static::once())
103  ->method('getAvailableCardTypes')
104  ->with($this->storeId)
105  ->willReturn(self::$configCardTypes);
106 
107  $this->gatewayConfigMock->expects(static::once())
108  ->method('getCountryAvailableCardTypes')
109  ->with($countryId, $this->storeId)
110  ->willReturn($availableTypes);
111 
112  $result = $this->block->getCcAvailableTypes();
113  static::assertEquals($expected, array_values($result));
114  }
115 
121  {
122  return [
123  ['US', ['AE', 'VI'], ['American Express', 'Visa']],
124  ['UK', ['VI'], ['Visa']],
125  ['CA', ['MC'], ['MasterCard']],
126  ['UA', [], ['American Express', 'Visa', 'MasterCard', 'Discover', 'JBC']],
127  ];
128  }
129 
133  public function testIsVaultEnabled()
134  {
135  $vaultPayment = $this->getMockForAbstractClass(VaultPaymentInterface::class);
136  $this->paymentDataHelperMock->expects(static::once())
137  ->method('getMethodInstance')
139  ->willReturn($vaultPayment);
140 
141  $vaultPayment->expects(static::once())
142  ->method('isActive')
143  ->with($this->storeId)
144  ->willReturn(true);
145 
146  static::assertTrue($this->block->isVaultEnabled());
147  }
148 
152  private function initCcTypeMock()
153  {
154  $this->ccTypeMock = $this->getMockBuilder(CcType::class)
155  ->disableOriginalConstructor()
156  ->setMethods(['getCcTypeLabelMap'])
157  ->getMock();
158 
159  $this->ccTypeMock->expects(static::any())
160  ->method('getCcTypeLabelMap')
161  ->willReturn(self::$baseCardTypes);
162  }
163 
167  private function initSessionQuoteMock()
168  {
169  $this->sessionQuoteMock = $this->getMockBuilder(Quote::class)
170  ->disableOriginalConstructor()
171  ->setMethods(['getQuote', 'getBillingAddress', 'getCountryId', '__wakeup', 'getStoreId'])
172  ->getMock();
173 
174  $this->sessionQuoteMock->expects(static::any())
175  ->method('getQuote')
176  ->willReturnSelf();
177  $this->sessionQuoteMock->expects(static::any())
178  ->method('getBillingAddress')
179  ->willReturnSelf();
180  $this->sessionQuoteMock->expects(static::any())
181  ->method('getStoreId')
182  ->willReturn($this->storeId);
183  }
184 
188  private function initGatewayConfigMock()
189  {
190  $this->gatewayConfigMock = $this->getMockBuilder(GatewayConfig::class)
191  ->disableOriginalConstructor()
192  ->setMethods(['getCountryAvailableCardTypes', 'getAvailableCardTypes'])
193  ->getMock();
194  }
195 }
testGetCcAvailableTypes($countryId, array $availableTypes, array $expected)
Definition: FormTest.php:96