From 5a06ac44bb83f678bcb6e677b50c42fe6c19480d Mon Sep 17 00:00:00 2001 From: Jan Goralski Date: Mon, 5 Dec 2022 13:51:41 +0100 Subject: [PATCH] [API][ExchangeRate] Update mapping --- .../config/api_resources/ExchangeRate.xml | 6 + tests/Api/Admin/ExchangeRatesTest.php | 131 ++++++++++++++++++ tests/Api/DataFixtures/ORM/exchange_rate.yaml | 1 - .../get_exchange_rate_response.json | 9 ++ .../get_exchange_rates_response.json | 53 +++++++ .../post_exchange_rate_response.json | 9 ++ .../put_exchange_rate_response.json | 9 ++ 7 files changed, 217 insertions(+), 1 deletion(-) create mode 100644 tests/Api/Admin/ExchangeRatesTest.php create mode 100644 tests/Api/Responses/Expected/admin/exchange_rate/get_exchange_rate_response.json create mode 100644 tests/Api/Responses/Expected/admin/exchange_rate/get_exchange_rates_response.json create mode 100644 tests/Api/Responses/Expected/admin/exchange_rate/post_exchange_rate_response.json create mode 100644 tests/Api/Responses/Expected/admin/exchange_rate/put_exchange_rate_response.json diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/ExchangeRate.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/ExchangeRate.xml index 5aec1b5a1f..0a3a3abaf6 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/ExchangeRate.xml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/ExchangeRate.xml @@ -44,6 +44,9 @@ admin:exchange_rate:create + + admin:exchange_rate:read + @@ -70,6 +73,9 @@ admin:exchange_rate:update + + admin:exchange_rate:read + diff --git a/tests/Api/Admin/ExchangeRatesTest.php b/tests/Api/Admin/ExchangeRatesTest.php new file mode 100644 index 0000000000..7dbd1bf05a --- /dev/null +++ b/tests/Api/Admin/ExchangeRatesTest.php @@ -0,0 +1,131 @@ +loadFixturesFromFiles([ + 'authentication/api_administrator.yaml', + 'channel.yaml', + 'exchange_rate.yaml', + ]); + $header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER); + + /** @var ExchangeRateInterface $exchangeRate */ + $exchangeRate = $fixtures['exchange_rate_CNYUSD']; + + $this->client->request( + method: 'GET', + uri: sprintf('/api/v2/admin/exchange-rates/%s', $exchangeRate->getId()), + server: $header, + ); + + $this->assertResponse( + $this->client->getResponse(), + 'admin/exchange_rate/get_exchange_rate_response', + Response::HTTP_OK, + ); + } + + /** @test */ + public function it_gets_exchange_rates(): void + { + $this->loadFixturesFromFiles([ + 'authentication/api_administrator.yaml', + 'channel.yaml', + 'exchange_rate.yaml', + ]); + $header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER); + + $this->client->request( + method: 'GET', + uri: '/api/v2/admin/exchange-rates', + server: $header, + ); + + $this->assertResponse( + $this->client->getResponse(), + 'admin/exchange_rate/get_exchange_rates_response', + Response::HTTP_OK, + ); + } + + /** @test */ + public function it_creates_an_exchange_rate(): void + { + $this->loadFixturesFromFiles([ + 'authentication/api_administrator.yaml', + 'channel.yaml', + 'exchange_rate.yaml', + ]); + $header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER); + + $this->client->request( + method: 'POST', + uri: '/api/v2/admin/exchange-rates', + server: $header, + content: json_encode([ + 'ratio' => '3.2', + "sourceCurrency" => "/api/v2/admin/currencies/CNY", + "targetCurrency" => "/api/v2/admin/currencies/PLN", + ], JSON_THROW_ON_ERROR), + ); + + $this->assertResponse( + $this->client->getResponse(), + 'admin/exchange_rate/post_exchange_rate_response', + Response::HTTP_CREATED, + ); + } + + /** @test */ + public function it_updates_an_existing_exchange_rate(): void + { + $fixtures = $this->loadFixturesFromFiles([ + 'authentication/api_administrator.yaml', + 'channel.yaml', + 'exchange_rate.yaml', + ]); + $header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER); + + /** @var ExchangeRateInterface $exchangeRate */ + $exchangeRate = $fixtures['exchange_rate_CNYUSD']; + + + $this->client->request( + method: 'PUT', + uri: '/api/v2/admin/exchange-rates/' . $exchangeRate->getId(), + server: $header, + content: json_encode([ + 'ratio' => '0.25', + ], JSON_THROW_ON_ERROR), + ); + + $this->assertResponse( + $this->client->getResponse(), + 'admin/exchange_rate/put_exchange_rate_response', + Response::HTTP_OK, + ); + } +} diff --git a/tests/Api/DataFixtures/ORM/exchange_rate.yaml b/tests/Api/DataFixtures/ORM/exchange_rate.yaml index 14d67bfef0..f712b7262b 100644 --- a/tests/Api/DataFixtures/ORM/exchange_rate.yaml +++ b/tests/Api/DataFixtures/ORM/exchange_rate.yaml @@ -16,7 +16,6 @@ Sylius\Component\Currency\Model\ExchangeRate: targetCurrency: '@currency_btn' ratio: 2.37 - Sylius\Component\Currency\Model\Currency: currency_pln: code: 'PLN' diff --git a/tests/Api/Responses/Expected/admin/exchange_rate/get_exchange_rate_response.json b/tests/Api/Responses/Expected/admin/exchange_rate/get_exchange_rate_response.json new file mode 100644 index 0000000000..dd92a966c4 --- /dev/null +++ b/tests/Api/Responses/Expected/admin/exchange_rate/get_exchange_rate_response.json @@ -0,0 +1,9 @@ +{ + "@context": "\/api\/v2\/contexts\/ExchangeRate", + "@id": "\/api\/v2\/admin\/exchange-rates\/@integer@", + "@type": "ExchangeRate", + "id": @integer@, + "ratio": 0.15, + "sourceCurrency": "\/api\/v2\/admin\/currencies\/CNY", + "targetCurrency": "\/api\/v2\/admin\/currencies\/USD" +} diff --git a/tests/Api/Responses/Expected/admin/exchange_rate/get_exchange_rates_response.json b/tests/Api/Responses/Expected/admin/exchange_rate/get_exchange_rates_response.json new file mode 100644 index 0000000000..23f4a867c7 --- /dev/null +++ b/tests/Api/Responses/Expected/admin/exchange_rate/get_exchange_rates_response.json @@ -0,0 +1,53 @@ +{ + "@context": "\/api\/v2\/contexts\/ExchangeRate", + "@id": "\/api\/v2\/admin\/exchange-rates", + "@type": "hydra:Collection", + "hydra:member": [ + { + "@id": "\/api\/v2\/admin\/exchange-rates\/@integer@", + "@type": "ExchangeRate", + "id": @integer@, + "ratio": 4.44, + "sourceCurrency": "\/api\/v2\/admin\/currencies\/USD", + "targetCurrency": "\/api\/v2\/admin\/currencies\/PLN" + }, + { + "@id": "\/api\/v2\/admin\/exchange-rates\/@integer@", + "@type": "ExchangeRate", + "id": @integer@, + "ratio": 0.7, + "sourceCurrency": "\/api\/v2\/admin\/currencies\/USD", + "targetCurrency": "\/api\/v2\/admin\/currencies\/GBP" + }, + { + "@id": "\/api\/v2\/admin\/exchange-rates\/@integer@", + "@type": "ExchangeRate", + "id": @integer@, + "ratio": 0.15, + "sourceCurrency": "\/api\/v2\/admin\/currencies\/CNY", + "targetCurrency": "\/api\/v2\/admin\/currencies\/USD" + }, + { + "@id": "\/api\/v2\/admin\/exchange-rates\/@integer@", + "@type": "ExchangeRate", + "id": @integer@, + "ratio": 2.37, + "sourceCurrency": "\/api\/v2\/admin\/currencies\/GBP", + "targetCurrency": "\/api\/v2\/admin\/currencies\/BTN" + } + ], + "hydra:totalItems": 4, + "hydra:search": { + "@type": "hydra:IriTemplate", + "hydra:template": "\/api\/v2\/admin\/exchange-rates{?currencyCode}", + "hydra:variableRepresentation": "BasicRepresentation", + "hydra:mapping": [ + { + "@type": "IriTemplateMapping", + "variable": "currencyCode", + "property": "currencyCode", + "required": false + } + ] + } +} diff --git a/tests/Api/Responses/Expected/admin/exchange_rate/post_exchange_rate_response.json b/tests/Api/Responses/Expected/admin/exchange_rate/post_exchange_rate_response.json new file mode 100644 index 0000000000..6395c054a5 --- /dev/null +++ b/tests/Api/Responses/Expected/admin/exchange_rate/post_exchange_rate_response.json @@ -0,0 +1,9 @@ +{ + "@context": "\/api\/v2\/contexts\/ExchangeRate", + "@id": "\/api\/v2\/admin\/exchange-rates\/@integer@", + "@type": "ExchangeRate", + "id": @integer@, + "ratio": 3.2, + "sourceCurrency": "\/api\/v2\/admin\/currencies\/CNY", + "targetCurrency": "\/api\/v2\/admin\/currencies\/PLN" +} diff --git a/tests/Api/Responses/Expected/admin/exchange_rate/put_exchange_rate_response.json b/tests/Api/Responses/Expected/admin/exchange_rate/put_exchange_rate_response.json new file mode 100644 index 0000000000..4fdfa6c63c --- /dev/null +++ b/tests/Api/Responses/Expected/admin/exchange_rate/put_exchange_rate_response.json @@ -0,0 +1,9 @@ +{ + "@context": "\/api\/v2\/contexts\/ExchangeRate", + "@id": "\/api\/v2\/admin\/exchange-rates\/@integer@", + "@type": "ExchangeRate", + "id": @integer@, + "ratio": 0.25, + "sourceCurrency": "\/api\/v2\/admin\/currencies\/CNY", + "targetCurrency": "\/api\/v2\/admin\/currencies\/USD" +}