RuleTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Controller\Adminhtml;
  7. use Magento\Directory\Model\CountryFactory;
  8. use Magento\Directory\Model\RegionFactory;
  9. use Magento\Framework\Api\DataObjectHelper;
  10. use Magento\Framework\Json\Helper\Data;
  11. use Magento\Tax\Api\Data\TaxRateInterfaceFactory;
  12. use Magento\Tax\Api\TaxRateRepositoryInterface;
  13. use Magento\TestFramework\Helper\Bootstrap;
  14. use Magento\Tax\Api\Data\TaxRateInterface;
  15. use Magento\Tax\Model\TaxRuleFixtureFactory;
  16. use Magento\Tax\Model\Rate\Provider as RatesProvider;
  17. /**
  18. * Tests for Tax Rules controllers.
  19. *
  20. * @magentoAppArea adminhtml
  21. */
  22. class RuleTest extends \Magento\TestFramework\TestCase\AbstractBackendController
  23. {
  24. /**
  25. * TaxRate factory
  26. *
  27. * @var TaxRateInterfaceFactory
  28. */
  29. private $taxRateFactory;
  30. /**
  31. * TaxRateService
  32. *
  33. * @var TaxRateRepositoryInterface
  34. */
  35. private $rateRepository;
  36. /**
  37. * Helps in creating required tax rules.
  38. *
  39. * @var TaxRuleFixtureFactory
  40. */
  41. private $taxRateFixtureFactory;
  42. /**
  43. * @var CountryFactory
  44. */
  45. private $countryFactory;
  46. /**
  47. * @var RegionFactory
  48. */
  49. private $regionFactory;
  50. /**
  51. * @var DataObjectHelper
  52. */
  53. private $dataObjectHelper;
  54. /**
  55. * @var RatesProvider
  56. */
  57. private $taxRatesProvider;
  58. /**
  59. * @inheritdoc
  60. */
  61. protected function setUp()
  62. {
  63. parent::setUp();
  64. $this->rateRepository = $this->_objectManager->get(TaxRateRepositoryInterface::class);
  65. $this->taxRateFactory = $this->_objectManager->create(TaxRateInterfaceFactory::class);
  66. $this->dataObjectHelper = $this->_objectManager->create(DataObjectHelper::class);
  67. $this->taxRateFixtureFactory = new TaxRuleFixtureFactory();
  68. $this->countryFactory = $this->_objectManager->create(CountryFactory::class);
  69. $this->regionFactory = $this->_objectManager->create(RegionFactory::class);
  70. $this->taxRatesProvider = $this->_objectManager->create(RatesProvider::class);
  71. $this->_generateTaxRates();
  72. }
  73. /**
  74. * Tests request of tax rates collection set.
  75. *
  76. * @param array $postData
  77. * @param int $itemsCount
  78. * @dataProvider ajaxActionDataProvider
  79. * @magentoDbIsolation enabled
  80. */
  81. public function testAjaxLoadRates($postData, $itemsCount)
  82. {
  83. $this->getRequest()->setPostValue($postData);
  84. $this->dispatch('backend/tax/rule/ajaxLoadRates');
  85. $jsonBody = $this->getResponse()->getBody();
  86. $response = Bootstrap::getObjectManager()->get(Data::class)
  87. ->jsonDecode($jsonBody);
  88. $this->assertArrayHasKey('success', $response);
  89. $this->assertTrue($response['success']);
  90. $this->assertArrayHasKey('errorMessage', $response);
  91. $this->assertEmpty($response['errorMessage']);
  92. $this->assertArrayHasKey('result', $response);
  93. $this->assertCount($itemsCount, $response['result']);
  94. }
  95. /**
  96. * Creates tax rates items in repository.
  97. */
  98. private function _generateTaxRates()
  99. {
  100. $ratesCount = $this->taxRatesProvider->getPageSize() + 1;
  101. for ($i = 0; $i <= $ratesCount; $i++) {
  102. $taxData = [
  103. 'tax_country_id' => 'US',
  104. 'tax_region_id' => '8',
  105. 'rate' => '8.25',
  106. 'code' => 'US-CA-*-Rate' . $i . rand(),
  107. 'zip_is_range' => true,
  108. 'zip_from' => 78765,
  109. 'zip_to' => 78780,
  110. ];
  111. // Tax rate data object created
  112. $taxRate = $this->taxRateFactory->create();
  113. $this->dataObjectHelper->populateWithArray($taxRate, $taxData, TaxRateInterface::class);
  114. //Tax rate service call
  115. $this->rateRepository->save($taxRate);
  116. }
  117. }
  118. /**
  119. * Provides POST data
  120. *
  121. * @return array
  122. */
  123. public function ajaxActionDataProvider()
  124. {
  125. $taxRatesProvider = Bootstrap::getObjectManager()->create(RatesProvider::class);
  126. return [
  127. [['p' => 1], $taxRatesProvider->getPageSize()],
  128. [['p' => 1, 's' => 'no_such_code'], 0]
  129. ];
  130. }
  131. }