QuoteManagementTest.php 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Test\Unit\Model;
  7. use Magento\Framework\Exception\NoSuchEntityException;
  8. use Magento\Quote\Model\CustomerManagement;
  9. use Magento\Sales\Api\Data\OrderAddressInterface;
  10. /**
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. * @SuppressWarnings(PHPMD.TooManyFields)
  13. */
  14. class QuoteManagementTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var \Magento\Quote\Model\QuoteManagement
  18. */
  19. protected $model;
  20. /**
  21. * @var \Magento\Quote\Model\QuoteValidator|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $quoteValidator;
  24. /**
  25. * @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $eventManager;
  28. /**
  29. * @var \Magento\Sales\Api\Data\OrderInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $orderFactory;
  32. /**
  33. * @var \Magento\Quote\Model\Quote\Address\ToOrder|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $quoteAddressToOrder;
  36. /**
  37. * @var \Magento\Quote\Model\Quote\Payment\ToOrderPayment|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $quotePaymentToOrderPayment;
  40. /**
  41. * @var \Magento\Quote\Model\Quote\Address\ToOrderAddress|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $quoteAddressToOrderAddress;
  44. /**
  45. * @var \Magento\Quote\Model\Quote\Item\ToOrderItem|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. protected $quoteItemToOrderItem;
  48. /**
  49. * @var \Magento\Quote\Model\Quote\Payment\ToOrderPayment|\PHPUnit_Framework_MockObject_MockObject
  50. */
  51. protected $orderManagement;
  52. /**
  53. * @var \Magento\Quote\Api\CartRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  54. */
  55. protected $quoteRepositoryMock;
  56. /**
  57. * @var CustomerManagement
  58. */
  59. protected $customerManagement;
  60. /**
  61. * @var \PHPUnit_Framework_MockObject_MockObject
  62. */
  63. protected $userContextMock;
  64. /**
  65. * @var \PHPUnit_Framework_MockObject_MockObject
  66. */
  67. protected $customerRepositoryMock;
  68. /**
  69. * @var \PHPUnit_Framework_MockObject_MockObject
  70. */
  71. protected $customerFactoryMock;
  72. /**
  73. * @var \PHPUnit_Framework_MockObject_MockObject
  74. */
  75. protected $quoteAddressFactory;
  76. /**
  77. * @var \PHPUnit_Framework_MockObject_MockObject
  78. */
  79. protected $storeManagerMock;
  80. /**
  81. * @var \PHPUnit_Framework_MockObject_MockObject
  82. */
  83. protected $checkoutSessionMock;
  84. /**
  85. * @var \PHPUnit_Framework_MockObject_MockObject
  86. */
  87. protected $customerSessionMock;
  88. /**
  89. * @var \PHPUnit_Framework_MockObject_MockObject
  90. */
  91. protected $dataObjectHelperMock;
  92. /**
  93. * @var \PHPUnit_Framework_MockObject_MockObject
  94. */
  95. protected $accountManagementMock;
  96. /**
  97. * @var \PHPUnit_Framework_MockObject_MockObject
  98. */
  99. protected $quoteMock;
  100. /**
  101. * @var \PHPUnit_Framework_MockObject_MockObject
  102. */
  103. private $quoteIdMock;
  104. /**
  105. * @var \Magento\Customer\Api\AddressRepositoryInterface
  106. */
  107. private $addressRepositoryMock;
  108. /**
  109. * @var \PHPUnit_Framework_MockObject_MockObject
  110. */
  111. private $quoteFactoryMock;
  112. /**
  113. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  114. */
  115. protected function setUp()
  116. {
  117. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  118. $this->quoteValidator = $this->createMock(\Magento\Quote\Model\QuoteValidator::class);
  119. $this->eventManager = $this->getMockForAbstractClass(\Magento\Framework\Event\ManagerInterface::class);
  120. $this->orderFactory = $this->createPartialMock(
  121. \Magento\Sales\Api\Data\OrderInterfaceFactory::class,
  122. ['create', 'populate']
  123. );
  124. $this->quoteAddressToOrder = $this->createMock(\Magento\Quote\Model\Quote\Address\ToOrder::class);
  125. $this->quotePaymentToOrderPayment = $this->createMock(\Magento\Quote\Model\Quote\Payment\ToOrderPayment::class);
  126. $this->quoteAddressToOrderAddress = $this->createMock(\Magento\Quote\Model\Quote\Address\ToOrderAddress::class);
  127. $this->quoteItemToOrderItem = $this->createMock(\Magento\Quote\Model\Quote\Item\ToOrderItem::class);
  128. $this->orderManagement = $this->createMock(\Magento\Sales\Api\OrderManagementInterface::class);
  129. $this->customerManagement = $this->createMock(\Magento\Quote\Model\CustomerManagement::class);
  130. $this->quoteRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
  131. $this->userContextMock = $this->createMock(\Magento\Authorization\Model\UserContextInterface::class);
  132. $this->customerRepositoryMock = $this->createPartialMock(
  133. \Magento\Customer\Api\CustomerRepositoryInterface::class,
  134. ['create', 'save', 'get', 'getById', 'getList', 'delete', 'deleteById']
  135. );
  136. $this->customerFactoryMock = $this->createPartialMock(
  137. \Magento\Customer\Model\CustomerFactory::class,
  138. ['create']
  139. );
  140. $this->storeManagerMock = $this->getMockForAbstractClass(
  141. \Magento\Store\Model\StoreManagerInterface::class,
  142. [],
  143. '',
  144. false,
  145. true,
  146. true,
  147. ['getStore', 'getStoreId']
  148. );
  149. $this->quoteMock = $this->createPartialMock(\Magento\Quote\Model\Quote::class, [
  150. 'getId',
  151. 'getCheckoutMethod',
  152. 'setCheckoutMethod',
  153. 'setCustomerId',
  154. 'setCustomerEmail',
  155. 'getBillingAddress',
  156. 'setCustomerIsGuest',
  157. 'setCustomerGroupId',
  158. 'assignCustomer',
  159. 'getPayment',
  160. 'collectTotals'
  161. ]);
  162. $this->quoteAddressFactory = $this->createPartialMock(
  163. \Magento\Quote\Model\Quote\AddressFactory::class,
  164. ['create']
  165. );
  166. $this->dataObjectHelperMock = $this->createMock(\Magento\Framework\Api\DataObjectHelper::class);
  167. $this->checkoutSessionMock = $this->createPartialMock(
  168. \Magento\Checkout\Model\Session::class,
  169. ['setLastQuoteId', 'setLastSuccessQuoteId', 'setLastOrderId', 'setLastRealOrderId', 'setLastOrderStatus']
  170. );
  171. $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
  172. $this->accountManagementMock = $this->createMock(\Magento\Customer\Api\AccountManagementInterface::class);
  173. $this->quoteFactoryMock = $this->createPartialMock(\Magento\Quote\Model\QuoteFactory::class, ['create']);
  174. $this->addressRepositoryMock = $this->getMockBuilder(\Magento\Customer\Api\AddressRepositoryInterface::class)
  175. ->getMockForAbstractClass();
  176. $this->model = $objectManager->getObject(
  177. \Magento\Quote\Model\QuoteManagement::class,
  178. [
  179. 'eventManager' => $this->eventManager,
  180. 'quoteValidator' => $this->quoteValidator,
  181. 'orderFactory' => $this->orderFactory,
  182. 'orderManagement' => $this->orderManagement,
  183. 'customerManagement' => $this->customerManagement,
  184. 'quoteAddressToOrder' => $this->quoteAddressToOrder,
  185. 'quoteAddressToOrderAddress' => $this->quoteAddressToOrderAddress,
  186. 'quoteItemToOrderItem' => $this->quoteItemToOrderItem,
  187. 'quotePaymentToOrderPayment' => $this->quotePaymentToOrderPayment,
  188. 'userContext' => $this->userContextMock,
  189. 'quoteRepository' => $this->quoteRepositoryMock,
  190. 'customerRepository' => $this->customerRepositoryMock,
  191. 'customerModelFactory' => $this->customerFactoryMock,
  192. 'quoteAddressFactory' => $this->quoteAddressFactory,
  193. 'dataObjectHelper' => $this->dataObjectHelperMock,
  194. 'storeManager' => $this->storeManagerMock,
  195. 'checkoutSession' => $this->checkoutSessionMock,
  196. 'customerSession' => $this->customerSessionMock,
  197. 'accountManagement' => $this->accountManagementMock,
  198. 'quoteFactory' => $this->quoteFactoryMock,
  199. 'addressRepository' => $this->addressRepositoryMock
  200. ]
  201. );
  202. // Set the new dependency
  203. $this->quoteIdMock = $this->createMock(\Magento\Quote\Model\QuoteIdMask::class);
  204. $quoteIdFactoryMock = $this->createPartialMock(\Magento\Quote\Model\QuoteIdMaskFactory::class, ['create']);
  205. $this->setPropertyValue($this->model, 'quoteIdMaskFactory', $quoteIdFactoryMock);
  206. }
  207. public function testCreateEmptyCartAnonymous()
  208. {
  209. $storeId = 345;
  210. $quoteId = 2311;
  211. $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  212. $quoteAddress = $this->createPartialMock(
  213. \Magento\Quote\Model\Quote\Address::class,
  214. ['setCollectShippingRates']
  215. );
  216. $quoteAddress->expects($this->once())->method('setCollectShippingRates')->with(true);
  217. $quoteMock->expects($this->any())->method('setBillingAddress')->with($quoteAddress)->willReturnSelf();
  218. $quoteMock->expects($this->any())->method('setShippingAddress')->with($quoteAddress)->willReturnSelf();
  219. $quoteMock->expects($this->any())->method('getShippingAddress')->willReturn($quoteAddress);
  220. $this->quoteAddressFactory->expects($this->any())->method('create')->willReturn($quoteAddress);
  221. $this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($quoteMock);
  222. $quoteMock->expects($this->any())->method('setStoreId')->with($storeId);
  223. $this->quoteRepositoryMock->expects($this->once())->method('save')->with($quoteMock);
  224. $quoteMock->expects($this->once())->method('getId')->willReturn($quoteId);
  225. $this->storeManagerMock->expects($this->once())->method('getStore')->willReturnSelf();
  226. $this->storeManagerMock->expects($this->once())->method('getStoreId')->willReturn($storeId);
  227. $this->assertEquals($quoteId, $this->model->createEmptyCart());
  228. }
  229. public function testCreateEmptyCartForCustomer()
  230. {
  231. $storeId = 345;
  232. $quoteId = 2311;
  233. $userId = 567;
  234. $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  235. $this->quoteRepositoryMock
  236. ->expects($this->once())
  237. ->method('getActiveForCustomer')
  238. ->with($userId)
  239. ->willThrowException(new NoSuchEntityException());
  240. $this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($quoteMock);
  241. $quoteMock->expects($this->any())->method('setStoreId')->with($storeId);
  242. $quoteMock->expects($this->any())->method('setCustomerIsGuest')->with(0);
  243. $this->quoteRepositoryMock->expects($this->once())->method('save')->with($quoteMock);
  244. $quoteMock->expects($this->once())->method('getId')->willReturn($quoteId);
  245. $this->storeManagerMock->expects($this->once())->method('getStore')->willReturnSelf();
  246. $this->storeManagerMock->expects($this->once())->method('getStoreId')->willReturn($storeId);
  247. $this->assertEquals($quoteId, $this->model->createEmptyCartForCustomer($userId));
  248. }
  249. public function testCreateEmptyCartForCustomerReturnExistsQuote()
  250. {
  251. $storeId = 345;
  252. $userId = 567;
  253. $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  254. $this->quoteRepositoryMock
  255. ->expects($this->once())
  256. ->method('getActiveForCustomer')
  257. ->with($userId)->willReturn($quoteMock);
  258. $this->quoteFactoryMock->expects($this->never())->method('create')->willReturn($quoteMock);
  259. $this->quoteRepositoryMock->expects($this->once())->method('save')->with($quoteMock);
  260. $this->storeManagerMock->expects($this->once())->method('getStore')->willReturnSelf();
  261. $this->storeManagerMock->expects($this->once())->method('getStoreId')->willReturn($storeId);
  262. $this->model->createEmptyCartForCustomer($userId);
  263. }
  264. /**
  265. * @expectedException \Magento\Framework\Exception\StateException
  266. * @expectedExceptionMessage The customer can't be assigned to the cart. The cart belongs to a different store.
  267. */
  268. public function testAssignCustomerFromAnotherStore()
  269. {
  270. $cartId = 220;
  271. $customerId = 455;
  272. $storeId = 5;
  273. $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  274. $customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  275. $this->quoteRepositoryMock
  276. ->expects($this->once())
  277. ->method('getActive')
  278. ->with($cartId)
  279. ->willReturn($quoteMock);
  280. $this->customerRepositoryMock
  281. ->expects($this->once())
  282. ->method('getById')
  283. ->with($customerId)
  284. ->willReturn($customerMock);
  285. $customerModelMock = $this->createPartialMock(
  286. \Magento\Customer\Model\Customer::class,
  287. ['load', 'getSharedStoreIds']
  288. );
  289. $this->customerFactoryMock->expects($this->once())->method('create')->willReturn($customerModelMock);
  290. $customerModelMock
  291. ->expects($this->once())
  292. ->method('load')
  293. ->with($customerId)
  294. ->willReturnSelf();
  295. $customerModelMock
  296. ->expects($this->once())
  297. ->method('getSharedStoreIds')
  298. ->willReturn([]);
  299. $this->model->assignCustomer($cartId, $customerId, $storeId);
  300. }
  301. /**
  302. * @expectedException \Magento\Framework\Exception\StateException
  303. * @expectedExceptionMessage The customer can't be assigned to the cart because the cart isn't anonymous.
  304. */
  305. public function testAssignCustomerToNonanonymousCart()
  306. {
  307. $cartId = 220;
  308. $customerId = 455;
  309. $storeId = 5;
  310. $quoteMock = $this->createPartialMock(
  311. \Magento\Quote\Model\Quote::class,
  312. ['getCustomerId', 'setCustomer', 'setCustomerIsGuest']
  313. );
  314. $customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  315. $this->quoteRepositoryMock
  316. ->expects($this->once())
  317. ->method('getActive')
  318. ->with($cartId)
  319. ->willReturn($quoteMock);
  320. $this->customerRepositoryMock
  321. ->expects($this->once())
  322. ->method('getById')
  323. ->with($customerId)
  324. ->willReturn($customerMock);
  325. $customerModelMock = $this->createPartialMock(
  326. \Magento\Customer\Model\Customer::class,
  327. ['load', 'getSharedStoreIds']
  328. );
  329. $this->customerFactoryMock->expects($this->once())->method('create')->willReturn($customerModelMock);
  330. $customerModelMock
  331. ->expects($this->once())
  332. ->method('load')
  333. ->with($customerId)
  334. ->willReturnSelf();
  335. $customerModelMock
  336. ->expects($this->once())
  337. ->method('getSharedStoreIds')
  338. ->willReturn([$storeId, 'some store value']);
  339. $quoteMock->expects($this->once())->method('getCustomerId')->willReturn(753);
  340. $this->model->assignCustomer($cartId, $customerId, $storeId);
  341. }
  342. /**
  343. * @expectedException \Magento\Framework\Exception\StateException
  344. */
  345. public function testAssignCustomerNoSuchCustomer()
  346. {
  347. $cartId = 220;
  348. $customerId = 455;
  349. $storeId = 5;
  350. $quoteMock = $this->createPartialMock(
  351. \Magento\Quote\Model\Quote::class,
  352. ['getCustomerId', 'setCustomer', 'setCustomerIsGuest']
  353. );
  354. $customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  355. $this->quoteRepositoryMock
  356. ->expects($this->once())
  357. ->method('getActive')
  358. ->with($cartId)
  359. ->willReturn($quoteMock);
  360. $this->customerRepositoryMock
  361. ->expects($this->once())
  362. ->method('getById')
  363. ->with($customerId)
  364. ->willReturn($customerMock);
  365. $customerModelMock = $this->createPartialMock(
  366. \Magento\Customer\Model\Customer::class,
  367. ['load', 'getSharedStoreIds']
  368. );
  369. $this->customerFactoryMock->expects($this->once())->method('create')->willReturn($customerModelMock);
  370. $customerModelMock
  371. ->expects($this->once())
  372. ->method('load')
  373. ->with($customerId)
  374. ->willReturnSelf();
  375. $customerModelMock
  376. ->expects($this->once())
  377. ->method('getSharedStoreIds')
  378. ->willReturn([$storeId, 'some store value']);
  379. $quoteMock->expects($this->once())->method('getCustomerId')->willReturn(null);
  380. $this->quoteRepositoryMock
  381. ->expects($this->once())
  382. ->method('getForCustomer')
  383. ->with($customerId);
  384. $this->model->assignCustomer($cartId, $customerId, $storeId);
  385. $this->expectExceptionMessage(
  386. "The customer can't be assigned to the cart because the customer already has an active cart."
  387. );
  388. }
  389. public function testAssignCustomer()
  390. {
  391. $cartId = 220;
  392. $customerId = 455;
  393. $storeId = 5;
  394. $this->getPropertyValue($this->model, 'quoteIdMaskFactory')
  395. ->expects($this->once())
  396. ->method('create')
  397. ->willReturn($this->quoteIdMock);
  398. $this->quoteIdMock->expects($this->once())->method('load')->with($cartId, 'quote_id')->willReturnSelf();
  399. $this->quoteIdMock->expects($this->once())->method('getId')->willReturn(10);
  400. $this->quoteIdMock->expects($this->once())->method('delete');
  401. $quoteMock = $this->createPartialMock(
  402. \Magento\Quote\Model\Quote::class,
  403. ['getCustomerId', 'setCustomer', 'setCustomerIsGuest']
  404. );
  405. $customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  406. $this->quoteRepositoryMock
  407. ->expects($this->once())
  408. ->method('getActive')
  409. ->with($cartId)
  410. ->willReturn($quoteMock);
  411. $this->customerRepositoryMock
  412. ->expects($this->once())
  413. ->method('getById')
  414. ->with($customerId)
  415. ->willReturn($customerMock);
  416. $customerModelMock = $this->createPartialMock(
  417. \Magento\Customer\Model\Customer::class,
  418. ['load', 'getSharedStoreIds']
  419. );
  420. $this->customerFactoryMock->expects($this->once())->method('create')->willReturn($customerModelMock);
  421. $customerModelMock
  422. ->expects($this->once())
  423. ->method('load')
  424. ->with($customerId)
  425. ->willReturnSelf();
  426. $customerModelMock
  427. ->expects($this->once())
  428. ->method('getSharedStoreIds')
  429. ->willReturn([$storeId, 'some store value']);
  430. $quoteMock->expects($this->once())->method('getCustomerId')->willReturn(null);
  431. $this->quoteRepositoryMock
  432. ->expects($this->once())
  433. ->method('getForCustomer')
  434. ->with($customerId)
  435. ->willThrowException(new NoSuchEntityException());
  436. $quoteMock->expects($this->once())->method('setCustomer')->with($customerMock);
  437. $quoteMock->expects($this->once())->method('setCustomerIsGuest')->with(0);
  438. $this->quoteRepositoryMock->expects($this->once())->method('save')->with($quoteMock);
  439. $this->model->assignCustomer($cartId, $customerId, $storeId);
  440. }
  441. public function testSubmit()
  442. {
  443. $orderData = [];
  444. $isGuest = true;
  445. $isVirtual = false;
  446. $customerId = 1;
  447. $quoteId = 1;
  448. $quoteItem = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
  449. $billingAddress = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
  450. $shippingAddress = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
  451. $payment = $this->createMock(\Magento\Quote\Model\Quote\Payment::class);
  452. $baseOrder = $this->createMock(\Magento\Sales\Api\Data\OrderInterface::class);
  453. $convertedBilling = $this->createPartialMockForAbstractClass(OrderAddressInterface::class, ['setData']);
  454. $convertedShipping = $this->createPartialMockForAbstractClass(OrderAddressInterface::class, ['setData']);
  455. $convertedPayment = $this->createMock(\Magento\Sales\Api\Data\OrderPaymentInterface::class);
  456. $convertedQuoteItem = $this->createMock(\Magento\Sales\Api\Data\OrderItemInterface::class);
  457. $addresses = [$convertedShipping, $convertedBilling];
  458. $quoteItems = [$quoteItem];
  459. $convertedItems = [$convertedQuoteItem];
  460. $quote = $this->getQuote(
  461. $isGuest,
  462. $isVirtual,
  463. $billingAddress,
  464. $payment,
  465. $customerId,
  466. $quoteId,
  467. $quoteItems,
  468. $shippingAddress
  469. );
  470. $this->quoteValidator->expects($this->once())->method('validateBeforeSubmit')->with($quote);
  471. $this->quoteAddressToOrder->expects($this->once())
  472. ->method('convert')
  473. ->with($shippingAddress, $orderData)
  474. ->willReturn($baseOrder);
  475. $this->quoteAddressToOrderAddress->expects($this->at(0))
  476. ->method('convert')
  477. ->with(
  478. $shippingAddress,
  479. [
  480. 'address_type' => 'shipping',
  481. 'email' => 'customer@example.com'
  482. ]
  483. )
  484. ->willReturn($convertedShipping);
  485. $this->quoteAddressToOrderAddress->expects($this->at(1))
  486. ->method('convert')
  487. ->with(
  488. $billingAddress,
  489. [
  490. 'address_type' => 'billing',
  491. 'email' => 'customer@example.com'
  492. ]
  493. )
  494. ->willReturn($convertedBilling);
  495. $billingAddress->expects($this->once())->method('getId')->willReturn(4);
  496. $convertedBilling->expects($this->once())->method('setData')->with('quote_address_id', 4);
  497. $this->quoteItemToOrderItem->expects($this->once())->method('convert')
  498. ->with($quoteItem, ['parent_item' => null])
  499. ->willReturn($convertedQuoteItem);
  500. $this->quotePaymentToOrderPayment->expects($this->once())->method('convert')->with($payment)
  501. ->willReturn($convertedPayment);
  502. $shippingAddress->expects($this->once())->method('getShippingMethod')->willReturn('free');
  503. $shippingAddress->expects($this->once())->method('getId')->willReturn(5);
  504. $convertedShipping->expects($this->once())->method('setData')->with('quote_address_id', 5);
  505. $order = $this->prepareOrderFactory(
  506. $baseOrder,
  507. $convertedBilling,
  508. $addresses,
  509. $convertedPayment,
  510. $convertedItems,
  511. $quoteId,
  512. $convertedShipping
  513. );
  514. $this->orderManagement->expects($this->once())
  515. ->method('place')
  516. ->with($order)
  517. ->willReturn($order);
  518. $this->eventManager->expects($this->at(0))
  519. ->method('dispatch')
  520. ->with('sales_model_service_quote_submit_before', ['order' => $order, 'quote' => $quote]);
  521. $this->eventManager->expects($this->at(1))
  522. ->method('dispatch')
  523. ->with('sales_model_service_quote_submit_success', ['order' => $order, 'quote' => $quote]);
  524. $this->quoteRepositoryMock->expects($this->once())->method('save')->with($quote);
  525. $this->assertEquals($order, $this->model->submit($quote, $orderData));
  526. }
  527. public function testPlaceOrderIfCustomerIsGuest()
  528. {
  529. $cartId = 100;
  530. $orderId = 332;
  531. $orderIncrementId = 100003332;
  532. $orderStatus = 'status1';
  533. $email = 'email@mail.com';
  534. $this->quoteRepositoryMock->expects($this->once())
  535. ->method('getActive')
  536. ->with($cartId)
  537. ->willReturn($this->quoteMock);
  538. $this->quoteMock->expects($this->once())
  539. ->method('getCheckoutMethod')
  540. ->willReturn(\Magento\Checkout\Model\Type\Onepage::METHOD_GUEST);
  541. $this->quoteMock->expects($this->once())->method('setCustomerId')->with(null)->willReturnSelf();
  542. $this->quoteMock->expects($this->once())->method('setCustomerEmail')->with($email)->willReturnSelf();
  543. $addressMock = $this->createPartialMock(\Magento\Quote\Model\Quote\Address::class, ['getEmail']);
  544. $addressMock->expects($this->once())->method('getEmail')->willReturn($email);
  545. $this->quoteMock->expects($this->once())->method('getBillingAddress')->with()->willReturn($addressMock);
  546. $this->quoteMock->expects($this->once())->method('setCustomerIsGuest')->with(true)->willReturnSelf();
  547. $this->quoteMock->expects($this->once())
  548. ->method('setCustomerGroupId')
  549. ->with(\Magento\Customer\Api\Data\GroupInterface::NOT_LOGGED_IN_ID);
  550. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Quote\Model\QuoteManagement $service */
  551. $service = $this->getMockBuilder(\Magento\Quote\Model\QuoteManagement::class)
  552. ->setMethods(['submit'])
  553. ->setConstructorArgs(
  554. [
  555. 'eventManager' => $this->eventManager,
  556. 'quoteValidator' => $this->quoteValidator,
  557. 'orderFactory' => $this->orderFactory,
  558. 'orderManagement' => $this->orderManagement,
  559. 'customerManagement' => $this->customerManagement,
  560. 'quoteAddressToOrder' => $this->quoteAddressToOrder,
  561. 'quoteAddressToOrderAddress' => $this->quoteAddressToOrderAddress,
  562. 'quoteItemToOrderItem' => $this->quoteItemToOrderItem,
  563. 'quotePaymentToOrderPayment' => $this->quotePaymentToOrderPayment,
  564. 'userContext' => $this->userContextMock,
  565. 'quoteRepository' => $this->quoteRepositoryMock,
  566. 'customerRepository' => $this->customerRepositoryMock,
  567. 'customerModelFactory' => $this->customerFactoryMock,
  568. 'quoteAddressFactory' => $this->quoteAddressFactory,
  569. 'dataObjectHelper' => $this->dataObjectHelperMock,
  570. 'storeManager' => $this->storeManagerMock,
  571. 'checkoutSession' => $this->checkoutSessionMock,
  572. 'customerSession' => $this->customerSessionMock,
  573. 'accountManagement' => $this->accountManagementMock,
  574. 'quoteFactory' => $this->quoteFactoryMock
  575. ]
  576. )
  577. ->getMock();
  578. $orderMock = $this->createPartialMock(
  579. \Magento\Sales\Model\Order::class,
  580. ['getId', 'getIncrementId', 'getStatus']
  581. );
  582. $service->expects($this->once())->method('submit')->willReturn($orderMock);
  583. $this->quoteMock->expects($this->atLeastOnce())->method('getId')->willReturn($cartId);
  584. $this->quoteMock->expects($this->once())->method('collectTotals')->willReturnSelf();
  585. $orderMock->expects($this->atLeastOnce())->method('getId')->willReturn($orderId);
  586. $orderMock->expects($this->atLeastOnce())->method('getIncrementId')->willReturn($orderIncrementId);
  587. $orderMock->expects($this->atLeastOnce())->method('getStatus')->willReturn($orderStatus);
  588. $this->checkoutSessionMock->expects($this->once())->method('setLastQuoteId')->with($cartId);
  589. $this->checkoutSessionMock->expects($this->once())->method('setLastSuccessQuoteId')->with($cartId);
  590. $this->checkoutSessionMock->expects($this->once())->method('setLastOrderId')->with($orderId);
  591. $this->checkoutSessionMock->expects($this->once())->method('setLastRealOrderId')->with($orderIncrementId);
  592. $this->checkoutSessionMock->expects($this->once())->method('setLastOrderStatus')->with($orderStatus);
  593. $this->assertEquals($orderId, $service->placeOrder($cartId));
  594. }
  595. public function testPlaceOrder()
  596. {
  597. $cartId = 323;
  598. $orderId = 332;
  599. $orderIncrementId = 100003332;
  600. $orderStatus = 'status1';
  601. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Quote\Model\QuoteManagement $service */
  602. $service = $this->getMockBuilder(\Magento\Quote\Model\QuoteManagement::class)
  603. ->setMethods(['submit'])
  604. ->setConstructorArgs(
  605. [
  606. 'eventManager' => $this->eventManager,
  607. 'quoteValidator' => $this->quoteValidator,
  608. 'orderFactory' => $this->orderFactory,
  609. 'orderManagement' => $this->orderManagement,
  610. 'customerManagement' => $this->customerManagement,
  611. 'quoteAddressToOrder' => $this->quoteAddressToOrder,
  612. 'quoteAddressToOrderAddress' => $this->quoteAddressToOrderAddress,
  613. 'quoteItemToOrderItem' => $this->quoteItemToOrderItem,
  614. 'quotePaymentToOrderPayment' => $this->quotePaymentToOrderPayment,
  615. 'userContext' => $this->userContextMock,
  616. 'quoteRepository' => $this->quoteRepositoryMock,
  617. 'customerRepository' => $this->customerRepositoryMock,
  618. 'customerModelFactory' => $this->customerFactoryMock,
  619. 'quoteAddressFactory' => $this->quoteAddressFactory,
  620. 'dataObjectHelper' => $this->dataObjectHelperMock,
  621. 'storeManager' => $this->storeManagerMock,
  622. 'checkoutSession' => $this->checkoutSessionMock,
  623. 'customerSession' => $this->customerSessionMock,
  624. 'accountManagement' => $this->accountManagementMock,
  625. 'quoteFactory' => $this->quoteFactoryMock
  626. ]
  627. )
  628. ->getMock();
  629. $orderMock = $this->createMock(\Magento\Sales\Model\Order::class);
  630. $this->quoteRepositoryMock->expects($this->once())
  631. ->method('getActive')
  632. ->with($cartId)
  633. ->willReturn($this->quoteMock);
  634. $quotePayment = $this->createMock(\Magento\Quote\Model\Quote\Payment::class);
  635. $quotePayment->expects($this->once())
  636. ->method('setQuote');
  637. $quotePayment->expects($this->once())
  638. ->method('importData');
  639. $this->quoteMock->expects($this->atLeastOnce())
  640. ->method('getPayment')
  641. ->willReturn($quotePayment);
  642. $this->quoteMock->expects($this->once())
  643. ->method('getCheckoutMethod')
  644. ->willReturn(\Magento\Checkout\Model\Type\Onepage::METHOD_CUSTOMER);
  645. $this->quoteMock->expects($this->never())
  646. ->method('setCustomerIsGuest')
  647. ->with(true);
  648. $service->expects($this->once())->method('submit')->willReturn($orderMock);
  649. $this->quoteMock->expects($this->atLeastOnce())->method('getId')->willReturn($cartId);
  650. $orderMock->expects($this->atLeastOnce())->method('getId')->willReturn($orderId);
  651. $orderMock->expects($this->atLeastOnce())->method('getIncrementId')->willReturn($orderIncrementId);
  652. $orderMock->expects($this->atLeastOnce())->method('getStatus')->willReturn($orderStatus);
  653. $this->checkoutSessionMock->expects($this->once())->method('setLastQuoteId')->with($cartId);
  654. $this->checkoutSessionMock->expects($this->once())->method('setLastSuccessQuoteId')->with($cartId);
  655. $this->checkoutSessionMock->expects($this->once())->method('setLastOrderId')->with($orderId);
  656. $this->checkoutSessionMock->expects($this->once())->method('setLastRealOrderId')->with($orderIncrementId);
  657. $this->checkoutSessionMock->expects($this->once())->method('setLastOrderStatus')->with($orderStatus);
  658. $paymentMethod = $this->createPartialMock(\Magento\Quote\Model\Quote\Payment::class, ['setChecks', 'getData']);
  659. $paymentMethod->expects($this->once())->method('setChecks');
  660. $paymentMethod->expects($this->once())->method('getData')->willReturn(['additional_data' => []]);
  661. $this->assertEquals($orderId, $service->placeOrder($cartId, $paymentMethod));
  662. }
  663. /**
  664. * @param $isGuest
  665. * @param $isVirtual
  666. * @param \Magento\Quote\Model\Quote\Address $billingAddress
  667. * @param \Magento\Quote\Model\Quote\Payment $payment
  668. * @param $customerId
  669. * @param $id
  670. * @param array $quoteItems
  671. * @param \Magento\Quote\Model\Quote\Address|null $shippingAddress
  672. * @return \PHPUnit_Framework_MockObject_MockObject
  673. */
  674. protected function getQuote(
  675. $isGuest,
  676. $isVirtual,
  677. \Magento\Quote\Model\Quote\Address $billingAddress,
  678. \Magento\Quote\Model\Quote\Payment $payment,
  679. $customerId,
  680. $id,
  681. array $quoteItems,
  682. \Magento\Quote\Model\Quote\Address $shippingAddress = null
  683. ) {
  684. $quote = $this->createPartialMock(
  685. \Magento\Quote\Model\Quote::class,
  686. [
  687. 'setIsActive',
  688. 'getCustomerEmail',
  689. 'getAllVisibleItems',
  690. 'getCustomerIsGuest',
  691. 'isVirtual',
  692. 'getBillingAddress',
  693. 'getShippingAddress',
  694. 'getId',
  695. 'getCustomer',
  696. 'getAllItems',
  697. 'getPayment',
  698. 'reserveOrderId',
  699. 'getCustomerId',
  700. 'addCustomerAddress'
  701. ]
  702. );
  703. $quote->expects($this->once())
  704. ->method('setIsActive')
  705. ->with(false);
  706. $quote->expects($this->any())
  707. ->method('getAllVisibleItems')
  708. ->willReturn($quoteItems);
  709. $quote->expects($this->once())
  710. ->method('getAllItems')
  711. ->willReturn($quoteItems);
  712. $quote->expects($this->once())
  713. ->method('getCustomerIsGuest')
  714. ->willReturn($isGuest);
  715. $quote->expects($this->any())
  716. ->method('isVirtual')
  717. ->willReturn($isVirtual);
  718. if ($shippingAddress) {
  719. $quote->expects($this->any())
  720. ->method('getShippingAddress')
  721. ->willReturn($shippingAddress);
  722. }
  723. $quote->expects($this->any())
  724. ->method('getBillingAddress')
  725. ->willReturn($billingAddress);
  726. $quote->expects($this->once())
  727. ->method('getPayment')
  728. ->willReturn($payment);
  729. $customer = $this->createPartialMock(
  730. \Magento\Customer\Model\Customer::class,
  731. ['getDefaultBilling', 'getId']
  732. );
  733. $quote->expects($this->any())->method('getCustomerId')->willReturn($customerId);
  734. $customer->expects($this->once())
  735. ->method('getId')
  736. ->willReturn($customerId);
  737. $quote->expects($this->atLeastOnce())
  738. ->method('getCustomerEmail')
  739. ->willReturn('customer@example.com');
  740. $quote->expects($this->any())
  741. ->method('getCustomer')
  742. ->willReturn($customer);
  743. $quote->expects($this->once())
  744. ->method('getId')
  745. ->willReturn($id);
  746. $this->customerRepositoryMock->expects($this->any())->method('getById')->willReturn($customer);
  747. $customer->expects($this->any())->method('getDefaultBilling')->willReturn(1);
  748. return $quote;
  749. }
  750. /**
  751. * @param \Magento\Sales\Api\Data\OrderInterface $baseOrder
  752. * @param OrderAddressInterface $billingAddress
  753. * @param array $addresses
  754. * @param $payment
  755. * @param array $items
  756. * @param $quoteId
  757. * @param OrderAddressInterface $shippingAddress
  758. * @return \PHPUnit_Framework_MockObject_MockObject
  759. */
  760. protected function prepareOrderFactory(
  761. \Magento\Sales\Api\Data\OrderInterface $baseOrder,
  762. OrderAddressInterface $billingAddress,
  763. array $addresses,
  764. $payment,
  765. array $items,
  766. $quoteId,
  767. OrderAddressInterface $shippingAddress = null,
  768. $customerId = null
  769. ) {
  770. $order = $this->createPartialMock(
  771. \Magento\Sales\Model\Order::class,
  772. [
  773. 'setShippingAddress',
  774. 'getAddressesCollection',
  775. 'getAddresses',
  776. 'getBillingAddress',
  777. 'addAddresses',
  778. 'setBillingAddress',
  779. 'setAddresses',
  780. 'setPayment',
  781. 'setItems',
  782. 'setQuoteId'
  783. ]
  784. );
  785. $this->orderFactory->expects($this->once())
  786. ->method('create')
  787. ->willReturn($order);
  788. $this->orderFactory->expects($this->never())
  789. ->method('populate')
  790. ->with($baseOrder);
  791. if ($shippingAddress) {
  792. $order->expects($this->once())->method('setShippingAddress')->with($shippingAddress);
  793. }
  794. if ($customerId) {
  795. $this->orderFactory->expects($this->once())
  796. ->method('setCustomerId')
  797. ->with($customerId);
  798. }
  799. $order->expects($this->any())->method('getAddressesCollection');
  800. $order->expects($this->any())->method('getAddresses');
  801. $order->expects($this->any())->method('getBillingAddress')->willReturn(false);
  802. $order->expects($this->any())->method('addAddresses')->withAnyParameters()->willReturnSelf();
  803. $order->expects($this->once())->method('setBillingAddress')->with($billingAddress);
  804. $order->expects($this->once())->method('setAddresses')->with($addresses);
  805. $order->expects($this->once())->method('setPayment')->with($payment);
  806. $order->expects($this->once())->method('setItems')->with($items);
  807. $order->expects($this->once())->method('setQuoteId')->with($quoteId);
  808. return $order;
  809. }
  810. public function testGetCartForCustomer()
  811. {
  812. $customerId = 100;
  813. $cartMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  814. $this->quoteRepositoryMock->expects($this->once())
  815. ->method('getActiveForCustomer')
  816. ->with($customerId)
  817. ->willReturn($cartMock);
  818. $this->assertEquals($cartMock, $this->model->getCartForCustomer($customerId));
  819. }
  820. /**
  821. * Get any object property value.
  822. *
  823. * @param $object
  824. * @param $property
  825. * @return mixed
  826. */
  827. protected function getPropertyValue($object, $property)
  828. {
  829. $reflection = new \ReflectionClass(get_class($object));
  830. $reflectionProperty = $reflection->getProperty($property);
  831. $reflectionProperty->setAccessible(true);
  832. return $reflectionProperty->getValue($object);
  833. }
  834. /**
  835. * Set object property value.
  836. *
  837. * @param $object
  838. * @param $property
  839. * @param $value
  840. */
  841. protected function setPropertyValue(&$object, $property, $value)
  842. {
  843. $reflection = new \ReflectionClass(get_class($object));
  844. $reflectionProperty = $reflection->getProperty($property);
  845. $reflectionProperty->setAccessible(true);
  846. $reflectionProperty->setValue($object, $value);
  847. return $object;
  848. }
  849. public function testSubmitForCustomer()
  850. {
  851. $orderData = [];
  852. $isGuest = false;
  853. $isVirtual = false;
  854. $customerId = 1;
  855. $quoteId = 1;
  856. $quoteItem = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
  857. $billingAddress = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
  858. $shippingAddress = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
  859. $payment = $this->createMock(\Magento\Quote\Model\Quote\Payment::class);
  860. $baseOrder = $this->createMock(\Magento\Sales\Api\Data\OrderInterface::class);
  861. $convertedBilling = $this->createPartialMockForAbstractClass(OrderAddressInterface::class, ['setData']);
  862. $convertedShipping = $this->createPartialMockForAbstractClass(OrderAddressInterface::class, ['setData']);
  863. $convertedPayment = $this->createMock(\Magento\Sales\Api\Data\OrderPaymentInterface::class);
  864. $convertedQuoteItem = $this->createMock(\Magento\Sales\Api\Data\OrderItemInterface::class);
  865. $addresses = [$convertedShipping, $convertedBilling];
  866. $quoteItems = [$quoteItem];
  867. $convertedItems = [$convertedQuoteItem];
  868. $quote = $this->getQuote(
  869. $isGuest,
  870. $isVirtual,
  871. $billingAddress,
  872. $payment,
  873. $customerId,
  874. $quoteId,
  875. $quoteItems,
  876. $shippingAddress
  877. );
  878. $this->quoteValidator->expects($this->once())->method('validateBeforeSubmit')->with($quote);
  879. $this->quoteAddressToOrder->expects($this->once())
  880. ->method('convert')
  881. ->with($shippingAddress, $orderData)
  882. ->willReturn($baseOrder);
  883. $this->quoteAddressToOrderAddress->expects($this->at(0))
  884. ->method('convert')
  885. ->with(
  886. $shippingAddress,
  887. [
  888. 'address_type' => 'shipping',
  889. 'email' => 'customer@example.com'
  890. ]
  891. )
  892. ->willReturn($convertedShipping);
  893. $this->quoteAddressToOrderAddress->expects($this->at(1))
  894. ->method('convert')
  895. ->with(
  896. $billingAddress,
  897. [
  898. 'address_type' => 'billing',
  899. 'email' => 'customer@example.com'
  900. ]
  901. )
  902. ->willReturn($convertedBilling);
  903. $this->quoteItemToOrderItem->expects($this->once())->method('convert')
  904. ->with($quoteItem, ['parent_item' => null])
  905. ->willReturn($convertedQuoteItem);
  906. $this->quotePaymentToOrderPayment->expects($this->once())->method('convert')->with($payment)
  907. ->willReturn($convertedPayment);
  908. $shippingAddress->expects($this->once())->method('getShippingMethod')->willReturn('free');
  909. $shippingAddress->expects($this->once())->method('getId')->willReturn(5);
  910. $convertedShipping->expects($this->once())->method('setData')->with('quote_address_id', 5);
  911. $order = $this->prepareOrderFactory(
  912. $baseOrder,
  913. $convertedBilling,
  914. $addresses,
  915. $convertedPayment,
  916. $convertedItems,
  917. $quoteId,
  918. $convertedShipping
  919. );
  920. $customerAddressMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
  921. ->getMockForAbstractClass();
  922. $shippingAddress->expects($this->once())->method('exportCustomerAddress')->willReturn($customerAddressMock);
  923. $this->addressRepositoryMock->expects($this->once())->method('save')->with($customerAddressMock);
  924. $quote->expects($this->any())->method('addCustomerAddress')->with($customerAddressMock);
  925. $billingAddress->expects($this->once())->method('getCustomerId')->willReturn(2);
  926. $billingAddress->expects($this->once())->method('getSaveInAddressBook')->willReturn(false);
  927. $billingAddress->expects($this->once())->method('getId')->willReturn(4);
  928. $convertedBilling->expects($this->once())->method('setData')->with('quote_address_id', 4);
  929. $this->orderManagement->expects($this->once())
  930. ->method('place')
  931. ->with($order)
  932. ->willReturn($order);
  933. $this->eventManager->expects($this->at(0))
  934. ->method('dispatch')
  935. ->with('sales_model_service_quote_submit_before', ['order' => $order, 'quote' => $quote]);
  936. $this->eventManager->expects($this->at(1))
  937. ->method('dispatch')
  938. ->with('sales_model_service_quote_submit_success', ['order' => $order, 'quote' => $quote]);
  939. $this->quoteRepositoryMock->expects($this->once())->method('save')->with($quote);
  940. $this->assertEquals($order, $this->model->submit($quote, $orderData));
  941. }
  942. /**
  943. * Get mock for abstract class with methods.
  944. *
  945. * @param string $className
  946. * @param array $methods
  947. *
  948. * @return \PHPUnit_Framework_MockObject_MockObject
  949. */
  950. private function createPartialMockForAbstractClass($className, $methods = [])
  951. {
  952. return $this->getMockForAbstractClass(
  953. $className,
  954. [],
  955. '',
  956. true,
  957. true,
  958. true,
  959. $methods
  960. );
  961. }
  962. }