Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API][Behat] covering tests for managing promotions #15635

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ Feature: Modifying taxons assigned to an existing product

@ui @api
Scenario: Modifying taxons assigned to a product
When I change that the "T-Shirt" product belongs to the "T-Shirts" taxon
When I change that the "T-Shirt" product does not belong to the "Clothes" taxon
And I add "T-Shirts" taxon to the "T-Shirt" product
Then the product "T-Shirt" should have the "T-Shirts" taxon

@ui @api
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ Feature: Not reapplying catalog promotions on variants once the product’s taxo
And there is disabled catalog promotion "Surprise sale" between "2021-07-01" and "2022-05-04" available in "Web-US" channel that reduces price by "90%" and applies on "Dishes" taxon
And I am logged in as an administrator

@ui
@api @ui
Scenario: Changing products taxon to taxon with scheduled catalog promotion
When I change that the "T-Shirt" product does not belong to the "Clothes" taxon
And I change that the "T-Shirt" product belongs to the "Shirts" taxon
And I add "Shirts" taxon to the "T-Shirt" product
Then the visitor should see that the "PHP T-Shirt" variant is not discounted
And the visitor should still see "$100.00" as the price of the "T-Shirt" product in the "Web-US" channel

@ui
@api @ui
Scenario: Changing products taxon to taxon with disabled catalog promotion
When I change that the "T-Shirt" product does not belong to the "Clothes" taxon
And I change that the "T-Shirt" product belongs to the "Dishes" taxon
And I add "Dishes" taxon to the "T-Shirt" product
Then the visitor should see that the "PHP T-Shirt" variant is not discounted
And the visitor should still see "$100.00" as the price of the "T-Shirt" product in the "Web-US" channel
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ Feature: Reapplying catalog promotions on variants once the product’s taxon ch
And there is another catalog promotion "Summer sale" that reduces price by "50%" and applies on "Dishes" taxon
And I am logged in as an administrator

@ui
@api @ui
Scenario: Removing a taxon from a product
When I change that the "T-Shirt" product does not belong to the "Clothes" taxon
Then the visitor should see that the "PHP T-Shirt" variant is not discounted

@ui @api
@api @ui
Scenario: Adding a taxon to a product
When I assign the "Dishes" taxon to the "Mug" product
Then the visitor should see that the "PHP Mug" variant is discounted from "$10.00" to "$5.00" with "Summer sale" promotion
Original file line number Diff line number Diff line change
Expand Up @@ -16,69 +16,69 @@ Feature: Sorting listed catalog promotion
And its priority is 2
And I am logged in as an administrator

@ui
@api @ui
Scenario: Catalog promotions are sorted by ascending code by default
When I browse catalog promotions
Then I should see 3 catalog promotions on the list
And the first catalog promotion should have code "a"

@ui
@api @ui
Scenario: Changing the code sorting order to descending
When I browse catalog promotions
And I sort catalog promotions by descending code
Then I should see 3 catalog promotions on the list
And the first catalog promotion should have code "not-b"

@ui
@api @ui
Scenario: Sorting catalog promotions by name in ascending order
When I browse catalog promotions
And I sort catalog promotions by ascending name
Then I should see 3 catalog promotions on the list
And the first catalog promotion should have code "a"

@ui
@api @ui
Scenario: Sorting catalog promotion by name in descending order
When I browse catalog promotions
And I sort catalog promotions by descending name
Then I should see 3 catalog promotions on the list
And the first catalog promotion should have code "c"

@ui @no-postgres
@api @ui @no-postgres
Scenario: Sorting catalog promotion by start date in ascending order
When I browse catalog promotions
And I sort catalog promotions by ascending "start date"
Then I should see 3 catalog promotions on the list
And the first catalog promotion should have code "not-b"

@ui @no-postgres
@api @ui @no-postgres
Scenario: Sorting catalog promotion by start date in descending order
When I browse catalog promotions
And I sort catalog promotions by descending "start date"
Then I should see 3 catalog promotions on the list
And the first catalog promotion should have code "a"

@ui @no-postgres
@api @ui @no-postgres
Scenario: Sorting catalog promotion by end date in ascending order
When I browse catalog promotions
And I sort catalog promotions by ascending "end date"
Then I should see 3 catalog promotions on the list
And the first catalog promotion should have code "a"

@ui @no-postgres
@api @ui @no-postgres
Scenario: Sorting catalog promotion by end date in descending order
When I browse catalog promotions
And I sort catalog promotions by descending "end date"
Then I should see 3 catalog promotions on the list
And the first catalog promotion should have code "c"

@ui
@api @ui
Scenario: Sorting catalog promotion by priority in ascending order
When I browse catalog promotions
And I sort catalog promotions by ascending priority
Then I should see 3 catalog promotions on the list
And the first catalog promotion should have code "not-b"

@ui
@api @ui
Scenario: Sorting catalog promotion by priority in descending order
When I browse catalog promotions
And I sort catalog promotions by descending priority
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Feature: Checking a promotion usage after placing an order
And there is a promotion "Christmas promotion"
And I am logged in as an administrator

@ui
@api @ui
Scenario: Seeing item fixed discount promotion usage unchanged after order placement
Given the promotion gives "$10.00" off on every product with minimum price at "$50.00"
And the promotion gives another "$5.00" off on every product classified as "T-Shirts"
Expand All @@ -26,7 +26,7 @@ Feature: Checking a promotion usage after placing an order
When I browse promotions
Then the promotion "Christmas promotion" should not be used

@ui
@api @ui
Scenario: Seeing item fixed discount promotion usage increased after order placement
Given the promotion gives "$10.00" off on every product with minimum price at "$50.00"
And the promotion gives another "$5.00" off on every product classified as "Mugs"
Expand All @@ -36,7 +36,7 @@ Feature: Checking a promotion usage after placing an order
When I browse promotions
Then the promotion "Christmas promotion" should be used 1 time

@ui
@api @ui
Scenario: Seeing shipping percentage discount promotion usage unchanged after order placement
Given the promotion gives "100%" discount on shipping to every order
And there is a customer "john.doe@gmail.com" that placed an order "#00000022"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Feature: Decreasing a promotion coupon usage after cancelling an order
And the store has promotion "Christmas sale" with coupon "SANTA2016"
And I am logged in as an administrator

@ui
@api @ui
Scenario: Seeing promotion coupon usage decreased after order cancellation
Given there is a customer "john.doe@gmail.com" that placed an order "#00000022"
And the customer bought a single "PHP T-Shirt" using "SANTA2016" coupon
Expand All @@ -21,7 +21,7 @@ Feature: Decreasing a promotion coupon usage after cancelling an order
When I browse all coupons of "Christmas sale" promotion
Then "SANTA2016" coupon should be used 0 times

@ui
@api @ui
Scenario: Seeing promotion coupon usage decreased to 1 after second order cancellation
Given there is a customer "john.doe@gmail.com" that placed an order "#00000022"
And the customer bought a single "PHP T-Shirt" using "SANTA2016" coupon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Feature: Decreasing a promotion usage after cancelling an order
And there is a promotion "Limited promotion" limited to 5 usages
And I am logged in as an administrator

@ui
@api @ui
Scenario: Seeing promotion usage decreased after order cancellation
Given there is a customer "john.doe@gmail.com" that placed an order "#00000022"
And the customer bought a single "PHP T-Shirt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ Feature: Increasing a promotion coupon usage after placing an order
And the store has promotion "Christmas sale" with coupon "SANTA2016"
And I am logged in as an administrator

@ui
@api @ui
Scenario: Seeing promotion coupon usage increased after order placement
Given there is a customer "john.doe@gmail.com" that placed an order "#00000022"
And the customer bought a single "PHP T-Shirt" using "SANTA2016" coupon
And the customer chose "Free" shipping method to "United States" with "Cash on Delivery" payment
When I browse all coupons of "Christmas sale" promotion
Then "SANTA2016" coupon should be used 1 time

@ui
@api @ui
Scenario: Seeing promotion coupon usage increased correctly after few orders placement
Given there is a customer "john.doe@gmail.com" that placed an order "#00000022"
And the customer bought a single "PHP T-Shirt" using "SANTA2016" coupon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Feature: Increasing a promotion usage after placing an order
And it gives "$10.00" discount to every order
And I am logged in as an administrator

@ui
@api @ui
Scenario: Seeing promotion usage increased after order placement
Given there is a customer "john.doe@gmail.com" that placed an order "#00000022"
And the customer bought a single "PHP T-Shirt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,18 @@ public function iFilterByDateFromDateToDate(string $dateType, string $fromDate,
$this->client->filter();
}

/**
* @When I sort catalog promotions by :order :field
*/
public function iSortCatalogPromotionByOrderField(string $order, string $field): void
{
$this->client->addFilter(
sprintf('order[%s]', lcfirst(str_replace(' ', '', ucwords($field)))),
$order === 'descending' ? 'desc' : 'asc',
);
$this->client->filter();
}

/**
* @When I request the removal of :catalogPromotion catalog promotion
*/
Expand Down Expand Up @@ -1554,6 +1566,24 @@ public function iShouldNotSeeACatalogPromotionWithName(string $name): void
);
}

/**
* @Then I should see :count catalog promotions on the list
*/
public function iShouldSeeCountCatalogPromotionsOnTheList(int $count): void
{
Assert::count($this->responseChecker->getCollection($this->client->getLastResponse()), $count);
}

/**
* @Then the first catalog promotion should have code :code
*/
public function theFirstCatalogPromotionShouldHaveCode(string $code): void
{
$catalogPromotions = $this->responseChecker->getCollection($this->client->getLastResponse());

Assert::same(reset($catalogPromotions)['code'], $code);
}

private function catalogPromotionAppliesOnVariants(ProductVariantInterface ...$productVariants): bool
{
$response = $this->responseChecker->getResponseContent($this->client->getLastResponse());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,11 @@ public function __construct(private ApiClientInterface $client, private IriConve
{
}

/**
* @When I change that the :product product belongs to the :taxon taxon
*/
public function iChangeThatTheProductBelongsToTheTaxon(ProductInterface $product, TaxonInterface $taxon): void
{
$this->client->buildUpdateRequest(Resources::PRODUCT_TAXONS, (string) $product->getProductTaxons()->current()->getId());
$this->client->updateRequestData(['taxon' => $this->iriConverter->getIriFromResource($taxon)]);
$this->client->update();
}

/**
* @When I assign the :taxon taxon to the :product product
* @When I (try to) add :taxon taxon to the :product product
*/
public function iAddTaxonToTheProduct(TaxonInterface $taxon, ProductInterface $product): void
public function iAddTaxonToTheProduct(ProductInterface $product, TaxonInterface $taxon): void
{
$this->client->buildCreateRequest(Resources::PRODUCT_TAXONS);
$this->client->addRequestData('taxon', $this->iriConverter->getIriFromResource($taxon));
Expand Down Expand Up @@ -76,15 +66,22 @@ public function iTryToAssignAnEmptyProductToTheTaxon(TaxonInterface $taxon): voi
public function iTryToAssignTheProductTaxonOfProductAndTaxonToTheProduct(
ProductInterface $productTaxonProduct,
TaxonInterface $productTaxonTaxon,
): void {
$this->iAddTaxonToTheProduct($productTaxonProduct, $productTaxonTaxon);
}

/**
* @When I change that the :product product does not belong to the :taxon taxon
*/
public function iChangeThatTheProductDoesNotBelongToTheTaxon(
ProductInterface $product,
TaxonInterface $taxon,
): void {
$productTaxon = $productTaxonProduct->getProductTaxons()->filter(
fn (ProductTaxonInterface $productTaxon) => $productTaxonTaxon === $productTaxon->getTaxon(),
$productTaxon = $product->getProductTaxons()->filter(
fn (ProductTaxonInterface $productTaxon) => $taxon === $productTaxon->getTaxon(),
)->first();

$this->client->buildUpdateRequest(Resources::PRODUCTS, $product->getCode());
$this->client->addRequestData('productTaxons', [$this->iriConverter->getIriFromResource($productTaxon)]);
$this->client->update();
$this->client->delete(Resources::PRODUCT_TAXONS, (string) $productTaxon->getId());
}

/**
Expand Down
22 changes: 7 additions & 15 deletions src/Sylius/Behat/Context/Api/Admin/ManagingProductsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -557,13 +557,13 @@ public function thisProductMainTaxonShouldBe(ProductInterface $product, TaxonInt
*/
public function thisProductTaxonShouldBe(ProductInterface $product, TaxonInterface $taxon): void
{
$productTaxonId = $this->getProductTaxonId($product);

$response = $this->client->show(Resources::PRODUCT_TAXONS, (string) $productTaxonId);
$productTaxonIri = $this->responseChecker->getValue($response, 'taxon');
$productTaxonCodes = explode('/', $productTaxonIri);

Assert::same(array_pop($productTaxonCodes), $taxon->getCode());
$this->client->index(Resources::PRODUCT_TAXONS);
Assert::true(
$this->responseChecker->hasItemWithValues($this->client->getLastResponse(), [
'product' => $this->sectionAwareIriConverter->getIriFromResourceInSection($product, 'admin'),
'taxon' => $this->sectionAwareIriConverter->getIriFromResourceInSection($taxon, 'admin'),
])
);
}

/**
Expand Down Expand Up @@ -871,14 +871,6 @@ private function getLastResponse(): Response
return $this->sharedStorage->has('response') ? $this->sharedStorage->get('response') : $this->client->getLastResponse();
}

private function getProductTaxonId(ProductInterface $product): string
{
$productResponse = $this->client->show(Resources::PRODUCTS, (string) $product->getCode());
$productTaxonUrl = explode('/', $this->responseChecker->getValue($productResponse, 'productTaxons')[0]);

return array_pop($productTaxonUrl);
}

private function getAttributeValueInProperType(
ProductAttributeInterface $productAttribute,
string $value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function __construct(
/**
* @Given /^I am browsing coupons of (this promotion)$/
* @When /^I want to view all coupons of (this promotion)$/
* @When /^I browse all coupons of ("[^"]+" promotion)$/
*/
public function iWantToViewAllCouponsOfThisPromotion(PromotionInterface $promotion): void
{
Expand Down Expand Up @@ -610,6 +611,24 @@ public function iShouldNotBeAbleToEditItsCode(): void
Assert::false($this->responseChecker->hasValue($this->client->update(), 'code', 'NEW_CODE'));
}

/**
* @Then /^("[^"]+" coupon) should be used (\d+) time(?:|s)$/
*/
public function couponShouldHaveUsageLimit(PromotionCouponInterface $promotionCoupon, int $used): void
{
$returnedPromotionCoupon = current($this->responseChecker->getCollectionItemsWithValue(
$this->client->getLastResponse(),
'code',
$promotionCoupon->getCode(),
));

Assert::same(
$returnedPromotionCoupon['used'],
$used,
sprintf('The promotion coupon %s has been used %s times', $promotionCoupon->getCode(), $returnedPromotionCoupon['used']),
);
}

private function sortBy(string $order, string $field): void
{
$this->client->sort([$field => str_starts_with($order, 'de') ? 'desc' : 'asc']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,25 @@ public function theFirstPromotionOnTheListShouldHave(string $togglePosition, str
Assert::same($item[$field], $value);
}

/**
* @Then the promotion :promotion should be used :usage time(s)
* @Then the promotion :promotion should not be used
*/
public function thePromotionShouldBeUsedTime(PromotionInterface $promotion, int $usage = 0): void
{
$returnedPromotion = current($this->responseChecker->getCollectionItemsWithValue(
$this->client->getLastResponse(),
'code',
$promotion->getCode()
));

Assert::same(
$returnedPromotion['used'],
$usage,
sprintf('The promotion %s has been used %s times', $promotion->getName(), $returnedPromotion['used']),
);
}

private function addToRequestAction(string $type, array $configuration): void
{
$data['actions'][] = [
Expand Down