From 31cf5322c807a146d8cf93b22bf0d58df61f8f49 Mon Sep 17 00:00:00 2001 From: Roshyo Date: Fri, 27 Aug 2021 11:44:27 +0200 Subject: [PATCH] Expose Gift card balance --- .../Action/GiftCardBalanceAction.php | 25 +++++++++ .../config/api_resources/GiftCard.xml | 14 +++++ .../config/serialization/GiftCardBalance.xml | 20 +++++++ .../config/services/api/controller.xml | 7 +++ .../Action/GiftCardBalanceActionTest.php | 56 +++++++++++++++++++ 5 files changed, 122 insertions(+) create mode 100644 src/Api/Controller/Action/GiftCardBalanceAction.php create mode 100644 src/Resources/config/serialization/GiftCardBalance.xml create mode 100644 tests/Unit/Api/Controller/Action/GiftCardBalanceActionTest.php diff --git a/src/Api/Controller/Action/GiftCardBalanceAction.php b/src/Api/Controller/Action/GiftCardBalanceAction.php new file mode 100644 index 00000000..8589f1cc --- /dev/null +++ b/src/Api/Controller/Action/GiftCardBalanceAction.php @@ -0,0 +1,25 @@ +giftCardRepository = $giftCardRepository; + } + + public function __invoke(): GiftCardBalanceCollection + { + return GiftCardBalanceCollection::createFromGiftCards( + $this->giftCardRepository->findEnabled() + ); + } +} diff --git a/src/Resources/config/api_resources/GiftCard.xml b/src/Resources/config/api_resources/GiftCard.xml index b3617163..0f85aae2 100644 --- a/src/Resources/config/api_resources/GiftCard.xml +++ b/src/Resources/config/api_resources/GiftCard.xml @@ -27,6 +27,20 @@ + + GET + /admin/gift-cards/balance + setono_sylius_gift_card.controller.action.get_balance + false + + Get the gift cards balance. + + + admin:gift_card_balance:read + + Setono\SyliusGiftCardPlugin\Model\GiftCardBalance + + GET /shop/gift-cards diff --git a/src/Resources/config/serialization/GiftCardBalance.xml b/src/Resources/config/serialization/GiftCardBalance.xml new file mode 100644 index 00000000..ad5d7f9f --- /dev/null +++ b/src/Resources/config/serialization/GiftCardBalance.xml @@ -0,0 +1,20 @@ + + + + + + admin:gift_card_balance:read + + + + admin:gift_card_balance:read + + + + admin:gift_card_balance:read + + + diff --git a/src/Resources/config/services/api/controller.xml b/src/Resources/config/services/api/controller.xml index c1d6f0f4..779afc92 100644 --- a/src/Resources/config/services/api/controller.xml +++ b/src/Resources/config/services/api/controller.xml @@ -29,5 +29,12 @@ + + + + + + diff --git a/tests/Unit/Api/Controller/Action/GiftCardBalanceActionTest.php b/tests/Unit/Api/Controller/Action/GiftCardBalanceActionTest.php new file mode 100644 index 00000000..c08352ce --- /dev/null +++ b/tests/Unit/Api/Controller/Action/GiftCardBalanceActionTest.php @@ -0,0 +1,56 @@ +setCurrencyCode('EUR'); + $giftCard1->setAmount(25); + $giftCard2 = new GiftCard(); + $giftCard2->setCurrencyCode('EUR'); + $giftCard2->setAmount(56); + + $giftCard3 = new GiftCard(); + $giftCard3->setCurrencyCode('USD'); + $giftCard3->setAmount(31); + $giftCard4 = new GiftCard(); + $giftCard4->setCurrencyCode('USD'); + $giftCard4->setAmount(84); + $giftCard5 = new GiftCard(); + $giftCard5->setCurrencyCode('USD'); + $giftCard5->setAmount(86); + + $expectedBalanceCollection = new GiftCardBalanceCollection(); + $expectedBalanceCollection->addGiftCard($giftCard1); + $expectedBalanceCollection->addGiftCard($giftCard2); + $expectedBalanceCollection->addGiftCard($giftCard3); + $expectedBalanceCollection->addGiftCard($giftCard4); + $expectedBalanceCollection->addGiftCard($giftCard5); + + $giftCardRepository = $this->prophesize(GiftCardRepositoryInterface::class); + + $giftCardRepository->findEnabled()->willReturn([$giftCard1, $giftCard2, $giftCard3, $giftCard4, $giftCard5]); + + $giftCardBalanceAction = new GiftCardBalanceAction($giftCardRepository->reveal()); + $returnedBalanceCollection = $giftCardBalanceAction(); + + self::assertEquals($expectedBalanceCollection, $returnedBalanceCollection); + } +}