From c14937cff9f1c29ab8235fec413fccae6a11dcb5 Mon Sep 17 00:00:00 2001 From: Kamil Grygierzec Date: Mon, 29 Jul 2024 15:38:42 +0200 Subject: [PATCH 1/2] [PromotionCoupon] Enable todo api scenario for filtering by code --- .../filtering_coupons.feature | 2 +- .../Admin/ManagingPromotionCouponsContext.php | 37 ++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/features/admin/promotion/managing_coupons/filtering_coupons.feature b/features/admin/promotion/managing_coupons/filtering_coupons.feature index d39ba3c8af..6067112c8c 100644 --- a/features/admin/promotion/managing_coupons/filtering_coupons.feature +++ b/features/admin/promotion/managing_coupons/filtering_coupons.feature @@ -19,7 +19,7 @@ Feature: Filtering coupons And this coupon expires on "20-02-2023" And I am logged in as an administrator - @ui @todo-api + @api @ui Scenario: Filtering coupons by code Given I am browsing coupons of this promotion When I filter by code containing "X" diff --git a/src/Sylius/Behat/Context/Api/Admin/ManagingPromotionCouponsContext.php b/src/Sylius/Behat/Context/Api/Admin/ManagingPromotionCouponsContext.php index ab2bc24ad3..580adce822 100644 --- a/src/Sylius/Behat/Context/Api/Admin/ManagingPromotionCouponsContext.php +++ b/src/Sylius/Behat/Context/Api/Admin/ManagingPromotionCouponsContext.php @@ -49,8 +49,21 @@ final class ManagingPromotionCouponsContext implements Context { $this->client->index(Resources::PROMOTION_COUPONS); $this->client->addFilter( - 'promotion', - $this->iriConverter->getIriFromResourceInSection($promotion, 'admin'), + 'promotion.code', + $promotion->getCode(), + ); + $this->client->filter(); + } + + /** + * @When I filter by code containing :phrase + */ + public function iFilterByCodeContaining(string $phrase): void + { + $this->client->index(Resources::PROMOTION_COUPONS); + $this->client->addFilter( + 'code', + $phrase, ); $this->client->filter(); } @@ -632,6 +645,26 @@ final class ManagingPromotionCouponsContext implements Context ); } + /** + * @Then I should see a single promotion coupon in the list + */ + public function iShouldSeeASinglePromotionCouponInTheList(): void + { + Assert::same($this->responseChecker->countCollectionItems($this->client->getLastResponse()), 1); + } + + /** + * @Then I should see the promotion coupon :coupon in the list + */ + public function iShouldSeeThePromotionCouponInTheList(PromotionCouponInterface $coupon): void + { + Assert::true($this->responseChecker->hasItemWithValue( + $this->client->getLastResponse(), + 'code', + $coupon->getCode(), + )); + } + private function sortBy(string $order, string $field): void { $this->client->sort([$field => str_starts_with($order, 'de') ? 'desc' : 'asc']); From 530bd800025721ffcfce650de040b725ebfdfe52 Mon Sep 17 00:00:00 2001 From: Kamil Grygierzec Date: Mon, 29 Jul 2024 15:39:32 +0200 Subject: [PATCH 2/2] [PromotionCoupon] replace filtering by promotion iri with filtering by promotion code --- .../PromotionCouponPromotionFilter.php | 88 ------------------- .../resources/PromotionCoupon.xml | 2 +- .../Resources/config/services/filters.xml | 14 +-- .../get_promotion_coupons_response.json | 24 ++++- 4 files changed, 30 insertions(+), 98 deletions(-) delete mode 100644 src/Sylius/Bundle/ApiBundle/Doctrine/ORM/Filters/PromotionCouponPromotionFilter.php diff --git a/src/Sylius/Bundle/ApiBundle/Doctrine/ORM/Filters/PromotionCouponPromotionFilter.php b/src/Sylius/Bundle/ApiBundle/Doctrine/ORM/Filters/PromotionCouponPromotionFilter.php deleted file mode 100644 index 9181fe39ca..0000000000 --- a/src/Sylius/Bundle/ApiBundle/Doctrine/ORM/Filters/PromotionCouponPromotionFilter.php +++ /dev/null @@ -1,88 +0,0 @@ - $properties */ - public function __construct( - private IriConverterInterface $iriConverter, - ManagerRegistry $managerRegistry, - ?RequestStack $requestStack = null, - ?LoggerInterface $logger = null, - ?array $properties = null, - ?NameConverterInterface $nameConverter = null, - ) { - parent::__construct($managerRegistry, $requestStack, $logger, $properties, $nameConverter); - } - - protected function filterProperty( - string $property, - $value, - QueryBuilder $queryBuilder, - QueryNameGeneratorInterface $queryNameGenerator, - string $resourceClass, - ?Operation $operation = null, - array $context = [], - ): void { - if (self::PROPERTY !== $property) { - return; - } - - $promotion = $this->iriConverter->getResourceFromIri($value); - - $parameterName = $queryNameGenerator->generateParameterName(':promotion'); - $promotionJoinAlias = $queryNameGenerator->generateJoinAlias('promotion'); - $rootAlias = $queryBuilder->getRootAliases()[0]; - - $queryBuilder - ->innerJoin( - sprintf('%s.promotion', $rootAlias), - $promotionJoinAlias, - Join::WITH, - $queryBuilder->expr()->eq(sprintf('%s.id', $promotionJoinAlias), $parameterName), - ) - ->setParameter($parameterName, $promotion) - ; - } - - /** @return array */ - public function getDescription(string $resourceClass): array - { - return [ - self::PROPERTY => [ - 'type' => 'string', - 'required' => false, - 'property' => null, - 'description' => 'Get a collection of promotion coupons for promotion', - 'schema' => [ - 'type' => 'string', - ], - ], - ]; - } -} diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/api_platform/resources/PromotionCoupon.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/api_platform/resources/PromotionCoupon.xml index 6923a1e120..5bf303052c 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/api_platform/resources/PromotionCoupon.xml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/api_platform/resources/PromotionCoupon.xml @@ -21,7 +21,7 @@ sylius.api.order_filter.promotion_coupon - sylius_api.doctrine.orm.filter.promotion_coupon_promotion + sylius_api.search_filter.promotion_coupon diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/services/filters.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/services/filters.xml index b12f3b4914..5dd0907fd9 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/services/filters.xml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/services/filters.xml @@ -314,6 +314,14 @@ + + + exact + exact + + + + exact @@ -339,12 +347,6 @@ - - - - - - exact diff --git a/tests/Api/Responses/admin/promotion_coupon/get_promotion_coupons_response.json b/tests/Api/Responses/admin/promotion_coupon/get_promotion_coupons_response.json index 4d2341d8f8..9a56f9c7b9 100644 --- a/tests/Api/Responses/admin/promotion_coupon/get_promotion_coupons_response.json +++ b/tests/Api/Responses/admin/promotion_coupon/get_promotion_coupons_response.json @@ -33,7 +33,7 @@ "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],promotion}", + "hydra:template": "\/api\/v2\/admin\/promotion-coupons{?order[code],order[expiresAt],order[usageLimit],order[perCustomerUsageLimit],order[used],code,code[],promotion.code,promotion.code[]}", "hydra:variableRepresentation": "BasicRepresentation", "hydra:mapping": [ { @@ -68,8 +68,26 @@ }, { "@type": "IriTemplateMapping", - "variable": "promotion", - "property": null, + "variable": "code", + "property": "code", + "required": false + }, + { + "@type": "IriTemplateMapping", + "variable": "code[]", + "property": "code", + "required": false + }, + { + "@type": "IriTemplateMapping", + "variable": "promotion.code", + "property": "promotion.code", + "required": false + }, + { + "@type": "IriTemplateMapping", + "variable": "promotion.code[]", + "property": "promotion.code", "required": false } ]