TaxTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Weee\Test\Unit\Model;
  7. /**
  8. * Class TaxTest
  9. *
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class TaxTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Weee\Model\Tax
  16. */
  17. protected $model;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $context;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $registry;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $attributeFactory;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $storeManager;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $calculationFactory;
  38. /**
  39. * @var \PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $customerSession;
  42. /**
  43. * @var \PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $accountManagement;
  46. /**
  47. * @var \PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected $taxData;
  50. /**
  51. * @var \PHPUnit_Framework_MockObject_MockObject
  52. */
  53. protected $resource;
  54. /**
  55. * @var \PHPUnit_Framework_MockObject_MockObject
  56. */
  57. protected $weeeConfig;
  58. /**
  59. * @var \PHPUnit_Framework_MockObject_MockObject
  60. */
  61. protected $priceCurrency;
  62. /**
  63. * @var \PHPUnit_Framework_MockObject_MockObject
  64. */
  65. protected $resourceCollection;
  66. /**
  67. * @var \PHPUnit_Framework_MockObject_MockObject
  68. */
  69. protected $data;
  70. /**
  71. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  72. */
  73. protected $objectManager;
  74. /**
  75. * Setup the test
  76. */
  77. protected function setUp()
  78. {
  79. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  80. $className = \Magento\Framework\Model\Context::class;
  81. $this->context = $this->createMock($className);
  82. $className = \Magento\Framework\Registry::class;
  83. $this->registry = $this->createMock($className);
  84. $className = \Magento\Eav\Model\Entity\AttributeFactory::class;
  85. $this->attributeFactory = $this->createPartialMock($className, ['create']);
  86. $className = \Magento\Store\Model\StoreManagerInterface::class;
  87. $this->storeManager = $this->createMock($className);
  88. $className = \Magento\Tax\Model\CalculationFactory::class;
  89. $this->calculationFactory = $this->createPartialMock($className, ['create']);
  90. $className = \Magento\Customer\Model\Session::class;
  91. $this->customerSession = $this->createPartialMock(
  92. $className,
  93. ['getCustomerId', 'getDefaultTaxShippingAddress', 'getDefaultTaxBillingAddress', 'getCustomerTaxClassId']
  94. );
  95. $this->customerSession->expects($this->any())->method('getCustomerId')->willReturn(null);
  96. $this->customerSession->expects($this->any())->method('getDefaultTaxShippingAddress')->willReturn(null);
  97. $this->customerSession->expects($this->any())->method('getDefaultTaxBillingAddress')->willReturn(null);
  98. $this->customerSession->expects($this->any())->method('getCustomerTaxClassId')->willReturn(null);
  99. $className = \Magento\Customer\Api\AccountManagementInterface::class;
  100. $this->accountManagement = $this->createMock($className);
  101. $className = \Magento\Tax\Helper\Data::class;
  102. $this->taxData = $this->createMock($className);
  103. $className = \Magento\Weee\Model\ResourceModel\Tax::class;
  104. $this->resource = $this->createMock($className);
  105. $className = \Magento\Weee\Model\Config::class;
  106. $this->weeeConfig = $this->createMock($className);
  107. $className = \Magento\Framework\Pricing\PriceCurrencyInterface::class;
  108. $this->priceCurrency = $this->createMock($className);
  109. $className = \Magento\Framework\Data\Collection\AbstractDb::class;
  110. $this->resourceCollection = $this->createMock($className);
  111. $this->model = $this->objectManager->getObject(
  112. \Magento\Weee\Model\Tax::class,
  113. [
  114. 'context' => $this->context,
  115. 'registry' => $this->registry,
  116. 'attributeFactory' => $this->attributeFactory,
  117. 'storeManager' => $this->storeManager,
  118. 'calculationFactory' => $this->calculationFactory,
  119. 'customerSession' => $this->customerSession,
  120. 'accountManagement' => $this->accountManagement,
  121. 'taxData' => $this->taxData,
  122. 'resource' => $this->resource,
  123. 'weeeConfig' => $this->weeeConfig,
  124. 'priceCurrency' => $this->priceCurrency,
  125. 'resourceCollection' => $this->resourceCollection,
  126. ]
  127. );
  128. }
  129. /**
  130. * @dataProvider getProductWeeeAttributesDataProvider
  131. * @param array $weeeTaxCalculationsByEntity
  132. * @param mixed $websitePassed
  133. * @param string $expectedFptLabel
  134. * @return void
  135. */
  136. public function testGetProductWeeeAttributes(
  137. array $weeeTaxCalculationsByEntity,
  138. $websitePassed,
  139. string $expectedFptLabel
  140. ): void {
  141. $product = $this->createMock(\Magento\Catalog\Model\Product::class);
  142. $website = $this->createMock(\Magento\Store\Model\Website::class);
  143. $store = $this->createMock(\Magento\Store\Model\Store::class);
  144. $group = $this->createMock(\Magento\Store\Model\Group::class);
  145. $attribute = $this->createMock(\Magento\Eav\Model\Entity\Attribute::class);
  146. $calculation = $this->createMock(\Magento\Tax\Model\Calculation::class);
  147. $obj = new \Magento\Framework\DataObject(['country' => 'US', 'region' => 'TX']);
  148. $calculation->expects($this->once())
  149. ->method('getRateRequest')
  150. ->willReturn($obj);
  151. $calculation->expects($this->once())
  152. ->method('getDefaultRateRequest')
  153. ->willReturn($obj);
  154. $calculation->expects($this->any())
  155. ->method('getRate')
  156. ->willReturn('10');
  157. $attribute->expects($this->once())
  158. ->method('getAttributeCodesByFrontendType')
  159. ->willReturn(['0'=>'fpt']);
  160. $this->storeManager->expects($this->any())
  161. ->method('getWebsite')
  162. ->willReturn($website);
  163. $website->expects($this->any())
  164. ->method('getId')
  165. ->willReturn($websitePassed);
  166. $website->expects($this->any())
  167. ->method('getDefaultGroup')
  168. ->willReturn($group);
  169. $group->expects($this->any())
  170. ->method('getDefaultStore')
  171. ->willReturn($store);
  172. $store->expects($this->any())
  173. ->method('getId')
  174. ->willReturn(1);
  175. if ($websitePassed) {
  176. $product->expects($this->never())
  177. ->method('getStore')
  178. ->willReturn($store);
  179. } else {
  180. $product->expects($this->once())
  181. ->method('getStore')
  182. ->willReturn($store);
  183. $store->expects($this->once())
  184. ->method('getWebsiteId')
  185. ->willReturn(1);
  186. }
  187. $product->expects($this->any())
  188. ->method('getId')
  189. ->willReturn(1);
  190. $this->weeeConfig->expects($this->any())
  191. ->method('isEnabled')
  192. ->willReturn(true);
  193. $this->weeeConfig->expects($this->any())
  194. ->method('isTaxable')
  195. ->willReturn(true);
  196. $this->attributeFactory->expects($this->any())
  197. ->method('create')
  198. ->willReturn($attribute);
  199. $this->calculationFactory->expects($this->any())
  200. ->method('create')
  201. ->willReturn($calculation);
  202. $this->priceCurrency->expects($this->any())
  203. ->method('round')
  204. ->with(0.1)
  205. ->willReturn(0.25);
  206. $this->resource->expects($this->any())
  207. ->method('fetchWeeeTaxCalculationsByEntity')
  208. ->willReturn([
  209. 0 => $weeeTaxCalculationsByEntity
  210. ]);
  211. $result = $this->model->getProductWeeeAttributes($product, null, null, $websitePassed, true);
  212. $this->assertTrue(is_array($result));
  213. $this->assertArrayHasKey(0, $result);
  214. $obj = $result[0];
  215. $this->assertEquals(1, $obj->getAmount());
  216. $this->assertEquals(0.25, $obj->getTaxAmount());
  217. $this->assertEquals($weeeTaxCalculationsByEntity['attribute_code'], $obj->getCode());
  218. $this->assertEquals(__($expectedFptLabel), $obj->getName());
  219. }
  220. /**
  221. * Test getWeeeAmountExclTax method
  222. *
  223. * @param string $productTypeId
  224. * @param string $productPriceType
  225. * @dataProvider getWeeeAmountExclTaxDataProvider
  226. */
  227. public function testGetWeeeAmountExclTax($productTypeId, $productPriceType)
  228. {
  229. $product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  230. ->disableOriginalConstructor()
  231. ->setMethods(['getTypeId', 'getPriceType'])
  232. ->getMock();
  233. $product->expects($this->any())->method('getTypeId')->willReturn($productTypeId);
  234. $product->expects($this->any())->method('getPriceType')->willReturn($productPriceType);
  235. $weeeDataHelper = $this->getMockBuilder(\Magento\Framework\DataObject::class)
  236. ->disableOriginalConstructor()
  237. ->setMethods(['getAmountExclTax'])
  238. ->getMock();
  239. $weeeDataHelper->expects($this->at(0))->method('getAmountExclTax')->willReturn(10);
  240. $weeeDataHelper->expects($this->at(1))->method('getAmountExclTax')->willReturn(30);
  241. $tax = $this->getMockBuilder(\Magento\Weee\Model\Tax::class)
  242. ->disableOriginalConstructor()
  243. ->setMethods(['getProductWeeeAttributes'])
  244. ->getMock();
  245. $tax->expects($this->once())->method('getProductWeeeAttributes')
  246. ->willReturn([$weeeDataHelper, $weeeDataHelper]);
  247. $this->assertEquals(40, $tax->getWeeeAmountExclTax($product));
  248. }
  249. /**
  250. * Test getWeeeAmountExclTax method for dynamic bundle product
  251. */
  252. public function testGetWeeeAmountExclTaxForDynamicBundleProduct()
  253. {
  254. $product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  255. ->disableOriginalConstructor()
  256. ->setMethods(['getTypeId', 'getPriceType'])
  257. ->getMock();
  258. $product->expects($this->once())->method('getTypeId')->willReturn('bundle');
  259. $product->expects($this->once())->method('getPriceType')->willReturn(0);
  260. $weeeDataHelper = $this->getMockBuilder(\Magento\Framework\DataObject::class)
  261. ->disableOriginalConstructor()
  262. ->getMock();
  263. $tax = $this->getMockBuilder(\Magento\Weee\Model\Tax::class)
  264. ->disableOriginalConstructor()
  265. ->setMethods(['getProductWeeeAttributes'])
  266. ->getMock();
  267. $tax->expects($this->once())->method('getProductWeeeAttributes')->willReturn([$weeeDataHelper]);
  268. $this->assertEquals(0, $tax->getWeeeAmountExclTax($product));
  269. }
  270. /**
  271. * @return array
  272. */
  273. public function getProductWeeeAttributesDataProvider()
  274. {
  275. return [
  276. 'store_label_defined' => [
  277. 'weeeTaxCalculationsByEntity' => [
  278. 'weee_value' => 1,
  279. 'label_value' => 'fpt_label',
  280. 'frontend_label' => 'fpt_label_frontend',
  281. 'attribute_code' => 'fpt_code',
  282. ],
  283. 'websitePassed' => 1,
  284. 'expectedFptLabel' => 'fpt_label',
  285. ],
  286. 'store_label_not_defined' => [
  287. 'weeeTaxCalculationsByEntity' => [
  288. 'weee_value' => 1,
  289. 'label_value' => '',
  290. 'frontend_label' => 'fpt_label_frontend',
  291. 'attribute_code' => 'fpt_code',
  292. ],
  293. 'websitePassed' => 1,
  294. 'expectedFptLabel' => 'fpt_label_frontend',
  295. ],
  296. 'website_not_passed' => [
  297. 'weeeTaxCalculationsByEntity' => [
  298. 'weee_value' => 1,
  299. 'label_value' => '',
  300. 'frontend_label' => 'fpt_label_frontend',
  301. 'attribute_code' => 'fpt_code',
  302. ],
  303. 'websitePassed' => null,
  304. 'expectedFptLabel' => 'fpt_label_frontend',
  305. ],
  306. ];
  307. }
  308. /**
  309. * @return array
  310. */
  311. public function getWeeeAmountExclTaxDataProvider()
  312. {
  313. return [
  314. [
  315. 'bundle', 1
  316. ],
  317. [
  318. 'simple', 0
  319. ],
  320. [
  321. 'simple', 1
  322. ]
  323. ];
  324. }
  325. }