AddressTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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\Quote;
  7. use Magento\Directory\Model\Currency;
  8. use Magento\Quote\Model\Quote\Address;
  9. use Magento\Quote\Model\Quote\Address\Rate;
  10. use Magento\Quote\Model\ResourceModel\Quote\Address\Rate\CollectionFactory as RateCollectionFactory;
  11. use Magento\Quote\Model\ResourceModel\Quote\Address\Rate\Collection as RatesCollection;
  12. use Magento\Shipping\Model\Rate\Result;
  13. use Magento\Store\Model\ScopeInterface;
  14. use Magento\Directory\Model\RegionFactory;
  15. use Magento\Quote\Model\Quote\Address\RateFactory;
  16. use Magento\Quote\Model\Quote\Address\RateCollectorInterfaceFactory;
  17. use Magento\Quote\Model\Quote\Address\RateRequest;
  18. use Magento\Quote\Model\Quote\Address\RateRequestFactory;
  19. use Magento\Quote\Model\Quote\Address\RateCollectorInterface;
  20. use Magento\Quote\Model\ResourceModel\Quote\Address\Item\CollectionFactory;
  21. use Magento\Quote\Model\ResourceModel\Quote\Address\Item\Collection;
  22. use Magento\Directory\Model\Region;
  23. use Magento\Store\Model\StoreManagerInterface;
  24. use Magento\Store\Api\Data\StoreInterface;
  25. use Magento\Store\Api\Data\WebsiteInterface;
  26. use Magento\Quote\Model\Quote\Address\RateResult\AbstractResult;
  27. use Magento\Framework\Serialize\Serializer\Json;
  28. /**
  29. * Test class for sales quote address model
  30. *
  31. * @see \Magento\Quote\Model\Quote\Address
  32. * @SuppressWarnings(PHPMD.TooManyFields)
  33. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  34. */
  35. class AddressTest extends \PHPUnit\Framework\TestCase
  36. {
  37. /**
  38. * @var Address
  39. */
  40. private $address;
  41. /**
  42. * @var \Magento\Quote\Model\Quote | \PHPUnit_Framework_MockObject_MockObject
  43. */
  44. private $quote;
  45. /**
  46. * @var \Magento\Quote\Model\Quote\Address\CustomAttributeListInterface | \PHPUnit_Framework_MockObject_MockObject
  47. */
  48. private $attributeList;
  49. /**
  50. * @var \Magento\Framework\App\Config | \PHPUnit_Framework_MockObject_MockObject
  51. */
  52. private $scopeConfig;
  53. /**
  54. * @var RateRequestFactory | \PHPUnit_Framework_MockObject_MockObject
  55. */
  56. private $requestFactory;
  57. /**
  58. * @var RateFactory | \PHPUnit_Framework_MockObject_MockObject
  59. */
  60. private $addressRateFactory;
  61. /**
  62. * @var RateCollectionFactory | \PHPUnit_Framework_MockObject_MockObject
  63. */
  64. private $rateCollectionFactory;
  65. /**
  66. * @var RateCollectorInterfaceFactory | \PHPUnit_Framework_MockObject_MockObject
  67. */
  68. private $rateCollector;
  69. /**
  70. * @var RateCollectorInterface | \PHPUnit_Framework_MockObject_MockObject
  71. */
  72. private $rateCollection;
  73. /**
  74. * @var CollectionFactory | \PHPUnit_Framework_MockObject_MockObject
  75. */
  76. private $itemCollectionFactory;
  77. /**
  78. * @var RegionFactory | \PHPUnit_Framework_MockObject_MockObject
  79. */
  80. private $regionFactory;
  81. /**
  82. * @var StoreManagerInterface | \PHPUnit_Framework_MockObject_MockObject
  83. */
  84. private $storeManager;
  85. /**
  86. * @var StoreInterface | \PHPUnit_Framework_MockObject_MockObject
  87. */
  88. private $store;
  89. /**
  90. * @var WebsiteInterface | \PHPUnit_Framework_MockObject_MockObject
  91. */
  92. private $website;
  93. /**
  94. * @var Region | \PHPUnit_Framework_MockObject_MockObject
  95. */
  96. private $region;
  97. /**
  98. * @var \Magento\Framework\Serialize\Serializer\Json | \PHPUnit_Framework_MockObject_MockObject
  99. */
  100. protected $serializer;
  101. protected function setUp()
  102. {
  103. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  104. $this->scopeConfig = $this->createMock(\Magento\Framework\App\Config::class);
  105. $this->serializer = new Json();
  106. $this->requestFactory = $this->getMockBuilder(RateRequestFactory::class)
  107. ->disableOriginalConstructor()
  108. ->getMock();
  109. $this->addressRateFactory = $this->getMockBuilder(RateFactory::class)
  110. ->disableOriginalConstructor()
  111. ->getMock();
  112. $this->rateCollector = $this->getMockBuilder(RateCollectorInterfaceFactory::class)
  113. ->disableOriginalConstructor()
  114. ->getMock();
  115. $this->rateCollectionFactory = $this->getMockBuilder(RateCollectionFactory::class)
  116. ->disableOriginalConstructor()
  117. ->getMock();
  118. $this->rateCollection = $this->getMockBuilder(RateCollectorInterface::class)
  119. ->disableOriginalConstructor()
  120. ->setMethods(['getResult'])
  121. ->getMockForAbstractClass();
  122. $this->itemCollectionFactory = $this->getMockBuilder(CollectionFactory::class)
  123. ->disableOriginalConstructor()
  124. ->getMock();
  125. $this->regionFactory = $this->getMockBuilder(RegionFactory::class)
  126. ->disableOriginalConstructor()
  127. ->getMock();
  128. $this->region = $this->getMockBuilder(Region::class)
  129. ->disableOriginalConstructor()
  130. ->getMock();
  131. $this->storeManager = $this->getMockBuilder(StoreManagerInterface::class)
  132. ->disableOriginalConstructor()
  133. ->getMock();
  134. $this->store = $this->getMockBuilder(StoreInterface::class)
  135. ->disableOriginalConstructor()
  136. ->setMethods(['getBaseCurrency', 'getCurrentCurrency', 'getCurrentCurrencyCode'])
  137. ->getMockForAbstractClass();
  138. $this->website = $this->getMockBuilder(WebsiteInterface::class)
  139. ->disableOriginalConstructor()
  140. ->getMock();
  141. $this->attributeList = $this->createMock(
  142. \Magento\Quote\Model\Quote\Address\CustomAttributeListInterface::class
  143. );
  144. $this->attributeList->method('getAttributes')->willReturn([]);
  145. $this->address = $objectManager->getObject(
  146. \Magento\Quote\Model\Quote\Address::class,
  147. [
  148. 'attributeList' => $this->attributeList,
  149. 'scopeConfig' => $this->scopeConfig,
  150. 'serializer' => $this->serializer,
  151. 'storeManager' => $this->storeManager,
  152. '_itemCollectionFactory' => $this->itemCollectionFactory,
  153. '_rateRequestFactory' => $this->requestFactory,
  154. '_rateCollectionFactory' => $this->rateCollectionFactory,
  155. '_rateCollector' => $this->rateCollector,
  156. '_regionFactory' => $this->regionFactory,
  157. '_addressRateFactory' => $this->addressRateFactory
  158. ]
  159. );
  160. $this->quote = $this->createMock(\Magento\Quote\Model\Quote::class);
  161. $this->address->setQuote($this->quote);
  162. }
  163. public function testValidateMinimumAmountDisabled()
  164. {
  165. $storeId = 1;
  166. $this->quote->expects($this->once())
  167. ->method('getStoreId')
  168. ->willReturn($storeId);
  169. $this->scopeConfig->expects($this->once())
  170. ->method('isSetFlag')
  171. ->with('sales/minimum_order/active', ScopeInterface::SCOPE_STORE, $storeId)
  172. ->willReturn(false);
  173. $this->assertTrue($this->address->validateMinimumAmount());
  174. }
  175. public function testValidateMinimumAmountVirtual()
  176. {
  177. $storeId = 1;
  178. $scopeConfigValues = [
  179. ['sales/minimum_order/active', ScopeInterface::SCOPE_STORE, $storeId, true],
  180. ['sales/minimum_order/amount', ScopeInterface::SCOPE_STORE, $storeId, 20],
  181. ['sales/minimum_order/include_discount_amount', ScopeInterface::SCOPE_STORE, $storeId, true],
  182. ['sales/minimum_order/tax_including', ScopeInterface::SCOPE_STORE, $storeId, true],
  183. ];
  184. $this->quote->expects($this->once())
  185. ->method('getStoreId')
  186. ->willReturn($storeId);
  187. $this->quote->expects($this->once())
  188. ->method('getIsVirtual')
  189. ->willReturn(true);
  190. $this->address->setAddressType(Address::TYPE_SHIPPING);
  191. $this->scopeConfig->expects($this->once())
  192. ->method('isSetFlag')
  193. ->willReturnMap($scopeConfigValues);
  194. $this->assertTrue($this->address->validateMinimumAmount());
  195. }
  196. public function testValidateMinimumAmount()
  197. {
  198. $storeId = 1;
  199. $scopeConfigValues = [
  200. ['sales/minimum_order/active', ScopeInterface::SCOPE_STORE, $storeId, true],
  201. ['sales/minimum_order/amount', ScopeInterface::SCOPE_STORE, $storeId, 20],
  202. ['sales/minimum_order/include_discount_amount', ScopeInterface::SCOPE_STORE, $storeId, true],
  203. ['sales/minimum_order/tax_including', ScopeInterface::SCOPE_STORE, $storeId, true],
  204. ];
  205. $this->quote->expects($this->once())
  206. ->method('getStoreId')
  207. ->willReturn($storeId);
  208. $this->quote->expects($this->once())
  209. ->method('getIsVirtual')
  210. ->willReturn(false);
  211. $this->scopeConfig->expects($this->once())
  212. ->method('isSetFlag')
  213. ->willReturnMap($scopeConfigValues);
  214. $this->assertTrue($this->address->validateMinimumAmount());
  215. }
  216. public function testValidateMiniumumAmountWithoutDiscount()
  217. {
  218. $storeId = 1;
  219. $scopeConfigValues = [
  220. ['sales/minimum_order/active', ScopeInterface::SCOPE_STORE, $storeId, true],
  221. ['sales/minimum_order/amount', ScopeInterface::SCOPE_STORE, $storeId, 20],
  222. ['sales/minimum_order/include_discount_amount', ScopeInterface::SCOPE_STORE, $storeId, false],
  223. ['sales/minimum_order/tax_including', ScopeInterface::SCOPE_STORE, $storeId, true],
  224. ];
  225. $this->quote->expects($this->once())
  226. ->method('getStoreId')
  227. ->willReturn($storeId);
  228. $this->quote->expects($this->once())
  229. ->method('getIsVirtual')
  230. ->willReturn(false);
  231. $this->scopeConfig->expects($this->once())
  232. ->method('isSetFlag')
  233. ->willReturnMap($scopeConfigValues);
  234. $this->assertTrue($this->address->validateMinimumAmount());
  235. }
  236. public function testValidateMinimumAmountNegative()
  237. {
  238. $storeId = 1;
  239. $scopeConfigValues = [
  240. ['sales/minimum_order/active', ScopeInterface::SCOPE_STORE, $storeId, true],
  241. ['sales/minimum_order/amount', ScopeInterface::SCOPE_STORE, $storeId, 20],
  242. ['sales/minimum_order/include_discount_amount', ScopeInterface::SCOPE_STORE, $storeId, true],
  243. ['sales/minimum_order/tax_including', ScopeInterface::SCOPE_STORE, $storeId, true],
  244. ];
  245. $this->quote->expects($this->once())
  246. ->method('getStoreId')
  247. ->willReturn($storeId);
  248. $this->quote->expects($this->once())
  249. ->method('getIsVirtual')
  250. ->willReturn(false);
  251. $this->address->setAddressType(Address::TYPE_SHIPPING);
  252. $this->scopeConfig->expects($this->once())
  253. ->method('isSetFlag')
  254. ->willReturnMap($scopeConfigValues);
  255. $this->assertTrue($this->address->validateMinimumAmount());
  256. }
  257. public function testSetAndGetAppliedTaxes()
  258. {
  259. $data = ['data'];
  260. self::assertInstanceOf(Address::class, $this->address->setAppliedTaxes($data));
  261. self::assertEquals($data, $this->address->getAppliedTaxes());
  262. }
  263. /**
  264. * Checks a case, when applied taxes are not provided.
  265. */
  266. public function testGetAppliedTaxesWithEmptyValue()
  267. {
  268. $this->address->setData('applied_taxes', null);
  269. self::assertEquals([], $this->address->getAppliedTaxes());
  270. }
  271. /**
  272. * Test of requesting shipping rates by address
  273. *
  274. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  275. */
  276. public function testRequestShippingRates()
  277. {
  278. $storeId = 12345;
  279. $webSiteId = 6789;
  280. $baseCurrency = $this->getMockBuilder(Currency::class)
  281. ->disableOriginalConstructor()
  282. ->setMethods(['getCurrentCurrencyCode','convert'])
  283. ->getMockForAbstractClass();
  284. $currentCurrency = $this->getMockBuilder(Currency::class)
  285. ->disableOriginalConstructor()
  286. ->setMethods(['getCurrentCurrencyCode','convert'])
  287. ->getMockForAbstractClass();
  288. $currentCurrencyCode = 'UAH';
  289. /** @var RateRequest */
  290. $request = $this->getMockBuilder(RateRequest::class)
  291. ->disableOriginalConstructor()
  292. ->setMethods(['setStoreId', 'setWebsiteId', 'setBaseCurrency', 'setPackageCurrency'])
  293. ->getMock();
  294. /** @var Collection */
  295. $collection = $this->getMockBuilder(Collection::class)
  296. ->disableOriginalConstructor()
  297. ->getMock();
  298. $collection->expects($this->once())
  299. ->method('setAddressFilter')
  300. ->willReturnSelf();
  301. /** @var RatesCollection */
  302. $ratesCollection = $this->getMockBuilder(RatesCollection::class)
  303. ->disableOriginalConstructor()
  304. ->getMock();
  305. $ratesCollection->expects($this->once())
  306. ->method('setAddressFilter')
  307. ->willReturnSelf();
  308. /** @var Result */
  309. $rates = $this->getMockBuilder(Result::class)
  310. ->disableOriginalConstructor()
  311. ->getMock();
  312. /** @var AbstractResult */
  313. $rateItem = $this->getMockBuilder(AbstractResult::class)
  314. ->disableOriginalConstructor()
  315. ->getMockForAbstractClass();
  316. /** @var Rate */
  317. $rate = $this->getMockBuilder(Rate::class)
  318. ->disableOriginalConstructor()
  319. ->getMock();
  320. $rate->expects($this->once())
  321. ->method('importShippingRate')
  322. ->willReturnSelf();
  323. $rates->expects($this->once())
  324. ->method('getAllRates')
  325. ->willReturn([$rateItem]);
  326. $this->requestFactory->expects($this->once())
  327. ->method('create')
  328. ->willReturn($request);
  329. $this->rateCollectionFactory->expects($this->once())
  330. ->method('create')
  331. ->willReturn($ratesCollection);
  332. $this->rateCollector->expects($this->once())
  333. ->method('create')
  334. ->willReturn($this->rateCollection);
  335. $this->rateCollection->expects($this->once())
  336. ->method('collectRates')
  337. ->willReturnSelf();
  338. $this->rateCollection->expects($this->once())
  339. ->method('getResult')
  340. ->willReturn($rates);
  341. $this->itemCollectionFactory->expects($this->once())
  342. ->method('create')
  343. ->willReturn($collection);
  344. $this->regionFactory->expects($this->once())
  345. ->method('create')
  346. ->willReturn($this->region);
  347. $this->region->expects($this->once())
  348. ->method('loadByCode')
  349. ->willReturnSelf();
  350. $this->storeManager->method('getStore')
  351. ->willReturn($this->store);
  352. $this->storeManager->expects($this->once())
  353. ->method('getWebsite')
  354. ->willReturn($this->website);
  355. $this->store->method('getId')
  356. ->willReturn($storeId);
  357. $this->store->method('getBaseCurrency')
  358. ->willReturn($baseCurrency);
  359. $this->store->expects($this->once())
  360. ->method('getCurrentCurrency')
  361. ->willReturn($currentCurrency);
  362. $this->store->expects($this->once())
  363. ->method('getCurrentCurrencyCode')
  364. ->willReturn($currentCurrencyCode);
  365. $this->website->expects($this->once())
  366. ->method('getId')
  367. ->willReturn($webSiteId);
  368. $this->addressRateFactory->expects($this->once())
  369. ->method('create')
  370. ->willReturn($rate);
  371. $request->expects($this->once())
  372. ->method('setStoreId')
  373. ->with($storeId);
  374. $request->expects($this->once())
  375. ->method('setWebsiteId')
  376. ->with($webSiteId);
  377. $request->expects($this->once())
  378. ->method('setBaseCurrency')
  379. ->with($baseCurrency);
  380. $request->expects($this->once())
  381. ->method('setPackageCurrency')
  382. ->with($currentCurrency);
  383. $baseCurrency->expects($this->once())
  384. ->method('convert')
  385. ->with(null, $currentCurrencyCode);
  386. $this->address->requestShippingRates();
  387. }
  388. }