PaymentMethodManagementTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Quote\Test\Unit\Model;
  8. class PaymentMethodManagementTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Quote\Model\PaymentMethodManagement
  12. */
  13. protected $model;
  14. /**
  15. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  16. */
  17. protected $objectManager;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $quoteRepositoryMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $methodListMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $zeroTotalMock;
  30. protected function setUp()
  31. {
  32. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  33. $this->quoteRepositoryMock = $this->getMockForAbstractClass(
  34. \Magento\Quote\Api\CartRepositoryInterface::class,
  35. [],
  36. '',
  37. false,
  38. true,
  39. true,
  40. []
  41. );
  42. $this->methodListMock = $this->createMock(\Magento\Payment\Model\MethodList::class);
  43. $this->zeroTotalMock = $this->createMock(\Magento\Payment\Model\Checks\ZeroTotal::class);
  44. $this->model = $this->objectManager->getObject(
  45. \Magento\Quote\Model\PaymentMethodManagement::class,
  46. [
  47. 'quoteRepository' => $this->quoteRepositoryMock,
  48. 'methodList' => $this->methodListMock,
  49. 'zeroTotalValidator' => $this->zeroTotalMock
  50. ]
  51. );
  52. }
  53. public function testGetPaymentIfPaymentMethodNotSet()
  54. {
  55. $cartId = 11;
  56. $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  57. $paymentMock = $this->createMock(\Magento\Quote\Model\Quote\Payment::class);
  58. $quoteMock->expects($this->once())->method('getPayment')->will($this->returnValue($paymentMock));
  59. $paymentMock->expects($this->once())->method('getId')->will($this->returnValue(null));
  60. $this->quoteRepositoryMock->expects($this->once())
  61. ->method('get')
  62. ->with($cartId)
  63. ->will($this->returnValue($quoteMock));
  64. $this->assertNull($this->model->get($cartId));
  65. }
  66. public function testGetPaymentSuccess()
  67. {
  68. $cartId = 11;
  69. $paymentMock = $this->createMock(\Magento\Quote\Model\Quote\Payment::class);
  70. $paymentMock->expects($this->once())->method('getId')->will($this->returnValue(1));
  71. $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  72. $quoteMock->expects($this->once())->method('getPayment')->will($this->returnValue($paymentMock));
  73. $this->quoteRepositoryMock->expects($this->once())
  74. ->method('get')
  75. ->with($cartId)
  76. ->will($this->returnValue($quoteMock));
  77. $this->assertEquals($paymentMock, $this->model->get($cartId));
  78. }
  79. public function testGetList()
  80. {
  81. $cartId = 10;
  82. $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  83. $this->quoteRepositoryMock->expects($this->once())
  84. ->method('get')
  85. ->with($cartId)
  86. ->will($this->returnValue($quoteMock));
  87. $paymentMethod = $this->createMock(\Magento\Quote\Api\Data\PaymentMethodInterface::class);
  88. $this->methodListMock->expects($this->once())
  89. ->method('getAvailableMethods')
  90. ->with($quoteMock)
  91. ->will($this->returnValue([$paymentMethod]));
  92. $this->assertEquals([$paymentMethod], $this->model->getList($cartId));
  93. }
  94. public function testSetVirtualProduct()
  95. {
  96. $cartId = 100;
  97. $paymentId = 200;
  98. $methodDataWithAdditionalData = ['method' => 'data', 'additional_data' => ['additional' => 'value']];
  99. $methodData = $methodDataWithAdditionalData;
  100. $paymentMethod = 'checkmo';
  101. $quoteMock = $this->createPartialMock(
  102. \Magento\Quote\Model\Quote::class,
  103. ['setTotalsCollectedFlag', 'getPayment', 'isVirtual', 'getBillingAddress', 'collectTotals', 'save']
  104. );
  105. $this->quoteRepositoryMock->expects($this->once())->method('get')->with($cartId)->willReturn($quoteMock);
  106. $methodMock = $this->createPartialMock(\Magento\Quote\Model\Quote\Payment::class, ['setChecks', 'getData']);
  107. $methodMock->expects($this->once())
  108. ->method('setChecks')
  109. ->with(
  110. [
  111. \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_CHECKOUT,
  112. \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_COUNTRY,
  113. \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_CURRENCY,
  114. \Magento\Payment\Model\Method\AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX,
  115. ]
  116. )
  117. ->willReturnSelf();
  118. $methodMock->expects($this->once())->method('getData')->willReturn($methodDataWithAdditionalData);
  119. $paymentMock = $this->createPartialMock(
  120. \Magento\Quote\Model\Quote\Payment::class,
  121. ['importData', 'getMethod', 'getMethodInstance', 'getId']
  122. );
  123. $paymentMock->expects($this->once())->method('importData')->with($methodData)->willReturnSelf();
  124. $paymentMock->expects($this->once())->method('getMethod')->willReturn($paymentMethod);
  125. $billingAddressMock = $this->createPartialMock(
  126. \Magento\Quote\Model\Quote\Address::class,
  127. ['getCountryId', 'setPaymentMethod']
  128. );
  129. $billingAddressMock->expects($this->once())
  130. ->method('setPaymentMethod')
  131. ->with($paymentMethod)
  132. ->willReturnSelf();
  133. $quoteMock->method('getPayment')->willReturn($paymentMock);
  134. $quoteMock->expects($this->once())->method('isVirtual')->willReturn(true);
  135. $quoteMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddressMock);
  136. $methodInstance = $this->getMockForAbstractClass(\Magento\Payment\Model\MethodInterface::class);
  137. $paymentMock->expects($this->once())->method('getMethodInstance')->willReturn($methodInstance);
  138. $this->zeroTotalMock->expects($this->once())
  139. ->method('isApplicable')
  140. ->with($methodInstance, $quoteMock)
  141. ->willReturn(true);
  142. $quoteMock->expects($this->once())->method('setTotalsCollectedFlag')->with(false)->willReturnSelf();
  143. $quoteMock->expects($this->once())->method('save')->willReturnSelf();
  144. $paymentMock->expects($this->once())->method('getId')->willReturn($paymentId);
  145. $this->assertEquals($paymentId, $this->model->set($cartId, $methodMock));
  146. }
  147. /**
  148. * @expectedException \Magento\Framework\Exception\State\InvalidTransitionException
  149. * @expectedExceptionMessage The requested Payment Method is not available.
  150. */
  151. public function testSetVirtualProductThrowsExceptionIfPaymentMethodNotAvailable()
  152. {
  153. $cartId = 100;
  154. $methodData = ['method' => 'data'];
  155. $paymentMethod = 'checkmo';
  156. $quoteMock = $this->createPartialMock(
  157. \Magento\Quote\Model\Quote::class,
  158. ['getPayment', 'isVirtual', 'getBillingAddress']
  159. );
  160. $this->quoteRepositoryMock->expects($this->once())->method('get')->with($cartId)->willReturn($quoteMock);
  161. $methodMock = $this->createPartialMock(\Magento\Quote\Model\Quote\Payment::class, ['setChecks', 'getData']);
  162. $methodMock->expects($this->once())
  163. ->method('setChecks')
  164. ->with(
  165. [
  166. \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_CHECKOUT,
  167. \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_COUNTRY,
  168. \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_CURRENCY,
  169. \Magento\Payment\Model\Method\AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX,
  170. ]
  171. )
  172. ->willReturnSelf();
  173. $methodMock->expects($this->once())->method('getData')->willReturn($methodData);
  174. $paymentMock = $this->createPartialMock(
  175. \Magento\Quote\Model\Quote\Payment::class,
  176. ['importData', 'getMethod', 'getMethodInstance']
  177. );
  178. $paymentMock->expects($this->once())->method('importData')->with($methodData)->willReturnSelf();
  179. $paymentMock->expects($this->once())->method('getMethod')->willReturn($paymentMethod);
  180. $billingAddressMock = $this->createPartialMock(
  181. \Magento\Quote\Model\Quote\Address::class,
  182. ['getCountryId', 'setPaymentMethod']
  183. );
  184. $billingAddressMock->expects($this->once())
  185. ->method('setPaymentMethod')
  186. ->with($paymentMethod)
  187. ->willReturnSelf();
  188. $quoteMock->method('getPayment')->willReturn($paymentMock);
  189. $quoteMock->method('isVirtual')->willReturn(true);
  190. $quoteMock->method('getBillingAddress')->willReturn($billingAddressMock);
  191. $methodInstance = $this->getMockForAbstractClass(\Magento\Payment\Model\MethodInterface::class);
  192. $paymentMock->expects($this->once())->method('getMethodInstance')->willReturn($methodInstance);
  193. $this->zeroTotalMock->expects($this->once())
  194. ->method('isApplicable')
  195. ->with($methodInstance, $quoteMock)
  196. ->willReturn(false);
  197. $this->model->set($cartId, $methodMock);
  198. }
  199. public function testSetSimpleProduct()
  200. {
  201. $cartId = 100;
  202. $paymentId = 20;
  203. $methodData = ['method' => 'data'];
  204. $paymentMethod = 'checkmo';
  205. $quoteMock = $this->createPartialMock(
  206. \Magento\Quote\Model\Quote::class,
  207. ['getPayment', 'isVirtual', 'getShippingAddress', 'setTotalsCollectedFlag', 'collectTotals', 'save']
  208. );
  209. $this->quoteRepositoryMock->expects($this->once())->method('get')->with($cartId)->willReturn($quoteMock);
  210. $methodMock = $this->createPartialMock(\Magento\Quote\Model\Quote\Payment::class, ['setChecks', 'getData']);
  211. $methodMock->expects($this->once())
  212. ->method('setChecks')
  213. ->with(
  214. [
  215. \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_CHECKOUT,
  216. \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_COUNTRY,
  217. \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_CURRENCY,
  218. \Magento\Payment\Model\Method\AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX,
  219. ]
  220. )
  221. ->willReturnSelf();
  222. $methodMock->expects($this->once())->method('getData')->willReturn($methodData);
  223. $paymentMock = $this->createPartialMock(
  224. \Magento\Quote\Model\Quote\Payment::class,
  225. ['importData', 'getMethod', 'getMethodInstance', 'getId']
  226. );
  227. $paymentMock->expects($this->once())->method('importData')->with($methodData)->willReturnSelf();
  228. $paymentMock->expects($this->once())->method('getMethod')->willReturn($paymentMethod);
  229. $shippingAddressMock = $this->createPartialMock(
  230. \Magento\Quote\Model\Quote\Address::class,
  231. ['getCountryId', 'setPaymentMethod', 'setCollectShippingRates']
  232. );
  233. $shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn(100);
  234. $shippingAddressMock->expects($this->once())
  235. ->method('setPaymentMethod')
  236. ->with($paymentMethod)
  237. ->willReturnSelf();
  238. $shippingAddressMock->expects($this->once())
  239. ->method('setCollectShippingRates')
  240. ->with(true);
  241. $quoteMock->method('getPayment')->willReturn($paymentMock);
  242. $quoteMock->method('isVirtual')->willReturn(false);
  243. $quoteMock->method('getShippingAddress')->willReturn($shippingAddressMock);
  244. $methodInstance = $this->getMockForAbstractClass(\Magento\Payment\Model\MethodInterface::class);
  245. $paymentMock->expects($this->once())->method('getMethodInstance')->willReturn($methodInstance);
  246. $this->zeroTotalMock->expects($this->once())
  247. ->method('isApplicable')
  248. ->with($methodInstance, $quoteMock)
  249. ->willReturn(true);
  250. $quoteMock->expects($this->once())->method('setTotalsCollectedFlag')->with(false)->willReturnSelf();
  251. $quoteMock->expects($this->once())->method('save')->willReturnSelf();
  252. $paymentMock->expects($this->once())->method('getId')->willReturn($paymentId);
  253. $this->assertEquals($paymentId, $this->model->set($cartId, $methodMock));
  254. }
  255. /**
  256. * @expectedException \Magento\Framework\Exception\State\InvalidTransitionException
  257. * @expectedExceptionMessage The shipping address is missing. Set the address and try again.
  258. */
  259. public function testSetSimpleProductTrowsExceptionIfShippingAddressNotSet()
  260. {
  261. $cartId = 100;
  262. $quoteMock = $this->createPartialMock(
  263. \Magento\Quote\Model\Quote::class,
  264. ['getPayment', 'isVirtual', 'getShippingAddress']
  265. );
  266. $this->quoteRepositoryMock->expects($this->once())->method('get')->with($cartId)->willReturn($quoteMock);
  267. /** @var \Magento\Quote\Model\Quote\Payment|\PHPUnit_Framework_MockObject_MockObject $methodMock */
  268. $methodMock = $this->createPartialMock(\Magento\Quote\Model\Quote\Payment::class, ['setChecks', 'getData']);
  269. $methodMock->expects($this->once())
  270. ->method('setChecks')
  271. ->with([
  272. \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_CHECKOUT,
  273. \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_COUNTRY,
  274. \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_CURRENCY,
  275. \Magento\Payment\Model\Method\AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX,
  276. ])
  277. ->willReturnSelf();
  278. $methodMock->expects($this->never())->method('getData');
  279. $shippingAddressMock = $this->createPartialMock(\Magento\Quote\Model\Quote\Address::class, ['getCountryId']);
  280. $shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn(null);
  281. $quoteMock->method('isVirtual')->willReturn(false);
  282. $quoteMock->method('getShippingAddress')->willReturn($shippingAddressMock);
  283. $this->model->set($cartId, $methodMock);
  284. }
  285. }