Skip to content

Commit

Permalink
[Behat][API] Refactor scenarios of sorting variants to have the same …
Browse files Browse the repository at this point in the history
…for UI and API contexts
  • Loading branch information
GSadee committed Dec 15, 2023
1 parent 21cd6b6 commit 7165dea
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,16 @@ Feature: Sorting listed product variants from a product by position
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 @no-api
@ui @javascript @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
Then the first variant in the list should have name "Opel Insignia Sedan"

@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
@ui @javascript @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
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"
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
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\ProductVariantInterface;
use Webmozart\Assert\Assert;

final class BrowsingProductVariantsContext implements Context
{
Expand All @@ -27,71 +24,4 @@ public function __construct (
private ResponseCheckerInterface $responseChecker,
) {
}

/**
* @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);

$this->assertProductVariantName($firstVariant['translations']['en_US']['name'], $variantName);
}

/**
* @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);

$this->assertProductVariantName($lastVariant['translations']['en_US']['name'], $variantName);
}

private function assertProductVariantName(string $variantName, string $expectedVariantName): void
{
Assert::same(
$variantName,
$expectedVariantName,
sprintf(
'Expected product variant to have name "%s", but it is named "%s".',
$expectedVariantName,
$variantName,
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Sylius\Behat\Client\ResponseCheckerInterface;
use Sylius\Behat\Context\Api\Resources;
use Sylius\Behat\Service\Converter\SectionAwareIriConverterInterface;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Core\Formatter\StringInflector;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ProductInterface;
Expand All @@ -39,6 +40,7 @@ public function __construct(
private ResponseCheckerInterface $responseChecker,
private IriConverterInterface $iriConverter,
private SectionAwareIriConverterInterface $sectionAwareIriConverter,
private SharedStorageInterface $sharedStorage,
) {
}

Expand Down Expand Up @@ -300,6 +302,36 @@ public function iChangeItsQuantityOfInventoryTo(int $amount): void
$this->client->updateRequestData(['onHand' => $amount]);
}

/**
* @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 I should be notified that it has been successfully created
*/
Expand Down Expand Up @@ -733,4 +765,45 @@ public function iShouldBeNotifiedThatOnHandQuantityMustBeGreaterThanTheNumberOfO
'On hand must be greater than the number of on hold units',
);
}

/**
* @Then the first variant in the list should have name :variantName
*/
public function theFirstVariantInTheListShouldHaveName(string $variantName): void
{
$product = $this->sharedStorage->get('product');
$this->iWantToViewAllVariantsOfThisProduct($product);
$variants = $this->responseChecker->getCollection($this->client->getLastResponse());

$firstVariant = reset($variants);

$this->assertProductVariantName($firstVariant['translations']['en_US']['name'], $variantName);
}

/**
* @Then the last variant in the list should have name :variantName
*/
public function theLastVariantInTheListShouldHaveName(string $variantName): void
{
$product = $this->sharedStorage->get('product');
$this->iWantToViewAllVariantsOfThisProduct($product);
$variants = $this->responseChecker->getCollection($this->client->getLastResponse());

$lastVariant = end($variants);

$this->assertProductVariantName($lastVariant['translations']['en_US']['name'], $variantName);
}

private function assertProductVariantName(string $variantName, string $expectedVariantName): void
{
Assert::same(
$variantName,
$expectedVariantName,
sprintf(
'Expected product variant to have name "%s", but it is named "%s".',
$expectedVariantName,
$variantName,
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
<argument type="service" id="Sylius\Behat\Client\ResponseCheckerInterface" />
<argument type="service" id="api_platform.iri_converter" />
<argument type="service" id="sylius.behat.section_iri_converter" />
<argument type="service" id="sylius.behat.shared_storage" />
</service>

<service id="Sylius\Behat\Context\Api\Admin\ManagingProductVariantsPricesContext">
Expand Down

0 comments on commit 7165dea

Please sign in to comment.