From 86b16e2c0b55c2dc52cf4cc98f777b802f25d756 Mon Sep 17 00:00:00 2001 From: Jan Goralski Date: Mon, 28 Nov 2022 12:34:56 +0100 Subject: [PATCH] [API][Currency] Update mapping --- .../config/api_resources/Currency.xml | 7 ++ .../config/serialization/Currency.xml | 5 +- tests/Api/Admin/CurrenciesTest.php | 102 ++++++++++++++++++ tests/Api/DataFixtures/ORM/currency.yaml | 4 + .../currency/get_currencies_response.json | 26 +++++ .../admin/currency/get_currency_response.json | 7 ++ .../currency/post_currency_response.json | 7 ++ 7 files changed, 154 insertions(+), 4 deletions(-) create mode 100644 tests/Api/Admin/CurrenciesTest.php create mode 100644 tests/Api/Responses/Expected/admin/currency/get_currencies_response.json create mode 100644 tests/Api/Responses/Expected/admin/currency/get_currency_response.json create mode 100644 tests/Api/Responses/Expected/admin/currency/post_currency_response.json 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 50155a3016..808014d315 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Currency.xml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Currency.xml @@ -30,6 +30,12 @@ POST /admin/currencies + + admin:currency:create + + + admin:currency:read + @@ -65,5 +71,6 @@ + diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/Currency.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/Currency.xml index fcdbdefb98..f2c9d1d989 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/Currency.xml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/Currency.xml @@ -16,12 +16,9 @@ xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping https://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd" > - - admin:currency:read - shop:currency:read - admin:currency:read + admin:currency:create shop:currency:read diff --git a/tests/Api/Admin/CurrenciesTest.php b/tests/Api/Admin/CurrenciesTest.php new file mode 100644 index 0000000000..d637a3b70a --- /dev/null +++ b/tests/Api/Admin/CurrenciesTest.php @@ -0,0 +1,102 @@ +loadFixturesFromFiles(['authentication/api_administrator.yaml', 'currency.yaml']); + $header = $this->getLoggedHeader(); + + /** @var CurrencyInterface $currency */ + $currency = $fixtures['currency_gbp']; + + $this->client->request( + 'GET', + sprintf('/api/v2/admin/currencies/%s', $currency->getCode()), + [], + [], + $header, + ); + + $this->assertResponse( + $this->client->getResponse(), + 'admin/currency/get_currency_response', + Response::HTTP_OK, + ); + } + + /** @test */ + public function it_gets_currencies(): void + { + $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'currency.yaml']); + $header = $this->getLoggedHeader(); + + $this->client->request( + 'GET', + '/api/v2/admin/currencies', + [], + [], + $header, + ); + + $this->assertResponse( + $this->client->getResponse(), + 'admin/currency/get_currencies_response', + Response::HTTP_OK, + ); + } + + /** @test */ + public function it_creates_a_currency(): void + { + $this->loadFixturesFromFiles(['authentication/api_administrator.yaml']); + $header = $this->getLoggedHeader(); + + $this->client->request( + 'POST', + '/api/v2/admin/currencies', + [], + [], + $header, + json_encode([ + 'code' => 'KRW', + ], JSON_THROW_ON_ERROR), + ); + + $this->assertResponse( + $this->client->getResponse(), + 'admin/currency/post_currency_response', + Response::HTTP_CREATED, + ); + } + + private function getLoggedHeader(): array + { + $token = $this->logInAdminUser('api@example.com'); + $authorizationHeader = self::$kernel->getContainer()->getParameter('sylius.api.authorization_header'); + $header['HTTP_' . $authorizationHeader] = 'Bearer ' . $token; + + return array_merge($header, self::CONTENT_TYPE_HEADER); + } +} diff --git a/tests/Api/DataFixtures/ORM/currency.yaml b/tests/Api/DataFixtures/ORM/currency.yaml index 5b7399c731..fbdadb92fc 100644 --- a/tests/Api/DataFixtures/ORM/currency.yaml +++ b/tests/Api/DataFixtures/ORM/currency.yaml @@ -1,3 +1,7 @@ Sylius\Component\Currency\Model\Currency: currency_usd: code: 'USD' + currency_gbp: + code: 'GBP' + currency_eur: + code: 'EUR' diff --git a/tests/Api/Responses/Expected/admin/currency/get_currencies_response.json b/tests/Api/Responses/Expected/admin/currency/get_currencies_response.json new file mode 100644 index 0000000000..46b55c4d1f --- /dev/null +++ b/tests/Api/Responses/Expected/admin/currency/get_currencies_response.json @@ -0,0 +1,26 @@ +{ + "@context": "\/api\/v2\/contexts\/Currency", + "@id": "\/api\/v2\/admin\/currencies", + "@type": "hydra:Collection", + "hydra:member": [ + { + "@id": "\/api\/v2\/admin\/currencies\/USD", + "@type": "Currency", + "code": "USD", + "name": "US Dollar" + }, + { + "@id": "\/api\/v2\/admin\/currencies\/GBP", + "@type": "Currency", + "code": "GBP", + "name": "British Pound" + }, + { + "@id": "\/api\/v2\/admin\/currencies\/EUR", + "@type": "Currency", + "code": "EUR", + "name": "Euro" + } + ], + "hydra:totalItems": 3 +} diff --git a/tests/Api/Responses/Expected/admin/currency/get_currency_response.json b/tests/Api/Responses/Expected/admin/currency/get_currency_response.json new file mode 100644 index 0000000000..4cf98a3988 --- /dev/null +++ b/tests/Api/Responses/Expected/admin/currency/get_currency_response.json @@ -0,0 +1,7 @@ +{ + "@context": "\/api\/v2\/contexts\/Currency", + "@id": "\/api\/v2\/admin\/currencies\/GBP", + "@type": "Currency", + "code": "GBP", + "name": "British Pound" +} diff --git a/tests/Api/Responses/Expected/admin/currency/post_currency_response.json b/tests/Api/Responses/Expected/admin/currency/post_currency_response.json new file mode 100644 index 0000000000..9a77053e36 --- /dev/null +++ b/tests/Api/Responses/Expected/admin/currency/post_currency_response.json @@ -0,0 +1,7 @@ +{ + "@context": "\/api\/v2\/contexts\/Currency", + "@id": "\/api\/v2\/admin\/currencies\/KRW", + "@type": "Currency", + "code": "KRW", + "name": "South Korean Won" +}