Skip to content

Commit

Permalink
feature #13092 [CatalogPromotion][Behat] Add scenarios for reapplying…
Browse files Browse the repository at this point in the history
… catalog promotions after editing its channels (GSadee)

This PR was merged into the 1.11-dev branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | master
| Bug fix?        | no
| New feature?    | yes
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | 
| License         | MIT


Commits
-------

4e93cec [CatalogPromotion][Behat] Add scenarios for reapplying catalog promotions after editing its channels
56494d0 [CatalogPromotion][Behat] Change actor of scenarios for reapplying catalog promotions after editing its channels
a68f5bf [CatalogPromotion][Behat] Implement scenarios for reapplying catalog promotions after editing its channels
  • Loading branch information
AdamKasp committed Sep 13, 2021
2 parents c94a9dd + a68f5bf commit 000a9e5
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@applying_catalog_promotions
Feature: Reapplying catalog promotions after editing its channels
In order to have proper discounts in different channels
As a Store Owner
I want to have discounts reapplied in product catalog once the channels of catalog promotion changes

Background:
Given the store operates on a channel named "Web-US" with hostname "web-us"
And the store operates on another channel named "Web-GB" with hostname "web-gb"
And the store has a "T-Shirt" configurable product
And this product has "PHP T-Shirt" variant priced at "$20.00" in "Web-US" channel
And "PHP T-Shirt" variant priced at "$30.00" in "Web-GB" channel
And this product is available in "Web-US" channel and "Web-GB" channel
And there is a catalog promotion "Winter sale" available in "Web-US" channel that reduces price by "30%" and applies on "PHP T-shirt" variant
And I am logged in as an administrator

@todo
Scenario: Removing applied catalog promotion after removing its channel
When I make this catalog promotion unavailable in the "Web-US" channel
Then the visitor should see "$20.00" as the price of the "T-Shirt" product in the "Web-US" channel
And the visitor should see "$20.00" as the original price of the "T-Shirt" product in the "Web-US" channel

@todo
Scenario: Reapplying catalog promotion after adding new channel to them
When I make this catalog promotion available in the "Web-GB" channel
Then the visitor should see "$21.00" as the price of the "T-Shirt" product in the "Web-GB" channel
And the visitor should see "$30.00" as the original price of the "T-Shirt" product in the "Web-GB" channel

@todo
Scenario: Reapplying catalog promotion after switching availability in channels
When I switch this catalog promotion availability from the "Web-US" channel to the "Web-GB" channel
Then the visitor should see "$20.00" as the price of the "T-Shirt" product in the "Web-US" channel
And the visitor should see "$20.00" as the original price of the "T-Shirt" product in the "Web-US" channel
And the visitor should see "$21.00" as the price of the "T-Shirt" product in the "Web-GB" channel
And the visitor should see "$30.00" as the original price of the "T-Shirt" product in the "Web-GB" channel
2 changes: 2 additions & 0 deletions src/Sylius/Behat/Client/ApiClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public function removeSubResource(string $subResource, string $id): void;

public function updateRequestData(array $data): void;

public function getContent(): array;

public function getLastResponse(): Response;

public function getToken(): ?string;
Expand Down
5 changes: 5 additions & 0 deletions src/Sylius/Behat/Client/ApiPlatformClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ public function removeSubResource(string $subResource, string $id): void
$this->request->removeSubResource($subResource, $id);
}

public function getContent(): array
{
return $this->request->getContent();
}

public function getLastResponse(): Response
{
return $this->client->getResponse();
Expand Down
5 changes: 5 additions & 0 deletions src/Sylius/Behat/Client/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ public function content(): string
return json_encode($this->content);
}

public function getContent(): array
{
return $this->content;
}

public function setContent(array $content): void
{
$this->content = $content;
Expand Down
2 changes: 2 additions & 0 deletions src/Sylius/Behat/Client/RequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public function headers(): array;

public function content(): string;

public function getContent(): array;

public function setContent(array $content): void;

public function updateContent(array $newValues): void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,60 @@ public function iAddCatalogPromotionActionWithNonexistentType(): void
$this->client->addRequestData('actions', $actions);
}

/**
* @When /^I make (this catalog promotion) unavailable in the ("[^"]+" channel)$/
*/
public function iMakeThisCatalogPromotionUnavailableInTheChannel(
CatalogPromotionInterface $catalogPromotion,
ChannelInterface $channel
): void {
$catalogPromotionCode = $catalogPromotion->getCode();
Assert::notNull($catalogPromotionCode);

$this->client->buildUpdateRequest($catalogPromotionCode);
$content = $this->client->getContent();
foreach (array_keys($content['channels'], $this->iriConverter->getIriFromItem($channel)) as $key) {
unset($content['channels'][$key]);
}

$this->client->setRequestData($content);
$this->client->update();
}

/**
* @When /^I make (this catalog promotion) available in the ("[^"]+" channel)$/
*/
public function iMakeThisCatalogPromotionAvailableInTheChannel(
CatalogPromotionInterface $catalogPromotion,
ChannelInterface $channel
): void {
$this->client->buildUpdateRequest($catalogPromotion->getCode());
$this->client->updateRequestData(['channels' => $this->iriConverter->getIriFromItem($channel)]);
$this->client->update();
}

/**
* @When /^I switch (this catalog promotion) availability from the ("[^"]+" channel) to the ("[^"]+" channel)$/
*/
public function iSwitchThisCatalogPromotionAvailabilityFromTheChannelToTheChannel(
CatalogPromotionInterface $catalogPromotion,
ChannelInterface $removedChannel,
ChannelInterface $addedChannel
): void {
$catalogPromotionCode = $catalogPromotion->getCode();
Assert::notNull($catalogPromotionCode);

$this->client->buildUpdateRequest($catalogPromotionCode);
$content = $this->client->getContent();
foreach (array_keys($content['channels'], $this->iriConverter->getIriFromItem($removedChannel)) as $key) {
unset($content['channels'][$key]);
}

$content['channels'][] = $this->iriConverter->getIriFromItem($addedChannel);
$this->client->setRequestData($content);
$this->client->update();
}

/**
* @Then there should be :amount new catalog promotion on the list
* @Then there should be :amount catalog promotions on the list
Expand Down
40 changes: 36 additions & 4 deletions src/Sylius/Behat/Context/Api/Shop/ProductContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
use Sylius\Behat\Client\ApiClientInterface;
use Sylius\Behat\Client\Request;
use Sylius\Behat\Client\ResponseCheckerInterface;
use Sylius\Behat\Service\Setter\ChannelContextSetterInterface;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Core\Formatter\StringInflector;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Product\Model\ProductVariantInterface;
use Sylius\Component\Taxonomy\Model\TaxonInterface;
Expand All @@ -40,16 +43,20 @@ final class ProductContext implements Context
/** @var IriConverterInterface */
private $iriConverter;

private ChannelContextSetterInterface $channelContextSetter;

public function __construct(
ApiClientInterface $client,
ResponseCheckerInterface $responseChecker,
SharedStorageInterface $sharedStorage,
IriConverterInterface $iriConverter
IriConverterInterface $iriConverter,
ChannelContextSetterInterface $channelContextSetter
) {
$this->client = $client;
$this->responseChecker = $responseChecker;
$this->sharedStorage = $sharedStorage;
$this->iriConverter = $iriConverter;
$this->channelContextSetter = $channelContextSetter;
}

/**
Expand Down Expand Up @@ -330,8 +337,33 @@ public function iShouldSeeTheProductDescription(string $description): void
);
}

private function hasProductWithPrice(array $products, int $price, ?string $productCode = null): bool
{
/**
* @Then /^the visitor should see ("[^"]+") as the (price|original price) of the ("[^"]+" product) in the ("[^"]+" channel)$/
*/
public function theVisitorShouldSeeAsThePriceOfTheProductInTheChannel(
int $price,
string $priceType,
ProductInterface $product,
ChannelInterface $channel
): void {
$this->sharedStorage->set('token', null);
$this->sharedStorage->set('hostname', $channel->getHostname());
$this->channelContextSetter->setChannel($channel);

Assert::true($this->hasProductWithPrice(
[$this->responseChecker->getResponseContent($this->client->show($product->getCode()))],
$price,
null,
StringInflector::nameToCamelCase($priceType)
));
}

private function hasProductWithPrice(
array $products,
int $price,
?string $productCode = null,
string $priceType = 'price'
): bool {
foreach ($products as $product) {
if ($productCode !== null && $product['code'] !== $productCode) {
continue;
Expand All @@ -341,7 +373,7 @@ private function hasProductWithPrice(array $products, int $price, ?string $produ
$this->client->executeCustomRequest(Request::custom($variantIri, HttpRequest::METHOD_GET));

/** @var int $variantPrice */
$variantPrice = $this->responseChecker->getValue($this->client->getLastResponse(), 'price');
$variantPrice = $this->responseChecker->getValue($this->client->getLastResponse(), $priceType);

if ($price === $variantPrice) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<argument type="service" id="Sylius\Behat\Client\ResponseCheckerInterface" />
<argument type="service" id="sylius.behat.shared_storage" />
<argument type="service" id="api_platform.iri_converter" />
<argument type="service" id="sylius.behat.channel_context_setter" />
</service>

<service id="sylius.behat.context.api.shop.product_variant" class="Sylius\Behat\Context\Api\Shop\ProductVariantContext">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ default:
contexts:
- sylius.behat.context.hook.doctrine_orm

- sylius.behat.context.setup.admin_api_security
- sylius.behat.context.setup.channel
- sylius.behat.context.setup.product
- sylius.behat.context.setup.product_taxon
Expand All @@ -20,6 +21,7 @@ default:
- sylius.behat.context.transform.shared_storage
- sylius.behat.context.transform.taxon

- sylius.behat.context.api.admin.managing_catalog_promotions
- sylius.behat.context.api.shop.product
- sylius.behat.context.api.shop.product_variant

Expand Down

0 comments on commit 000a9e5

Please sign in to comment.