Skip to content

Commit

Permalink
minor #15616 [API] Finish covering taxation scenarios (NoResponseMate)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.13 branch.

Discussion
----------

| Q               | A                                                            |
|-----------------|--------------------------------------------------------------|
| Branch?         | 1.13 |
| Bug fix?        | kinda                                                       |
| New feature?    | no                                                       |
| BC breaks?      | no                                                       |
| Deprecations?   | no |
| Related tickets | -                      |
| License         | MIT                                                          |

Commits
-------
  [API] Finish covering taxation scenarios
  [API][TaxRate] Treat missing/null amount as default
  • Loading branch information
jakubtobiasz committed Dec 11, 2023
2 parents f161d21 + 76b6a21 commit 45f31a9
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,8 @@ Feature: Editing tax category
Given the store has a tax category "Alcohol" with a code "alcohol"
And I am logged in as an administrator

@todo
Scenario: Trying to change tax category code
When I want to modify a tax category "Alcohol"
And I change its code to "beverages"
And I save my changes
Then I should be notified that code cannot be changed
And tax category "Alcohol" should still have code "alcohol"

@ui @api
Scenario: Seeing disabled code field when editing tax category
Scenario: Inability of changing the code of an existing tax category
When I want to modify a tax category "Alcohol"
Then I should not be able to edit its code

Expand Down
13 changes: 13 additions & 0 deletions features/taxation/managing_tax_rates/adding_tax_rate.feature
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,16 @@ Feature: Adding a new tax rate
Then I should be notified that it has been successfully created
And the tax rate "United States Sales Tax" should appear in the registry
And the tax rate "United States Sales Tax" should be included in price

@api @no-ui
Scenario: Adding a new tax rate with no amount sets the default
When I want to create a new tax rate
And I specify its code as "US_SALES_TAX"
And I name it "United States Sales Tax"
And I define it for the "United States" zone
And I make it applicable for the "Food and Beverage" tax category
And I choose the default tax calculator
And I add it
Then I should be notified that it has been successfully created
And the tax rate "United States Sales Tax" should appear in the registry
And this tax rate amount should be 0%
12 changes: 2 additions & 10 deletions features/taxation/managing_tax_rates/editing_tax_rate.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,10 @@ Feature: Editing tax rate
And the store has "United States Sales Tax" tax rate of 20% for "Sports gear" within the "US" zone
And I am logged in as an administrator

@todo
Scenario: Trying to change tax rate code
When I want to modify a tax rate "United States Sales Tax"
And I change its code to "us_vat"
And I save my changes
Then I should be notified that code cannot be changed
And tax rate "United States Sales Tax" should still have code "united_states_sales_tax"

@ui @api
Scenario: Seeing disabled code field when editing tax rate
Scenario: Inability of changing the code of an existing tax rate
When I want to modify a tax rate "United States Sales Tax"
Then the code field should be disabled
Then I should not be able to edit its code

@ui @api
Scenario: Renaming the tax rate
Expand Down
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 @@ -18,7 +18,7 @@ Feature: Tax rate validation
Then I should be notified that code is required
And tax rate with name "Food and Beverage Tax Rates" should not be added

@ui
@ui @no-api
Scenario: Trying to add a new tax rate without specifying its amount
When I want to create a new tax rate
And I name it "Food and Beverage Tax Rates"
Expand Down Expand Up @@ -56,7 +56,7 @@ Feature: Tax rate validation
Then I should be notified that category has to be selected
And tax rate with name "Food and Beverage Tax Rates" should not be added

@ui
@ui @no-api
Scenario: Trying to remove amount 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 All @@ -74,15 +74,15 @@ Feature: Tax rate validation
Then I should be notified that name is required
And this tax rate should still be named "United States Sales Tax"

@ui
@ui @no-api
Scenario: Trying to remove zone 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
And I remove its zone
And I try to save my changes
Then I should be notified that zone has to be selected

@ui
@ui @api
Scenario: Trying to add a new tax rate with negative amount
When I want to create a new tax rate
And I name it "Food and Beverage Tax Rates"
Expand All @@ -91,7 +91,7 @@ Feature: Tax rate validation
Then I should be notified that amount is invalid
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 with end date before start date
When I want to create a new tax rate
And I name it "Food and Beverage Tax Rates"
Expand Down
89 changes: 49 additions & 40 deletions src/Sylius/Behat/Context/Api/Admin/ManagingTaxRatesContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,64 +44,58 @@ 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): void
{
$this->client->addRequestData('code', $code);
}

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

/**
* @When I define it for the :zone zone
* @When I do not specify its zone
* @When I change its zone to :zone
*/
public function iDefineItForTheZone(?ZoneInterface $zone = null): void
public function iDefineItForTheZone(ZoneInterface $zone): void
{
if (null === $zone) {
return;
}

$this->client->addRequestData('zone', $this->iriConverter->getIriFromResource($zone));
}

/**
* @When I make it applicable for the :taxCategory tax category
* @When I change it to be applicable for the :taxCategory tax category
* @When I do not specify related tax category
*/
public function iMakeItApplicableForTheTaxCategory(?TaxCategoryInterface $taxCategory = null): void
public function iMakeItApplicableForTheTaxCategory(TaxCategoryInterface $taxCategory): void
{
if (null === $taxCategory) {
return;
}

$this->client->addRequestData('category', $this->iriConverter->getIriFromResource($taxCategory));
}

/**
* @When I specify its amount as :amount%
* @When I do not specify its amount
*/
public function iSpecifyItsAmountAs(?string $amount = null): void
public function iSpecifyItsAmountAs(string $amount): void
{
if (null === $amount) {
return;
}

$this->client->addRequestData('amount', $amount);
}

/**
* @When I do not specify related tax category
* @When I do not specify its zone
* @When I do not name it
* @When I do not specify its code
*/
public function iDoNotSpecifyItsField(): void
{
// Intentionally left blank
}

/**
* @When I choose the default tax calculator
*/
Expand Down Expand Up @@ -162,14 +156,6 @@ public function iWantToModifyThisTaxRate(TaxRateInterface $taxRate): void
$this->client->addRequestData('amount', (string) $taxRate->getAmount());
}

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

/**
* @When I browse tax rates
*/
Expand Down Expand Up @@ -267,6 +253,8 @@ public function iShouldBeNotifiedThatItHasBeenSuccessfullyCreated(): void
*/
public function theTaxRateShouldAppearInTheRegistry(TaxRateInterface $taxRate): void
{
$this->sharedStorage->set('tax_rate', $taxRate);

$name = $taxRate->getName();

Assert::true(
Expand Down Expand Up @@ -349,7 +337,7 @@ public function thereShouldStillBeOnlyOneTaxRateWithCode(string $code): void
/**
* @Then I should be notified that :element is required
*/
public function iShouldBeNotifiedThatCodeIsRequired(string $element): void
public function iShouldBeNotifiedThatElementIsRequired(string $element): void
{
Assert::contains(
$this->responseChecker->getError($this->client->getLastResponse()),
Expand Down Expand Up @@ -401,14 +389,6 @@ public function iShouldNotSeeATaxRateWithName(string $name): void
);
}

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

/**
* @Then /^(this tax rate) should still be named "([^"]+)"$/
* @Then /^(this tax rate) name should be "([^"]*)"$/
Expand All @@ -423,8 +403,9 @@ public function thisTaxRateShouldStillBeNamed(TaxRateInterface $taxRate, string

/**
* @Then the code field should be disabled
* @Then I should not be able to edit its code
*/
public function theCodeFieldShouldBeDisabled(): void
public function iShouldNotBeAbleToEditItsCode(): void
{
$this->client->updateRequestData(['code' => 'NEW_CODE']);

Expand Down Expand Up @@ -463,4 +444,32 @@ public function thisTaxRateShouldBeApplicableInZone(TaxRateInterface $taxRate, Z
sprintf('Tax rate is not applicable for %s zone', $zone),
);
}

/**
* @Then I should be notified that amount is invalid
*/
public function iShouldBeNotifiedThatAmountIsInvalid(): void
{
Assert::true(
$this->responseChecker->hasViolationWithMessage(
$this->client->getLastResponse(),
'The tax rate amount is invalid.',
'amount',
),
);
}

/**
* @Then I should be notified that tax rate should not end before it starts
*/
public function iShouldBeNotifiedThatTaxRateShouldNotEndBeforeItStarts(): void
{
Assert::true(
$this->responseChecker->hasViolationWithMessage(
$this->client->getLastResponse(),
'The tax rate should not end before it starts',
'endDate',
),
);
}
}
3 changes: 2 additions & 1 deletion src/Sylius/Behat/Context/Ui/Admin/ManagingTaxRateContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,9 @@ public function iWantToModifyTaxRate(TaxRateInterface $taxRate)

/**
* @Then the code field should be disabled
* @Then I should not be able to edit its code
*/
public function theCodeFieldShouldBeDisabled()
public function iShouldNotBeAbleToEditItsCode(): void
{
Assert::true($this->updatePage->isCodeDisabled());
}
Expand Down
48 changes: 48 additions & 0 deletions src/Sylius/Bundle/ApiBundle/Serializer/TaxRateDenormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ApiBundle\Serializer;

use Sylius\Component\Core\Model\TaxRateInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;

/** @experimental */
final class TaxRateDenormalizer implements ContextAwareDenormalizerInterface, DenormalizerAwareInterface
{
use DenormalizerAwareTrait;

private const ALREADY_CALLED = 'sylius_tax_rate_denormalizer_already_called';

public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool
{
return
!isset($context[self::ALREADY_CALLED]) &&
is_array($data) &&
is_a($type, TaxRateInterface::class, true)
;
}

public function denormalize(mixed $data, string $type, string $format = null, array $context = [])
{
$context[self::ALREADY_CALLED] = true;
$data = (array) $data;

if (null === ($data['amount'] ?? null)) {
unset($data['amount']);
}

return $this->denormalizer->denormalize($data, $type, $format, $context);
}
}
Loading

0 comments on commit 45f31a9

Please sign in to comment.