RequestTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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\Hostedpro;
  7. use Magento\Framework\DataObject;
  8. use Magento\Sales\Model\Order;
  9. use Magento\Sales\Model\Order\Payment;
  10. class RequestTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  14. */
  15. protected $helper;
  16. /**
  17. * @var \Magento\Paypal\Model\Hostedpro\Request
  18. */
  19. protected $_model;
  20. protected $localeResolverMock;
  21. /**
  22. * @var \Magento\Tax\Helper\Data
  23. */
  24. protected $taxData;
  25. protected function setUp()
  26. {
  27. $this->helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  28. $this->localeResolverMock = $this->getMockBuilder(\Magento\Framework\Locale\Resolver::class)
  29. ->disableOriginalConstructor()
  30. ->getMock();
  31. $this->taxData = $this->helper->getObject(\Magento\Tax\Helper\Data::class);
  32. $this->_model = $this->helper->getObject(
  33. \Magento\Paypal\Model\Hostedpro\Request::class,
  34. [
  35. 'localeResolver' => $this->localeResolverMock,
  36. 'taxData' => $this->taxData
  37. ]
  38. );
  39. }
  40. /**
  41. * @param $billing
  42. * @param $shipping
  43. * @param $billingState
  44. * @param $state
  45. * @param $countryId
  46. * @dataProvider addressesDataProvider
  47. */
  48. public function testSetOrderAddresses($billing, $shipping, $billingState, $state, $countryId)
  49. {
  50. $payment = $this->getMockBuilder(Payment::class)
  51. ->disableOriginalConstructor()
  52. ->setMethods(['__wakeup'])
  53. ->getMock();
  54. $order = $this->getMockBuilder(Order::class)
  55. ->disableOriginalConstructor()
  56. ->setMethods(['getPayment', '__wakeup', 'getBillingAddress', 'getShippingAddress'])
  57. ->getMock();
  58. $order->expects(static::any())
  59. ->method('getPayment')
  60. ->will($this->returnValue($payment));
  61. $order->expects(static::any())
  62. ->method('getBillingAddress')
  63. ->will($this->returnValue($billing));
  64. $order->expects(static::any())
  65. ->method('getShippingAddress')
  66. ->will($this->returnValue($shipping));
  67. $this->_model->setOrder($order);
  68. static::assertEquals($billingState, $this->_model->getData('billing_state'));
  69. static::assertEquals($state, $this->_model->getData('state'));
  70. static::assertEquals($countryId, $this->_model->getData('billing_country'));
  71. static::assertEquals($countryId, $this->_model->getData('country'));
  72. }
  73. /**
  74. * @return array
  75. */
  76. public function addressesDataProvider()
  77. {
  78. $billing = new DataObject([
  79. 'firstname' => 'Firstname',
  80. 'lastname' => 'Lastname',
  81. 'city' => 'City',
  82. 'region_code' => 'CA',
  83. 'postcode' => '12346',
  84. 'country_id' => 'US',
  85. 'street' => '1 Ln Ave',
  86. ]);
  87. $shipping = new DataObject([
  88. 'firstname' => 'ShipFirstname',
  89. 'lastname' => 'ShipLastname',
  90. 'city' => 'ShipCity',
  91. 'region' => 'olala',
  92. 'postcode' => '12346',
  93. 'country_id' => 'US',
  94. 'street' => '1 Ln Ave',
  95. ]);
  96. $billing2 = new DataObject([
  97. 'firstname' => 'Firstname',
  98. 'lastname' => 'Lastname',
  99. 'city' => 'Culver City',
  100. 'region_code' => 'CA',
  101. 'postcode' => '12346',
  102. 'country_id' => 'US',
  103. 'street' => '1 Ln Ave',
  104. ]);
  105. $shipping2 = new DataObject([
  106. 'firstname' => 'ShipFirstname',
  107. 'lastname' => 'ShipLastname',
  108. 'city' => 'ShipCity',
  109. 'postcode' => '12346',
  110. 'country_id' => 'US',
  111. 'street' => '1 Ln Ave',
  112. ]);
  113. return [
  114. [$billing, $shipping, 'CA', 'olala', 'US'],
  115. [$billing2, $shipping2, 'CA', 'ShipCity', 'US']
  116. ];
  117. }
  118. public function testSetPaymentMethod()
  119. {
  120. $expectedData = [
  121. 'paymentaction' => 'authorization',
  122. 'notify_url' => 'https://test.com/notifyurl',
  123. 'cancel_return' => 'https://test.com/cancelurl',
  124. 'return' => 'https://test.com/returnurl',
  125. 'lc' => 'US',
  126. 'template' => 'mobile-iframe',
  127. 'showBillingAddress' => 'false',
  128. 'showShippingAddress' => 'true',
  129. 'showBillingEmail' => 'false',
  130. 'showBillingPhone' => 'false',
  131. 'showCustomerName' => 'false',
  132. 'showCardInfo' => 'true',
  133. 'showHostedThankyouPage' => 'false'
  134. ];
  135. $paymentMethodMock = $this->getMockBuilder(\Magento\Paypal\Model\Hostedpro::class)
  136. ->disableOriginalConstructor()
  137. ->setMethods([])
  138. ->getMock();
  139. $paymentMethodMock->expects($this->once())
  140. ->method('getConfigData')->with('payment_action')->willReturn('Authorization');
  141. $paymentMethodMock->expects($this->once())->method('getNotifyUrl')->willReturn('https://test.com/notifyurl');
  142. $paymentMethodMock->expects($this->once())->method('getCancelUrl')->willReturn('https://test.com/cancelurl');
  143. $paymentMethodMock->expects($this->once())->method('getReturnUrl')->willReturn('https://test.com/returnurl');
  144. $this->localeResolverMock->expects($this->once())->method('getLocale')->willReturn('en_US');
  145. $this->assertEquals($this->_model, $this->_model->setPaymentMethod($paymentMethodMock));
  146. $this->assertEquals('US', $this->_model->getData('lc'));
  147. $this->assertEquals($expectedData, $this->_model->getData());
  148. }
  149. /**
  150. * @covers \Magento\Paypal\Model\Hostedpro\Request::setOrder
  151. */
  152. public function testSetOrder()
  153. {
  154. $expectation = [
  155. 'invoice' => '#000001',
  156. 'address_override' => 'true',
  157. 'currency_code' => 'USD',
  158. 'buyer_email' => 'buyer@email.com',
  159. ];
  160. $order = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
  161. ->disableOriginalConstructor()
  162. ->getMock();
  163. $order->expects(static::once())
  164. ->method('getIncrementId')
  165. ->willReturn($expectation['invoice']);
  166. $order->expects(static::once())
  167. ->method('getBaseCurrencyCode')
  168. ->willReturn($expectation['currency_code']);
  169. $order->expects(static::once())
  170. ->method('getCustomerEmail')
  171. ->willReturn($expectation['buyer_email']);
  172. $this->_model->setOrder($order);
  173. static::assertEquals($expectation, $this->_model->getData());
  174. }
  175. /**
  176. * @covers \Magento\Paypal\Model\Hostedpro\Request::setAmount()
  177. * @param $subtotal
  178. * @param $total
  179. * @param $tax
  180. * @param $shipping
  181. * @param $discount
  182. * @dataProvider amountWithoutTaxDataProvider
  183. */
  184. public function testSetAmountWithoutTax($total, $subtotal, $tax, $shipping, $discount)
  185. {
  186. $expectation = [
  187. 'subtotal' => $subtotal,
  188. 'total' => $total,
  189. 'tax' => $tax,
  190. 'shipping' => $shipping,
  191. 'discount' => abs($discount)
  192. ];
  193. static::assertFalse($this->taxData->priceIncludesTax());
  194. $payment = $this->getMockBuilder(Payment::class)
  195. ->disableOriginalConstructor()
  196. ->getMock();
  197. $order = $this->getMockBuilder(Order::class)
  198. ->disableOriginalConstructor()
  199. ->getMock();
  200. $payment->expects(static::once())
  201. ->method('getBaseAmountAuthorized')
  202. ->willReturn($total);
  203. $order->expects(static::once())
  204. ->method('getPayment')
  205. ->willReturn($payment);
  206. $order->expects(static::once())
  207. ->method('getBaseDiscountAmount')
  208. ->willReturn($discount);
  209. $order->expects(static::once())
  210. ->method('getBaseTaxAmount')
  211. ->willReturn($tax);
  212. $order->expects(static::once())
  213. ->method('getBaseShippingAmount')
  214. ->willReturn($shipping);
  215. $order->expects(static::once())
  216. ->method('getBaseSubtotal')
  217. ->willReturn($subtotal);
  218. $this->_model->setAmount($order);
  219. static::assertEquals($expectation, $this->_model->getData());
  220. }
  221. /**
  222. * @covers \Magento\Paypal\Model\Hostedpro\Request::setAmount()
  223. * @param $total
  224. * @param $subtotal
  225. * @param $tax
  226. * @param $shipping
  227. * @param $discount
  228. * @dataProvider amountWithoutTaxZeroSubtotalDataProvider
  229. */
  230. public function testSetAmountWithoutTaxZeroSubtotal($total, $subtotal, $tax, $shipping, $discount)
  231. {
  232. $expectation = [
  233. 'subtotal' => $total,
  234. 'total' => $total,
  235. 'tax' => $tax,
  236. 'shipping' => $shipping,
  237. 'discount' => abs($discount)
  238. ];
  239. static::assertFalse($this->taxData->priceIncludesTax());
  240. $payment = $this->getMockBuilder(Payment::class)
  241. ->disableOriginalConstructor()
  242. ->getMock();
  243. $order = $this->getMockBuilder(Order::class)
  244. ->disableOriginalConstructor()
  245. ->getMock();
  246. $payment->expects(static::exactly(2))
  247. ->method('getBaseAmountAuthorized')
  248. ->willReturn($total);
  249. $order->expects(static::exactly(2))
  250. ->method('getPayment')
  251. ->willReturn($payment);
  252. $order->expects(static::once())
  253. ->method('getBaseDiscountAmount')
  254. ->willReturn($discount);
  255. $order->expects(static::once())
  256. ->method('getBaseTaxAmount')
  257. ->willReturn($tax);
  258. $order->expects(static::once())
  259. ->method('getBaseShippingAmount')
  260. ->willReturn($shipping);
  261. $order->expects(static::once())
  262. ->method('getBaseSubtotal')
  263. ->willReturn($subtotal);
  264. $this->_model->setAmount($order);
  265. static::assertEquals($expectation, $this->_model->getData());
  266. }
  267. /**
  268. * @covers \Magento\Paypal\Model\Hostedpro\Request::setAmount()
  269. */
  270. public function testSetAmountWithIncludedTax()
  271. {
  272. /** @var \Magento\Tax\Model\Config $config */
  273. $config = $this->helper->getObject(\Magento\Tax\Model\Config::class);
  274. $config->setPriceIncludesTax(true);
  275. $this->taxData = $this->helper->getObject(
  276. \Magento\Tax\Helper\Data::class,
  277. [
  278. 'taxConfig' => $config
  279. ]
  280. );
  281. $this->_model = $this->helper->getObject(
  282. \Magento\Paypal\Model\Hostedpro\Request::class,
  283. [
  284. 'localeResolver' => $this->localeResolverMock,
  285. 'taxData' => $this->taxData
  286. ]
  287. );
  288. static::assertTrue($this->taxData->getConfig()->priceIncludesTax());
  289. $amount = 19.65;
  290. $expectation = [
  291. 'amount' => $amount,
  292. 'subtotal' => $amount
  293. ];
  294. $payment = $this->getMockBuilder(\Magento\Sales\Model\Order\Payment::class)
  295. ->disableOriginalConstructor()
  296. ->getMock();
  297. $order = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
  298. ->disableOriginalConstructor()
  299. ->getMock();
  300. $payment->expects(static::once())
  301. ->method('getBaseAmountAuthorized')
  302. ->willReturn($amount);
  303. $order->expects(static::once())
  304. ->method('getPayment')
  305. ->willReturn($payment);
  306. $this->_model->setAmount($order);
  307. static::assertEquals($expectation, $this->_model->getData());
  308. }
  309. /**
  310. * Get data for amount with tax tests
  311. * @return array
  312. */
  313. public function amountWithoutTaxDataProvider()
  314. {
  315. return [
  316. ['total' => 31.00, 'subtotal' => 10.00, 'tax' => 1.00, 'shipping' => 20.00, 'discount' => 0.00],
  317. ['total' => 5.00, 'subtotal' => 10.00, 'tax' => 0.00, 'shipping' => 20.00, 'discount' => -25.00],
  318. ];
  319. }
  320. /**
  321. * @return array
  322. */
  323. public function amountWithoutTaxZeroSubtotalDataProvider()
  324. {
  325. return [
  326. ['total' => 10.00, 'subtotal' => 0.00, 'tax' => 0.00, 'shipping' => 20.00, 'discount' => 0.00],
  327. ];
  328. }
  329. }