CreateTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Unit\Model\AdminOrder;
  7. use Magento\Backend\Model\Session\Quote as SessionQuote;
  8. use Magento\Customer\Api\Data\AttributeMetadataInterface;
  9. use Magento\Customer\Api\Data\CustomerInterface;
  10. use Magento\Customer\Api\Data\CustomerInterfaceFactory;
  11. use Magento\Customer\Api\Data\GroupInterface;
  12. use Magento\Customer\Api\GroupRepositoryInterface;
  13. use Magento\Customer\Model\Customer\Mapper;
  14. use Magento\Customer\Model\Metadata\Form;
  15. use Magento\Customer\Model\Metadata\FormFactory;
  16. use Magento\Framework\Api\DataObjectHelper;
  17. use Magento\Framework\App\RequestInterface;
  18. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  19. use Magento\Quote\Api\CartRepositoryInterface;
  20. use Magento\Quote\Model\Quote;
  21. use Magento\Quote\Model\Quote\Address;
  22. use Magento\Quote\Model\Quote\Item;
  23. use Magento\Quote\Model\Quote\Item\Updater;
  24. use Magento\Sales\Model\AdminOrder\Create;
  25. use Magento\Sales\Model\AdminOrder\Product;
  26. use Magento\Quote\Model\QuoteFactory;
  27. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  28. /**
  29. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  30. * @SuppressWarnings(PHPMD.TooManyFields)
  31. */
  32. class CreateTest extends \PHPUnit\Framework\TestCase
  33. {
  34. const CUSTOMER_ID = 1;
  35. /**
  36. * @var Create
  37. */
  38. private $adminOrderCreate;
  39. /**
  40. * @var CartRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  41. */
  42. private $quoteRepository;
  43. /**
  44. * @var QuoteFactory|\PHPUnit_Framework_MockObject_MockObject
  45. */
  46. private $quoteFactory;
  47. /**
  48. * @var SessionQuote|MockObject
  49. */
  50. private $sessionQuote;
  51. /**
  52. * @var FormFactory|MockObject
  53. */
  54. private $formFactory;
  55. /**
  56. * @var CustomerInterfaceFactory|MockObject
  57. */
  58. private $customerFactory;
  59. /**
  60. * @var Updater|MockObject
  61. */
  62. private $itemUpdater;
  63. /**
  64. * @var Mapper|MockObject
  65. */
  66. private $customerMapper;
  67. /**
  68. * @var GroupRepositoryInterface|MockObject
  69. */
  70. private $groupRepository;
  71. /**
  72. * @var DataObjectHelper|MockObject
  73. */
  74. private $dataObjectHelper;
  75. protected function setUp()
  76. {
  77. $this->formFactory = $this->createPartialMock(FormFactory::class, ['create']);
  78. $this->quoteFactory = $this->createPartialMock(QuoteFactory::class, ['create']);
  79. $this->customerFactory = $this->createPartialMock(CustomerInterfaceFactory::class, ['create']);
  80. $this->itemUpdater = $this->createMock(Updater::class);
  81. $this->quoteRepository = $this->getMockBuilder(CartRepositoryInterface::class)
  82. ->disableOriginalConstructor()
  83. ->setMethods(['getForCustomer'])
  84. ->getMockForAbstractClass();
  85. $this->sessionQuote = $this->getMockBuilder(\Magento\Backend\Model\Session\Quote::class)
  86. ->disableOriginalConstructor()
  87. ->setMethods(['getQuote', 'getStoreId', 'getCustomerId'])
  88. ->getMock();
  89. $this->customerMapper = $this->getMockBuilder(Mapper::class)
  90. ->setMethods(['toFlatArray'])
  91. ->disableOriginalConstructor()
  92. ->getMock();
  93. $this->groupRepository = $this->getMockForAbstractClass(GroupRepositoryInterface::class);
  94. $this->dataObjectHelper = $this->getMockBuilder(DataObjectHelper::class)
  95. ->disableOriginalConstructor()
  96. ->getMock();
  97. $objectManagerHelper = new ObjectManagerHelper($this);
  98. $this->adminOrderCreate = $objectManagerHelper->getObject(
  99. Create::class,
  100. [
  101. 'quoteSession' => $this->sessionQuote,
  102. 'metadataFormFactory' => $this->formFactory,
  103. 'customerFactory' => $this->customerFactory,
  104. 'groupRepository' => $this->groupRepository,
  105. 'quoteItemUpdater' => $this->itemUpdater,
  106. 'customerMapper' => $this->customerMapper,
  107. 'dataObjectHelper' => $this->dataObjectHelper,
  108. 'quoteRepository' => $this->quoteRepository,
  109. 'quoteFactory' => $this->quoteFactory,
  110. ]
  111. );
  112. }
  113. public function testSetAccountData()
  114. {
  115. $taxClassId = 1;
  116. $attributes = [
  117. ['email', 'user@example.com'],
  118. ['group_id', 1]
  119. ];
  120. $attributeMocks = [];
  121. foreach ($attributes as $value) {
  122. $attribute = $this->createMock(AttributeMetadataInterface::class);
  123. $attribute->method('getAttributeCode')
  124. ->willReturn($value[0]);
  125. $attributeMocks[] = $attribute;
  126. }
  127. $customerGroup = $this->getMockForAbstractClass(GroupInterface::class);
  128. $customerGroup->method('getTaxClassId')
  129. ->willReturn($taxClassId);
  130. $customerForm = $this->createMock(Form::class);
  131. $customerForm->method('getAttributes')
  132. ->willReturn([$attributeMocks[1]]);
  133. $customerForm
  134. ->method('extractData')
  135. ->willReturn([]);
  136. $customerForm
  137. ->method('restoreData')
  138. ->willReturn(['group_id' => 1]);
  139. $customerForm->method('prepareRequest')
  140. ->willReturn($this->createMock(RequestInterface::class));
  141. $customer = $this->createMock(CustomerInterface::class);
  142. $this->customerMapper->expects(self::atLeastOnce())
  143. ->method('toFlatArray')
  144. ->willReturn(['group_id' => 1]);
  145. $quote = $this->createMock(Quote::class);
  146. $quote->method('getCustomer')->willReturn($customer);
  147. $quote->method('addData')->with(
  148. [
  149. 'customer_group_id' => $attributes[1][1],
  150. 'customer_tax_class_id' => $taxClassId
  151. ]
  152. );
  153. $this->dataObjectHelper->method('populateWithArray')
  154. ->with(
  155. $customer,
  156. ['group_id' => 1],
  157. CustomerInterface::class
  158. );
  159. $this->formFactory->method('create')
  160. ->willReturn($customerForm);
  161. $this->sessionQuote
  162. ->method('getQuote')
  163. ->willReturn($quote);
  164. $this->customerFactory->method('create')
  165. ->willReturn($customer);
  166. $this->groupRepository->method('getById')
  167. ->willReturn($customerGroup);
  168. $this->adminOrderCreate->setAccountData(['group_id' => 1]);
  169. }
  170. public function testUpdateQuoteItemsNotArray()
  171. {
  172. $object = $this->adminOrderCreate->updateQuoteItems('string');
  173. self::assertEquals($this->adminOrderCreate, $object);
  174. }
  175. public function testUpdateQuoteItemsEmptyConfiguredOption()
  176. {
  177. $items = [
  178. 1 => [
  179. 'qty' => 10,
  180. 'configured' => false,
  181. 'action' => false
  182. ]
  183. ];
  184. $item = $this->createMock(Item::class);
  185. $quote = $this->createMock(Quote::class);
  186. $quote->method('getItemById')
  187. ->willReturn($item);
  188. $this->sessionQuote->method('getQuote')
  189. ->willReturn($quote);
  190. $this->itemUpdater->method('update')
  191. ->with(self::equalTo($item), self::equalTo($items[1]))
  192. ->willReturnSelf();
  193. $this->adminOrderCreate->setRecollect(false);
  194. $object = $this->adminOrderCreate->updateQuoteItems($items);
  195. self::assertEquals($this->adminOrderCreate, $object);
  196. }
  197. public function testUpdateQuoteItemsWithConfiguredOption()
  198. {
  199. $qty = 100000000;
  200. $items = [
  201. 1 => [
  202. 'qty' => 10,
  203. 'configured' => true,
  204. 'action' => false
  205. ]
  206. ];
  207. $item = $this->createMock(Item::class);
  208. $item->method('getQty')
  209. ->willReturn($qty);
  210. $quote = $this->createMock(Quote::class);
  211. $quote->method('updateItem')
  212. ->willReturn($item);
  213. $this->sessionQuote
  214. ->method('getQuote')
  215. ->willReturn($quote);
  216. $expectedInfo = $items[1];
  217. $expectedInfo['qty'] = $qty;
  218. $this->itemUpdater->method('update')
  219. ->with(self::equalTo($item), self::equalTo($expectedInfo));
  220. $this->adminOrderCreate->setRecollect(false);
  221. $object = $this->adminOrderCreate->updateQuoteItems($items);
  222. self::assertEquals($this->adminOrderCreate, $object);
  223. }
  224. public function testApplyCoupon()
  225. {
  226. $couponCode = '123';
  227. $quote = $this->createPartialMock(Quote::class, ['getShippingAddress', 'setCouponCode']);
  228. $this->sessionQuote->method('getQuote')
  229. ->willReturn($quote);
  230. $address = $this->createPartialMock(Address::class, ['setCollectShippingRates', 'setFreeShipping']);
  231. $quote->method('getShippingAddress')
  232. ->willReturn($address);
  233. $quote->method('setCouponCode')
  234. ->with($couponCode)
  235. ->willReturnSelf();
  236. $address->method('setCollectShippingRates')
  237. ->with(true)
  238. ->willReturnSelf();
  239. $address->method('setFreeShipping')
  240. ->with(0)
  241. ->willReturnSelf();
  242. $object = $this->adminOrderCreate->applyCoupon($couponCode);
  243. self::assertEquals($this->adminOrderCreate, $object);
  244. }
  245. public function testGetCustomerCart()
  246. {
  247. $storeId = 2;
  248. $customerId = 2;
  249. $cartResult = [
  250. 'cart' => true,
  251. ];
  252. $this->quoteFactory->expects($this->once())
  253. ->method('create');
  254. $this->sessionQuote->expects($this->once())
  255. ->method('getStoreId')
  256. ->willReturn($storeId);
  257. $this->sessionQuote->expects($this->once())
  258. ->method('getCustomerId')
  259. ->willReturn($customerId);
  260. $this->quoteRepository->expects($this->once())
  261. ->method('getForCustomer')
  262. ->with($customerId, [$storeId])
  263. ->willReturn($cartResult);
  264. $this->assertEquals($cartResult, $this->adminOrderCreate->getCustomerCart());
  265. }
  266. }