[Behat] Extract steps from ProductContext to ProductAttributeContext

This commit is contained in:
Kevin Kaniaburka 2022-05-11 11:41:59 +02:00
parent 54dd3a5f58
commit 5ebde2272f
8 changed files with 251 additions and 201 deletions

View file

@ -0,0 +1,120 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Behat\Context\Api\Shop;
use Behat\Behat\Context\Context;
use Sylius\Behat\Service\SharedStorageInterface;
use Webmozart\Assert\Assert;
use Webmozart\Assert\InvalidArgumentException;
final class ProductAttributeContext implements Context
{
public function __construct(
private SharedStorageInterface $sharedStorage,
) {
}
/**
* @Then I should (also) see the product attribute :attributeName with value :expectedAttribute
*/
public function iShouldSeeTheProductAttributeWithValue(string $attributeName, string $expectedAttribute): void
{
$attribute = $this->getAttributeByName($attributeName);
Assert::same($attribute['value'], $expectedAttribute);
}
/**
* @Then /^I should(?:| also) see the product attribute "([^"]+)" with (positive|negative) value$/
*/
public function iShouldSeeTheProductAttributeWithBoolean(string $attributeName, string $expectedAttribute): void
{
$attribute = $this->getAttributeByName($attributeName);
Assert::same($attribute['value'], 'positive' === $expectedAttribute);
}
/**
* @Then /^I should(?:| also) see the product attribute "([^"]+)" with value ([^"]+)%$/
*/
public function iShouldSeeTheProductAttributeWithPercentage(string $attributeName, int $expectedAttribute): void
{
$attribute = $this->getAttributeByName($attributeName);
Assert::same($attribute['value'], $expectedAttribute / 100);
}
/**
* @Then I should (also) see the product attribute :attributeName with value :expectedAttribute on the list
*/
public function iShouldSeeTheProductAttributeWithValueOnTheList(string $attributeName, string $expectedAttribute): void
{
$attribute = $this->getAttributeByName($attributeName);
Assert::inArray($expectedAttribute, $attribute['value']);
}
/**
* @Then I should (also) see the product attribute :attributeName with date :expectedAttribute
*/
public function iShouldSeeTheProductAttributeWithDate(string $attributeName, string $expectedAttribute): void
{
$attribute = $this->getAttributeByName($attributeName);
Assert::true(new \DateTime($attribute['value']) == new \DateTime($expectedAttribute));
}
/**
* @Then I should see :count attributes
*/
public function iShouldSeeAttributes(int $count): void
{
Assert::count($this->sharedStorage->get('product_attributes'), $count);
}
/**
* @Then the first attribute should be :name
*/
public function theFirstAttributeShouldBe(string $name): void
{
$attributes = $this->sharedStorage->get('product_attributes');
$attribute = reset($attributes);
Assert::isArray($attribute);
Assert::same($attribute['name'], $name);
}
/**
* @Then the last attribute should be :name
*/
public function theLastAttributeShouldBe(string $name): void
{
$attributes = $this->sharedStorage->get('product_attributes');
$attribute = end($attributes);
Assert::isArray($attribute);
Assert::same($attribute['name'], $name);
}
private function getAttributeByName(string $name): array
{
foreach ($this->sharedStorage->get('product_attributes') as $attribute) {
if ($attribute['name'] === $name) {
return $attribute;
}
}
throw new InvalidArgumentException('Expected a value other than null.');
}
}

View file

@ -29,7 +29,6 @@ use Sylius\Component\Product\Model\ProductVariantInterface;
use Sylius\Component\Taxonomy\Model\TaxonInterface;
use Symfony\Component\HttpFoundation\Request as HttpRequest;
use Webmozart\Assert\Assert;
use Webmozart\Assert\InvalidArgumentException;
final class ProductContext implements Context
{
@ -171,99 +170,6 @@ final class ProductContext implements Context
$this->client->filter();
}
/**
* @Then I should (also) see the product attribute :attributeName with value :expectedAttribute
*/
public function iShouldSeeTheProductAttributeWithValue(string $attributeName, string $expectedAttribute): void
{
$attribute = $this->getAttributeByName($attributeName);
Assert::same(
$attribute['value'],
$expectedAttribute
);
}
/**
* @Then /^I should(?:| also) see the product attribute "([^"]+)" with (positive|negative) value$/
*/
public function iShouldSeeTheProductAttributeWithBoolean(string $attributeName, string $expectedAttribute): void
{
$attribute = $this->getAttributeByName($attributeName);
Assert::same(
$attribute['value'],
'positive' === $expectedAttribute
);
}
/**
* @Then /^I should(?:| also) see the product attribute "([^"]+)" with value ([^"]+)%$/
*/
public function iShouldSeeTheProductAttributeWithPercentage(string $attributeName, int $expectedAttribute): void
{
$attribute = $this->getAttributeByName($attributeName);
Assert::same(
$attribute['value'],
$expectedAttribute / 100
);
}
/**
* @Then I should (also) see the product attribute :attributeName with value :expectedAttribute on the list
*/
public function iShouldSeeTheProductAttributeWithValueOnTheList(string $attributeName, string $expectedAttribute): void
{
$attribute = $this->getAttributeByName($attributeName);
Assert::inArray(
$expectedAttribute,
$attribute['value']
);
}
/**
* @Then I should (also) see the product attribute :attributeName with date :expectedAttribute
*/
public function iShouldSeeTheProductAttributeWithDate(string $attributeName, string $expectedAttribute): void
{
$attribute = $this->getAttributeByName($attributeName);
Assert::true(new \DateTime($attribute['value']) == new \DateTime($expectedAttribute));
}
/**
* @Then I should see :count attributes
*/
public function iShouldSeeAttributes($count): void
{
Assert::same(
count($this->sharedStorage->get('product_attributes')),
(int) $count
);
}
/**
* @Then the first attribute should be :name
*/
public function theFirstAttributeShouldBe($name): void
{
$attributes = $this->sharedStorage->get('product_attributes');
Assert::same(reset($attributes)['name'], $name);
}
/**
* @Then the last attribute should be :name
*/
public function theLastAttributeShouldBe($name): void
{
$attributes = $this->sharedStorage->get('product_attributes');
Assert::same(end($attributes)['name'], $name);
}
/**
* @Then I should see :rating as its average rating
*/
@ -758,15 +664,4 @@ final class ProductContext implements Context
return $images[0]['type'] === 'main' && $images[0]['path'];
}
private function getAttributeByName($name): array
{
foreach ($this->sharedStorage->get('product_attributes') as $attribute) {
if ($attribute['name'] === $name) {
return $attribute;
}
}
throw new InvalidArgumentException('Expected a value other than null.');
}
}

View file

@ -0,0 +1,121 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Behat\Context\Ui\Shop;
use Behat\Behat\Context\Context;
use Behat\Mink\Element\NodeElement;
use Sylius\Behat\Page\Shop\Product\ShowPageInterface;
use Webmozart\Assert\Assert;
final class ProductAttributeContext implements Context
{
public function __construct(private ShowPageInterface $showPage)
{
}
/**
* @Then I should (also) see the product attribute :attributeName with value :expectedAttribute
*/
public function iShouldSeeTheProductAttributeWithValue(string $attributeName, string $expectedAttribute): void
{
Assert::same($this->showPage->getAttributeByName($attributeName), $expectedAttribute);
}
/**
* @Then /^I should(?:| also) see the product attribute "([^"]+)" with (positive|negative) value$/
*/
public function iShouldSeeTheProductAttributeWithBoolean(string $attributeName, string $expectedAttribute): void
{
Assert::same($this->showPage->getAttributeByName($attributeName), 'positive' === $expectedAttribute ? 'Yes' : 'No');
}
/**
* @Then I should (also) see the product attribute :attributeName with value :expectedAttribute on the list
*/
public function iShouldSeeTheProductAttributeWithValueOnTheList(string $attributeName, string $expectedAttribute): void
{
Assert::inArray($expectedAttribute, $this->showPage->getAttributeListByName($attributeName));
}
/**
* @Then I should not see the product attribute :attributeName
*/
public function iShouldNotSeeTheProductAttribute(string $attributeName): void
{
$this->showPage->getAttributeByName($attributeName);
}
/**
* @Then I should (also) see the product attribute :attributeName with date :expectedAttribute
*/
public function iShouldSeeTheProductAttributeWithDate(string $attributeName, string $expectedAttribute): void
{
Assert::eq(
new \DateTime($this->showPage->getAttributeByName($attributeName)),
new \DateTime($expectedAttribute)
);
}
/**
* @Then /^I should(?:| also) see the product attribute "([^"]+)" with value ([^"]+)%$/
*/
public function iShouldSeeTheProductAttributeWithPercentage(string $attributeName, int $expectedAttribute): void
{
Assert::eq(
$this->showPage->getAttributeByName($attributeName),
sprintf('%d %%', $expectedAttribute)
);
}
/**
* @Then I should see :count attributes
*/
public function iShouldSeeAttributes(int $count): void
{
Assert::count($this->getProductAttributes(), $count);
}
/**
* @Then the first attribute should be :name
*/
public function theFirstAttributeShouldBe(string $name): void
{
$attributes = $this->getProductAttributes();
Assert::same(reset($attributes)->getText(), $name);
}
/**
* @Then the last attribute should be :name
*/
public function theLastAttributeShouldBe(string $name): void
{
$attributes = $this->getProductAttributes();
Assert::same(end($attributes)->getText(), $name);
}
/**
* @return NodeElement[]
*
* @throws \InvalidArgumentException
*/
private function getProductAttributes(): array
{
$attributes = $this->showPage->getAttributes();
Assert::notNull($attributes, 'The product has no attributes.');
return $attributes;
}
}

View file

@ -14,7 +14,6 @@ declare(strict_types=1);
namespace Sylius\Behat\Context\Ui\Shop;
use Behat\Behat\Context\Context;
use Behat\Mink\Element\NodeElement;
use Sylius\Behat\Element\Product\IndexPage\VerticalMenuElementInterface;
use Sylius\Behat\Page\ErrorPageInterface;
use Sylius\Behat\Page\Shop\Product\IndexPageInterface;
@ -186,88 +185,6 @@ final class ProductContext implements Context
Assert::true($this->showPage->isOpen(['slug' => $product->getSlug()]));
}
/**
* @Then I should (also) see the product attribute :attributeName with value :expectedAttribute
*/
public function iShouldSeeTheProductAttributeWithValue($attributeName, $expectedAttribute): void
{
Assert::same($this->showPage->getAttributeByName($attributeName), $expectedAttribute);
}
/**
* @Then /^I should(?:| also) see the product attribute "([^"]+)" with (positive|negative) value$/
*/
public function iShouldSeeTheProductAttributeWithBoolean($attributeName, $expectedAttribute): void
{
Assert::same($this->showPage->getAttributeByName($attributeName), 'positive' === $expectedAttribute ? 'Yes' : 'No');
}
/**
* @Then I should (also) see the product attribute :attributeName with value :expectedAttribute on the list
*/
public function iShouldSeeTheProductAttributeWithValueOnTheList($attributeName, $expectedAttribute): void
{
Assert::inArray($expectedAttribute, $this->showPage->getAttributeListByName($attributeName));
}
/**
* @Then I should not see the product attribute :attributeName
*/
public function iShouldNotSeeTheProductAttribute(string $attributeName): void
{
$this->showPage->getAttributeByName($attributeName);
}
/**
* @Then I should (also) see the product attribute :attributeName with date :expectedAttribute
*/
public function iShouldSeeTheProductAttributeWithDate($attributeName, $expectedAttribute): void
{
Assert::eq(
new \DateTime($this->showPage->getAttributeByName($attributeName)),
new \DateTime($expectedAttribute)
);
}
/**
* @Then /^I should(?:| also) see the product attribute "([^"]+)" with value ([^"]+)%$/
*/
public function iShouldSeeTheProductAttributeWithPercentage(string $attributeName, int $expectedAttribute): void
{
Assert::eq(
$this->showPage->getAttributeByName($attributeName),
sprintf('%d %%', $expectedAttribute)
);
}
/**
* @Then I should see :count attributes
*/
public function iShouldSeeAttributes($count): void
{
Assert::same(count($this->getProductAttributes()), (int) $count);
}
/**
* @Then the first attribute should be :name
*/
public function theFirstAttributeShouldBe($name): void
{
$attributes = $this->getProductAttributes();
Assert::same(reset($attributes)->getText(), $name);
}
/**
* @Then the last attribute should be :name
*/
public function theLastAttributeShouldBe($name): void
{
$attributes = $this->getProductAttributes();
Assert::same(end($attributes)->getText(), $name);
}
/**
* @When I browse products from taxon :taxon
*/
@ -998,17 +915,4 @@ final class ProductContext implements Context
)
);
}
/**
* @return NodeElement[]
*
* @throws \InvalidArgumentException
*/
private function getProductAttributes(): array
{
$attributes = $this->showPage->getAttributes();
Assert::notNull($attributes, 'The product has no attributes.');
return $attributes;
}
}

View file

@ -107,6 +107,10 @@
<argument>%sylius.security.new_api_route%</argument>
</service>
<service id="sylius.behat.context.api.shop.product_attribute" class="Sylius\Behat\Context\Api\Shop\ProductAttributeContext">
<argument type="service" id="sylius.behat.shared_storage" />
</service>
<service id="sylius.behat.context.api.shop.product_variant" class="Sylius\Behat\Context\Api\Shop\ProductVariantContext">
<argument type="service" id="sylius.behat.api_platform_client.shop" />
<argument type="service" id="Sylius\Behat\Client\ResponseCheckerInterface" />

View file

@ -490,6 +490,10 @@
<argument type="service" id="sylius.behat.channel_context_setter" />
</service>
<service id="sylius.behat.context.ui.shop.product_attribute" class="Sylius\Behat\Context\Ui\Shop\ProductAttributeContext">
<argument type="service" id="sylius.behat.page.shop.product.show" />
</service>
<service id="sylius.behat.context.ui.shop.browsing_product" class="Sylius\Behat\Context\Ui\Shop\BrowsingProductContext">
<argument type="service" id="sylius.behat.page.shop.product.show" />
</service>

View file

@ -29,6 +29,7 @@ default:
- sylius.behat.context.api.shop.channel
- sylius.behat.context.api.shop.product
- sylius.behat.context.api.shop.product_attribute
- sylius.behat.context.api.shop.product_variant
filters:

View file

@ -44,6 +44,7 @@ default:
- sylius.behat.context.ui.channel
- sylius.behat.context.ui.shop.locale
- sylius.behat.context.ui.shop.product
- sylius.behat.context.ui.shop.product_attribute
- sylius.behat.context.ui.shop.browsing_product
filters: