27 private $fixtureTaxRates;
30 private $fixtureTaxClasses;
33 private $fixtureTaxRules;
38 private $taxRateService;
41 private $filterBuilder;
44 private $searchCriteriaBuilder;
47 private $sortOrderBuilder;
54 private $otherRates = [];
64 \
Magento\Framework\Api\SearchCriteriaBuilder::class
67 \
Magento\Framework\Api\FilterBuilder::class
70 \
Magento\Framework\Api\SortOrderBuilder::class
73 $this->getFixtureTaxRates();
74 $this->getFixtureTaxClasses();
75 $this->getFixtureTaxRules();
82 $taxRates = $this->getFixtureTaxRates();
94 if (count($this->otherRates)) {
95 foreach ($this->otherRates as
$taxRate) {
103 $expectedMessage =
'%1 already exists.';
106 'tax_country_id' =>
'US',
107 'tax_region_id' => 12,
108 'tax_postcode' =>
'*',
109 'code' =>
'US-CA-*-Rate 1',
122 'operation' => self::SERVICE_NAME .
'Save',
127 $this->fail(
'Expected exception was not raised');
128 }
catch (\SoapFault $e) {
129 $this->assertContains(
132 'SoapFault does not contain expected message.' 134 }
catch (\Exception $e) {
136 $this->assertEquals($expectedMessage, $errorObj[
'message']);
137 $this->assertEquals([
'Code'], $errorObj[
'parameters']);
145 'tax_country_id' =>
'US',
146 'tax_region_id' => 12,
147 'tax_postcode' =>
'*',
148 'code' =>
'US-CA-*-Rate 1',
160 'operation' => self::SERVICE_NAME .
'Save',
165 $this->fail(
'Expected exception was not raised');
166 }
catch (\SoapFault $e) {
167 $this->assertContains(
168 'SOAP-ERROR: Encoding: object has no \'rate\' property',
170 'SoapFault does not contain expected message.' 172 }
catch (\Exception $e) {
174 $this->assertEquals(
'"%fieldName" is required. Enter and try again.', $errorObj[
'message']);
175 $this->assertEquals([
'fieldName' =>
'percentage_rate'], $errorObj[
'parameters']);
179 public function testCreateTaxRate()
183 'tax_country_id' =>
'US',
184 'tax_region_id' => 12,
185 'tax_postcode' =>
'*',
186 'code' =>
'Test Tax Rate ' . microtime(),
199 'operation' => self::SERVICE_NAME .
'Save',
203 $this->assertArrayHasKey(
'id',
$result);
208 $this->assertEquals($taxRateId,
$taxRate->load($taxRateId)->getId(),
'Tax rate was not created in DB.');
212 public function testCreateTaxRateWithZipRange()
216 'tax_country_id' =>
'US',
217 'tax_region_id' => 12,
218 'code' =>
'Test Tax Rate ' . microtime(),
234 'operation' => self::SERVICE_NAME .
'Save',
238 $this->assertArrayHasKey(
'id',
$result);
243 $this->assertEquals($taxRateId,
$taxRate->load($taxRateId)->getId(),
'Tax rate was not created in DB.');
244 $this->assertEquals(
'17-25',
$taxRate->getTaxPostcode(),
'Zip range is not saved in DB.');
248 public function testCreateTaxRateWithZeroValue()
252 'tax_country_id' =>
'US',
253 'tax_region_id' => 12,
254 'tax_postcode' =>
'*',
255 'code' =>
'Test Tax Rate ' . microtime(),
268 'operation' => self::SERVICE_NAME .
'Save',
272 $this->assertArrayHasKey(
'id',
$result);
277 $taxModel =
$taxRate->load($taxRateId);
278 $this->assertEquals($taxRateId, $taxModel->getId(),
'Tax rate was not created in DB.');
279 $this->assertEquals(0, $taxModel->getRate(),
'Tax rate value is wrong.');
286 public function testUpdateTaxRate()
288 $fixtureRate = $this->getFixtureTaxRates()[0];
292 'id' => $fixtureRate->getId(),
293 'tax_region_id' => 43,
294 'tax_country_id' =>
'US',
295 'tax_postcode' =>
'07400',
296 'code' =>
'Test Tax Rate ' . microtime(),
309 'operation' => self::SERVICE_NAME .
'Save',
313 $expectedRateData =
$data[
'tax_rate'];
317 $taxRateModel =
$taxRate->load($fixtureRate->getId());
318 $this->assertEquals($expectedRateData[
'id'], $taxRateModel->getId(),
'Tax rate was not updated in DB.');
320 $expectedRateData[
'tax_region_id'],
321 $taxRateModel->getTaxRegionId(),
322 'Tax rate was not updated in DB.' 325 $expectedRateData[
'tax_country_id'],
326 $taxRateModel->getTaxCountryId(),
327 'Tax rate was not updated in DB.' 330 $expectedRateData[
'tax_postcode'],
331 $taxRateModel->getTaxPostcode(),
332 'Tax rate was not updated in DB.' 334 $this->assertEquals($expectedRateData[
'code'], $taxRateModel->getCode(),
'Tax rate was not updated in DB.');
336 $expectedRateData[
'rate'],
337 $taxRateModel->getRate(),
338 'Tax rate was not updated in DB.' 347 'tax_region_id' => 43,
348 'tax_country_id' =>
'US',
349 'tax_postcode' =>
'07400',
350 'code' =>
'Test Tax Rate ' . microtime(),
363 'operation' => self::SERVICE_NAME .
'Save',
368 $this->fail(
'Expected exception was not raised');
369 }
catch (\Exception $e) {
370 $expectedMessage =
'No such entity with %fieldName = %fieldValue';
372 $this->assertContains(
375 "Exception does not contain expected message." 385 'resourcePath' => self::RESOURCE_PATH .
"/$taxRateId",
391 'operation' => self::SERVICE_NAME .
'Get',
396 $expectedRateData = [
398 'tax_country_id' =>
'US',
399 'tax_region_id' => 43,
400 'tax_postcode' =>
'*',
401 'code' =>
'US-NY-*-Rate 1',
404 'region_name' =>
'NY',
406 $this->assertEquals($expectedRateData,
$result);
414 'resourcePath' => self::RESOURCE_PATH .
"/$taxRateId",
420 'operation' => self::SERVICE_NAME .
'Get',
424 $this->
_webApiCall($serviceInfo, [
'rateId' => $taxRateId]);
425 $this->fail(
'Expected exception was not raised');
426 }
catch (\Exception $e) {
427 $expectedMessage =
'No such entity with %fieldName = %fieldValue';
429 $this->assertContains(
432 "Exception does not contain expected message." 440 public function testDeleteTaxRate()
448 $fixtureRate = $this->getFixtureTaxRates()[0];
449 $taxRateId = $fixtureRate->getId();
452 'resourcePath' => self::RESOURCE_PATH .
"/$taxRateId",
458 'operation' => self::SERVICE_NAME .
'DeleteById',
467 $this->assertNull(
$taxRate->load($taxRateId)->getId(),
'Tax rate was not deleted from DB.');
477 $fixtureRate = $this->getFixtureTaxRates()[0];
478 $taxRateId = $fixtureRate->getId();
481 'resourcePath' => self::RESOURCE_PATH .
"/$taxRateId",
487 'operation' => self::SERVICE_NAME .
'DeleteById',
491 $this->
_webApiCall($serviceInfo, [
'rateId' => $taxRateId]);
492 $this->fail(
'Expected exception was not raised');
493 }
catch (\Exception $e) {
494 $expectedMessage =
"The tax rate can't be removed because it exists in a tax rule.";
496 $this->assertContains(
499 "Exception does not contain expected message." 504 public function testSearchTaxRates()
506 $rates = $this->setupTaxRatesForSearch();
510 ->setValue(
'codeUs12')
513 $this->searchCriteriaBuilder->addFilters([$filter]);
515 $searchData = $this->searchCriteriaBuilder->create()->__toArray();
519 'resourcePath' => self::RESOURCE_PATH .
'/search' .
'?' . http_build_query(
$requestData),
525 'operation' => self::SERVICE_NAME .
'GetList',
532 $this->assertEquals(1, $searchResults[
'total_count']);
534 $expectedRuleData = [
536 'id' => (int)
$rates[
'codeUs12']->getId(),
537 'tax_country_id' =>
$rates[
'codeUs12']->getTaxCountryId(),
538 'tax_region_id' => (int)
$rates[
'codeUs12']->getTaxRegionId(),
539 'region_name' =>
'CA',
540 'tax_postcode' =>
$rates[
'codeUs12']->getTaxPostcode(),
541 'code' =>
$rates[
'codeUs12']->getCode(),
542 'rate' => ((float)
$rates[
'codeUs12']->getRate()),
546 $this->assertEquals($expectedRuleData, $searchResults[
'items']);
549 public function testSearchTaxRatesCz()
553 $rates = $this->setupTaxRatesForSearch();
565 $sortOrder = $this->sortOrderBuilder
572 $this->searchCriteriaBuilder->addFilters([$filterBR, $filterUS, $filterCZ]);
574 $this->searchCriteriaBuilder->addFilters([$filterCZ, $filterRate])
575 ->addSortOrder($sortOrder);
576 $searchData = $this->searchCriteriaBuilder->create()->__toArray();
580 'resourcePath' => self::RESOURCE_PATH .
'/search' .
'?' . http_build_query(
$requestData),
586 'operation' => self::SERVICE_NAME .
'GetList',
593 $this->assertEquals(2, $searchResults[
'total_count']);
595 $expectedRuleData = [
597 'id' => (int)
$rates[
'codeCz2']->getId(),
598 'tax_country_id' =>
$rates[
'codeCz2']->getTaxCountryId(),
599 'tax_postcode' =>
$rates[
'codeCz2']->getTaxPostcode(),
600 'code' =>
$rates[
'codeCz2']->getCode(),
601 'rate' => ((float)
$rates[
'codeCz2']->getRate()),
602 'tax_region_id' => 0,
606 'id' => (
int)
$rates[
'codeCz1']->getId(),
607 'tax_country_id' =>
$rates[
'codeCz1']->getTaxCountryId(),
608 'tax_postcode' =>
$rates[
'codeCz1']->getTaxPostcode(),
609 'code' =>
$rates[
'codeCz1']->getCode(),
610 'rate' => ((float)
$rates[
'codeCz1']->getRate()),
611 'tax_region_id' => 0,
615 $this->assertEquals($expectedRuleData, $searchResults[
'items']);
623 private function getFixtureTaxRates()
625 if ($this->fixtureTaxRates ===
null) {
626 $this->fixtureTaxRates = [];
627 if ($this->getFixtureTaxRules()) {
628 $taxRateIds = (array)$this->getFixtureTaxRules()[0]->getRates();
629 foreach ($taxRateIds as $taxRateId) {
632 $this->fixtureTaxRates[] =
$taxRate->load($taxRateId);
636 return $this->fixtureTaxRates;
644 private function getFixtureTaxClasses()
646 if ($this->fixtureTaxClasses ===
null) {
647 $this->fixtureTaxClasses = [];
648 if ($this->getFixtureTaxRules()) {
649 $taxClassIds = array_merge(
650 (array)$this->getFixtureTaxRules()[0]->getCustomerTaxClasses(),
651 (array)$this->getFixtureTaxRules()[0]->getProductTaxClasses()
653 foreach ($taxClassIds as $taxClassId) {
656 $this->fixtureTaxClasses[] = $taxClass->load($taxClassId);
660 return $this->fixtureTaxClasses;
668 private function getFixtureTaxRules()
670 if ($this->fixtureTaxRules ===
null) {
671 $this->fixtureTaxRules = [];
672 $taxRuleCodes = [
'Test Rule Duplicate',
'Test Rule'];
673 foreach ($taxRuleCodes as $taxRuleCode) {
676 $taxRule->load($taxRuleCode,
'code');
678 $this->fixtureTaxRules[] =
$taxRule;
682 return $this->fixtureTaxRules;
690 private function setupTaxRatesForSearch()
695 'tax_country_id' =>
'US',
696 'tax_region_id' => 12,
697 'tax_postcode' =>
'*',
698 'code' =>
'codeUs12',
700 'region_name' =>
'CA',
703 ->setData($taxRateUs12)
707 'tax_country_id' =>
'US',
708 'tax_region_id' => 14,
709 'tax_postcode' =>
'*',
710 'code' =>
'codeUs14',
714 ->setData($taxRateUs14)
717 'tax_country_id' =>
'BR',
718 'tax_region_id' => 13,
719 'tax_postcode' =>
'*',
720 'code' =>
'codeBr13',
724 ->setData($taxRateBr13)
728 'tax_country_id' =>
'CZ',
729 'tax_postcode' =>
'110 00',
734 ->setData($taxRateCz1)
737 'tax_country_id' =>
'CZ',
738 'tax_postcode' =>
'250 00',
743 ->setData($taxRateCz2)
747 $this->otherRates =
$rates;
_markTestAsRestOnly($message=null)
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)
testCreateTaxRateExistingCode()
testCreateTaxRateWithoutValue()
processRestExceptionResult(\Exception $e)
testCannotDeleteTaxRate()
const KEY_PERCENTAGE_RATE
testUpdateTaxRateNotExisting()
static getObjectManager()