Skip to content

Commit

Permalink
Fix typecasting in GetTaxForEditingHandler and add bulk actions behats
Browse files Browse the repository at this point in the history
  • Loading branch information
zuk3975 committed Jul 17, 2019
1 parent b74cc2a commit 13d0cd4
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 10 deletions.
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
);
}
}
Expand Up @@ -30,8 +30,11 @@
use Configuration;
use Exception;
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\TaxException;
use PrestaShop\PrestaShop\Core\Domain\Tax\Exception\TaxNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\Tax\Query\GetTaxForEditing;
Expand Down Expand Up @@ -113,14 +116,49 @@ public function editTaxUsingCommand($taxReference, TableNode $table)
SharedStorage::getStorage()->set($taxReference, new Tax($taxId));
}

/**
* @When I toggle tax :taxReference status
*/
public function toggleStatus($taxReference)
{
/** @var Tax $tax */
$tax = SharedStorage::getStorage()->get($taxReference);
$taxId = (int) $tax->id;
$expectedStatus = !(bool) $tax->active;

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

/**
* @When I :action taxes with ids: :ids
*/
public function bulkToggleStatusByIds($action, $ids)
{
$expectedStatus = 'enable' === $action ? true : false;

$this->getCommandBus()->handle(new BulkToggleTaxStatusCommand(
array_map('intval', explode(',', $ids)),
$expectedStatus
));
}

/**
* @When I delete tax with id :id
*/
public function deleteTaxUsingCommand($id)
public function deleteTaxById($id)
{
$command = new DeleteTaxCommand((int) $id);
$this->getCommandBus()->handle(new DeleteTaxCommand((int) $id));
}

$this->getCommandBus()->handle($command);
/**
* @When I delete taxes with ids: :ids in bulk action
*/
public function bulkDeleteTaxesByIds($ids)
{
$this->getCommandBus()->handle(
new BulkDeleteTaxCommand(array_map('intval', explode(',', $ids)))
);
}

/**
Expand Down Expand Up @@ -160,9 +198,38 @@ public function assertTaxRate($taxReference, $rate)
}

/**
* @Then Tax with id :id should not exist
* @Then taxes with ids: :ids should be :status
*/
public function assertTaxByIdShouldNotExist($id)
public function assertStatusByIds($ids, $status)
{
$isEnabled = 'enabled' === $status ? true : false;
foreach (array_map('intval', explode(',', $ids)) as $id) {
$editableTax = $this->getQueryBus()->handle(new GetTaxForEditing($id));
if ($isEnabled !== $editableTax->isActive()) {
throw new RuntimeException(sprintf(
'Tax with id "%s" is %s, but expected to be %s',
$id,
$editableTax->isActive ? 'enabled' : 'disabled',
$status
));
}
}
}

/**
* @Then taxes with ids: :ids should not be found
*/
public function assertTaxesByIdsShouldNotBeFound($ids)
{
foreach (explode(',', $ids) as $id) {
$this->assertTaxByIdShouldNotBeFound($id);
}
}

/**
* @Then tax with id :id should not be found
*/
public function assertTaxByIdShouldNotBeFound($id)
{
try {
$query = new GetTaxForEditing((int) $id);
Expand All @@ -174,7 +241,17 @@ public function assertTaxByIdShouldNotExist($id)
}

/**
* @Given Tax with id :id exists
* @Given taxes with ids: :ids exists
*/
public function assertTaxesExistsByIds($ids)
{
foreach (explode(',', $ids) as $id) {
$this->assertTaxExistsById($id);
}
}

/**
* @Given tax with id :id exists
*/
public function assertTaxExistsById($id)
{
Expand Down
Expand Up @@ -33,7 +33,28 @@ Feature: Manage tax
And tax "tax-1" rate should be 0.150
And tax "tax-1" should be disabled

Scenario: Toggling tax status
Given tax "tax-1" should be disabled
When I toggle tax "tax-1" status
Then tax "tax-1" should be enabled

Scenario: Disabling taxes status in bulk action
Given taxes with ids: "1,2,3" exists
And taxes with ids: "1,2,3" should be enabled
When I disable taxes with ids: "1,2,3"
Then taxes with ids: "1,2,3" should be disabled

Scenario: Enabling taxes status in bulk action
And taxes with ids: "1,2,3" should be disabled
When I enable taxes with ids: "1,2,3"
Then taxes with ids: "1,2,3" should be enabled

Scenario: Deleting tax
Given Tax with id "2" exists
Given tax with id "2" exists
When I delete tax with id "2"
Then Tax with id "2" should not exist
Then tax with id "2" should not be found

Scenario: Deleting taxes in bulk action
Given taxes with ids: "3,4,5" exists
When I delete taxes with ids: "3,4,5" in bulk action
Then taxes with ids: "3,4,5" should not be found

0 comments on commit 13d0cd4

Please sign in to comment.