TaxRuleFixtureFactory.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Model;
  7. use Magento\TestFramework\Helper\Bootstrap;
  8. /**
  9. * TaxRuleFixtureFactory is meant to help in testing tax by creating/destroying tax rules/classes/rates easily.
  10. */
  11. class TaxRuleFixtureFactory
  12. {
  13. /**
  14. * Object Manager
  15. *
  16. * @var \Magento\Framework\ObjectManagerInterface
  17. */
  18. private $objectManager;
  19. /**
  20. * @var \Magento\Framework\Api\DataObjectHelper
  21. */
  22. private $dataObjectHelper;
  23. public function __construct()
  24. {
  25. $this->objectManager = Bootstrap::getObjectManager();
  26. $this->dataObjectHelper = $this->objectManager->create(\Magento\Framework\Api\DataObjectHelper::class);
  27. }
  28. /**
  29. * Helper to create tax rules.
  30. *
  31. * @param array $rulesData Keys match populateWithArray
  32. * @return array code => rule id
  33. */
  34. public function createTaxRules($rulesData)
  35. {
  36. /** @var \Magento\Tax\Api\Data\TaxRuleInterfaceFactory $taxRuleFactory */
  37. $taxRuleFactory = $this->objectManager->create(\Magento\Tax\Api\Data\TaxRuleInterfaceFactory::class);
  38. /** @var \Magento\Tax\Api\TaxRuleRepositoryInterface $taxRuleService */
  39. $taxRuleService = $this->objectManager->create(\Magento\Tax\Api\TaxRuleRepositoryInterface::class);
  40. $rules = [];
  41. foreach ($rulesData as $ruleData) {
  42. $taxRule = $taxRuleFactory->create();
  43. $this->dataObjectHelper->populateWithArray(
  44. $taxRule,
  45. $ruleData,
  46. \Magento\Tax\Api\Data\TaxRuleInterface::class
  47. );
  48. $rules[$ruleData['code']] = $taxRuleService->save($taxRule)->getId();
  49. }
  50. return $rules;
  51. }
  52. /**
  53. * Helper function that deletes tax rules
  54. *
  55. * @param int[] $ruleIds
  56. */
  57. public function deleteTaxRules($ruleIds)
  58. {
  59. /** @var \Magento\Tax\Api\TaxRuleRepositoryInterface $taxRuleService */
  60. $taxRuleService = $this->objectManager->create(\Magento\Tax\Api\TaxRuleRepositoryInterface::class);
  61. foreach ($ruleIds as $ruleId) {
  62. $taxRuleService->deleteById($ruleId);
  63. }
  64. }
  65. /**
  66. * Helper function that creates rates based on a set of input percentages.
  67. *
  68. * Returns a map of percentage => rate
  69. *
  70. * @param array $ratesData array of rate data, keys are 'country', 'region' and 'percentage'
  71. * @return int[] Tax Rate Id
  72. */
  73. public function createTaxRates($ratesData)
  74. {
  75. /** @var \Magento\Tax\Api\Data\TaxRateInterfaceFactory $taxRateFactory */
  76. $taxRateFactory = $this->objectManager->create(\Magento\Tax\Api\Data\TaxRateInterfaceFactory::class);
  77. /** @var \Magento\Tax\Api\TaxRateRepositoryInterface $taxRateService */
  78. $taxRateService = $this->objectManager->create(\Magento\Tax\Api\TaxRateRepositoryInterface::class);
  79. $rates = [];
  80. foreach ($ratesData as $rateData) {
  81. $code = "{$rateData['country']} - {$rateData['region']} - {$rateData['percentage']}";
  82. $postcode = '*';
  83. if (isset($rateData['postcode'])) {
  84. $postcode = $rateData['postcode'];
  85. $code = $code . " - " . $postcode;
  86. }
  87. $taxRate = $taxRateFactory->create();
  88. $taxRate->setTaxCountryId($rateData['country'])
  89. ->setTaxRegionId($rateData['region'])
  90. ->setTaxPostcode($postcode)
  91. ->setCode($code)
  92. ->setRate($rateData['percentage']);
  93. $rates[$code] = $taxRateService->save($taxRate)->getId();
  94. }
  95. return $rates;
  96. }
  97. /**
  98. * Helper function that deletes tax rates
  99. *
  100. * @param int[] $rateIds
  101. */
  102. public function deleteTaxRates($rateIds)
  103. {
  104. /** @var \Magento\Tax\Api\TaxRateRepositoryInterface $taxRateService */
  105. $taxRateService = $this->objectManager->create(\Magento\Tax\Api\TaxRateRepositoryInterface::class);
  106. foreach ($rateIds as $rateId) {
  107. $taxRateService->deleteById($rateId);
  108. }
  109. }
  110. /**
  111. * Helper function that creates tax classes based on input.
  112. *
  113. * @param array $classesData Keys include 'name' and 'type'
  114. * @return array ClassName => ClassId
  115. */
  116. public function createTaxClasses($classesData)
  117. {
  118. $classes = [];
  119. foreach ($classesData as $classData) {
  120. /** @var \Magento\Tax\Model\ClassModel $class */
  121. $class = $this->objectManager->create(\Magento\Tax\Model\ClassModel::class)
  122. ->setClassName($classData['name'])
  123. ->setClassType($classData['type'])
  124. ->save();
  125. $classes[$classData['name']] = $class->getId();
  126. }
  127. return $classes;
  128. }
  129. /**
  130. * Helper function that deletes tax classes
  131. *
  132. * @param int[] $classIds
  133. */
  134. public function deleteTaxClasses($classIds)
  135. {
  136. /** @var \Magento\Tax\Model\ClassModel $class */
  137. $class = $this->objectManager->create(\Magento\Tax\Model\ClassModel::class);
  138. foreach ($classIds as $classId) {
  139. $class->load($classId);
  140. $class->delete();
  141. }
  142. }
  143. }