Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TransparentTest.php
Go to the documentation of this file.
1 <?php
8 
14 
21 class TransparentTest extends \PHPUnit\Framework\TestCase
22 {
24  protected $object;
25 
27  protected $gatewayMock;
28 
30  protected $storeManagerMock;
31 
33  protected $configFactoryMock;
34 
36  protected $configMock;
37 
39  protected $responseMock;
40 
42  protected $paymentMock;
43 
45  protected $orderMock;
46 
49 
52 
57 
62  protected $responseValidator;
63 
64  protected function setUp()
65  {
66  $this->paymentMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Payment::class)
67  ->setMethods([])
68  ->disableOriginalConstructor()
69  ->getMock();
70 
71  $this->paymentTokenFactory = $this->getMockBuilder(CreditCardTokenFactory::class)
72  ->disableOriginalConstructor()
73  ->setMethods(['create'])
74  ->getMock();
75 
76  $this->gatewayMock = $this->getMockBuilder(\Magento\Paypal\Model\Payflow\Service\Gateway::class)
77  ->setMethods(['postRequest'])
78  ->disableOriginalConstructor()
79  ->getMock();
80  $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
81  ->setMethods(['getStore', 'getId'])
82  ->disableOriginalConstructor()
83  ->getMockForAbstractClass();
84  $this->storeManagerMock->method('getStore')
85  ->willReturnSelf();
86  $this->configMock = $this->getMockBuilder(\Magento\Paypal\Model\PayflowConfig::class)
87  ->disableOriginalConstructor()
88  ->getMock();
89  $this->configFactoryMock = $this->getMockBuilder(\Magento\Payment\Model\Method\ConfigInterfaceFactory::class)
90  ->setMethods(['create'])
91  ->disableOriginalConstructor()
92  ->getMock();
93  $this->configFactoryMock->method('create')
94  ->willReturn($this->configMock);
95  $this->responseMock = new \Magento\Framework\DataObject();
96  $this->responseValidator = $this->getMockBuilder(
97  \Magento\Paypal\Model\Payflow\Service\Response\Validator\ResponseValidator::class
98  )->disableOriginalConstructor()
99  ->setMethods(['validate'])
100  ->getMock();
101 
102  $objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
103  $this->object = $objectHelper->getObject(
104  \Magento\Paypal\Model\Payflow\Transparent::class,
105  [
106  'gateway' => $this->gatewayMock,
107  'storeManager' => $this->storeManagerMock,
108  'configFactory' => $this->configFactoryMock,
109  'responseValidator' => $this->responseValidator,
110  'paymentTokenFactory' => $this->paymentTokenFactory
111  ]
112  );
113  }
114 
120  protected function initializationAuthorizeMock()
121  {
122  $this->orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
123  ->setMethods([
124  'getCustomerId', 'getBillingAddress', 'getShippingAddress', 'getCustomerEmail',
125  'getId', 'getIncrementId', 'getBaseCurrencyCode'
126  ])
127  ->disableOriginalConstructor()
128  ->getMock();
129  $this->addressBillingMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
130  ->setMethods(
131  [
132  'getFirstname',
133  'getLastname',
134  'getStreet',
135  'getCity',
136  'getRegionCode',
137  'getPostcode',
138  'getCountryId'
139  ]
140  )->disableOriginalConstructor()
141  ->getMock();
142  $this->addressShippingMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
143  ->setMethods(
144  [
145  'getFirstname',
146  'getLastname',
147  'getStreet',
148  'getCity',
149  'getRegionCode',
150  'getPostcode',
151  'getCountryId'
152  ]
153  )->disableOriginalConstructor()
154  ->getMock();
155  }
156 
162  protected function buildRequestData()
163  {
164  $this->paymentMock->expects($this->once())
165  ->method('getOrder')
166  ->willReturn($this->orderMock);
167  $this->orderMock->expects($this->once())
168  ->method('getBaseCurrencyCode')
169  ->willReturn('USD');
170  $this->orderMock->expects($this->once())
171  ->method('getBillingAddress')
172  ->willReturn($this->addressBillingMock);
173  $this->orderMock->expects(static::once())
174  ->method('getId')
175  ->willReturn(1);
176  $this->orderMock->expects(static::once())
177  ->method('getIncrementId')
178  ->willReturn('0000001');
179  $this->orderMock->expects($this->once())
180  ->method('getShippingAddress')
181  ->willReturn($this->addressShippingMock);
182  $this->addressBillingMock->expects($this->once())
183  ->method('getFirstname')
184  ->willReturn('Firstname');
185  $this->addressBillingMock->expects($this->once())
186  ->method('getLastname')
187  ->willReturn('Lastname');
188  $this->addressBillingMock->expects($this->once())
189  ->method('getStreet')
190  ->willReturn(['street-1', 'street-2']);
191  $this->addressBillingMock->expects($this->once())
192  ->method('getCity')
193  ->willReturn('City');
194  $this->addressBillingMock->expects($this->once())
195  ->method('getRegionCode')
196  ->willReturn('RegionCode');
197  $this->addressBillingMock->expects($this->once())
198  ->method('getPostcode')
199  ->willReturn('Postcode');
200  $this->addressBillingMock->expects($this->once())
201  ->method('getCountryId')
202  ->willReturn('CountryId');
203  $this->orderMock->expects($this->once())
204  ->method('getCustomerEmail')
205  ->willReturn('[email protected]');
206  $this->addressShippingMock->expects($this->once())
207  ->method('getFirstname')
208  ->willReturn('Firstname');
209  $this->addressShippingMock->expects($this->once())
210  ->method('getLastname')
211  ->willReturn('Lastname');
212  $this->addressShippingMock->expects($this->once())
213  ->method('getStreet')
214  ->willReturn(['street-1', 'street-2']);
215  $this->addressShippingMock->expects($this->once())
216  ->method('getCity')
217  ->willReturn('City');
218  $this->addressShippingMock->expects($this->once())
219  ->method('getRegionCode')
220  ->willReturn('RegionCode');
221  $this->addressShippingMock->expects($this->once())
222  ->method('getPostcode')
223  ->willReturn('Postcode');
224  $this->addressShippingMock->expects($this->once())
225  ->method('getCountryId')
226  ->willReturn('CountryId');
227  }
228 
232  protected function crateVoidResponseMock()
233  {
234  $voidResponseMock = new \Magento\Framework\DataObject(
235  [
236  'result_code' => Transparent::RESPONSE_CODE_APPROVED,
237  'pnref' => 'test-pnref'
238  ]
239  );
240 
241  $this->responseMock->setData(Transparent::PNREF, 'test-pnref');
242 
243  $this->paymentMock->expects($this->once())
244  ->method('setParentTransactionId')
245  ->with('test-pnref');
246  $this->paymentMock->expects($this->once())
247  ->method('getParentTransactionId')
248  ->willReturn('test-pnref');
249  $this->paymentMock->expects($this->once())
250  ->method('setTransactionId')
251  ->with('test-pnref')
252  ->willReturnSelf();
253  $this->paymentMock->expects($this->once())
254  ->method('setIsTransactionClosed')
255  ->with(1)
256  ->willReturnSelf();
257  $this->paymentMock->expects($this->once())
258  ->method('setShouldCloseParentTransaction')
259  ->with(1);
260 
261  return $voidResponseMock;
262  }
263 
267  public function testAuthorizeException()
268  {
270  $this->buildRequestData();
271 
272  $this->gatewayMock->expects($this->once())
273  ->method('postRequest')
274  ->willThrowException(new \Exception());
275 
276  $this->object->authorize($this->paymentMock, 33);
277  }
278 
284  {
286  $this->buildRequestData();
287  $voidResponseMock = $this->crateVoidResponseMock();
288 
289  $this->gatewayMock->expects($this->at(0))
290  ->method('postRequest')
291  ->willReturn($this->responseMock);
292 
293  $this->responseValidator->expects($this->once())
294  ->method('validate')
295  ->with($this->responseMock)
296  ->willThrowException(new \Magento\Framework\Exception\LocalizedException(__('Error')));
297 
298  $this->gatewayMock->expects($this->at(1))
299  ->method('postRequest')
300  ->willReturn($voidResponseMock);
301 
302  $this->paymentMock->expects($this->once())
303  ->method('getAdditionalInformation')
304  ->with(Payflowpro::PNREF)
305  ->willReturn('test-pnref');
306 
307  $this->responseMock->setData('result_code', Payflowpro::RESPONSE_CODE_FRAUDSERVICE_FILTER);
308 
309  $this->object->authorize($this->paymentMock, 33);
310  }
311 
320  $resultCode,
321  $origResult
322  ) {
324  $this->buildRequestData();
325 
326  $this->responseMock->setData('result_code', $resultCode);
327  $this->responseMock->setData('origresult', $origResult);
328 
329  $this->gatewayMock->expects($this->exactly(1))
330  ->method('postRequest')
331  ->willReturn($this->responseMock);
332  $this->object->authorize($this->paymentMock, 33);
333  }
334 
339  {
340  return [
341  [
342  'origResult' => Payflowpro::RESPONSE_CODE_APPROVED,
344  ],
345  [
348  ],
349  [
350  'origResult' => Payflowpro::RESPONSE_CODE_DECLINED,
351  'resultCode' => 1111111111
352  ],
353  [
354  'origResult' => 3432432423,
355  'resultCode' => 23233432423
356  ],
357  ];
358  }
359 
364  public function testAuthorize()
365  {
367  $this->buildRequestData();
368 
369  $paymentTokenMock = $this->createMock(PaymentTokenInterface::class);
370  $extensionAttributes = $this->getMockBuilder(\Magento\Sales\Api\Data\OrderPaymentExtensionInterface::class)
371  ->disableOriginalConstructor()
372  ->setMethods(['setVaultPaymentToken'])
373  ->getMockForAbstractClass();
374  $ccDetails = [
375  'cc_type' => 'VI',
376  'cc_number' => '1111'
377  ];
378 
379  $this->responseMock->setData('result_code', Payflowpro::RESPONSE_CODE_APPROVED);
380  $this->responseMock->setData('origresult', 0);
381  $this->responseMock->setData('pnref', 'test-pnref');
382 
383  $this->gatewayMock->expects($this->once())->method('postRequest')->willReturn($this->responseMock);
384 
385  $this->responseValidator->expects($this->once())
386  ->method('validate')
387  ->with($this->responseMock);
388 
389  $this->paymentMock->expects($this->once())
390  ->method('setTransactionId')
391  ->with('test-pnref')
392  ->willReturnSelf();
393  $this->paymentMock->expects($this->once())
394  ->method('setIsTransactionClosed')
395  ->with(0);
396  $this->paymentMock->expects($this->once())
397  ->method('getCcExpYear')
398  ->willReturn('2017');
399  $this->paymentMock->expects($this->once())
400  ->method('getCcExpMonth')
401  ->willReturn('12');
402  $this->paymentMock->expects(static::any())
403  ->method('getAdditionalInformation')
404  ->willReturnMap(
405  [
406  [Transparent::CC_DETAILS, $ccDetails],
407  [Transparent::PNREF, 'test-pnref']
408  ]
409  );
410 
411  $this->paymentTokenFactory->expects(static::once())
412  ->method('create')
413  ->willReturn($paymentTokenMock);
414  $paymentTokenMock->expects(static::once())
415  ->method('setGatewayToken')
416  ->with('test-pnref');
417  $paymentTokenMock->expects(static::once())
418  ->method('setTokenDetails')
419  ->with(json_encode($ccDetails));
420  $paymentTokenMock->expects(static::once())
421  ->method('setExpiresAt')
422  ->with('2018-01-01 00:00:00');
423 
424  $this->paymentMock->expects(static::once())
425  ->method('getExtensionAttributes')
426  ->willReturn($extensionAttributes);
427  $extensionAttributes->expects(static::once())
428  ->method('setVaultPaymentToken')
429  ->with($paymentTokenMock);
430 
431  $this->paymentMock->expects($this->at(8))
432  ->method('unsAdditionalInformation')
433  ->with(Transparent::CC_DETAILS);
434  $this->paymentMock->expects($this->at(9))
435  ->method('unsAdditionalInformation')
436  ->with(Transparent::PNREF);
437 
438  $this->assertSame($this->object, $this->object->authorize($this->paymentMock, 33));
439  }
440 
444  public function testGetInfoBlockType()
445  {
446  static::assertEquals(Info::class, $this->object->getInfoBlockType());
447  }
448 }
__()
Definition: __.php:13
$extensionAttributes
Definition: payment.php:22