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

Expose Gift card balance #144

Merged
merged 1 commit into from
Sep 1, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Api/Controller/Action/GiftCardBalanceAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusGiftCardPlugin\Api\Controller\Action;

use Setono\SyliusGiftCardPlugin\Model\GiftCardBalanceCollection;
use Setono\SyliusGiftCardPlugin\Repository\GiftCardRepositoryInterface;

final class GiftCardBalanceAction
{
private GiftCardRepositoryInterface $giftCardRepository;

public function __construct(GiftCardRepositoryInterface $giftCardRepository)
{
$this->giftCardRepository = $giftCardRepository;
}

public function __invoke(): GiftCardBalanceCollection
{
return GiftCardBalanceCollection::createFromGiftCards(
$this->giftCardRepository->findEnabled()
Roshyo marked this conversation as resolved.
Show resolved Hide resolved
);
}
}
14 changes: 14 additions & 0 deletions src/Resources/config/api_resources/GiftCard.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@
</attribute>
</collectionOperation>

<collectionOperation name="admin_get_balance">
<attribute name="method">GET</attribute>
<attribute name="path">/admin/gift-cards/balance</attribute>
<attribute name="controller">setono_sylius_gift_card.controller.action.get_balance</attribute>
<attribute name="pagination_enabled">false</attribute>
<attribute name="openapi_context">
<attribute name="summary">Get the gift cards balance.</attribute>
</attribute>
<attribute name="normalization_context">
<attribute name="groups">admin:gift_card_balance:read</attribute>
</attribute>
<attribute name="output">Setono\SyliusGiftCardPlugin\Model\GiftCardBalance</attribute>
</collectionOperation>

<collectionOperation name="shop_get">
<attribute name="method">GET</attribute>
<attribute name="path">/shop/gift-cards</attribute>
Expand Down
20 changes: 20 additions & 0 deletions src/Resources/config/serialization/GiftCardBalance.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" ?>

<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping https://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
>
<class name="Setono\SyliusGiftCardPlugin\Model\GiftCardBalance">
<attribute name="currencyCode">
<group>admin:gift_card_balance:read</group>
</attribute>

<attribute name="count">
<group>admin:gift_card_balance:read</group>
</attribute>

<attribute name="total">
<group>admin:gift_card_balance:read</group>
</attribute>
</class>
</serializer>
7 changes: 7 additions & 0 deletions src/Resources/config/services/api/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,12 @@

<tag name="controller.service_arguments" />
</service>

<service id="setono_sylius_gift_card.controller.action.get_balance"
class="Setono\SyliusGiftCardPlugin\Api\Controller\Action\GiftCardBalanceAction">
<argument type="service" id="setono_sylius_gift_card.repository.gift_card"/>

<tag name="controller.service_arguments" />
</service>
</services>
</container>
56 changes: 56 additions & 0 deletions tests/Unit/Api/Controller/Action/GiftCardBalanceActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Tests\Setono\SyliusGiftCardPlugin\Unit\Api\Controller\Action;

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Setono\SyliusGiftCardPlugin\Api\Controller\Action\GiftCardBalanceAction;
use Setono\SyliusGiftCardPlugin\Model\GiftCard;
use Setono\SyliusGiftCardPlugin\Model\GiftCardBalanceCollection;
use Setono\SyliusGiftCardPlugin\Repository\GiftCardRepositoryInterface;

final class GiftCardBalanceActionTest extends TestCase
{
use ProphecyTrait;

/**
* @test
*/
public function it_returns_the_balance(): void
{
$giftCard1 = new GiftCard();
$giftCard1->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);
}
}