Cover scenarios in sorting_product_variants_within_product_by_position.feature

This commit is contained in:
Jacob Tobiasz 2023-12-12 11:46:27 +01:00 committed by Grzegorz Sadowski
parent 9cf2423b53
commit 905ec5e240
No known key found for this signature in database
GPG key ID: EF87FF4E6E3BD364
6 changed files with 153 additions and 8 deletions

View file

@ -12,44 +12,58 @@ Feature: Sorting listed product variants from a product by position
And this product has also an "Opel Insignia Sedan" variant at position 1
And I am logged in as an administrator
@ui
@ui @api
Scenario: Product variants are sorted by position in ascending order by default
When I view all variants of the product "Opel Insignia"
Then I should see 3 variants in the list
And the first variant in the list should have name "Opel Insignia Hatchback"
And the last variant in the list should have name "Opel Insignia Sports Tourer"
@ui
@ui @api
Scenario: Sorting product variants in descending order
When I view all variants of the product "Opel Insignia"
And I start sorting variants by position
Then the first variant in the list should have name "Opel Insignia Sports Tourer"
And the last variant in the list should have name "Opel Insignia Hatchback"
@ui
@ui @api
Scenario: New product variant with no position is added as the last one
Given the product "Opel Insignia" has also an "Opel Insignia Country Tourer" variant
When I view all variants of the product "Opel Insignia"
Then I should see 4 variants in the list
And the last variant in the list should have name "Opel Insignia Country Tourer"
@ui
@ui @api
Scenario: New product variant with position 0 is added as the first one
Given the product "Opel Insignia" has also an "Opel Insignia Country Tourer" variant at position 0
When I view all variants of the product "Opel Insignia"
Then I should see 4 variants in the list
And the first variant in the list should have name "Opel Insignia Country Tourer"
@ui @javascript
@ui @javascript @no-api
Scenario: Setting product variant as the first one in the list
When I view all variants of the product "Opel Insignia"
And I set the position of "Opel Insignia Sedan" to 0
And I save my new configuration
And the first variant in the list should have name "Opel Insignia Sedan"
Then the first variant in the list should have name "Opel Insignia Sedan"
@ui @javascript
@api @no-ui
Scenario: Setting product variant as the first one in the list
When I set the position of "Opel Insignia Sedan" to 0
And I save my new configuration
And I view all variants of the product "Opel Insignia"
Then the first variant in the list should have name "Opel Insignia Sedan"
@ui @javascript @no-api
Scenario: Setting product variant as the last one in the list
When I view all variants of the product "Opel Insignia"
And I set the position of "Opel Insignia Sedan" to 7
And I save my new configuration
And the last variant in the list should have name "Opel Insignia Sedan"
Then the last variant in the list should have name "Opel Insignia Sedan"
@api @no-ui
Scenario: Setting product variant as the last one in the list
When I set the position of "Opel Insignia Sedan" to 7
And I save my new configuration
And I view all variants of the product "Opel Insignia"
Then the last variant in the list should have name "Opel Insignia Sedan"

View file

@ -0,0 +1,117 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* 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\Admin;
use ApiPlatform\Api\IriConverterInterface;
use Behat\Behat\Context\Context;
use Sylius\Behat\Client\ApiClientInterface;
use Sylius\Behat\Client\ResponseCheckerInterface;
use Sylius\Behat\Context\Api\Resources;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Webmozart\Assert\Assert;
final class BrowsingProductVariantsContext implements Context
{
public function __construct (
private ApiClientInterface $client,
private ResponseCheckerInterface $responseChecker,
private IriConverterInterface $iriConverter,
) {
}
/**
* @When I view all variants of the product :product
*/
public function iViewAllVariantsOfTheProduct(ProductInterface $product): void
{
$this->client->index(
Resources::PRODUCT_VARIANTS,
[
'product' => $this->iriConverter->getIriFromResource($product),
'order[position]' => 'asc',
],
);
}
/**
* @When I start sorting variants by position
*/
public function iSortProductsByPosition(): void
{
$this->client->index(
Resources::PRODUCT_VARIANTS,
[
'order[position]' => 'desc',
],
);
}
/**
* @When I set the position of :productVariant to :position
*/
public function iSetThePositionOfTo(ProductVariantInterface $productVariant, int $position): void
{
$this->client->buildUpdateRequest(Resources::PRODUCT_VARIANTS, $productVariant->getCode());
$this->client->updateRequestData(['position' => $position]);
}
/**
* @When I save my new configuration
*/
public function iSaveMyNewConfiguration(): void
{
$this->client->update();
}
/**
* @Then the first variant in the list should have name :variantName
*/
public function theFirstVariantInTheListShouldHaveName(string $variantName): void
{
$variants = $this->responseChecker->getCollection($this->client->getLastResponse());
$firstVariant = reset($variants);
Assert::same(
$firstVariant['translations']['en_US']['name'],
$variantName,
sprintf(
'Expected first variant to have name "%s", but it is named "%s".',
$variantName,
$firstVariant['translations']['en_US']['name'],
),
);
}
/**
* @Then the last variant in the list should have name :variantName
*/
public function theLastVariantInTheListShouldHaveName(string $variantName): void
{
$variants = $this->responseChecker->getCollection($this->client->getLastResponse());
$lastVariant = end($variants);
Assert::same(
$lastVariant['translations']['en_US']['name'],
$variantName,
sprintf(
'Expected last variant to have name "%s", but it is named "%s".',
$variantName,
$lastVariant['translations']['en_US']['name'],
),
);
}
}

View file

@ -40,6 +40,12 @@
<argument type="service" id="api_platform.iri_converter" />
</service>
<service id="Sylius\Behat\Context\Api\Admin\BrowsingProductVariantsContext">
<argument type="service" id="sylius.behat.api_platform_client.admin" />
<argument type="service" id="Sylius\Behat\Client\ResponseCheckerInterface" />
<argument type="service" id="api_platform.iri_converter" />
</service>
<service id="Sylius\Behat\Context\Api\Admin\CreatingProductVariantContext">
<argument type="service" id="sylius.behat.api_platform_client.admin" />
<argument type="service" id="api_platform.iri_converter" />

View file

@ -40,6 +40,7 @@ default:
- sylius.behat.context.api.admin.managing_product_variants
- sylius.behat.context.api.admin.response
- sylius.behat.context.api.admin.save
- Sylius\Behat\Context\Api\Admin\BrowsingProductVariantsContext
- Sylius\Behat\Context\Api\Admin\ChannelPricingLogEntryContext
filters:

View file

@ -57,6 +57,7 @@
<attribute name="method">GET</attribute>
<attribute name="path">/admin/product-variants</attribute>
<attribute name="filters">
<attribute>sylius.api.product_variant_position_filter</attribute>
<attribute>sylius.api.product_variant_product_filter</attribute>
<attribute>Sylius\Bundle\ApiBundle\Filter\Doctrine\ProductVariantCatalogPromotionFilter</attribute>
</attribute>

View file

@ -389,5 +389,11 @@
<tag name="api_platform.filter" />
</service>
<service id="sylius.api.product_variant_position_filter" parent="api_platform.doctrine.orm.order_filter" public="true">
<argument type="collection">
<argument key="position" />
</argument>
<tag name="api_platform.filter" />
</service>
</services>
</container>