QuoteRepositoryTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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\Api\SortOrder;
  8. use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
  9. use Magento\Quote\Api\Data\CartInterface;
  10. use Magento\Quote\Model\QuoteRepository\LoadHandler;
  11. use Magento\Quote\Model\QuoteRepository\SaveHandler;
  12. use Magento\Quote\Model\ResourceModel\Quote\CollectionFactory;
  13. /**
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class QuoteRepositoryTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var \Magento\Quote\Api\CartRepositoryInterface
  20. */
  21. private $model;
  22. /**
  23. * @var \Magento\Quote\Model\QuoteFactory|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $quoteFactoryMock;
  26. /**
  27. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $storeManagerMock;
  30. /**
  31. * @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $storeMock;
  34. /**
  35. * @var \Magento\Quote\Model\Quote|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $quoteMock;
  38. /**
  39. * @var \Magento\Quote\Api\Data\CartSearchResultsInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $searchResultsDataFactory;
  42. /**
  43. * @var \Magento\Quote\Model\ResourceModel\Quote\Collection|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $quoteCollectionMock;
  46. /**
  47. * @var JoinProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. private $extensionAttributesJoinProcessorMock;
  50. /**
  51. * @var LoadHandler|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. private $loadHandlerMock;
  54. /**
  55. * @var LoadHandler|\PHPUnit_Framework_MockObject_MockObject
  56. */
  57. private $saveHandlerMock;
  58. /**
  59. * @var \PHPUnit_Framework_MockObject_MockObject
  60. */
  61. private $collectionProcessor;
  62. /**
  63. * @var \PHPUnit_Framework_MockObject_MockObject
  64. */
  65. private $objectManagerMock;
  66. /**
  67. * @var \Magento\Quote\Model\ResourceModel\Quote\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  68. */
  69. private $quoteCollectionFactoryMock;
  70. protected function setUp()
  71. {
  72. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  73. $this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  74. \Magento\Framework\App\ObjectManager::setInstance($this->objectManagerMock);
  75. $this->quoteFactoryMock = $this->createPartialMock(\Magento\Quote\Model\QuoteFactory::class, ['create']);
  76. $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  77. $this->quoteMock = $this->createPartialMock(
  78. \Magento\Quote\Model\Quote::class,
  79. [
  80. 'load',
  81. 'loadByIdWithoutStore',
  82. 'loadByCustomer',
  83. 'getIsActive',
  84. 'getId',
  85. '__wakeup',
  86. 'setSharedStoreIds',
  87. 'save',
  88. 'delete',
  89. 'getCustomerId',
  90. 'getStoreId',
  91. 'getData'
  92. ]
  93. );
  94. $this->storeMock = $this->createMock(\Magento\Store\Model\Store::class);
  95. $this->searchResultsDataFactory = $this->createPartialMock(
  96. \Magento\Quote\Api\Data\CartSearchResultsInterfaceFactory::class,
  97. ['create']
  98. );
  99. $this->quoteCollectionMock =
  100. $this->createMock(\Magento\Quote\Model\ResourceModel\Quote\Collection::class);
  101. $this->extensionAttributesJoinProcessorMock = $this->createMock(
  102. \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface::class
  103. );
  104. $this->collectionProcessor = $this->createMock(
  105. \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class
  106. );
  107. $this->quoteCollectionFactoryMock = $this->createPartialMock(
  108. \Magento\Quote\Model\ResourceModel\Quote\CollectionFactory::class,
  109. ['create']
  110. );
  111. $this->model = $objectManager->getObject(
  112. \Magento\Quote\Model\QuoteRepository::class,
  113. [
  114. 'quoteFactory' => $this->quoteFactoryMock,
  115. 'storeManager' => $this->storeManagerMock,
  116. 'searchResultsDataFactory' => $this->searchResultsDataFactory,
  117. 'quoteCollection' => $this->quoteCollectionMock,
  118. 'extensionAttributesJoinProcessor' => $this->extensionAttributesJoinProcessorMock,
  119. 'collectionProcessor' => $this->collectionProcessor,
  120. 'quoteCollectionFactory' => $this->quoteCollectionFactoryMock
  121. ]
  122. );
  123. $this->loadHandlerMock = $this->createMock(LoadHandler::class);
  124. $this->saveHandlerMock = $this->createMock(SaveHandler::class);
  125. $reflection = new \ReflectionClass(get_class($this->model));
  126. $reflectionProperty = $reflection->getProperty('loadHandler');
  127. $reflectionProperty->setAccessible(true);
  128. $reflectionProperty->setValue($this->model, $this->loadHandlerMock);
  129. $reflectionProperty = $reflection->getProperty('saveHandler');
  130. $reflectionProperty->setAccessible(true);
  131. $reflectionProperty->setValue($this->model, $this->saveHandlerMock);
  132. }
  133. /**
  134. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  135. * @expectedExceptionMessage No such entity with cartId = 14
  136. */
  137. public function testGetWithExceptionById()
  138. {
  139. $cartId = 14;
  140. $this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteMock);
  141. $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
  142. $this->storeMock->expects($this->once())->method('getId')->willReturn(1);
  143. $this->quoteMock->expects($this->never())->method('setSharedStoreIds');
  144. $this->quoteMock->expects($this->once())
  145. ->method('loadByIdWithoutStore')
  146. ->with($cartId)
  147. ->willReturn($this->storeMock);
  148. $this->quoteMock->expects($this->once())->method('getId')->willReturn(false);
  149. $this->model->get($cartId);
  150. }
  151. public function testGet()
  152. {
  153. $cartId = 15;
  154. $this->quoteFactoryMock->expects(static::once())
  155. ->method('create')
  156. ->willReturn($this->quoteMock);
  157. $this->storeManagerMock->expects(static::once())
  158. ->method('getStore')
  159. ->willReturn($this->storeMock);
  160. $this->storeMock->expects(static::once())
  161. ->method('getId')
  162. ->willReturn(1);
  163. $this->quoteMock->expects(static::never())
  164. ->method('setSharedStoreIds');
  165. $this->quoteMock->expects(static::once())
  166. ->method('loadByIdWithoutStore')
  167. ->with($cartId)
  168. ->willReturn($this->storeMock);
  169. $this->quoteMock->expects(static::once())
  170. ->method('getId')
  171. ->willReturn($cartId);
  172. $this->quoteMock->expects(static::never())
  173. ->method('getCustomerId');
  174. $this->loadHandlerMock->expects(static::once())
  175. ->method('load')
  176. ->with($this->quoteMock);
  177. static::assertEquals($this->quoteMock, $this->model->get($cartId));
  178. static::assertEquals($this->quoteMock, $this->model->get($cartId));
  179. }
  180. public function testGetForCustomerAfterGet()
  181. {
  182. $cartId = 15;
  183. $customerId = 23;
  184. $this->quoteFactoryMock->expects(static::exactly(2))
  185. ->method('create')
  186. ->willReturn($this->quoteMock);
  187. $this->storeManagerMock->expects(static::exactly(2))
  188. ->method('getStore')
  189. ->willReturn($this->storeMock);
  190. $this->storeMock->expects(static::exactly(2))
  191. ->method('getId')
  192. ->willReturn(1);
  193. $this->quoteMock->expects(static::never())
  194. ->method('setSharedStoreIds');
  195. $this->quoteMock->expects(static::once())
  196. ->method('loadByIdWithoutStore')
  197. ->with($cartId)
  198. ->willReturn($this->storeMock);
  199. $this->quoteMock->expects(static::once())
  200. ->method('loadByCustomer')
  201. ->with($customerId)
  202. ->willReturn($this->storeMock);
  203. $this->quoteMock->expects(static::exactly(3))
  204. ->method('getId')
  205. ->willReturn($cartId);
  206. $this->quoteMock->expects(static::any())
  207. ->method('getCustomerId')
  208. ->willReturn($customerId);
  209. $this->loadHandlerMock->expects(static::exactly(2))
  210. ->method('load')
  211. ->with($this->quoteMock);
  212. static::assertEquals($this->quoteMock, $this->model->get($cartId));
  213. static::assertEquals($this->quoteMock, $this->model->getForCustomer($customerId));
  214. }
  215. public function testGetWithSharedStoreIds()
  216. {
  217. $cartId = 16;
  218. $sharedStoreIds = [1, 2];
  219. $this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteMock);
  220. $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
  221. $this->storeMock->expects($this->once())->method('getId')->willReturn(1);
  222. $this->quoteMock->expects($this->once())
  223. ->method('setSharedStoreIds')
  224. ->with($sharedStoreIds)
  225. ->willReturnSelf();
  226. $this->quoteMock->expects($this->once())
  227. ->method('loadByIdWithoutStore')
  228. ->with($cartId)
  229. ->willReturn($this->storeMock);
  230. $this->quoteMock->expects($this->once())->method('getId')->willReturn($cartId);
  231. $this->loadHandlerMock->expects($this->once())
  232. ->method('load')
  233. ->with($this->quoteMock)
  234. ->willReturn($this->quoteMock);
  235. $this->assertEquals($this->quoteMock, $this->model->get($cartId, $sharedStoreIds));
  236. }
  237. public function testGetForCustomer()
  238. {
  239. $cartId = 17;
  240. $customerId = 23;
  241. $this->quoteFactoryMock->expects(static::once())
  242. ->method('create')
  243. ->willReturn($this->quoteMock);
  244. $this->storeManagerMock->expects(static::once())
  245. ->method('getStore')
  246. ->willReturn($this->storeMock);
  247. $this->storeMock->expects(static::once())
  248. ->method('getId')
  249. ->willReturn(1);
  250. $this->quoteMock->expects(static::never())
  251. ->method('setSharedStoreIds');
  252. $this->quoteMock->expects(static::once())
  253. ->method('loadByCustomer')
  254. ->with($customerId)
  255. ->willReturn($this->storeMock);
  256. $this->quoteMock->expects(static::exactly(2))
  257. ->method('getId')
  258. ->willReturn($cartId);
  259. $this->loadHandlerMock->expects(static::once())
  260. ->method('load')
  261. ->with($this->quoteMock);
  262. static::assertEquals($this->quoteMock, $this->model->getForCustomer($customerId));
  263. static::assertEquals($this->quoteMock, $this->model->getForCustomer($customerId));
  264. }
  265. /**
  266. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  267. * @expectedExceptionMessage No such entity with cartId = 14
  268. */
  269. public function testGetActiveWithExceptionById()
  270. {
  271. $cartId = 14;
  272. $this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteMock);
  273. $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
  274. $this->storeMock->expects($this->once())->method('getId')->willReturn(1);
  275. $this->quoteMock->expects($this->never())->method('setSharedStoreIds');
  276. $this->quoteMock->expects($this->once())
  277. ->method('loadByIdWithoutStore')
  278. ->with($cartId)
  279. ->willReturn($this->storeMock);
  280. $this->quoteMock->expects($this->once())->method('getId')->willReturn(false);
  281. $this->quoteMock->expects($this->never())->method('getIsActive');
  282. $this->model->getActive($cartId);
  283. }
  284. /**
  285. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  286. * @expectedExceptionMessage No such entity with cartId = 15
  287. */
  288. public function testGetActiveWithExceptionByIsActive()
  289. {
  290. $cartId = 15;
  291. $this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteMock);
  292. $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
  293. $this->storeMock->expects($this->once())->method('getId')->willReturn(1);
  294. $this->quoteMock->expects($this->never())->method('setSharedStoreIds');
  295. $this->quoteMock->expects($this->once())
  296. ->method('loadByIdWithoutStore')
  297. ->with($cartId)
  298. ->willReturn($this->storeMock);
  299. $this->quoteMock->expects($this->once())->method('getId')->willReturn($cartId);
  300. $this->quoteMock->expects($this->once())->method('getIsActive')->willReturn(0);
  301. $this->loadHandlerMock->expects($this->once())
  302. ->method('load')
  303. ->with($this->quoteMock)
  304. ->willReturn($this->quoteMock);
  305. $this->model->getActive($cartId);
  306. }
  307. public function testGetActive()
  308. {
  309. $cartId = 15;
  310. $this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteMock);
  311. $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
  312. $this->storeMock->expects($this->once())->method('getId')->willReturn(1);
  313. $this->quoteMock->expects($this->never())->method('setSharedStoreIds');
  314. $this->quoteMock->expects($this->once())
  315. ->method('loadByIdWithoutStore')
  316. ->with($cartId)
  317. ->willReturn($this->storeMock);
  318. $this->quoteMock->expects($this->once())->method('getId')->willReturn($cartId);
  319. $this->quoteMock->expects($this->once())->method('getIsActive')->willReturn(1);
  320. $this->loadHandlerMock->expects($this->once())
  321. ->method('load')
  322. ->with($this->quoteMock)
  323. ->willReturn($this->quoteMock);
  324. $this->assertEquals($this->quoteMock, $this->model->getActive($cartId));
  325. }
  326. public function testGetActiveWithSharedStoreIds()
  327. {
  328. $cartId = 16;
  329. $sharedStoreIds = [1, 2];
  330. $this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteMock);
  331. $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
  332. $this->storeMock->expects($this->once())->method('getId')->willReturn(1);
  333. $this->quoteMock->expects($this->once())
  334. ->method('setSharedStoreIds')
  335. ->with($sharedStoreIds)
  336. ->willReturnSelf();
  337. $this->quoteMock->expects($this->once())
  338. ->method('loadByIdWithoutStore')
  339. ->with($cartId)
  340. ->willReturn($this->storeMock);
  341. $this->quoteMock->expects($this->once())->method('getId')->willReturn($cartId);
  342. $this->quoteMock->expects($this->once())->method('getIsActive')->willReturn(1);
  343. $this->loadHandlerMock->expects($this->once())
  344. ->method('load')
  345. ->with($this->quoteMock)
  346. ->willReturn($this->quoteMock);
  347. $this->assertEquals($this->quoteMock, $this->model->getActive($cartId, $sharedStoreIds));
  348. }
  349. public function testGetActiveForCustomer()
  350. {
  351. $cartId = 17;
  352. $customerId = 23;
  353. $this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteMock);
  354. $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
  355. $this->storeMock->expects($this->once())->method('getId')->willReturn(1);
  356. $this->quoteMock->expects($this->never())->method('setSharedStoreIds');
  357. $this->quoteMock->expects($this->once())
  358. ->method('loadByCustomer')
  359. ->with($customerId)
  360. ->willReturn($this->storeMock);
  361. $this->quoteMock->expects($this->exactly(2))->method('getId')->willReturn($cartId);
  362. $this->quoteMock->expects($this->exactly(2))->method('getIsActive')->willReturn(1);
  363. $this->loadHandlerMock->expects($this->once())
  364. ->method('load')
  365. ->with($this->quoteMock)
  366. ->willReturn($this->quoteMock);
  367. $this->assertEquals($this->quoteMock, $this->model->getActiveForCustomer($customerId));
  368. $this->assertEquals($this->quoteMock, $this->model->getActiveForCustomer($customerId));
  369. }
  370. public function testSave()
  371. {
  372. $cartId = 100;
  373. $quoteMock = $this->createPartialMock(
  374. \Magento\Quote\Model\Quote::class,
  375. ['getId', 'getCustomerId', 'getStoreId', 'hasData', 'setData']
  376. );
  377. $quoteMock->expects($this->exactly(3))->method('getId')->willReturn($cartId);
  378. $quoteMock->expects($this->once())->method('getCustomerId')->willReturn(2);
  379. $quoteMock->expects($this->once())->method('getStoreId')->willReturn(5);
  380. $this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteMock);
  381. $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
  382. $this->storeMock->expects($this->once())->method('getId')->willReturn(1);
  383. $this->quoteMock->expects($this->once())->method('getId')->willReturn($cartId);
  384. $this->quoteMock->expects($this->once())->method('setSharedStoreIds');
  385. $this->quoteMock->expects($this->once())
  386. ->method('loadByIdWithoutStore')
  387. ->with($cartId)
  388. ->willReturn($this->storeMock);
  389. $this->loadHandlerMock->expects($this->once())
  390. ->method('load')
  391. ->with($this->quoteMock)
  392. ->willReturn($this->quoteMock);
  393. $this->quoteMock->expects($this->once())->method('getData')->willReturn(['key' => 'value']);
  394. $quoteMock->expects($this->once())->method('hasData')->with('key')->willReturn(false);
  395. $quoteMock->expects($this->once())->method('setData')->with('key', 'value')->willReturnSelf();
  396. $this->saveHandlerMock->expects($this->once())->method('save')->with($quoteMock)->willReturn($quoteMock);
  397. $this->model->save($quoteMock);
  398. }
  399. public function testDelete()
  400. {
  401. $this->quoteMock->expects($this->once())
  402. ->method('delete');
  403. $this->quoteMock->expects($this->exactly(1))->method('getId')->willReturn(1);
  404. $this->quoteMock->expects($this->exactly(1))->method('getCustomerId')->willReturn(2);
  405. $this->model->delete($this->quoteMock);
  406. }
  407. public function testGetList()
  408. {
  409. $pageSize = 10;
  410. $this->quoteCollectionFactoryMock->expects($this->once())
  411. ->method('create')
  412. ->willReturn($this->quoteCollectionMock);
  413. $cartMock = $this->createMock(CartInterface::class);
  414. $this->loadHandlerMock->expects($this->once())
  415. ->method('load')
  416. ->with($cartMock);
  417. $searchResult = $this->createMock(\Magento\Quote\Api\Data\CartSearchResultsInterface::class);
  418. $searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
  419. $this->searchResultsDataFactory
  420. ->expects($this->once())
  421. ->method('create')
  422. ->willReturn($searchResult);
  423. $this->collectionProcessor->expects($this->once())
  424. ->method('process')
  425. ->with($searchCriteriaMock, $this->quoteCollectionMock);
  426. $this->extensionAttributesJoinProcessorMock->expects($this->once())
  427. ->method('process')
  428. ->with(
  429. $this->isInstanceOf(\Magento\Quote\Model\ResourceModel\Quote\Collection::class)
  430. );
  431. $this->quoteCollectionMock->expects($this->atLeastOnce())->method('getItems')->willReturn([$cartMock]);
  432. $searchResult->expects($this->once())->method('setTotalCount')->with($pageSize);
  433. $this->quoteCollectionMock->expects($this->once())
  434. ->method('getSize')
  435. ->willReturn($pageSize);
  436. $searchResult->expects($this->once())
  437. ->method('setItems')
  438. ->with([$cartMock]);
  439. $this->assertEquals($searchResult, $this->model->getList($searchCriteriaMock));
  440. }
  441. /**
  442. * @deprecated
  443. * @return array
  444. */
  445. public function getListSuccessDataProvider()
  446. {
  447. return [
  448. 'asc' => [SortOrder::SORT_ASC, 'ASC'],
  449. 'desc' => [SortOrder::SORT_DESC, 'DESC']
  450. ];
  451. }
  452. }