PageRenderTime 44ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Magento/Weee/Test/Unit/Model/TaxTest.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 281 lines | 168 code | 47 blank | 66 comment | 0 complexity | 58916fce984f06564ae78f1c47a1a339 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Weee\Test\Unit\Model;
  7. use Magento\Weee\Model\Tax;
  8. /**
  9. * Class TaxTest
  10. *
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class TaxTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @var \Magento\Weee\Model\Tax
  17. */
  18. protected $model;
  19. /**
  20. * @var \PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $context;
  23. /**
  24. * @var \PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $registry;
  27. /**
  28. * @var \PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $attributeFactory;
  31. /**
  32. * @var \PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $storeManager;
  35. /**
  36. * @var \PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $calculationFactory;
  39. /**
  40. * @var \PHPUnit_Framework_MockObject_MockObject
  41. */
  42. protected $customerSession;
  43. /**
  44. * @var \PHPUnit_Framework_MockObject_MockObject
  45. */
  46. protected $accountManagement;
  47. /**
  48. * @var \PHPUnit_Framework_MockObject_MockObject
  49. */
  50. protected $taxData;
  51. /**
  52. * @var \PHPUnit_Framework_MockObject_MockObject
  53. */
  54. protected $resource;
  55. /**
  56. * @var \PHPUnit_Framework_MockObject_MockObject
  57. */
  58. protected $weeeConfig;
  59. /**
  60. * @var \PHPUnit_Framework_MockObject_MockObject
  61. */
  62. protected $priceCurrency;
  63. /**
  64. * @var \PHPUnit_Framework_MockObject_MockObject
  65. */
  66. protected $resourceCollection;
  67. /**
  68. * @var \PHPUnit_Framework_MockObject_MockObject
  69. */
  70. protected $data;
  71. /**
  72. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  73. */
  74. protected $objectManager;
  75. /**
  76. * Setup the test
  77. */
  78. protected function setUp()
  79. {
  80. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  81. $className = \Magento\Framework\Model\Context::class;
  82. $this->context = $this->getMock($className, [], [], '', false);
  83. $className = \Magento\Framework\Registry::class;
  84. $this->registry = $this->getMock($className, [], [], '', false);
  85. $className = \Magento\Eav\Model\Entity\AttributeFactory::class;
  86. $this->attributeFactory = $this->getMock($className, ['create'], [], '', false);
  87. $className = \Magento\Store\Model\StoreManagerInterface::class;
  88. $this->storeManager = $this->getMock($className, [], [], '', false);
  89. $className = \Magento\Tax\Model\CalculationFactory::class;
  90. $this->calculationFactory = $this->getMock($className, ['create'], [], '', false);
  91. $className = \Magento\Customer\Model\Session::class;
  92. $this->customerSession = $this->getMock(
  93. $className,
  94. ['getCustomerId', 'getDefaultTaxShippingAddress', 'getDefaultTaxBillingAddress', 'getCustomerTaxClassId'],
  95. [],
  96. '',
  97. false
  98. );
  99. $this->customerSession->expects($this->any())->method('getCustomerId')->willReturn(null);
  100. $this->customerSession->expects($this->any())->method('getDefaultTaxShippingAddress')->willReturn(null);
  101. $this->customerSession->expects($this->any())->method('getDefaultTaxBillingAddress')->willReturn(null);
  102. $this->customerSession->expects($this->any())->method('getCustomerTaxClassId')->willReturn(null);
  103. $className = \Magento\Customer\Api\AccountManagementInterface::class;
  104. $this->accountManagement = $this->getMock($className, [], [], '', false);
  105. $className = \Magento\Tax\Helper\Data::class;
  106. $this->taxData = $this->getMock($className, [], [], '', false);
  107. $className = \Magento\Weee\Model\ResourceModel\Tax::class;
  108. $this->resource = $this->getMock($className, [], [], '', false);
  109. $className = \Magento\Weee\Model\Config::class;
  110. $this->weeeConfig = $this->getMock($className, [], [], '', false);
  111. $className = \Magento\Framework\Pricing\PriceCurrencyInterface::class;
  112. $this->priceCurrency = $this->getMock($className, [], [], '', false);
  113. $className = \Magento\Framework\Data\Collection\AbstractDb::class;
  114. $this->resourceCollection = $this->getMock($className, [], [], '', false);
  115. $this->model = $this->objectManager->getObject(
  116. \Magento\Weee\Model\Tax::class,
  117. [
  118. 'context' => $this->context,
  119. 'registry' => $this->registry,
  120. 'attributeFactory' => $this->attributeFactory,
  121. 'storeManager' => $this->storeManager,
  122. 'calculationFactory' => $this->calculationFactory,
  123. 'customerSession' => $this->customerSession,
  124. 'accountManagement' => $this->accountManagement,
  125. 'taxData' => $this->taxData,
  126. 'resource' => $this->resource,
  127. 'weeeConfig' => $this->weeeConfig,
  128. 'priceCurrency' => $this->priceCurrency,
  129. 'resourceCollection' => $this->resourceCollection,
  130. ]
  131. );
  132. }
  133. /**
  134. * test GetProductWeeeAttributes
  135. * @dataProvider getProductWeeeAttributesDataProvider
  136. * @param array $weeeTaxCalculationsByEntity
  137. * @param array $expectedFptLabel
  138. */
  139. public function testGetProductWeeeAttributes($weeeTaxCalculationsByEntity, $expectedFptLabel)
  140. {
  141. $product = $this->getMock(\Magento\Catalog\Model\Product::class, [], [], '', false);
  142. $website = $this->getMock(\Magento\Store\Model\Website::class, [], [], '', false);
  143. $store = $this->getMock(\Magento\Store\Model\Store::class, [], [], '', false);
  144. $group = $this->getMock(\Magento\Store\Model\Group::class, [], [], '', false);
  145. $attribute = $this->getMock(\Magento\Eav\Model\Entity\Attribute::class, [], [], '', false);
  146. $calculation = $this->getMock(\Magento\Tax\Model\Calculation::class, [], [], '', false);
  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. $store->expects($this->any())
  161. ->method('getId')
  162. ->willReturn(1);
  163. $product->expects($this->any())
  164. ->method('getId')
  165. ->willReturn(1);
  166. $website->expects($this->any())
  167. ->method('getId')
  168. ->willReturn(1);
  169. $website->expects($this->any())
  170. ->method('getDefaultGroup')
  171. ->willReturn($group);
  172. $group->expects($this->any())
  173. ->method('getDefaultStore')
  174. ->willReturn($store);
  175. $this->storeManager->expects($this->any())
  176. ->method('getWebsite')
  177. ->willReturn($website);
  178. $this->weeeConfig->expects($this->any())
  179. ->method('isEnabled')
  180. ->willReturn(true);
  181. $this->weeeConfig->expects($this->any())
  182. ->method('isTaxable')
  183. ->willReturn(true);
  184. $this->attributeFactory->expects($this->any())
  185. ->method('create')
  186. ->willReturn($attribute);
  187. $this->calculationFactory->expects($this->any())
  188. ->method('create')
  189. ->willReturn($calculation);
  190. $this->priceCurrency->expects($this->any())
  191. ->method('round')
  192. ->with(0.1)
  193. ->willReturn(0.25);
  194. $this->resource->expects($this->any())
  195. ->method('fetchWeeeTaxCalculationsByEntity')
  196. ->willReturn([
  197. 0 => $weeeTaxCalculationsByEntity
  198. ]);
  199. $result = $this->model->getProductWeeeAttributes($product, null, null, null, true);
  200. $this->assertTrue(is_array($result));
  201. $this->assertArrayHasKey(0, $result);
  202. $obj = $result[0];
  203. $this->assertEquals(1, $obj->getAmount());
  204. $this->assertEquals(0.25, $obj->getTaxAmount());
  205. $this->assertEquals($weeeTaxCalculationsByEntity['attribute_code'], $obj->getCode());
  206. $this->assertEquals(__($expectedFptLabel), $obj->getName());
  207. }
  208. /**
  209. * @return array
  210. */
  211. public function getProductWeeeAttributesDataProvider()
  212. {
  213. return [
  214. 'store_label_defined' => [
  215. 'weeeTaxCalculationsByEntity' => [
  216. 'weee_value' => 1,
  217. 'label_value' => 'fpt_label',
  218. 'frontend_label' => 'fpt_label_frontend',
  219. 'attribute_code' => 'fpt_code',
  220. ],
  221. 'expectedFptLabel' => 'fpt_label'
  222. ],
  223. 'store_label_not_defined' => [
  224. 'weeeTaxCalculationsByEntity' => [
  225. 'weee_value' => 1,
  226. 'label_value' => '',
  227. 'frontend_label' => 'fpt_label_frontend',
  228. 'attribute_code' => 'fpt_code',
  229. ],
  230. 'expectedFptLabel' => 'fpt_label_frontend'
  231. ]
  232. ];
  233. }
  234. }