QuoteTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Test\Unit\Model\Session;
  7. /**
  8. * Class QuoteTest
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. * @SuppressWarnings(PHPMD.TooManyFields)
  11. */
  12. class QuoteTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  16. */
  17. private $objectManager;
  18. /**
  19. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $storeManagerMock;
  22. /**
  23. * @var \Magento\Sales\Model\OrderFactory|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $orderFactoryMock;
  26. /**
  27. * @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $cookieMetadataFactoryMock;
  30. /**
  31. * @var \Magento\Framework\Stdlib\CookieManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $cookieManagerMock;
  34. /**
  35. * @var \Magento\Framework\Session\StorageInterface
  36. */
  37. protected $storage;
  38. /**
  39. * @var \Magento\Framework\Session\ValidatorInterface|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $validatorMock;
  42. /**
  43. * @var \Magento\Framework\Session\SaveHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $saveHandlerMock;
  46. /**
  47. * @var \Magento\Framework\Session\Config\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected $sessionConfigMock;
  50. /**
  51. * @var \Magento\Framework\Session\SidResolverInterface|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. protected $sidResolverMock;
  54. /**
  55. * @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
  56. */
  57. protected $requestMock;
  58. /**
  59. * @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  60. */
  61. protected $customerRepositoryMock;
  62. /**
  63. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  64. */
  65. protected $scopeConfigMock;
  66. /**
  67. * @var \Magento\Quote\Model\QuoteFactory|\PHPUnit_Framework_MockObject_MockObject
  68. */
  69. protected $quoteRepositoryMock;
  70. /**
  71. * @var \Magento\Backend\Model\Session\Quote|\PHPUnit_Framework_MockObject_MockObject
  72. */
  73. protected $quote;
  74. /**
  75. * @var \Magento\Customer\Api\GroupManagementInterface|\PHPUnit_Framework_MockObject_MockObject
  76. */
  77. protected $groupManagementMock;
  78. /**
  79. * @var \PHPUnit_Framework_MockObject_MockObject
  80. */
  81. protected $quoteFactoryMock;
  82. /**
  83. * Set up
  84. *
  85. * @return void
  86. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  87. */
  88. protected function setUp()
  89. {
  90. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  91. $this->customerRepositoryMock = $this->getMockForAbstractClass(
  92. \Magento\Customer\Api\CustomerRepositoryInterface::class,
  93. [],
  94. '',
  95. false,
  96. true,
  97. true,
  98. ['getCustomer']
  99. );
  100. $this->groupManagementMock = $this->getMockForAbstractClass(
  101. \Magento\Customer\Api\GroupManagementInterface::class,
  102. [],
  103. '',
  104. false,
  105. true,
  106. true,
  107. ['getDefaultGroup']
  108. );
  109. $this->scopeConfigMock = $this->getMockForAbstractClass(
  110. \Magento\Framework\App\Config\ScopeConfigInterface::class,
  111. [],
  112. '',
  113. false,
  114. true,
  115. true,
  116. ['getValue']
  117. );
  118. $this->quoteRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
  119. $this->requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
  120. $this->sidResolverMock = $this->getMockForAbstractClass(
  121. \Magento\Framework\Session\SidResolverInterface::class,
  122. [],
  123. '',
  124. false
  125. );
  126. $this->sessionConfigMock = $this->getMockForAbstractClass(
  127. \Magento\Framework\Session\Config\ConfigInterface::class,
  128. [],
  129. '',
  130. false
  131. );
  132. $this->saveHandlerMock = $this->getMockForAbstractClass(
  133. \Magento\Framework\Session\SaveHandlerInterface::class,
  134. [],
  135. '',
  136. false
  137. );
  138. $this->validatorMock = $this->getMockForAbstractClass(
  139. \Magento\Framework\Session\ValidatorInterface::class,
  140. [],
  141. '',
  142. false
  143. );
  144. $this->storage = new \Magento\Framework\Session\Storage();
  145. $this->cookieManagerMock = $this->createMock(\Magento\Framework\Stdlib\CookieManagerInterface::class);
  146. $this->cookieMetadataFactoryMock = $this->createMock(
  147. \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class
  148. );
  149. $this->orderFactoryMock = $this->createPartialMock(\Magento\Sales\Model\OrderFactory::class, ['create']);
  150. $appStateMock = $this->createMock(\Magento\Framework\App\State::class);
  151. $this->storeManagerMock = $this->getMockForAbstractClass(
  152. \Magento\Store\Model\StoreManagerInterface::class,
  153. [],
  154. '',
  155. false
  156. );
  157. $this->quoteFactoryMock = $this->createPartialMock(\Magento\Quote\Model\QuoteFactory::class, ['create']);
  158. $this->quote = $this->getMockBuilder(\Magento\Backend\Model\Session\Quote::class)
  159. ->setMethods(['getStoreId', 'getQuoteId', 'setQuoteId', 'hasCustomerId', 'getCustomerId'])
  160. ->setConstructorArgs(
  161. [
  162. 'request' => $this->requestMock,
  163. 'sidResolver' => $this->sidResolverMock,
  164. 'sessionConfig' => $this->sessionConfigMock,
  165. 'saveHandler' => $this->saveHandlerMock,
  166. 'validator' => $this->validatorMock,
  167. 'storage' => $this->storage,
  168. 'cookieManager' => $this->cookieManagerMock,
  169. 'cookieMetadataFactory' => $this->cookieMetadataFactoryMock,
  170. 'appState' => $appStateMock,
  171. 'customerRepository' => $this->customerRepositoryMock,
  172. 'quoteRepository' => $this->quoteRepositoryMock,
  173. 'orderFactory' => $this->orderFactoryMock,
  174. 'storeManager' => $this->storeManagerMock,
  175. 'groupManagement' => $this->groupManagementMock,
  176. 'quoteFactory' => $this->quoteFactoryMock
  177. ]
  178. )
  179. ->getMock();
  180. }
  181. /**
  182. * Run test getQuote method
  183. *
  184. * @return void
  185. */
  186. public function testGetQuoteWithoutQuoteId()
  187. {
  188. $quoteId = 22;
  189. $storeId = 10;
  190. $customerId = 66;
  191. $customerGroupId = 77;
  192. $this->quote->expects($this->any())->method('getQuoteId')->will($this->returnValue(null));
  193. $this->quote->expects($this->any())->method('setQuoteId')->with($quoteId);
  194. $cartInterfaceMock = $this->createPartialMock(
  195. \Magento\Quote\Api\Data\CartInterface::class,
  196. [
  197. 'getId',
  198. 'setId',
  199. 'getCreatedAt',
  200. 'setCreatedAt',
  201. 'getUpdatedAt',
  202. 'setUpdatedAt',
  203. 'getConvertedAt',
  204. 'setConvertedAt',
  205. 'getIsActive',
  206. 'setIsActive',
  207. 'getIsVirtual',
  208. 'getItems',
  209. 'setItems',
  210. 'getItemsCount',
  211. 'setItemsCount',
  212. 'getItemsQty',
  213. 'setItemsQty',
  214. 'getCustomer',
  215. 'setCustomer',
  216. 'getBillingAddress',
  217. 'setBillingAddress',
  218. 'getReservedOrderId',
  219. 'setReservedOrderId',
  220. 'getOrigOrderId',
  221. 'setOrigOrderId',
  222. 'getCurrency',
  223. 'setCurrency',
  224. 'getCustomerIsGuest',
  225. 'setCustomerIsGuest',
  226. 'getCustomerNote',
  227. 'setCustomerNote',
  228. 'getCustomerNoteNotify',
  229. 'setCustomerNoteNotify',
  230. 'getCustomerTaxClassId',
  231. 'setCustomerTaxClassId',
  232. 'getStoreId',
  233. 'setStoreId',
  234. 'getExtensionAttributes',
  235. 'setExtensionAttributes',
  236. 'setIgnoreOldQty',
  237. 'setIsSuperMode',
  238. 'setCustomerGroupId'
  239. ]
  240. );
  241. $this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($cartInterfaceMock);
  242. $this->quote->expects($this->any())->method('getStoreId')->will($this->returnValue($storeId));
  243. $this->quote->expects($this->any())->method('getCustomerId')->will($this->returnValue($customerId));
  244. $cartInterfaceMock->expects($this->atLeastOnce())->method('getId')->willReturn($quoteId);
  245. $defaultGroup = $this->getMockBuilder(\Magento\Customer\Api\Data\GroupInterface::class)->getMock();
  246. $defaultGroup->expects($this->any())->method('getId')->will($this->returnValue($customerGroupId));
  247. $this->groupManagementMock->expects($this->any())->method('getDefaultGroup')->willReturn($defaultGroup);
  248. $dataCustomerMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
  249. ->disableOriginalConstructor()
  250. ->getMock();
  251. $this->customerRepositoryMock->expects($this->once())
  252. ->method('getById')
  253. ->with($customerId)
  254. ->willReturn($dataCustomerMock);
  255. $quoteMock = $this->createPartialMock(\Magento\Quote\Model\Quote::class, [
  256. 'setStoreId',
  257. 'setCustomerGroupId',
  258. 'setIsActive',
  259. 'assignCustomer',
  260. 'setIgnoreOldQty',
  261. 'setIsSuperMode',
  262. '__wakeup'
  263. ]);
  264. $this->quoteRepositoryMock->expects($this->once())->method('get')->willReturn($quoteMock);
  265. $cartInterfaceMock->expects($this->once())->method('setCustomerGroupId')->with($customerGroupId)
  266. ->willReturnSelf();
  267. $quoteMock->expects($this->once())->method('assignCustomer')->with($dataCustomerMock);
  268. $quoteMock->expects($this->once())->method('setIgnoreOldQty')->with(true);
  269. $quoteMock->expects($this->once())->method('setIsSuperMode')->with(true);
  270. $this->assertEquals($quoteMock, $this->quote->getQuote());
  271. }
  272. /**
  273. * Run test getQuote method
  274. *
  275. * @return void
  276. * @dataProvider getQuoteDataProvider
  277. */
  278. public function testGetQuoteWithQuoteId($customerId, $quoteCustomerId, $expectedNumberOfInvokes)
  279. {
  280. $quoteId = 22;
  281. $storeId = 10;
  282. $this->quote->expects($this->any())
  283. ->method('getQuoteId')
  284. ->will($this->returnValue($quoteId));
  285. $this->quote->expects($this->any())
  286. ->method('setQuoteId')
  287. ->with($quoteId);
  288. $this->quote->expects($this->any())
  289. ->method('getStoreId')
  290. ->will($this->returnValue($storeId));
  291. $this->quote->expects($this->any())
  292. ->method('getCustomerId')
  293. ->will($this->returnValue($customerId));
  294. $dataCustomerMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
  295. ->disableOriginalConstructor()
  296. ->getMock();
  297. $this->customerRepositoryMock->expects($this->$expectedNumberOfInvokes())
  298. ->method('getById')
  299. ->with($customerId)
  300. ->willReturn($dataCustomerMock);
  301. $quoteMock = $this->createPartialMock(\Magento\Quote\Model\Quote::class, [
  302. 'setStoreId',
  303. 'setCustomerGroupId',
  304. 'setIsActive',
  305. 'getId',
  306. 'assignCustomer',
  307. 'setIgnoreOldQty',
  308. 'setIsSuperMode',
  309. 'getCustomerId',
  310. '__wakeup'
  311. ]);
  312. $quoteMock->expects($this->once())
  313. ->method('setStoreId')
  314. ->with($storeId);
  315. $quoteMock->expects($this->$expectedNumberOfInvokes())
  316. ->method('assignCustomer')
  317. ->with($dataCustomerMock);
  318. $quoteMock->expects($this->once())
  319. ->method('setIgnoreOldQty')
  320. ->with(true);
  321. $quoteMock->expects($this->once())
  322. ->method('setIsSuperMode')
  323. ->with(true);
  324. $quoteMock->expects($this->once())
  325. ->method('getCustomerId')
  326. ->will($this->returnValue($quoteCustomerId));
  327. $this->quoteRepositoryMock->expects($this->once())
  328. ->method('get')
  329. ->with($quoteId)
  330. ->willReturn($quoteMock);
  331. $this->assertEquals($quoteMock, $this->quote->getQuote());
  332. }
  333. /**
  334. * @return array
  335. */
  336. public function getQuoteDataProvider()
  337. {
  338. return [
  339. 'customer ids different' => [66, null, 'once'],
  340. 'customer ids same' => [66, 66, 'never'],
  341. ];
  342. }
  343. }