Skip to content

Commit

Permalink
[API][Admin] Sorting coupons
Browse files Browse the repository at this point in the history
  • Loading branch information
NoResponseMate authored and Wojdylak committed Nov 30, 2023
1 parent 4715396 commit 41efb5f
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 11 deletions.
20 changes: 10 additions & 10 deletions features/promotion/managing_coupons/sorting_coupons.feature
Original file line number Diff line number Diff line change
Expand Up @@ -19,69 +19,69 @@ Feature: Sorting listed coupons
And this coupon expires on "20-02-2023"
And I am logged in as an administrator

@ui
@ui @api
Scenario: Coupons are sorted by descending number of uses by default
When I want to view all coupons of this promotion
Then I should see 3 coupons on the list
And the first coupon should have code "Y"

@ui
@ui @api
Scenario: Changing the number of uses sorting order to ascending
Given I am browsing coupons of this promotion
When I sort coupons by ascending number of uses
Then I should see 3 coupons on the list
And the first coupon should have code "X"

@ui
@ui @api
Scenario: Sorting coupons by code in descending order
Given I am browsing coupons of this promotion
When I sort coupons by descending code
Then I should see 3 coupons on the list
And the first coupon should have code "Z"

@ui
@ui @api
Scenario: Sorting coupons by code in ascending order
Given I am browsing coupons of this promotion
When I sort coupons by ascending code
Then I should see 3 coupons on the list
And the first coupon should have code "X"

@ui @no-postgres
@ui @no-postgres @api
Scenario: Sorting coupons by usage limit in descending order
Given I am browsing coupons of this promotion
When I sort coupons by descending usage limit
Then I should see 3 coupons on the list
And the first coupon should have code "X"

@ui @no-postgres
@ui @no-postgres @api
Scenario: Sorting coupons by usage limit in ascending order
Given I am browsing coupons of this promotion
When I sort coupons by ascending usage limit
Then I should see 3 coupons on the list
And the first coupon should have code "Z"

@ui @no-postgres
@ui @no-postgres @api
Scenario: Sorting coupons by usage limit per customer in descending order
Given I am browsing coupons of this promotion
When I sort coupons by descending usage limit per customer
Then I should see 3 coupons on the list
And the first coupon should have code "X"

@ui @no-postgres
@ui @no-postgres @api
Scenario: Sorting coupons by usage limit per customer in ascending order
Given I am browsing coupons of this promotion
When I sort coupons by ascending usage limit per customer
Then I should see 3 coupons on the list
And the first coupon should have code "Y"

@ui @no-postgres
@ui @no-postgres @api
Scenario: Sorting coupons by expiration date in descending order
Given I am browsing coupons of this promotion
When I sort coupons by descending expiration date
Then I should see 3 coupons on the list
And the first coupon should have code "Z"

@ui @no-postgres
@ui @no-postgres @api
Scenario: Sorting coupons by expiration date in ascending order
Given I am browsing coupons of this promotion
When I sort coupons by ascending expiration date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function __construct(
}

/**
* @Given /^I am browsing coupons of (this promotion)$/
* @When /^I want to view all coupons of (this promotion)$/
*/
public function iWantToViewAllCouponsOfThisPromotion(PromotionInterface $promotion): void
Expand All @@ -34,6 +35,46 @@ public function iWantToViewAllCouponsOfThisPromotion(PromotionInterface $promoti
$this->client->filter();
}

/**
* @When /^I sort coupons by (ascending|descending) number of uses$/
*/
public function iSortCouponsByNumberOfUses(string $order): void
{
$this->sortBy($order, 'used');
}

/**
* @When /^I sort coupons by (ascending|descending) code$/
*/
public function iSortCouponsByCode(string $order): void
{
$this->sortBy($order, 'code');
}

/**
* @When /^I sort coupons by (ascending|descending) usage limit$/
*/
public function iSortCouponsByUsageLimit(string $order): void
{
$this->sortBy($order, 'usageLimit');
}

/**
* @When /^I sort coupons by (ascending|descending) usage limit per customer$/
*/
public function iSortCouponsByPerCustomerUsageLimit(string $order): void
{
$this->sortBy($order, 'perCustomerUsageLimit');
}

/**
* @When /^I sort coupons by (ascending|descending) expiration date$/
*/
public function iSortCouponsByExpirationDate(string $order): void
{
$this->sortBy($order, 'expiresAt');
}

/**
* @Then /^there should(?:| still) be (\d+) coupons? related to (this promotion)$/
*/
Expand All @@ -55,4 +96,30 @@ public function thereShouldBeACouponWithCode(string $code): void
{
Assert::true($this->responseChecker->hasItemWithValue($this->client->getLastResponse(), 'code', $code));
}

/**
* @Then I should see :count coupons on the list
*/
public function iShouldSeeCountCouponsOnTheList(int $count): void
{
Assert::same($this->responseChecker->countCollectionItems($this->client->getLastResponse()), $count);
}

/**
* @Then the first coupon should have code :code
*/
public function theFirstCouponShouldHaveCode(string $code): void
{
Assert::true($this->responseChecker->hasItemOnPositionWithValue(
$this->client->getLastResponse(),
0,
'code',
$code,
));
}

private function sortBy(string $order, string $field): void
{
$this->client->sort([$field => str_starts_with($order, 'de') ? 'desc' : 'asc']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ default:
- sylius.behat.context.hook.doctrine_orm

- sylius.behat.context.transform.coupon
- sylius.behat.context.transform.date_time
- sylius.behat.context.transform.promotion
- sylius.behat.context.transform.shared_storage

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@
<collectionOperations>
<collectionOperation name="admin_get">
<attribute name="method">GET</attribute>
<attribute name="filters">
<attribute>sylius.api.promotion_coupon_order_filter</attribute>
</attribute>
<attribute name="normalization_context">
<attribute name="groups">admin:promotion_coupon:read</attribute>
</attribute>
<attribute name="order">
<attribute name="used">DESC</attribute>
</attribute>
</collectionOperation>
</collectionOperations>

Expand Down
11 changes: 11 additions & 0 deletions src/Sylius/Bundle/ApiBundle/Resources/config/services/filters.xml
Original file line number Diff line number Diff line change
Expand Up @@ -273,5 +273,16 @@
</argument>
<tag name="api_platform.filter" />
</service>

<service id="sylius.api.promotion_coupon_order_filter" parent="api_platform.doctrine.orm.order_filter" public="true">
<argument type="collection">
<argument key="code" />
<argument key="expiresAt" />
<argument key="usageLimit" />
<argument key="perCustomerUsageLimit" />
<argument key="used" />
</argument>
<tag name="api_platform.filter" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,42 @@
"updatedAt": @date@
}
],
"hydra:totalItems": 2
"hydra:totalItems": 2,
"hydra:search": {
"@type": "hydra:IriTemplate",
"hydra:template": "\/api\/v2\/admin\/promotion-coupons{?order[code],order[expiresAt],order[usageLimit],order[perCustomerUsageLimit],order[used]}",
"hydra:variableRepresentation": "BasicRepresentation",
"hydra:mapping": [
{
"@type": "IriTemplateMapping",
"variable": "order[code]",
"property": "code",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "order[expiresAt]",
"property": "expiresAt",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "order[usageLimit]",
"property": "usageLimit",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "order[perCustomerUsageLimit]",
"property": "perCustomerUsageLimit",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "order[used]",
"property": "used",
"required": false
}
]
}
}

0 comments on commit 41efb5f

Please sign in to comment.