Skip to content

Commit

Permalink
Adding behat tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JevgenijVisockij committed Feb 23, 2023
1 parent cfcac21 commit e01842f
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,117 @@

use Behat\Gherkin\Node\TableNode;
use PHPUnit\Framework\Assert;
use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Command\AddAttributeGroupCommand;
use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Command\EditAttributeGroupCommand;
use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Query\GetAttributeGroupForEditing;
use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\ValueObject\AttributeGroupId;
use PrestaShop\PrestaShop\Core\Domain\Manufacturer\Query\GetManufacturerForEditing;
use PrestaShop\PrestaShop\Core\Domain\Product\AttributeGroup\Command\DeleteAttributeGroupCommand;
use PrestaShop\PrestaShop\Core\Domain\Product\AttributeGroup\Exception\AttributeGroupNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\Product\AttributeGroup\Query\GetAttributeGroupList;
use PrestaShop\PrestaShop\Core\Domain\Product\AttributeGroup\Query\GetProductAttributeGroups;
use PrestaShop\PrestaShop\Core\Domain\Product\AttributeGroup\QueryResult\AttributeGroup;
use PrestaShop\PrestaShop\Core\Domain\Shop\ValueObject\ShopConstraint;
use RuntimeException;
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 AttributeGroupFeatureContext extends AbstractDomainFeatureContext
{
/**
* @var int default language id from configs
*/
private $defaultLangId;

/**
* @var int default shop id from configs
*/
private $defaultShopId;

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

/**
* @When I create attribute group :reference with specified properties:
*/
public function createAttributeGroup(string $reference, TableNode $node): void
{
$properties = $node->getRowsHash();
$attributeGroupId = $this->createAttributeGroupUsingCommand($properties['name'], $properties['public_name'], $properties['type']);

$this->getSharedStorage()->set($reference, $attributeGroupId->getValue());
}

/**
* @When I edit attribute group :reference with specified properties:
*/
public function editAttributeGroup(string $reference, TableNode $node): void
{
$attributeGroupId = SharedStorage::getStorage()->get($reference);

$properties = $node->getRowsHash();
$this->editAttributeGroupUsingCommand($attributeGroupId, $properties['name'], $properties['public_name'], $properties['type']);
}

/**
* @When I delete attribute group :reference
*/
public function deleteManufacturer(string $reference): void
{
$attributeGroupId = SharedStorage::getStorage()->get($reference);

$this->getCommandBus()->handle(new DeleteAttributeGroupCommand($attributeGroupId));
}

/**
* @Then attribute group :reference should be deleted
*/
public function assertAttributeGroupIsDeleted(string $reference): void
{
$attributeGroupId = SharedStorage::getStorage()->get($reference);

try {
$this->getQueryBus()->handle(new GetAttributeGroupForEditing($attributeGroupId));

throw new NoExceptionAlthoughExpectedException(sprintf('Attribute group %s exists, but it was expected to be deleted', $reference));
} catch (AttributeGroupNotFoundException $e) {
SharedStorage::getStorage()->clear($reference);
}
}


/**
* @Then attribute group :reference :field should be :value
*/
public function assertFieldValue(string $reference, string $field, string $value): void
{
$attributeGroupId = SharedStorage::getStorage()->get($reference);
$attributeGroup = new \AttributeGroup($attributeGroupId);

if ($attributeGroup->$field !== $value) {
throw new RuntimeException(sprintf('Attribute group "%s" has "%s" %s, but "%s" was expected.', $reference, $attributeGroup->$field, $field, $value));
}
}

/**
* @Then attribute group :reference :field in default language should be :value
*/
public function assertFieldWithLangValue(string $reference, string $field, string $value): void
{
$attributeGroupId = SharedStorage::getStorage()->get($reference);
$attributeGroup = new \AttributeGroup($attributeGroupId);

if ($attributeGroup->$field[$this->defaultLangId] !== $value) {
throw new RuntimeException(sprintf('Attribute group "%s" has "%s" %s, but "%s" was expected.', $reference, $attributeGroup->$field[$this->defaultLangId], $field, $value));
}
}

/**
* @Given there is a list of following attribute groups:
*
Expand Down Expand Up @@ -421,4 +523,48 @@ private function performAttributesInGroupAssertion(?TableNode $tableNode, array
Assert::assertEquals($expectedId, $attribute->getAttributeId());
}
}

/**
* @param string $nameInDefaultLanguage
* @param string $publicNameInDefaultLanguage
* @param string $type
* @return AttributeGroupId
* @throws \PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Exception\AttributeGroupConstraintException
*/
private function createAttributeGroupUsingCommand(string $nameInDefaultLanguage, string $publicNameInDefaultLanguage, string $type): AttributeGroupId
{
$command = new AddAttributeGroupCommand(
[$this->defaultLangId => $nameInDefaultLanguage],
[$this->defaultLangId => $publicNameInDefaultLanguage],
$type,
[$this->defaultShopId]
);

return $this->getCommandBus()->handle($command);
}

/**
* @param string $nameInDefaultLanguage
* @param string $publicNameInDefaultLanguage
* @param string $type
* @return AttributeGroupId
* @throws \PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Exception\AttributeGroupConstraintException
*/
private function editAttributeGroupUsingCommand(
int $attributeGroupId,
string $nameInDefaultLanguage,
string $publicNameInDefaultLanguage,
string $type
): AttributeGroupId
{
$command = new EditAttributeGroupCommand(
$attributeGroupId,
[$this->defaultLangId => $nameInDefaultLanguage],
[$this->defaultLangId => $publicNameInDefaultLanguage],
$type,
[$this->defaultShopId]
);

return $this->getCommandBus()->handle($command);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# ./vendor/bin/behat -c tests/Integration/Behaviour/behat.yml -s attribute_group
@restore-all-tables-before-feature
@feature-management
Feature: Attribute group management
PrestaShop allows BO users to manage product attribute groups
As a BO user
I must be able to create, edit and delete product features

Background:
Given shop "shop1" with name "test_shop" exists

Scenario: Adding new attribute group
When I create attribute group "attributeGroup1" with specified properties:
| name | Color |
| public_name | Public color |
| type | radio |
Then attribute group "attributeGroup1" "name" in default language should be "Color"
And attribute group "attributeGroup1" "public_name" in default language should be "Public color"
And attribute group "attributeGroup1" "group_type" should be "radio"

Scenario: Editing attribute group
When I edit attribute group "attributeGroup1" with specified properties:
| name | Colour |
| public_name | Public colour |
| type | color |
Then attribute group "attributeGroup1" "name" in default language should be "Colour"
And attribute group "attributeGroup1" "public_name" in default language should be "Public colour"
And attribute group "attributeGroup1" "group_type" should be "color"

Scenario: Deleting attribute group
When I delete attribute group "attributeGroup1"
Then attribute group "attributeGroup1" should be deleted
9 changes: 9 additions & 0 deletions tests/Integration/Behaviour/behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,15 @@ default:
- Tests\Integration\Behaviour\Features\Context\LanguageFeatureContext
- Tests\Integration\Behaviour\Features\Context\ShopFeatureContext
- Tests\Integration\Behaviour\Features\Transform\LocalizedArrayTransformContext
attribute_group:
paths:
- "%paths.base%/Features/Scenario/AttributeGroup"
contexts:
- Tests\Integration\Behaviour\Features\Context\CommonFeatureContext
- Tests\Integration\Behaviour\Features\Context\Domain\AttributeGroupFeatureContext
- Tests\Integration\Behaviour\Features\Context\LanguageFeatureContext
- Tests\Integration\Behaviour\Features\Context\ShopFeatureContext
- Tests\Integration\Behaviour\Features\Transform\LocalizedArrayTransformContext
order_message:
paths:
- "%paths.base%/Features/Scenario/OrderMessage"
Expand Down

0 comments on commit e01842f

Please sign in to comment.