InfoTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Test\Unit\Model;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. use Magento\Paypal\Model\Info;
  9. class InfoTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /** @var \Magento\Paypal\Model\Info */
  12. protected $info;
  13. /** @var ObjectManagerHelper */
  14. protected $objectManagerHelper;
  15. protected function setUp()
  16. {
  17. $this->objectManagerHelper = new ObjectManagerHelper($this);
  18. $this->info = $this->objectManagerHelper->getObject(
  19. \Magento\Paypal\Model\Info::class
  20. );
  21. }
  22. /**
  23. * @dataProvider additionalInfoDataProvider
  24. * @param array $additionalInfo
  25. * @param array $expectation
  26. */
  27. public function testGetPaymentInfo($additionalInfo, $expectation)
  28. {
  29. /** @var \Magento\Payment\Model\InfoInterface $paymentInfo */
  30. $paymentInfo = $this->objectManagerHelper->getObject(\Magento\Payment\Model\Info::class);
  31. $paymentInfo->setAdditionalInformation($additionalInfo);
  32. $this->assertEquals($expectation, $this->info->getPaymentInfo($paymentInfo));
  33. }
  34. /**
  35. * @dataProvider additionalInfoDataProvider
  36. * @param array $additionalInfo
  37. * @param array $expectation
  38. */
  39. public function testGetPaymentInfoLabelValues($additionalInfo, $expectation)
  40. {
  41. /** @var \Magento\Payment\Model\InfoInterface $paymentInfo */
  42. $paymentInfo = $this->objectManagerHelper->getObject(\Magento\Payment\Model\Info::class);
  43. $paymentInfo->setAdditionalInformation($additionalInfo);
  44. $this->assertEquals(
  45. $this->_prepareLabelValuesExpectation($expectation),
  46. $this->info->getPaymentInfo($paymentInfo, true)
  47. );
  48. }
  49. /**
  50. * @dataProvider additionalInfoPublicDataProvider
  51. * @param array $additionalInfo
  52. * @param array $expectation
  53. */
  54. public function testGetPublicPaymentInfo($additionalInfo, $expectation)
  55. {
  56. /** @var \Magento\Payment\Model\InfoInterface $paymentInfo */
  57. $paymentInfo = $this->objectManagerHelper->getObject(\Magento\Payment\Model\Info::class);
  58. $paymentInfo->setAdditionalInformation($additionalInfo);
  59. $this->assertEquals(
  60. $this->_prepareLabelValuesExpectation($expectation),
  61. $this->info->getPublicPaymentInfo($paymentInfo, true)
  62. );
  63. }
  64. /**
  65. * @dataProvider additionalInfoPublicDataProvider
  66. * @param array $additionalInfo
  67. * @param array $expectation
  68. */
  69. public function testGetPublicPaymentInfoLabelValues($additionalInfo, $expectation)
  70. {
  71. /** @var \Magento\Payment\Model\InfoInterface $paymentInfo */
  72. $paymentInfo = $this->objectManagerHelper->getObject(\Magento\Payment\Model\Info::class);
  73. $paymentInfo->setAdditionalInformation($additionalInfo);
  74. $this->assertEquals($expectation, $this->info->getPublicPaymentInfo($paymentInfo));
  75. }
  76. /**
  77. * @dataProvider importToPaymentDataProvider
  78. * @param array $mapping
  79. * @param array $expectation
  80. */
  81. public function testImportToPayment($mapping, $expectation)
  82. {
  83. // we create $from object, based on mapping
  84. $from = new \Magento\Framework\DataObject($mapping);
  85. /** @var \Magento\Payment\Model\InfoInterface $paymentInfo */
  86. $paymentInfo = $this->objectManagerHelper->getObject(\Magento\Payment\Model\Info::class);
  87. $this->info->importToPayment($from, $paymentInfo);
  88. $this->assertEquals($expectation, $paymentInfo->getAdditionalInformation());
  89. }
  90. /**
  91. * @dataProvider importToPaymentDataProvider
  92. * @param array $mapping
  93. * @param array $expectation
  94. */
  95. public function testExportFromPayment($mapping, $expectation)
  96. {
  97. /** @var \Magento\Payment\Model\InfoInterface $paymentInfo */
  98. $paymentInfo = $this->objectManagerHelper->getObject(\Magento\Payment\Model\Info::class);
  99. $paymentInfo->setAdditionalInformation($expectation);
  100. // we create $to empty object
  101. $to = new \Magento\Framework\DataObject();
  102. $this->info->exportFromPayment($paymentInfo, $to);
  103. $this->assertEquals($mapping, $to->getData());
  104. }
  105. /**
  106. * @dataProvider importToPaymentDataProvider
  107. * @param array $mapping
  108. * @param array $expectation
  109. */
  110. public function testExportFromPaymentCustomMapping($mapping, $expectation)
  111. {
  112. /** @var \Magento\Payment\Model\InfoInterface $paymentInfo */
  113. $paymentInfo = $this->objectManagerHelper->getObject(\Magento\Payment\Model\Info::class);
  114. $paymentInfo->setAdditionalInformation($expectation);
  115. // we create $to empty object
  116. $to = new \Magento\Framework\DataObject();
  117. $this->info->exportFromPayment($paymentInfo, $to, array_flip($mapping));
  118. $this->assertEquals($mapping, $to->getData());
  119. }
  120. /**
  121. * Converts expectation result from ['key' => ['label' => 'Label', 'value' => 'Value']] to ['Label' => 'Value']
  122. *
  123. * @param $expectation
  124. * @return array
  125. */
  126. private function _prepareLabelValuesExpectation($expectation)
  127. {
  128. $labelValueExpectation = [];
  129. foreach ($expectation as $data) {
  130. $labelValueExpectation[$data['label']] = $data['value'];
  131. }
  132. return $labelValueExpectation;
  133. }
  134. /**
  135. * List of Labels
  136. *
  137. * @return array
  138. */
  139. public function additionalInfoDataProvider()
  140. {
  141. return include __DIR__ . '/_files/additional_info_data.php';
  142. }
  143. /**
  144. * List of public labels
  145. *
  146. * @return array
  147. */
  148. public function additionalInfoPublicDataProvider()
  149. {
  150. return [
  151. [
  152. [
  153. Info::PAYPAL_PAYER_EMAIL => Info::PAYPAL_PAYER_EMAIL,
  154. Info::BUYER_TAX_ID => Info::BUYER_TAX_ID,
  155. Info::BUYER_TAX_ID_TYPE => Info::BUYER_TAX_ID_TYPE_CNPJ,
  156. ],
  157. [
  158. Info::PAYPAL_PAYER_EMAIL => [
  159. 'label' => 'Payer Email',
  160. 'value' => Info::PAYPAL_PAYER_EMAIL,
  161. ],
  162. Info::BUYER_TAX_ID => [
  163. 'label' => 'Buyer\'s Tax ID',
  164. 'value' => Info::BUYER_TAX_ID,
  165. ],
  166. Info::BUYER_TAX_ID_TYPE => [
  167. 'label' => 'Buyer\'s Tax ID Type',
  168. 'value' => 'CNPJ',
  169. ]
  170. ],
  171. ],
  172. [
  173. [
  174. Info::PAYPAL_PAYER_EMAIL => Info::PAYPAL_PAYER_EMAIL,
  175. Info::BUYER_TAX_ID => Info::BUYER_TAX_ID,
  176. Info::BUYER_TAX_ID_TYPE => Info::BUYER_TAX_ID_TYPE,
  177. ],
  178. [
  179. Info::PAYPAL_PAYER_EMAIL => [
  180. 'label' => 'Payer Email',
  181. 'value' => Info::PAYPAL_PAYER_EMAIL,
  182. ],
  183. Info::BUYER_TAX_ID => [
  184. 'label' => 'Buyer\'s Tax ID',
  185. 'value' => Info::BUYER_TAX_ID,
  186. ]
  187. ]
  188. ]
  189. ];
  190. }
  191. /**
  192. * Mapping and expectation
  193. *
  194. * @return array
  195. */
  196. public function importToPaymentDataProvider()
  197. {
  198. return [
  199. [
  200. [
  201. Info::PAYER_ID => Info::PAYPAL_PAYER_ID,
  202. Info::PAYER_EMAIL => Info::PAYPAL_PAYER_EMAIL,
  203. Info::PAYER_STATUS => Info::PAYPAL_PAYER_STATUS,
  204. Info::ADDRESS_ID => Info::PAYPAL_ADDRESS_ID,
  205. Info::ADDRESS_STATUS => Info::PAYPAL_ADDRESS_STATUS,
  206. Info::PROTECTION_EL => Info::PAYPAL_PROTECTION_ELIGIBILITY,
  207. Info::FRAUD_FILTERS => Info::PAYPAL_FRAUD_FILTERS,
  208. Info::CORRELATION_ID => Info::PAYPAL_CORRELATION_ID,
  209. Info::AVS_CODE => Info::PAYPAL_AVS_CODE,
  210. Info::CVV_2_MATCH => Info::PAYPAL_CVV_2_MATCH,
  211. Info::BUYER_TAX_ID => Info::BUYER_TAX_ID,
  212. Info::BUYER_TAX_ID_TYPE => Info::BUYER_TAX_ID_TYPE,
  213. Info::PAYMENT_STATUS => Info::PAYMENT_STATUS_GLOBAL,
  214. Info::PENDING_REASON => Info::PENDING_REASON_GLOBAL,
  215. Info::IS_FRAUD => Info::IS_FRAUD_GLOBAL,
  216. ],
  217. [
  218. Info::PAYPAL_PAYER_ID => Info::PAYPAL_PAYER_ID,
  219. Info::PAYPAL_PAYER_EMAIL => Info::PAYPAL_PAYER_EMAIL,
  220. Info::PAYPAL_PAYER_STATUS => Info::PAYPAL_PAYER_STATUS,
  221. Info::PAYPAL_ADDRESS_ID => Info::PAYPAL_ADDRESS_ID,
  222. Info::PAYPAL_ADDRESS_STATUS => Info::PAYPAL_ADDRESS_STATUS,
  223. Info::PAYPAL_PROTECTION_ELIGIBILITY => Info::PAYPAL_PROTECTION_ELIGIBILITY,
  224. Info::PAYPAL_FRAUD_FILTERS => Info::PAYPAL_FRAUD_FILTERS,
  225. Info::PAYPAL_CORRELATION_ID => Info::PAYPAL_CORRELATION_ID,
  226. Info::PAYPAL_AVS_CODE => Info::PAYPAL_AVS_CODE,
  227. Info::PAYPAL_CVV_2_MATCH => Info::PAYPAL_CVV_2_MATCH,
  228. Info::BUYER_TAX_ID => Info::BUYER_TAX_ID,
  229. Info::BUYER_TAX_ID_TYPE => Info::BUYER_TAX_ID_TYPE,
  230. Info::PAYMENT_STATUS_GLOBAL => Info::PAYMENT_STATUS_GLOBAL,
  231. Info::PENDING_REASON_GLOBAL => Info::PENDING_REASON_GLOBAL,
  232. Info::IS_FRAUD_GLOBAL => Info::IS_FRAUD_GLOBAL
  233. ],
  234. ]
  235. ];
  236. }
  237. }