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

Adds an endpoint to get a list of countries, and provinces by country #722

Merged
merged 13 commits into from
Jul 1, 2022
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
27 changes: 27 additions & 0 deletions src/Resources/config/routing/checkout.yml
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
20 changes: 20 additions & 0 deletions src/Resources/config/serializer/Address/Model.Country.yml
Original file line number Diff line number Diff line change
@@ -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]
50 changes: 50 additions & 0 deletions tests/Controller/Checkout/CountriesApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

medteck marked this conversation as resolved.
Show resolved Hide resolved
namespace Tests\Sylius\ShopApiPlugin\Controller\Checkout;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing license block

use Symfony\Component\HttpFoundation\Response;
use Tests\Sylius\ShopApiPlugin\Controller\JsonApiTestCase;

final class CountriesApiTest extends JsonApiTestCase
{
/**
* @test
*/
public function it_shows_list_of_all_countries(): void
{
$this->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);
}
}
7 changes: 7 additions & 0 deletions tests/Responses/Expected/checkout/all_countries_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"id": @integer@,
"code": "GB",
"name": "United Kingdom"
}
]
27 changes: 27 additions & 0 deletions tests/Responses/Expected/checkout/one_of_countries_response.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}