123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Tax\Api;
- use Magento\Framework\Api\FilterBuilder;
- use Magento\Framework\Api\SearchCriteriaBuilder;
- use Magento\Framework\Api\SortOrder;
- use Magento\Framework\Api\SortOrderBuilder;
- use Magento\Tax\Model\Calculation\Rate;
- use Magento\TestFramework\Helper\Bootstrap;
- use Magento\TestFramework\TestCase\WebapiAbstract;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class TaxRateRepositoryTest extends WebapiAbstract
- {
- const SERVICE_NAME = "taxTaxRateRepositoryV1";
- const SERVICE_VERSION = "V1";
- const RESOURCE_PATH = "/V1/taxRates";
- /** @var \Magento\Tax\Model\Calculation\Rate[] */
- private $fixtureTaxRates;
- /** @var \Magento\Tax\Model\ClassModel[] */
- private $fixtureTaxClasses;
- /** @var \Magento\Tax\Model\Calculation\Rule[] */
- private $fixtureTaxRules;
- /**
- * @var \Magento\Tax\Api\TaxRateRepositoryInterface
- */
- private $taxRateService;
- /** @var FilterBuilder */
- private $filterBuilder;
- /** @var SearchCriteriaBuilder */
- private $searchCriteriaBuilder;
- /** @var SortOrderBuilder */
- private $sortOrderBuilder;
- /**
- * Other rates created during tests, to be deleted in tearDown()
- *
- * @var \Magento\Tax\Model\Calculation\Rate[]
- */
- private $otherRates = [];
- /**
- * Execute per test initialization.
- */
- public function setUp()
- {
- $objectManager = Bootstrap::getObjectManager();
- $this->taxRateService = $objectManager->get(\Magento\Tax\Api\TaxRateRepositoryInterface::class);
- $this->searchCriteriaBuilder = $objectManager->create(
- \Magento\Framework\Api\SearchCriteriaBuilder::class
- );
- $this->filterBuilder = $objectManager->create(
- \Magento\Framework\Api\FilterBuilder::class
- );
- $this->sortOrderBuilder = $objectManager->create(
- \Magento\Framework\Api\SortOrderBuilder::class
- );
- /** Initialize tax classes, tax rates and tax rules defined in fixture Magento/Tax/_files/tax_classes.php */
- $this->getFixtureTaxRates();
- $this->getFixtureTaxClasses();
- $this->getFixtureTaxRules();
- }
- public function tearDown()
- {
- $taxRules = $this->getFixtureTaxRules();
- if (count($taxRules)) {
- $taxRates = $this->getFixtureTaxRates();
- $taxClasses = $this->getFixtureTaxClasses();
- foreach ($taxRules as $taxRule) {
- $taxRule->delete();
- }
- foreach ($taxRates as $taxRate) {
- $taxRate->delete();
- }
- foreach ($taxClasses as $taxClass) {
- $taxClass->delete();
- }
- }
- if (count($this->otherRates)) {
- foreach ($this->otherRates as $taxRate) {
- $taxRate->delete();
- }
- }
- }
- public function testCreateTaxRateExistingCode()
- {
- $expectedMessage = '%1 already exists.';
- $data = [
- 'tax_rate' => [
- 'tax_country_id' => 'US',
- 'tax_region_id' => 12,
- 'tax_postcode' => '*',
- 'code' => 'US-CA-*-Rate 1',
- 'rate' => '8.2501',
- ],
- ];
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH,
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'Save',
- ],
- ];
- try {
- $this->_webApiCall($serviceInfo, $data);
- $this->fail('Expected exception was not raised');
- } catch (\SoapFault $e) {
- $this->assertContains(
- $expectedMessage,
- $e->getMessage(),
- 'SoapFault does not contain expected message.'
- );
- } catch (\Exception $e) {
- $errorObj = $this->processRestExceptionResult($e);
- $this->assertEquals($expectedMessage, $errorObj['message']);
- $this->assertEquals(['Code'], $errorObj['parameters']);
- }
- }
- public function testCreateTaxRateWithoutValue()
- {
- $data = [
- 'tax_rate' => [
- 'tax_country_id' => 'US',
- 'tax_region_id' => 12,
- 'tax_postcode' => '*',
- 'code' => 'US-CA-*-Rate 1',
- ],
- ];
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH,
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'Save',
- ],
- ];
- try {
- $this->_webApiCall($serviceInfo, $data);
- $this->fail('Expected exception was not raised');
- } catch (\SoapFault $e) {
- $this->assertContains(
- 'SOAP-ERROR: Encoding: object has no \'rate\' property',
- $e->getMessage(),
- 'SoapFault does not contain expected message.'
- );
- } catch (\Exception $e) {
- $errorObj = $this->processRestExceptionResult($e);
- $this->assertEquals('"%fieldName" is required. Enter and try again.', $errorObj['message']);
- $this->assertEquals(['fieldName' => 'percentage_rate'], $errorObj['parameters']);
- }
- }
- public function testCreateTaxRate()
- {
- $data = [
- 'tax_rate' => [
- 'tax_country_id' => 'US',
- 'tax_region_id' => 12,
- 'tax_postcode' => '*',
- 'code' => 'Test Tax Rate ' . microtime(),
- 'rate' => '8.2501',
- ],
- ];
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH,
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'Save',
- ],
- ];
- $result = $this->_webApiCall($serviceInfo, $data);
- $this->assertArrayHasKey('id', $result);
- $taxRateId = $result['id'];
- /** Ensure that tax rate was actually created in DB */
- /** @var \Magento\Tax\Model\Calculation\Rate $taxRate */
- $taxRate = Bootstrap::getObjectManager()->create(\Magento\Tax\Model\Calculation\Rate::class);
- $this->assertEquals($taxRateId, $taxRate->load($taxRateId)->getId(), 'Tax rate was not created in DB.');
- $taxRate->delete();
- }
- public function testCreateTaxRateWithZipRange()
- {
- $data = [
- 'tax_rate' => [
- 'tax_country_id' => 'US',
- 'tax_region_id' => 12,
- 'code' => 'Test Tax Rate ' . microtime(),
- 'rate' => '8.2501',
- 'zip_is_range' => 1,
- 'zip_from' => 17,
- 'zip_to' => 25,
- ],
- ];
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH,
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'Save',
- ],
- ];
- $result = $this->_webApiCall($serviceInfo, $data);
- $this->assertArrayHasKey('id', $result);
- $taxRateId = $result['id'];
- /** Ensure that tax rate was actually created in DB */
- /** @var \Magento\Tax\Model\Calculation\Rate $taxRate */
- $taxRate = Bootstrap::getObjectManager()->create(\Magento\Tax\Model\Calculation\Rate::class);
- $this->assertEquals($taxRateId, $taxRate->load($taxRateId)->getId(), 'Tax rate was not created in DB.');
- $this->assertEquals('17-25', $taxRate->getTaxPostcode(), 'Zip range is not saved in DB.');
- $taxRate->delete();
- }
- public function testCreateTaxRateWithZeroValue()
- {
- $data = [
- 'tax_rate' => [
- 'tax_country_id' => 'US',
- 'tax_region_id' => 12,
- 'tax_postcode' => '*',
- 'code' => 'Test Tax Rate ' . microtime(),
- 'rate' => '0.0',
- ],
- ];
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH,
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'Save',
- ],
- ];
- $result = $this->_webApiCall($serviceInfo, $data);
- $this->assertArrayHasKey('id', $result);
- $taxRateId = $result['id'];
- /** Ensure that tax rate was actually created in DB */
- /** @var \Magento\Tax\Model\Calculation\Rate $taxRate */
- $taxRate = Bootstrap::getObjectManager()->create(\Magento\Tax\Model\Calculation\Rate::class);
- $taxModel = $taxRate->load($taxRateId);
- $this->assertEquals($taxRateId, $taxModel->getId(), 'Tax rate was not created in DB.');
- $this->assertEquals(0, $taxModel->getRate(), 'Tax rate value is wrong.');
- $taxRate->delete();
- }
- /**
- * @magentoApiDataFixture Magento/Tax/_files/tax_classes.php
- */
- public function testUpdateTaxRate()
- {
- $fixtureRate = $this->getFixtureTaxRates()[0];
- $data = [
- 'tax_rate' => [
- 'id' => $fixtureRate->getId(),
- 'tax_region_id' => 43,
- 'tax_country_id' => 'US',
- 'tax_postcode' => '07400',
- 'code' => 'Test Tax Rate ' . microtime(),
- 'rate' => 3.456,
- ],
- ];
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH,
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'Save',
- ],
- ];
- $this->_webApiCall($serviceInfo, $data);
- $expectedRateData = $data['tax_rate'];
- /** Ensure that tax rate was actually updated in DB */
- /** @var \Magento\Tax\Model\Calculation\Rate $taxRate */
- $taxRate = Bootstrap::getObjectManager()->create(\Magento\Tax\Model\Calculation\Rate::class);
- $taxRateModel = $taxRate->load($fixtureRate->getId());
- $this->assertEquals($expectedRateData['id'], $taxRateModel->getId(), 'Tax rate was not updated in DB.');
- $this->assertEquals(
- $expectedRateData['tax_region_id'],
- $taxRateModel->getTaxRegionId(),
- 'Tax rate was not updated in DB.'
- );
- $this->assertEquals(
- $expectedRateData['tax_country_id'],
- $taxRateModel->getTaxCountryId(),
- 'Tax rate was not updated in DB.'
- );
- $this->assertEquals(
- $expectedRateData['tax_postcode'],
- $taxRateModel->getTaxPostcode(),
- 'Tax rate was not updated in DB.'
- );
- $this->assertEquals($expectedRateData['code'], $taxRateModel->getCode(), 'Tax rate was not updated in DB.');
- $this->assertEquals(
- $expectedRateData['rate'],
- $taxRateModel->getRate(),
- 'Tax rate was not updated in DB.'
- );
- }
- public function testUpdateTaxRateNotExisting()
- {
- $data = [
- 'tax_rate' => [
- 'id' => 555,
- 'tax_region_id' => 43,
- 'tax_country_id' => 'US',
- 'tax_postcode' => '07400',
- 'code' => 'Test Tax Rate ' . microtime(),
- 'rate' => 3.456,
- ],
- ];
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH,
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'Save',
- ],
- ];
- try {
- $this->_webApiCall($serviceInfo, $data);
- $this->fail('Expected exception was not raised');
- } catch (\Exception $e) {
- $expectedMessage = 'No such entity with %fieldName = %fieldValue';
- $this->assertContains(
- $expectedMessage,
- $e->getMessage(),
- "Exception does not contain expected message."
- );
- }
- }
- public function testGetTaxRate()
- {
- $taxRateId = 2;
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . "/$taxRateId",
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'Get',
- ],
- ];
- $result = $this->_webApiCall($serviceInfo, ['rateId' => $taxRateId]);
- $expectedRateData = [
- 'id' => 2,
- 'tax_country_id' => 'US',
- 'tax_region_id' => 43,
- 'tax_postcode' => '*',
- 'code' => 'US-NY-*-Rate 1',
- 'rate' => 8.375,
- 'titles' => [],
- 'region_name' => 'NY',
- ];
- $this->assertEquals($expectedRateData, $result);
- }
- public function testGetTaxRateNotExist()
- {
- $taxRateId = 37865;
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . "/$taxRateId",
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'Get',
- ],
- ];
- try {
- $this->_webApiCall($serviceInfo, ['rateId' => $taxRateId]);
- $this->fail('Expected exception was not raised');
- } catch (\Exception $e) {
- $expectedMessage = 'No such entity with %fieldName = %fieldValue';
- $this->assertContains(
- $expectedMessage,
- $e->getMessage(),
- "Exception does not contain expected message."
- );
- }
- }
- /**
- * @magentoApiDataFixture Magento/Tax/_files/tax_classes.php
- */
- public function testDeleteTaxRate()
- {
- /** Tax rules must be deleted since tax rate cannot be deleted if there are any tax rules associated with it */
- $taxRules = $this->getFixtureTaxRules();
- foreach ($taxRules as $taxRule) {
- $taxRule->delete();
- }
- $fixtureRate = $this->getFixtureTaxRates()[0];
- $taxRateId = $fixtureRate->getId();
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . "/$taxRateId",
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'DeleteById',
- ],
- ];
- $result = $this->_webApiCall($serviceInfo, ['rateId' => $taxRateId]);
- $this->assertTrue($result);
- /** Ensure that tax rate was actually removed from DB */
- /** @var \Magento\Tax\Model\Calculation\Rate $taxRate */
- $taxRate = Bootstrap::getObjectManager()->create(\Magento\Tax\Model\Calculation\Rate::class);
- $this->assertNull($taxRate->load($taxRateId)->getId(), 'Tax rate was not deleted from DB.');
- }
- /**
- * Insure that tax rate cannot be deleted if it is used for a tax rule.
- *
- * @magentoApiDataFixture Magento/Tax/_files/tax_classes.php
- */
- public function testCannotDeleteTaxRate()
- {
- $fixtureRate = $this->getFixtureTaxRates()[0];
- $taxRateId = $fixtureRate->getId();
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . "/$taxRateId",
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'DeleteById',
- ],
- ];
- try {
- $this->_webApiCall($serviceInfo, ['rateId' => $taxRateId]);
- $this->fail('Expected exception was not raised');
- } catch (\Exception $e) {
- $expectedMessage = "The tax rate can't be removed because it exists in a tax rule.";
- $this->assertContains(
- $expectedMessage,
- $e->getMessage(),
- "Exception does not contain expected message."
- );
- }
- }
- public function testSearchTaxRates()
- {
- $rates = $this->setupTaxRatesForSearch();
- // Find rates whose code is 'codeUs12'
- $filter = $this->filterBuilder->setField(Rate::KEY_CODE)
- ->setValue('codeUs12')
- ->create();
- $this->searchCriteriaBuilder->addFilters([$filter]);
- $searchData = $this->searchCriteriaBuilder->create()->__toArray();
- $requestData = ['searchCriteria' => $searchData];
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . '/search' . '?' . http_build_query($requestData),
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'GetList',
- ],
- ];
- /** @var \Magento\Framework\Api\SearchResults $searchResults */
- $searchResults = $this->_webApiCall($serviceInfo, $requestData);
- $this->assertEquals(1, $searchResults['total_count']);
- $expectedRuleData = [
- [
- 'id' => (int)$rates['codeUs12']->getId(),
- 'tax_country_id' => $rates['codeUs12']->getTaxCountryId(),
- 'tax_region_id' => (int)$rates['codeUs12']->getTaxRegionId(),
- 'region_name' => 'CA',
- 'tax_postcode' => $rates['codeUs12']->getTaxPostcode(),
- 'code' => $rates['codeUs12']->getCode(),
- 'rate' => ((float) $rates['codeUs12']->getRate()),
- 'titles' => [],
- ],
- ];
- $this->assertEquals($expectedRuleData, $searchResults['items']);
- }
- public function testSearchTaxRatesCz()
- {
- // TODO: This test fails in SOAP, a generic bug searching in SOAP
- $this->_markTestAsRestOnly();
- $rates = $this->setupTaxRatesForSearch();
- $filterBR = $this->filterBuilder->setField(Rate::KEY_COUNTRY_ID)
- ->setValue('BR')
- ->create();
- $filterUS = $this->filterBuilder->setField(Rate::KEY_COUNTRY_ID)
- ->setValue('US')
- ->create();
- // Find rates which country id 'CZ'
- $filterCZ = $this->filterBuilder->setField(Rate::KEY_COUNTRY_ID)
- ->setValue('CZ')
- ->create();
- $sortOrder = $this->sortOrderBuilder
- ->setField(Rate::KEY_POSTCODE)
- ->setDirection(SortOrder::SORT_DESC)
- ->create();
- $filterRate = $this->filterBuilder->setField(Rate::KEY_PERCENTAGE_RATE)
- ->setValue('2.2000')
- ->create();
- $this->searchCriteriaBuilder->addFilters([$filterBR, $filterUS, $filterCZ]);
- // Order them by descending postcode (not the default order)
- $this->searchCriteriaBuilder->addFilters([$filterCZ, $filterRate])
- ->addSortOrder($sortOrder);
- $searchData = $this->searchCriteriaBuilder->create()->__toArray();
- $requestData = ['searchCriteria' => $searchData];
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . '/search' . '?' . http_build_query($requestData),
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'GetList',
- ],
- ];
- /** @var \Magento\Framework\Api\SearchResults $searchResults */
- $searchResults = $this->_webApiCall($serviceInfo, $requestData);
- $this->assertEquals(2, $searchResults['total_count']);
- $expectedRuleData = [
- [
- 'id' => (int)$rates['codeCz2']->getId(),
- 'tax_country_id' => $rates['codeCz2']->getTaxCountryId(),
- 'tax_postcode' => $rates['codeCz2']->getTaxPostcode(),
- 'code' => $rates['codeCz2']->getCode(),
- 'rate' => ((float) $rates['codeCz2']->getRate()),
- 'tax_region_id' => 0,
- 'titles' => [],
- ],
- [
- 'id' => (int)$rates['codeCz1']->getId(),
- 'tax_country_id' => $rates['codeCz1']->getTaxCountryId(),
- 'tax_postcode' => $rates['codeCz1']->getTaxPostcode(),
- 'code' => $rates['codeCz1']->getCode(),
- 'rate' => ((float) $rates['codeCz1']->getRate()),
- 'tax_region_id' => 0,
- 'titles' => [],
- ],
- ];
- $this->assertEquals($expectedRuleData, $searchResults['items']);
- }
- /**
- * Get tax rates created in Magento\Tax\_files\tax_classes.php
- *
- * @return \Magento\Tax\Model\Calculation\Rate[]
- */
- private function getFixtureTaxRates()
- {
- if ($this->fixtureTaxRates === null) {
- $this->fixtureTaxRates = [];
- if ($this->getFixtureTaxRules()) {
- $taxRateIds = (array)$this->getFixtureTaxRules()[0]->getRates();
- foreach ($taxRateIds as $taxRateId) {
- /** @var \Magento\Tax\Model\Calculation\Rate $taxRate */
- $taxRate = Bootstrap::getObjectManager()->create(\Magento\Tax\Model\Calculation\Rate::class);
- $this->fixtureTaxRates[] = $taxRate->load($taxRateId);
- }
- }
- }
- return $this->fixtureTaxRates;
- }
- /**
- * Get tax classes created in Magento\Tax\_files\tax_classes.php
- *
- * @return \Magento\Tax\Model\ClassModel[]
- */
- private function getFixtureTaxClasses()
- {
- if ($this->fixtureTaxClasses === null) {
- $this->fixtureTaxClasses = [];
- if ($this->getFixtureTaxRules()) {
- $taxClassIds = array_merge(
- (array)$this->getFixtureTaxRules()[0]->getCustomerTaxClasses(),
- (array)$this->getFixtureTaxRules()[0]->getProductTaxClasses()
- );
- foreach ($taxClassIds as $taxClassId) {
- /** @var \Magento\Tax\Model\ClassModel $taxClass */
- $taxClass = Bootstrap::getObjectManager()->create(\Magento\Tax\Model\ClassModel::class);
- $this->fixtureTaxClasses[] = $taxClass->load($taxClassId);
- }
- }
- }
- return $this->fixtureTaxClasses;
- }
- /**
- * Get tax rule created in Magento\Tax\_files\tax_classes.php
- *
- * @return \Magento\Tax\Model\Calculation\Rule[]
- */
- private function getFixtureTaxRules()
- {
- if ($this->fixtureTaxRules === null) {
- $this->fixtureTaxRules = [];
- $taxRuleCodes = ['Test Rule Duplicate', 'Test Rule'];
- foreach ($taxRuleCodes as $taxRuleCode) {
- /** @var \Magento\Tax\Model\Calculation\Rule $taxRule */
- $taxRule = Bootstrap::getObjectManager()->create(\Magento\Tax\Model\Calculation\Rule::class);
- $taxRule->load($taxRuleCode, 'code');
- if ($taxRule->getId()) {
- $this->fixtureTaxRules[] = $taxRule;
- }
- }
- }
- return $this->fixtureTaxRules;
- }
- /**
- * Creates rates for search tests.
- *
- * @return \Magento\Tax\Model\Calculation\Rate[]
- */
- private function setupTaxRatesForSearch()
- {
- $objectManager = Bootstrap::getObjectManager();
- $taxRateUs12 = [
- 'tax_country_id' => 'US',
- 'tax_region_id' => 12,
- 'tax_postcode' => '*',
- 'code' => 'codeUs12',
- 'rate' => 22,
- 'region_name' => 'CA',
- ];
- $rates['codeUs12'] = $objectManager->create(\Magento\Tax\Model\Calculation\Rate::class)
- ->setData($taxRateUs12)
- ->save();
- $taxRateUs14 = [
- 'tax_country_id' => 'US',
- 'tax_region_id' => 14,
- 'tax_postcode' => '*',
- 'code' => 'codeUs14',
- 'rate' => 22,
- ];
- $rates['codeUs14'] = $objectManager->create(\Magento\Tax\Model\Calculation\Rate::class)
- ->setData($taxRateUs14)
- ->save();
- $taxRateBr13 = [
- 'tax_country_id' => 'BR',
- 'tax_region_id' => 13,
- 'tax_postcode' => '*',
- 'code' => 'codeBr13',
- 'rate' => 7.5,
- ];
- $rates['codeBr13'] = $objectManager->create(\Magento\Tax\Model\Calculation\Rate::class)
- ->setData($taxRateBr13)
- ->save();
- $taxRateCz1 = [
- 'tax_country_id' => 'CZ',
- 'tax_postcode' => '110 00',
- 'code' => 'codeCz1',
- 'rate' => 1.1,
- ];
- $rates['codeCz1'] = $objectManager->create(\Magento\Tax\Model\Calculation\Rate::class)
- ->setData($taxRateCz1)
- ->save();
- $taxRateCz2 = [
- 'tax_country_id' => 'CZ',
- 'tax_postcode' => '250 00',
- 'code' => 'codeCz2',
- 'rate' => 2.2,
- ];
- $rates['codeCz2'] = $objectManager->create(\Magento\Tax\Model\Calculation\Rate::class)
- ->setData($taxRateCz2)
- ->save();
- // Set class variable so rates will be deleted on tearDown()
- $this->otherRates = $rates;
- return $rates;
- }
- }
|