From f3824d6f041792e9270c770b7eecb299fcf7dc52 Mon Sep 17 00:00:00 2001 From: Paula Date: Thu, 11 Nov 2021 16:42:33 +0100 Subject: [PATCH] List of channel currencies endpoint --- .../switching_current_currency.feature | 7 +- .../Context/Api/Shop/CurrencyContext.php | 57 ++++++++++++ .../Behat/Resources/config/services/api.xml | 5 ++ .../config/services/contexts/api/shop.xml | 6 ++ .../config/suites/api/currency/currencies.yml | 1 + .../CurrencyCollectionDataProvider.php | 61 +++++++++++++ .../config/api_resources/Currency.xml | 9 ++ .../config/services/data_providers.xml | 6 ++ .../CurrencyCollectionDataProviderSpec.php | 87 +++++++++++++++++++ 9 files changed, 238 insertions(+), 1 deletion(-) create mode 100644 src/Sylius/Behat/Context/Api/Shop/CurrencyContext.php create mode 100644 src/Sylius/Bundle/ApiBundle/DataProvider/CurrencyCollectionDataProvider.php create mode 100644 src/Sylius/Bundle/ApiBundle/spec/DataProvider/CurrencyCollectionDataProviderSpec.php diff --git a/features/currency/switching_current_currency.feature b/features/currency/switching_current_currency.feature index 54d73f699e..9b52b9f2a5 100644 --- a/features/currency/switching_current_currency.feature +++ b/features/currency/switching_current_currency.feature @@ -13,11 +13,16 @@ Feature: Switching the current currency When I browse that channel Then I should shop using the "EUR" currency - @ui + @ui @no-api Scenario: Showing available currencies When I browse that channel Then I should be able to shop using the "USD" currency + @api + Scenario: Showing available currencies + When I browse currencies + Then I should see "USD" and "EUR" in the list + @ui @no-api Scenario: Switching the current currency When I browse that channel diff --git a/src/Sylius/Behat/Context/Api/Shop/CurrencyContext.php b/src/Sylius/Behat/Context/Api/Shop/CurrencyContext.php new file mode 100644 index 0000000000..530b173386 --- /dev/null +++ b/src/Sylius/Behat/Context/Api/Shop/CurrencyContext.php @@ -0,0 +1,57 @@ +client = $client; + $this->responseChecker = $responseChecker; + } + + /** + * @When /^I (?:start browsing|try to browse|browse) currencies$/ + */ + public function iBrowseCurrencies(): void + { + $this->client->index(); + } + + /** + * @Then I should see :firstCurrency in the list + * @Then I should see :firstCurrency and :secondCurrency in the list + * @Then I should see :firstCurrency, :secondCurrency and :thirdCurrency in the list + */ + public function iShouldSeeCurrenciesInTheList(... $currencies): void + { + $response = $this->client->getLastResponse(); + + foreach ($currencies as $currency) { + $this->responseChecker->getCollectionItemsWithValue($response, 'code', $currency); + } + } +} diff --git a/src/Sylius/Behat/Resources/config/services/api.xml b/src/Sylius/Behat/Resources/config/services/api.xml index 155f9a3d82..c968f1511a 100644 --- a/src/Sylius/Behat/Resources/config/services/api.xml +++ b/src/Sylius/Behat/Resources/config/services/api.xml @@ -44,6 +44,11 @@ shop + + currencies + shop + + countries admin diff --git a/src/Sylius/Behat/Resources/config/services/contexts/api/shop.xml b/src/Sylius/Behat/Resources/config/services/contexts/api/shop.xml index ce0cb2a387..1f0d4525d0 100644 --- a/src/Sylius/Behat/Resources/config/services/contexts/api/shop.xml +++ b/src/Sylius/Behat/Resources/config/services/contexts/api/shop.xml @@ -32,6 +32,12 @@ + + + + + + diff --git a/src/Sylius/Behat/Resources/config/suites/api/currency/currencies.yml b/src/Sylius/Behat/Resources/config/suites/api/currency/currencies.yml index f682f46025..31867e582f 100644 --- a/src/Sylius/Behat/Resources/config/suites/api/currency/currencies.yml +++ b/src/Sylius/Behat/Resources/config/suites/api/currency/currencies.yml @@ -16,6 +16,7 @@ default: - sylius.behat.context.setup.currency - sylius.behat.context.api.shop.channel + - sylius.behat.context.api.shop.currency filters: tags: "@currencies&&@api" diff --git a/src/Sylius/Bundle/ApiBundle/DataProvider/CurrencyCollectionDataProvider.php b/src/Sylius/Bundle/ApiBundle/DataProvider/CurrencyCollectionDataProvider.php new file mode 100644 index 0000000000..bcdc048735 --- /dev/null +++ b/src/Sylius/Bundle/ApiBundle/DataProvider/CurrencyCollectionDataProvider.php @@ -0,0 +1,61 @@ +userContext = $userContext; + $this->currencyRepository = $currencyRepository; + } + + public function supports(string $resourceClass, string $operationName = null, array $context = []): bool + { + return is_a($resourceClass, CurrencyInterface::class, true); + } + + public function getCollection(string $resourceClass, string $operationName = null, array $context = []) + { + $user = $this->userContext->getUser(); + + if ($user instanceof AdminUserInterface && in_array('ROLE_API_ACCESS', $user->getRoles())) { + return $this->currencyRepository->findAll(); + } + + Assert::keyExists($context, ContextKeys::CHANNEL); + + /** @var ChannelInterface $channel */ + $channel = $context[ContextKeys::CHANNEL]; + + return $channel->getCurrencies(); + } +} diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Currency.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Currency.xml index af8cd48405..50155a3016 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Currency.xml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Currency.xml @@ -31,6 +31,15 @@ POST /admin/currencies + + + GET + shop + /shop/currencies + + shop:currency:read + + diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/services/data_providers.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/services/data_providers.xml index f30ca2c40e..9176383a97 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/services/data_providers.xml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/services/data_providers.xml @@ -66,6 +66,12 @@ + + + + + + diff --git a/src/Sylius/Bundle/ApiBundle/spec/DataProvider/CurrencyCollectionDataProviderSpec.php b/src/Sylius/Bundle/ApiBundle/spec/DataProvider/CurrencyCollectionDataProviderSpec.php new file mode 100644 index 0000000000..2002988b56 --- /dev/null +++ b/src/Sylius/Bundle/ApiBundle/spec/DataProvider/CurrencyCollectionDataProviderSpec.php @@ -0,0 +1,87 @@ +beConstructedWith($currencyRepository, $userContext); + } + + function it_supports_only_currencies(): void + { + $this->supports(ProductInterface::class, 'get')->shouldReturn(false); + $this->supports(CurrencyInterface::class, 'get')->shouldReturn(true); + } + + function it_throws_an_exception_if_context_has_not_channel(): void + { + $this + ->shouldThrow(\InvalidArgumentException::class) + ->during('getCollection', [CurrencyInterface::class, 'shop_get', []]) + ; + } + + function it_provides_currencies_available_in_channel_if_user_is_not_admin( + UserContextInterface $userContext, + UserInterface $user, + ChannelInterface $channel, + CurrencyInterface $firstCurrency, + CurrencyInterface $secondCurrency + ): void { + $userContext->getUser()->willReturn($user); + $user->getRoles()->willReturn(['ROLE_USER']); + + $channel->addCurrency($firstCurrency); + $channel->addCurrency($secondCurrency); + + $channel->getCurrencies()->willReturn(new ArrayCollection([$firstCurrency->getWrappedObject(), $secondCurrency->getWrappedObject()])); + + $this + ->getCollection(CurrencyInterface::class, 'get', [ContextKeys::CHANNEL => $channel]) + ->shouldBeLike(new ArrayCollection([$firstCurrency->getWrappedObject(), $secondCurrency->getWrappedObject()])) + ; + } + + function it_provides_all_currencies_if_user_is_admin( + UserContextInterface $userContext, + AdminUserInterface $user, + ChannelInterface $channel, + CurrencyInterface $firstCurrency, + CurrencyInterface $secondCurrency, + RepositoryInterface $currencyRepository + ): void { + $userContext->getUser()->willReturn($user); + $user->getRoles()->willReturn(['ROLE_API_ACCESS']); + + $currencyRepository->findAll()->willReturn([$firstCurrency, $secondCurrency]); + + $this + ->getCollection(CurrencyInterface::class, 'get', [ContextKeys::CHANNEL => $channel]) + ->shouldReturn([$firstCurrency, $secondCurrency]) + ; + } +}