Skip to content

Commit

Permalink
[API][Tax rate] Implement tax_rate_validation feature
Browse files Browse the repository at this point in the history
  • Loading branch information
hatem20 authored and jakubtobiasz committed Feb 15, 2023
1 parent bacef86 commit 161d25d
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 15 deletions.
10 changes: 5 additions & 5 deletions features/taxation/managing_tax_rates/tax_rate_validation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Feature: Tax rate validation
And the store has a tax category "Food and Beverage"
And I am logged in as an administrator

@ui
@ui @api
Scenario: Trying to add a new tax rate without specifying its code
When I want to create a new tax rate
And I name it "Food and Beverage Tax Rates"
Expand All @@ -27,7 +27,7 @@ Feature: Tax rate validation
Then I should be notified that amount is required
And tax rate with name "Food and Beverage Tax Rates" should not be added

@ui
@ui @api
Scenario: Trying to add a new tax rate without specifying its name
When I want to create a new tax rate
And I specify its code as "UNITED_STATES_SALES_TAX"
Expand All @@ -36,7 +36,7 @@ Feature: Tax rate validation
Then I should be notified that name is required
And tax rate with code "UNITED_STATES_SALES_TAX" should not be added

@ui
@ui @api
Scenario: Trying to add a new tax rate without specifying its zone
Given the store does not have any zones defined
When I want to create a new tax rate
Expand All @@ -46,7 +46,7 @@ Feature: Tax rate validation
Then I should be notified that zone has to be selected
And tax rate with name "Food and Beverage Tax Rates" should not be added

@ui
@ui @api
Scenario: Trying to add a new tax rate without specifying its category
Given the store does not have any categories defined
When I want to create a new tax rate
Expand All @@ -65,7 +65,7 @@ Feature: Tax rate validation
Then I should be notified that amount is required
And this tax rate amount should still be 20%

@ui
@ui @api
Scenario: Trying to remove name from existing tax rate
Given the store has "United States Sales Tax" tax rate of 20% for "Sports gear" within the "US" zone
When I want to modify this tax rate
Expand Down
132 changes: 122 additions & 10 deletions src/Sylius/Behat/Context/Api/Admin/ManagingTaxRateContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,42 +59,57 @@ public function iWantToCreateANewTaxRate(): void

/**
* @When I specify its code as :code
* @When I do not specify its code
*/
public function iSpecifyItsCodeAs(string $code): void
public function iSpecifyItsCodeAs(?string $code = null): void
{
$this->client->addRequestData('code', $code);
if ($code) {
$this->client->addRequestData('code', $code);
}
}

/**
* @When I name it :name
* @When I do not name it
*/
public function iNameIt(string $name): void
public function iNameIt(?string $name = null): void
{
$this->client->addRequestData('name', $name);
if ($name) {
$this->client->addRequestData('name', $name);
}
}

/**
* @When I define it for the :zone zone
* @When I do not specify its zone
*/
public function iDefineItForTheZone(ZoneInterface $zone): void
public function iDefineItForTheZone(?ZoneInterface $zone = null): void
{
$this->client->addRequestData('zone', $this->iriConverter->getIriFromItem($zone));
if ($zone) {
$this->client->addRequestData('zone', $this->iriConverter->getIriFromItem($zone));
}
}

/**
* @When I make it applicable for the :taxCategory tax category
* @When I do not specify related tax category
*/
public function iMakeItApplicableForTheTaxCategory(TaxCategoryInterface $taxCategory): void
public function iMakeItApplicableForTheTaxCategory(?TaxCategoryInterface $taxCategory = null): void
{
$this->client->addRequestData('category', $this->iriConverter->getIriFromItem($taxCategory));
if ($taxCategory) {
$this->client->addRequestData('category', $this->iriConverter->getIriFromItem($taxCategory));
}
}

/**
* @When I specify its amount as :amount%
* @When I do not specify its amount
*/
public function iSpecifyItsAmountAs(int $amount): void
public function iSpecifyItsAmountAs(?string $amount = null): void
{
$this->client->addRequestData('amount', $amount);
if ($amount) {
$this->client->addRequestData('amount', $amount);
}
}

/**
Expand Down Expand Up @@ -268,4 +283,101 @@ public function thereShouldStillBeOnlyOneTaxRateWithCode(string $code): void
sprintf('There is more than one tax rate with code %s', $code)
);
}

/**
* @Then I should be notified that :element is required
*/
public function iShouldBeNotifiedThatCodeIsRequired(string $element): void
{
Assert::contains(
$this->responseChecker->getError($this->client->getLastResponse()),
sprintf('%s: Please enter tax rate %s.', $element, $element)
);
}

/**
* @Then tax rate with :element :code should not be added
*/
public function taxRateWithCodeShouldNotBeAdded(string $element, string $code): void
{
Assert::false($this->isItemOnIndex($element, $code), sprintf('Tax rate with %s %s exist', $element, $code));
}

private function isItemOnIndex(string $property, string $value): bool
{
return $this->responseChecker->hasItemWithValue($this->client->index(), $property, $value);
}

/**
* @Then I should be notified that zone has to be selected
*/
public function iShouldBeNotifiedThatZoneHasToBeSelected(): void
{
Assert::contains(
$this->responseChecker->getError($this->client->getLastResponse()),
'zone: Please select tax zone.'
);
}

/**
* @Then I should be notified that category has to be selected
*/
public function iShouldBeNotifiedThatCategoryHasToBeSelected(): void
{
Assert::contains(
$this->responseChecker->getError($this->client->getLastResponse()),
'category: Please select tax category.'
);
}

/**
* @When /^I want to modify (this tax rate)$/
*/
public function iWantToModifyThisTaxRate(TaxRateInterface $taxRate): void
{
$this->client->buildUpdateRequest((string) $taxRate->getId());
}

/**
* @When I remove its amount
*/
public function iRemoveItsAmount(): void
{
$this->client->addRequestData('amount', '');
}

/**
* @When I try to save my changes
*/
public function iSaveMyChanges(): void
{
$this->client->update();
}

/**
* @Then /^(this tax rate) amount should still be ([^"]+)%$/
*/
public function thisTaxRateAmountShouldStillBe(TaxRateInterface $taxRate, $taxRateAmount): void
{
Assert::true($taxRate->getAmount(), $taxRateAmount);
}

/**
* @When I remove its name
*/
public function iRemoveItsName(): void
{
$this->client->addRequestData('name', '');
}

/**
* @Then /^(this tax rate) should still be named "([^"]+)"$/
*/
public function thisTaxRateShouldStillBeNamed(TaxRateInterface $taxRate, string $taxRateName): void
{
Assert::true(
$this->responseChecker->hasValue($this->client->show((string) $taxRate->getId()), 'name', $taxRateName),
sprintf('Tax category rate is not %s', $taxRateName)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
<attribute name="method">GET</attribute>
</itemOperation>

<itemOperation name="admin_put">
<attribute name="method">PUT</attribute>
<attribute name="denormalization_context">
<attribute name="groups">tax_rate:update</attribute>
</attribute>
</itemOperation>

<itemOperation name="admin_delete">
<attribute name="method">DELETE</attribute>
</itemOperation>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,32 @@
<attribute name="zone">
<group>tax_rate:read</group>
<group>tax_rate:create</group>
<group>tax_rate:update</group>
</attribute>
<attribute name="name">
<group>tax_rate:read</group>
<group>tax_rate:create</group>
<group>tax_rate:update</group>
</attribute>
<attribute name="category">
<group>tax_rate:read</group>
<group>tax_rate:create</group>
<group>tax_rate:update</group>
</attribute>
<attribute name="calculator">
<group>tax_rate:read</group>
<group>tax_rate:create</group>
<group>tax_rate:update</group>
</attribute>
<attribute name="amount">
<group>tax_rate:read</group>
<group>tax_rate:create</group>
<group>tax_rate:update</group>
</attribute>
<attribute name="includedInPrice">
<group>tax_rate:read</group>
<group>tax_rate:create</group>
<group>tax_rate:update</group>
</attribute>
</class>
</serializer>

0 comments on commit 161d25d

Please sign in to comment.