[Behat] Refactor viewing enabled variants

This commit is contained in:
Rafikooo 2023-05-09 11:35:59 +02:00
parent 1200f8cd7b
commit 952e62f844
No known key found for this signature in database
GPG key ID: 4A26D8327BC2442B
2 changed files with 22 additions and 39 deletions

View file

@ -10,9 +10,9 @@ Feature: Viewing product's enabled variants only
And this product has option "Size" with values "Small", "Medium" and "Large"
And this product has option "Color" with values "Blue", "Green" and "Yellow"
And this product has all possible variants
But all the product variants with the "Yellow" color are disabled
But all the product variants with the "Yellow" Color are disabled
@ui @api
Scenario: Seeing only enabled variants options
When I view product "Super Cool T-Shirt"
Then I should not be able to select the "Yellow" color option value
Then I should not be able to select the "Yellow" Color option value

View file

@ -571,9 +571,9 @@ final class ProductContext implements Context
/**
* @Then /^I should not be able to select the "([^"]+)" ([^\s]+) option value$/
*/
public function iShouldNotBeAbleToSelectTheOptionValue(string $optionValue, string $optionName): void
public function iShouldNotBeAbleToSelectTheOptionValue(string $optionValueName, string $optionName): void
{
Assert::false($this->hasProductOptionWithNameAndValue($optionValue, $optionName));
Assert::false($this->hasProductOptionWithNameAndValue($optionName, $optionValueName));
}
/**
@ -705,19 +705,23 @@ final class ProductContext implements Context
return false;
}
private function hasProductOptionWithNameAndValue(string $optionValue, string $optionName): bool
private function hasProductOptionWithNameAndValue(string $expectedOptionName, string $expectedOptionValueCode): bool
{
$response = $this->client->getLastResponse();
$productOptions = $this->responseChecker->getValue($response, 'options');
foreach ($productOptions as $optionIri) {
if (!$this->hasProductOptionWithName($optionIri, $optionName)) {
continue;
}
$productVariants = $this->responseChecker->getCollection(
$this->client->index(
Resources::PRODUCT_VARIANTS,
['product' => $this->responseChecker->getValue($response, '@id')],
),
);
$variants = $this->responseChecker->getValue($response, 'variants');
foreach ($variants as $variantIri) {
if ($this->variantHasProductOptionValue($variantIri, $optionValue)) {
foreach ($productVariants as $productVariant) {
foreach ($productVariant['optionValues'] as $optionValueIri) {
$optionValue = $this->fetchItemByIri($optionValueIri);
$option = $this->fetchItemByIri($optionValue['option']);
if ($option['name'] === $expectedOptionName && $optionValue['code'] === $expectedOptionValueCode) {
return true;
}
}
@ -726,32 +730,6 @@ final class ProductContext implements Context
return false;
}
private function hasProductOptionWithName(string $optionIri, string $optionName): bool
{
$response = $this->client->showByIri($optionIri);
return $this->responseChecker->hasValue($response, 'code', StringInflector::nameToUppercaseCode($optionName));
}
private function variantHasProductOptionValue(string $variantIri, string $optionValue): bool
{
$variants = $this->client->showByIri($variantIri);
$optionValues = $this->responseChecker->getValue($variants, 'optionValues');
foreach ($optionValues as $valueIri) {
if ($this->hasProductOptionWithValue($valueIri, $optionValue)) {
return true;
}
}
return false;
}
private function hasProductOptionWithValue(string $valueIri, string $optionValue): bool
{
return $this->responseChecker->hasValue($this->client->ShowByIri($valueIri), 'code', StringInflector::nameToUppercaseCode($optionValue));
}
private function productHasProductVariantWithName(array $variants, string $variantName): bool
{
foreach ($variants as $variantIri) {
@ -813,4 +791,9 @@ final class ProductContext implements Context
return in_array($productIri, $associatedProducts, true);
}
private function fetchItemByIri(string $iri): array
{
return $this->responseChecker->getResponseContent($this->client->showByIri($iri));
}
}