TaxRateManagementTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. class TaxRateManagementTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Tax\Api\TaxRuleRepositoryInterface
  12. */
  13. private $taxRuleRepository;
  14. /**
  15. * @var TaxRuleFixtureFactory
  16. */
  17. private $taxRuleFixtureFactory;
  18. /**
  19. * Array of default tax classes ids
  20. *
  21. * Key is class name
  22. *
  23. * @var int[]
  24. */
  25. private $taxClasses;
  26. /**
  27. * Array of default tax rates ids.
  28. *
  29. * Key is rate percentage as string.
  30. *
  31. * @var int[]
  32. */
  33. private $taxRates;
  34. /**
  35. * Array of default tax rules ids.
  36. *
  37. * Key is rule code.
  38. *
  39. * @var int[]
  40. */
  41. private $taxRules;
  42. /**
  43. * @var \Magento\Tax\Api\TaxRateRepositoryInterface
  44. */
  45. private $taxRateRepository;
  46. /**
  47. * @var \Magento\Tax\Api\TaxRateManagementInterface
  48. */
  49. private $taxRateManagement;
  50. protected function setUp()
  51. {
  52. $objectManager = Bootstrap::getObjectManager();
  53. $this->taxRuleRepository = $objectManager->get(\Magento\Tax\Api\TaxRuleRepositoryInterface::class);
  54. $this->taxRateManagement = $objectManager->get(\Magento\Tax\Api\TaxRateManagementInterface::class);
  55. $this->taxRateRepository = $objectManager->get(\Magento\Tax\Api\TaxRateRepositoryInterface::class);
  56. $this->taxRuleFixtureFactory = new TaxRuleFixtureFactory();
  57. }
  58. /**
  59. * @magentoDbIsolation enabled
  60. */
  61. public function testGetRatesByCustomerAndProductTaxClassId()
  62. {
  63. $this->setUpDefaultRules();
  64. $taxRateIds = $this->taxRuleRepository->get(current($this->taxRules))->getTaxRateIds();
  65. $expectedRates = [];
  66. foreach ($taxRateIds as $rateId) {
  67. $expectedRates[] = $this->taxRateRepository->get($rateId);
  68. }
  69. $rates = $this->taxRateManagement->getRatesByCustomerAndProductTaxClassId(
  70. $this->taxClasses['DefaultCustomerClass'],
  71. $this->taxClasses['DefaultProductClass']
  72. );
  73. $this->assertCount(2, $rates);
  74. $this->assertEquals($expectedRates, $rates);
  75. }
  76. private function setUpDefaultRules()
  77. {
  78. $this->taxRates = $this->taxRuleFixtureFactory->createTaxRates([
  79. ['percentage' => 7.5, 'country' => 'US', 'region' => 42],
  80. ['percentage' => 7.5, 'country' => 'US', 'region' => 12], // Default store rate
  81. ]);
  82. $this->taxClasses = $this->taxRuleFixtureFactory->createTaxClasses([
  83. ['name' => 'DefaultCustomerClass', 'type' => ClassModel::TAX_CLASS_TYPE_CUSTOMER],
  84. ['name' => 'DefaultProductClass', 'type' => ClassModel::TAX_CLASS_TYPE_PRODUCT],
  85. ['name' => 'HigherProductClass', 'type' => ClassModel::TAX_CLASS_TYPE_PRODUCT],
  86. ]);
  87. $this->taxRules = $this->taxRuleFixtureFactory->createTaxRules([
  88. [
  89. 'code' => 'Default Rule',
  90. 'customer_tax_class_ids' => [$this->taxClasses['DefaultCustomerClass'], 3],
  91. 'product_tax_class_ids' => [$this->taxClasses['DefaultProductClass']],
  92. 'tax_rate_ids' => array_values($this->taxRates),
  93. 'sort_order' => 0,
  94. 'priority' => 0,
  95. 'calculate_subtotal' => 1,
  96. ],
  97. ]);
  98. }
  99. }