diff --git a/src/Resources/config/routing/checkout.yml b/src/Resources/config/routing/checkout.yml index aab7471b0..0d03bb2b8 100644 --- a/src/Resources/config/routing/checkout.yml +++ b/src/Resources/config/routing/checkout.yml @@ -1,3 +1,30 @@ +sylius_shop_api_country_checkout: + path: /countries + methods: [GET] + controller: sylius.controller.country:indexAction + defaults: + _sylius: + paginate: false + filterable: true + criteria: + enabled: true + sorting: + name: asc + serialization_groups: [Default] + +sylius_shop_api_country_show_details_by_code_checkout: + path: /countries/{code} + methods: [GET] + controller: sylius.controller.country:showAction + defaults: + _sylius: + paginate: false + filterable: true + criteria: + enabled: true + code: $code + serialization_groups: [Default, Detailed] + sylius_shop_api_address_checkout: path: /{token}/address methods: [PUT] diff --git a/src/Resources/config/serializer/Address/Model.Country.yml b/src/Resources/config/serializer/Address/Model.Country.yml new file mode 100644 index 000000000..89bf56ba2 --- /dev/null +++ b/src/Resources/config/serializer/Address/Model.Country.yml @@ -0,0 +1,20 @@ +Sylius\Component\Addressing\Model\Country: + exclusion_policy: ALL + xml_root_name: country + properties: + id: + expose: true + type: integer + xml_attribute: true + code: + expose: true + type: string + xml_attribute: true + groups: [Default, Detailed] + provinces: + expose: true + groups: [Detailed] + virtual_properties: + getName: + serialized_name: name + groups: [Default, Detailed] diff --git a/tests/Controller/Checkout/CountriesApiTest.php b/tests/Controller/Checkout/CountriesApiTest.php new file mode 100644 index 000000000..967ebec6c --- /dev/null +++ b/tests/Controller/Checkout/CountriesApiTest.php @@ -0,0 +1,50 @@ +loadFixturesFromFiles(['country.yml']); + + $this->client->request('GET', '/shop-api/checkout/countries', [], [], self::CONTENT_TYPE_HEADER); + $response = $this->client->getResponse(); + + $this->assertResponse($response, 'checkout/all_countries_response', Response::HTTP_OK); + } + + /** + * @test + */ + public function it_shows_country_details_by_code(): void + { + $this->loadFixturesFromFiles(['country.yml']); + + $this->client->request('GET', '/shop-api/checkout/countries/GB', [], [], self::CONTENT_TYPE_HEADER); + $response = $this->client->getResponse(); + + $this->assertResponse($response, 'checkout/one_of_countries_response', Response::HTTP_OK); + } + + /** + * @test + */ + public function it_fails_with_404_when_country_code_does_not_exist(): void + { + $this->loadFixturesFromFiles(['country.yml']); + + $this->client->request('GET', '/shop-api/checkout/countries/XX', [], [], self::CONTENT_TYPE_HEADER); + $response = $this->client->getResponse(); + + $this->assertResponseCode($response, Response::HTTP_NOT_FOUND); + } +} diff --git a/tests/Responses/Expected/checkout/all_countries_response.json b/tests/Responses/Expected/checkout/all_countries_response.json new file mode 100644 index 000000000..b1a606e26 --- /dev/null +++ b/tests/Responses/Expected/checkout/all_countries_response.json @@ -0,0 +1,7 @@ +[ + { + "id": @integer@, + "code": "GB", + "name": "United Kingdom" + } +] diff --git a/tests/Responses/Expected/checkout/one_of_countries_response.json b/tests/Responses/Expected/checkout/one_of_countries_response.json new file mode 100644 index 000000000..6f8626a24 --- /dev/null +++ b/tests/Responses/Expected/checkout/one_of_countries_response.json @@ -0,0 +1,27 @@ +{ + "id": @integer@, + "name": "United Kingdom", + "code": "GB", + "provinces": [ + { + "id": @integer@, + "code": "GB-ENG", + "name": "England" + }, + { + "id": @integer@, + "code": "GB-NIR", + "name": "Northern Ireland" + }, + { + "id": @integer@, + "code": "GB-SCT", + "name": "Scotland" + }, + { + "id": @integer@, + "code": "GB-WLS", + "name": "Wales" + } + ] +}