[API] Finish covering viewing product attributes

This commit is contained in:
Jan Goralski 2023-12-12 16:08:13 +01:00
parent d9f9022660
commit 46f3afb02b
No known key found for this signature in database
GPG key ID: 95D91BA380F31EDD
6 changed files with 87 additions and 50 deletions

View file

@ -17,24 +17,24 @@ Feature: Viewing product's select attributes
Then I should see the product attribute "T-Shirt material" with value "Banana skin" on the list
And I should also see the product attribute "T-Shirt material" with value "Cotton" on the list
@ui
@ui @api
Scenario: Viewing a detailed page with product's select attribute after changing a value
Given this product has select attribute "T-Shirt material" with values "Banana skin" and "Cotton"
When the administrator changes this product attribute's value "Cotton" to "Orange skin"
When this product attribute's value changed from "Cotton" to "Orange skin"
And I check this product's details
Then I should see the product attribute "T-Shirt material" with value "Banana skin" on the list
And I should also see the product attribute "T-Shirt material" with value "Orange skin" on the list
@ui @javascript
@ui @javascript @api @no-postgres
Scenario: Viewing a detailed page with product's select attribute after removing an only value
Given this product has select attribute "T-Shirt material" with value "Cotton"
When the administrator deletes the value "Cotton" from this product attribute
When this product attribute's value "Cotton" has been removed
And I check this product's details
Then I should not see the product attribute "T-Shirt material"
@ui @javascript
@ui @javascript @api @no-postgres
Scenario: Viewing a detailed page with product's select attribute after removing one of the value
Given this product has select attribute "T-Shirt material" with values "Banana skin" and "Cotton"
When the administrator deletes the value "Cotton" from this product attribute
When this product attribute's value "Cotton" has been removed
And I check this product's details
Then I should see the product attribute "T-Shirt material" with value "Banana skin"

View file

@ -37,8 +37,15 @@ final class ProductAttributeContext implements Context
public function iShouldSeeTheProductAttributeWithValue(string $attributeName, string $expectedAttribute): void
{
$attribute = $this->getAttributeByName($attributeName);
$attributeValue = $attribute['value'];
Assert::same($attribute['value'], $expectedAttribute);
if (is_array($attributeValue)) {
Assert::inArray($expectedAttribute, $attributeValue);
return;
}
Assert::same($attributeValue, $expectedAttribute);
}
/**
@ -71,6 +78,14 @@ final class ProductAttributeContext implements Context
Assert::inArray($expectedAttribute, $attribute['value']);
}
/**
* @Then I should not see the product attribute :attributeName
*/
public function iShouldNotSeeTheProductAttribute(string $attributeName): void
{
Assert::false($this->hasAttributeByName($attributeName));
}
/**
* @Then I should (also) see the product attribute :attributeName with date :expectedAttribute
*/
@ -113,6 +128,17 @@ final class ProductAttributeContext implements Context
Assert::same($attribute['name'], $name);
}
private function hasAttributeByName(string $name): bool
{
foreach ($this->getAttributes() as $attribute) {
if ($attribute['name'] === $name) {
return true;
}
}
return false;
}
private function getAttributeByName(string $name): array
{
foreach ($this->getAttributes() as $attribute) {

View file

@ -423,6 +423,59 @@ final class ProductAttributeContext implements Context
$this->objectManager->flush();
}
/**
* @When /^(this product attribute)'s value changed from "([^"]+)" to "([^"]+)"$/
*/
public function thisAttributeValueChangedFromTo(
ProductAttributeInterface $attribute,
string $from,
string $to,
): void {
$configuration = $attribute->getConfiguration();
$choices = $configuration['choices'] ?? [];
foreach ($choices as $uuid => $choice) {
foreach ($choice as $localeCode => $item) {
if ($item === $from) {
$choices[$uuid][$localeCode] = $to;
break 2;
}
}
}
$configuration['choices'] = $choices;
$attribute->setConfiguration($configuration);
$this->objectManager->flush();
}
/**
* @When /^(this product attribute)'s value "([^"]+)" has been removed$/
*/
public function thisAttributeValueHasBeenRemoved(
ProductAttributeInterface $attribute,
string $value,
): void {
$configuration = $attribute->getConfiguration();
$choices = $configuration['choices'] ?? [];
foreach ($choices as $uuid => $choice) {
foreach ($choice as $item) {
if ($value === $item) {
unset($choices[$uuid]);
break 2;
}
}
}
$configuration['choices'] = $choices;
$attribute->setConfiguration($configuration);
$this->objectManager->flush();
}
private function createProductAttribute(
string $type,
string $name,

View file

@ -18,8 +18,6 @@ use Sylius\Behat\Page\Admin\Crud\IndexPageInterface;
use Sylius\Behat\Page\Admin\ProductAttribute\CreatePageInterface;
use Sylius\Behat\Page\Admin\ProductAttribute\UpdatePageInterface;
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
use Sylius\Behat\Service\SharedSecurityServiceInterface;
use Sylius\Component\Core\Model\AdminUserInterface;
use Sylius\Component\Product\Model\ProductAttributeInterface;
use Webmozart\Assert\Assert;
@ -30,7 +28,6 @@ final class ManagingProductAttributesContext implements Context
private IndexPageInterface $indexPage,
private UpdatePageInterface $updatePage,
private CurrentPageResolverInterface $currentPageResolver,
private SharedSecurityServiceInterface $sharedSecurityService,
) {
}
@ -233,25 +230,6 @@ final class ManagingProductAttributesContext implements Context
$this->indexPage->open();
}
/**
* @When /^(the administrator) changes (this product attribute)'s value "([^"]*)" to "([^"]*)"$/
*/
public function theAdministratorChangesThisProductAttributesValueTo(
AdminUserInterface $user,
ProductAttributeInterface $productAttribute,
string $oldValue,
string $newValue,
): void {
$this->sharedSecurityService->performActionAsAdminUser(
$user,
function () use ($productAttribute, $oldValue, $newValue) {
$this->iWantToEditThisAttribute($productAttribute);
$this->iChangeItsValueTo($oldValue, $newValue);
$this->iSaveMyChanges();
},
);
}
/**
* @When I specify its min length as :min
* @When I specify its min entries value as :min
@ -286,24 +264,6 @@ final class ManagingProductAttributesContext implements Context
// Intentionally left blank to fulfill context expectation
}
/**
* @When /^(the administrator) deletes the value "([^"]+)" from (this product attribute)$/
*/
public function theAdministratorDeletesTheValueFromThisProductAttribute(
AdminUserInterface $user,
string $value,
ProductAttributeInterface $productAttribute,
): void {
$this->sharedSecurityService->performActionAsAdminUser(
$user,
function () use ($productAttribute, $value) {
$this->iWantToEditThisAttribute($productAttribute);
$this->iDeleteValue($value);
$this->iSaveMyChanges();
},
);
}
/**
* @When I check (also) the :productAttributeName product attribute
*/

View file

@ -207,7 +207,6 @@
<argument type="service" id="sylius.behat.page.admin.product_attribute.index" />
<argument type="service" id="sylius.behat.page.admin.product_attribute.update" />
<argument type="service" id="sylius.behat.current_page_resolver" />
<argument type="service" id="sylius.behat.shared_security" />
</service>
<service id="sylius.behat.context.ui.admin.managing_product_options" class="Sylius\Behat\Context\Ui\Admin\ManagingProductOptionsContext">

View file

@ -18,8 +18,7 @@ default:
- sylius.behat.context.transform.taxon
- Sylius\Behat\Context\Transform\CatalogPromotionContext
# - sylius.behat.context.setup.admin_user
# - sylius.behat.context.setup.admin_api_security
- sylius.behat.context.setup.admin_user
- sylius.behat.context.setup.channel
- sylius.behat.context.setup.customer
- sylius.behat.context.setup.locale