Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Behavioral tests for Tax commands #14132

Merged
merged 17 commits into from Sep 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Adapter/Tax/QueryHandler/GetTaxForEditingHandler.php
Expand Up @@ -46,8 +46,8 @@ public function handle(GetTaxForEditing $query)
return new EditableTax(
$query->getTaxId(),
$tax->name,
$tax->rate,
$tax->active
(float) $tax->rate,
(bool) $tax->active
);
}
}
@@ -0,0 +1,274 @@
<?php
/**
* 2007-2019 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2019 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

namespace Tests\Integration\Behaviour\Features\Context\Domain;

use Behat\Gherkin\Node\TableNode;
use PrestaShop\PrestaShop\Core\Domain\Tax\Command\AddTaxCommand;
use PrestaShop\PrestaShop\Core\Domain\Tax\Command\BulkDeleteTaxCommand;
use PrestaShop\PrestaShop\Core\Domain\Tax\Command\BulkToggleTaxStatusCommand;
use PrestaShop\PrestaShop\Core\Domain\Tax\Command\DeleteTaxCommand;
use PrestaShop\PrestaShop\Core\Domain\Tax\Command\EditTaxCommand;
use PrestaShop\PrestaShop\Core\Domain\Tax\Command\ToggleTaxStatusCommand;
use PrestaShop\PrestaShop\Core\Domain\Tax\Exception\TaxNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\Tax\Query\GetTaxForEditing;
use PrestaShop\PrestaShop\Core\Domain\Tax\ValueObject\TaxId;
use RuntimeException;
use Tax;
use Tests\Integration\Behaviour\Features\Context\CommonFeatureContext;
use Tests\Integration\Behaviour\Features\Context\SharedStorage;
use Tests\Integration\Behaviour\Features\Context\Util\NoExceptionAlthoughExpectedException;
use Tests\Integration\Behaviour\Features\Context\Util\PrimitiveUtils;

class TaxFeatureContext extends AbstractDomainFeatureContext
{
/**
* @var int default language id from configuration
*/
private $defaultLangId;

public function __construct()
{
$this->defaultLangId = CommonFeatureContext::getContainer()
->get('prestashop.adapter.legacy.configuration')
->get('PS_LANG_DEFAULT');
}

/**
* @When I add new tax :taxReference with following properties:
*/
public function createTax($taxReference, TableNode $table)
{
$data = $table->getRowsHash();

$this->createTaxUsingCommand($taxReference, $data);
}

/**
* @When I edit tax :taxReference with following properties:
*/
public function editTaxUsingCommand($taxReference, TableNode $table)
{
$data = $table->getRowsHash();

/** @var Tax $tax */
$tax = SharedStorage::getStorage()->get($taxReference);
$taxId = (int) $tax->id;
$command = new EditTaxCommand($taxId);
if (isset($data['name'])) {
$command->setLocalizedNames([$this->defaultLangId => $data['name']]);
}
if (isset($data['rate'])) {
$command->setRate($data['rate']);
}

if (isset($data['is_enabled'])) {
$command->setEnabled(PrimitiveUtils::castStringBooleanIntoBoolean($data['is_enabled']));
}
$this->getCommandBus()->handle($command);

SharedStorage::getStorage()->set($taxReference, new Tax($taxId));
}

/**
* @When /^I (enable|disable)? tax "(.*)"$/
*/
public function toggleStatus($action, $taxReference)
{
$expectedStatus = 'enable' === $action;

/** @var Tax $tax */
$tax = SharedStorage::getStorage()->get($taxReference);
$taxId = (int) $tax->id;

$this->getCommandBus()->handle(new ToggleTaxStatusCommand($taxId, $expectedStatus));
SharedStorage::getStorage()->set($taxReference, new Tax($taxId));
}

/**
* @When /^I (enable|disable)? taxes: "([^"]*)"$/
*/
public function bulkToggleStatus($action, $taxReferences)
{
$taxReferences = PrimitiveUtils::castStringArrayIntoArray($taxReferences);
$expectedStatus = 'enable' === $action;

$idsByReference = [];
foreach ($taxReferences as $reference) {
$tax = SharedStorage::getStorage()->get($reference);
$idsByReference[$reference] = (int) $tax->id;
}

$this->getCommandBus()->handle(new BulkToggleTaxStatusCommand(
$idsByReference,
$expectedStatus
));

foreach ($idsByReference as $reference => $id) {
SharedStorage::getStorage()->set($reference, new Tax($id));
}
}

/**
* @When I delete tax :taxReference
*/
public function deleteTax($taxReference)
{
$tax = SharedStorage::getStorage()->get($taxReference);
$taxId = (int) $tax->id;

$this->getCommandBus()->handle(new DeleteTaxCommand($taxId));
}

/**
* @When I delete taxes: :taxReferences in bulk action
*/
public function bulkDeleteTax($taxReferences)
{
foreach (PrimitiveUtils::castStringArrayIntoArray($taxReferences) as $taxReference) {
$tax = SharedStorage::getStorage()->get($taxReference);
$taxIds[] = (int) $tax->id;
}

$this->getCommandBus()->handle(new BulkDeleteTaxCommand($taxIds));
}

/**
* @Then taxes: :taxReferences should be deleted
*/
public function assertTaxesAreDeleted($taxReferences)
{
foreach (PrimitiveUtils::castStringArrayIntoArray($taxReferences) as $taxReference) {
$this->assertTaxIsDeleted($taxReference);
}
}

/**
* @Then tax :taxReference should be deleted
*/
public function assertTaxIsDeleted($taxReference)
{
$tax = SharedStorage::getStorage()->get($taxReference);
$taxId = (int) $tax->id;
try {
$this->getQueryBus()->handle(new GetTaxForEditing($taxId));

throw new NoExceptionAlthoughExpectedException(sprintf(
'Tax %s expected to be deleted, but it was found',
$taxReference
));
} catch (TaxNotFoundException $e) {
SharedStorage::getStorage()->clear($taxReference);
}
}

/**
* @Then tax :taxReference name in default language should be :name
*/
public function assertTaxNameInDefaultLang($taxReference, $name)
{
/** @var Tax $tax */
$tax = SharedStorage::getStorage()->get($taxReference);

if ($tax->name[$this->defaultLangId] !== $name) {
throw new RuntimeException(sprintf(
'Tax "%s" has "%s" name, but "%s" was expected.',
$taxReference,
$tax->name,
$name
));
}
}

/**
* @Then tax :taxReference rate should be :rate
*/
public function assertTaxRate($taxReference, $rate)
{
/** @var Tax $tax */
$tax = SharedStorage::getStorage()->get($taxReference);

if ($tax->rate !== $rate) {
throw new RuntimeException(sprintf(
'Tax "%s" has "%s" rate, but "%s" was expected.',
$taxReference,
$tax->rate,
$rate
));
}
}

/**
* @Then /^taxes: "(.*)" should be (enabled|disabled)?$/
*/
public function assertTaxesStatus($taxReferences, $status)
{
$taxReferences = PrimitiveUtils::castStringArrayIntoArray($taxReferences);

foreach ($taxReferences as $reference) {
$this->assertTaxStatus($reference, $status);
}
}

/**
* @Then /^tax "(.*)" should be (enabled|disabled)?$/
* @Given /^tax "(.*)" is (enabled|disabled)?$/
*/
public function assertTaxStatus($taxReference, $status)
{
/** @var Tax $tax */
$tax = SharedStorage::getStorage()->get($taxReference);
$isEnabled = $status === 'enabled';
$actualStatus = (bool) $tax->active;

if ($isEnabled !== $actualStatus) {
throw new RuntimeException(sprintf(
'Tax "%s" is %s, but it was expected to be %s',
$taxReference,
$actualStatus ? 'enabled' : 'disabled',
$status
));
}
}

/**
* @param $taxReference
* @param array $data
*/
private function createTaxUsingCommand($taxReference, array $data)
{
$command = new AddTaxCommand(
[$this->defaultLangId => $data['name']],
$data['rate'],
PrimitiveUtils::castStringBooleanIntoBoolean($data['is_enabled'])
);

/** @var TaxId $taxId */
$taxId = $this->getCommandBus()->handle($command);

SharedStorage::getStorage()->set($taxReference, new Tax($taxId->getValue()));
}
}
@@ -0,0 +1,64 @@
@reset-database-before-feature
#./vendor/bin/behat -c tests/Integration/Behaviour/behat.yml -s tax
Feature: Manage tax
As an employee
I must be able to add, edit and delete taxes

Scenario: Adding new tax
When I add new tax "sales-tax" with following properties:
| name | my sales tax 500 |
| rate | 0.5 |
| is_enabled | true |
Then tax "sales-tax" name in default language should be "my sales tax 500"
And tax "sales-tax" rate should be 0.500
And tax "sales-tax" should be enabled

Scenario: Editing tax
When I edit tax "sales-tax" with following properties:
| name | my sales tax 300 |
| rate | 0.15 |
| is_enabled | false |
Then tax "sales-tax" name in default language should be "my sales tax 300"
And tax "sales-tax" rate should be 0.150
And tax "sales-tax" should be disabled

Scenario: It is possible to modify only the name of a tax, without modifying anything else
When I edit tax "sales-tax" with following properties:
| name | tax for fun |
Then tax "sales-tax" name in default language should be "tax for fun"
And tax "sales-tax" rate should be 0.150
And tax "sales-tax" should be disabled

Scenario: Enabling tax status
Given tax "sales-tax" is disabled
When I enable tax "sales-tax"
Then tax "sales-tax" should be enabled

Scenario: Deleting tax right after disabling its status
When I disable tax "sales-tax"
Then tax "sales-tax" should be disabled
When I delete tax "sales-tax"
Then tax "sales-tax" should be deleted

Scenario: Disabling multiple taxes in bulk action
When I add new tax "beard-tax" with following properties:
| name | Beard tax |
| rate | 0.1 |
| is_enabled | true |
And I add new tax "state-tax" with following properties:
| name | State tax |
| rate | 15.23 |
| is_enabled | true |
And I add new tax "pvm-tax" with following properties:
| name | PVM |
| rate | 99.9 |
| is_enabled | false |
When I disable taxes: "beard-tax, state-tax, pvm-tax"
Then taxes: "beard-tax, state-tax" should be disabled

Scenario: Deleting multiple taxes right after their status was enabled
When I enable taxes: "beard-tax, state-tax, pvm-tax"
Then taxes: "beard-tax, state-tax, pvm-tax" should be enabled
When I delete taxes: "beard-tax, state-tax, pvm-tax" in bulk action
Then taxes: "beard-tax, state-tax, pvm-tax" should be deleted

7 changes: 7 additions & 0 deletions tests/Integration/Behaviour/behat.yml
Expand Up @@ -72,9 +72,16 @@ default:
- Tests\Integration\Behaviour\Features\Context\ShopFeatureContext
- Tests\Integration\Behaviour\Features\Context\Domain\WebserviceKeyFeatureContext
- Tests\Integration\Behaviour\Features\Context\WebserviceKeyFeatureContext
tax:
paths:
features: Features/Scenario/Tax
contexts:
- Tests\Integration\Behaviour\Features\Context\Domain\TaxFeatureContext
- Tests\Integration\Behaviour\Features\Context\CommonFeatureContext
manufacturer:
paths:
features: Features/Scenario/Manufacturer
contexts:
- Tests\Integration\Behaviour\Features\Context\CommonFeatureContext
- Tests\Integration\Behaviour\Features\Context\Domain\ManufacturerFeatureContext