[API][Country] Update mapping

This commit is contained in:
Jan Goralski 2022-11-28 12:14:32 +01:00
parent d7d23046df
commit 3ec9e1de98
No known key found for this signature in database
GPG key ID: 95D91BA380F31EDD
16 changed files with 340 additions and 22 deletions

View file

@ -41,6 +41,9 @@
<attribute name="denormalization_context">
<attribute name="groups">admin:country:create</attribute>
</attribute>
<attribute name="normalization_context">
<attribute name="groups">admin:country:read</attribute>
</attribute>
</collectionOperation>
</collectionOperations>
@ -66,6 +69,9 @@
<attribute name="denormalization_context">
<attribute name="groups">admin:country:update</attribute>
</attribute>
<attribute name="normalization_context">
<attribute name="groups">admin:country:read</attribute>
</attribute>
</itemOperation>
</itemOperations>

View file

@ -16,9 +16,6 @@
xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping https://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
>
<class name="Sylius\Component\Addressing\Model\Country">
<attribute name="id">
<group>admin:country:read</group>
</attribute>
<attribute name="code">
<group>admin:country:read</group>
<group>admin:country:create</group>

View file

@ -0,0 +1,136 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Tests\Api\Admin;
use Sylius\Component\Addressing\Model\CountryInterface;
use Sylius\Tests\Api\JsonApiTestCase;
use Sylius\Tests\Api\Utils\AdminUserLoginTrait;
use Symfony\Component\HttpFoundation\Response;
final class CountriesTest extends JsonApiTestCase
{
use AdminUserLoginTrait;
/** @test */
public function it_gets_a_country(): void
{
$fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'country.yaml']);
$header = $this->getLoggedHeader();
/** @var CountryInterface $country */
$country = $fixtures['country_DE'];
$this->client->request(
'GET',
sprintf('/api/v2/admin/countries/%s', $country->getCode()),
[],
[],
$header,
);
$this->assertResponse(
$this->client->getResponse(),
'admin/country/get_country_response',
Response::HTTP_OK,
);
}
/** @test */
public function it_gets_countries(): void
{
$this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'country.yaml']);
$header = $this->getLoggedHeader();
$this->client->request(
'GET',
'/api/v2/admin/countries',
[],
[],
$header,
);
$this->assertResponse(
$this->client->getResponse(),
'admin/country/get_countries_response',
Response::HTTP_OK,
);
}
/** @test */
public function it_creates_a_country(): void
{
$this->loadFixturesFromFiles(['authentication/api_administrator.yaml']);
$header = $this->getLoggedHeader();
$this->client->request(
'POST',
'/api/v2/admin/countries',
[],
[],
$header,
json_encode([
'code' => 'IE',
'enabled' => true,
], JSON_THROW_ON_ERROR),
);
$this->assertResponse(
$this->client->getResponse(),
'admin/country/post_country_response',
Response::HTTP_CREATED,
);
}
/** @test */
public function it_updates_an_existing_country(): void
{
$fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'country.yaml']);
/** @var CountryInterface $country */
$country = $fixtures['country_US'];
$header = $this->getLoggedHeader();
$this->client->request(
'PUT',
'/api/v2/admin/countries/' . $country->getCode(),
[],
[],
$header,
json_encode([
'enabled' => false,
'provinces' => [[
'code' => 'US-WA',
'name' => 'Washington',
'country' => $country->getCode(),
]]
], JSON_THROW_ON_ERROR),
);
$this->assertResponse(
$this->client->getResponse(),
'admin/country/put_country_response',
Response::HTTP_OK,
);
}
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);
}
}

View file

@ -5,3 +5,13 @@ Sylius\Component\Addressing\Model\Country:
code: 'FR'
country_DE:
code: 'DE'
Sylius\Component\Addressing\Model\Province:
province_US_WY:
code: US-WY
name: Wyoming
country: "@country_US"
province_US_MI:
code: US-MI
name: Minnesota
country: "@country_US"

View file

@ -0,0 +1,35 @@
{
"@context": "\/api\/v2\/contexts\/Country",
"@id": "\/api\/v2\/admin\/countries",
"@type": "hydra:Collection",
"hydra:member": [
{
"@id": "\/api\/v2\/admin\/countries\/US",
"@type": "Country",
"code": "US",
"name": "United States",
"enabled": true,
"provinces": [
"\/api\/v2\/admin\/provinces\/US-MI",
"\/api\/v2\/admin\/provinces\/US-WY"
]
},
{
"@id": "\/api\/v2\/admin\/countries\/FR",
"@type": "Country",
"code": "FR",
"name": "France",
"enabled": true,
"provinces": []
},
{
"@id": "\/api\/v2\/admin\/countries\/DE",
"@type": "Country",
"code": "DE",
"name": "Germany",
"enabled": true,
"provinces": []
}
],
"hydra:totalItems": 3
}

View file

@ -0,0 +1,9 @@
{
"@context": "\/api\/v2\/contexts\/Country",
"@id": "\/api\/v2\/admin\/countries\/DE",
"@type": "Country",
"code": "DE",
"enabled": true,
"name": "Germany",
"provinces": []
}

View file

@ -0,0 +1,9 @@
{
"@context": "\/api\/v2\/contexts\/Country",
"@id": "\/api\/v2\/admin\/countries\/IE",
"@type": "Country",
"code": "IE",
"enabled": true,
"name": "Ireland",
"provinces": []
}

View file

@ -0,0 +1,11 @@
{
"@context": "\/api\/v2\/contexts\/Country",
"@id": "\/api\/v2\/admin\/countries\/US",
"@type": "Country",
"code": "US",
"enabled": false,
"name": "United States",
"provinces": [
"\/api\/v2\/admin\/provinces\/US-WA"
]
}

View file

@ -0,0 +1,16 @@
{
"@context": "\/api\/v2\/contexts\/Address",
"@id": "\/api\/v2\/shop\/addresses\/@integer@",
"@type": "Address",
"id": "@integer@",
"firstName":"TEST",
"lastName":"TEST",
"phoneNumber":"666111333",
"company":"Potato Corp.",
"countryCode":"US",
"provinceCode":"US-MI",
"provinceName":null,
"street":"Top secret",
"city":"Nebraska",
"postcode":"12343"
}

View file

@ -0,0 +1,16 @@
{
"@context": "\/api\/v2\/contexts\/Address",
"@id": "\/api\/v2\/shop\/addresses\/@integer@",
"@type": "Address",
"id": "@integer@",
"firstName":"TEST",
"lastName":"TEST",
"phoneNumber":"666111333",
"company":"Potato Corp.",
"countryCode":"DE",
"provinceCode":null,
"provinceName":"Munich",
"street":"Top secret",
"city":"Nebraska",
"postcode":"12343"
}

View file

@ -7,7 +7,7 @@
"lastName":"TEST",
"phoneNumber":"666111333",
"company":"Potato Corp.",
"countryCode":"US",
"countryCode":"DE",
"provinceCode":null,
"provinceName":null,
"street":"Top secret",

View file

@ -8,7 +8,20 @@
"@type": "Country",
"code": "US",
"name": "United States",
"provinces": []
"provinces": [
{
"@id": "\/api\/v2\/admin\/provinces\/US-MI",
"@type": "Province",
"code": "US-MI",
"name": "Minnesota"
},
{
"@id": "\/api\/v2\/admin\/provinces\/US-WY",
"@type": "Province",
"code": "US-WY",
"name": "Wyoming"
}
]
},
{
"@id": "\/api\/v2\/shop\/countries\/FR",

View file

@ -48,7 +48,7 @@ final class AddressesGetTest extends JsonApiTestCase
array_merge($authorizationHeader, self::CONTENT_TYPE_HEADER)
);
$this->assertResponse($this->client->getResponse(), 'shop/get_addresses_response');
$this->assertResponse($this->client->getResponse(), 'shop/address/get_addresses_response');
}
/** @test */
@ -70,6 +70,6 @@ final class AddressesGetTest extends JsonApiTestCase
array_merge($authorizationHeader, self::CONTENT_TYPE_HEADER)
);
$this->assertResponse($this->client->getResponse(), 'shop/get_an_address_response');
$this->assertResponse($this->client->getResponse(), 'shop/address/get_an_address_response');
}
}

View file

@ -14,6 +14,7 @@ declare(strict_types=1);
namespace Sylius\Tests\Api\Shop;
use Sylius\Component\Addressing\Model\CountryInterface;
use Sylius\Component\Addressing\Model\ProvinceInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Tests\Api\JsonApiTestCase;
use Symfony\Component\HttpFoundation\Response;
@ -25,7 +26,7 @@ final class AddressesPostTest extends JsonApiTestCase
{
$fixtures = $this->loadFixturesFromFiles(['authentication/customer.yaml', 'country.yaml']);
/** @var CountryInterface $country */
$country = $fixtures['country_US'];
$country = $fixtures['country_DE'];
$bodyRequest = $this->createBodyRequest($country->getCode());
@ -43,13 +44,69 @@ final class AddressesPostTest extends JsonApiTestCase
}
/** @test */
public function it_creates_new_address_for_logged_customer(): void
public function it_creates_new_address_for_logged_customer_with_country_with_provinces(): void
{
$fixtures = $this->loadFixturesFromFiles(['authentication/customer.yaml', 'country.yaml']);
/** @var CustomerInterface $customer */
$customer = $fixtures['customer_oliver'];
/** @var CountryInterface $country */
$country = $fixtures['country_US'];
/** @var ProvinceInterface $province */
$province = $fixtures['province_US_MI'];
$authorizationHeader = $this->getAuthorizationHeaderAsCustomer($customer->getEmailCanonical(), 'sylius');
$bodyRequest = $this->createBodyRequest($country->getCode(), $province->getCode());
$this->client->request(
'POST',
'/api/v2/shop/addresses',
[],
[],
array_merge($authorizationHeader, self::CONTENT_TYPE_HEADER),
json_encode($bodyRequest)
);
$response = $this->client->getResponse();
$this->assertResponse($response, 'shop/address/create_address_with_province_code_response', Response::HTTP_CREATED);
}
/** @test */
public function it_creates_new_address_for_logged_customer_without_province(): void
{
$fixtures = $this->loadFixturesFromFiles(['authentication/customer.yaml', 'country.yaml']);
/** @var CustomerInterface $customer */
$customer = $fixtures['customer_oliver'];
/** @var CountryInterface $country */
$country = $fixtures['country_DE'];
$authorizationHeader = $this->getAuthorizationHeaderAsCustomer($customer->getEmailCanonical(), 'sylius');
$bodyRequest = $this->createBodyRequest($country->getCode(), provinceName: 'Munich');
$this->client->request(
'POST',
'/api/v2/shop/addresses',
[],
[],
array_merge($authorizationHeader, self::CONTENT_TYPE_HEADER),
json_encode($bodyRequest)
);
$response = $this->client->getResponse();
$this->assertResponse($response, 'shop/address/create_address_with_province_name_response', Response::HTTP_CREATED);
}
/** @test */
public function it_creates_new_address_for_logged_customer_with_country_with_custom_provinces(): void
{
$fixtures = $this->loadFixturesFromFiles(['authentication/customer.yaml', 'country.yaml']);
/** @var CustomerInterface $customer */
$customer = $fixtures['customer_oliver'];
/** @var CountryInterface $country */
$country = $fixtures['country_DE'];
$authorizationHeader = $this->getAuthorizationHeaderAsCustomer($customer->getEmailCanonical(), 'sylius');
@ -66,22 +123,25 @@ final class AddressesPostTest extends JsonApiTestCase
$response = $this->client->getResponse();
$this->assertResponse($response, 'shop/create_address_response', Response::HTTP_CREATED);
$this->assertResponse($response, 'shop/address/create_address_without_province_response', Response::HTTP_CREATED);
}
private function createBodyRequest(string $countryCode): array
{
private function createBodyRequest(
string $countryCode,
?string $provinceCode = null,
?string $provinceName = null
): array {
return [
'firstName'=> 'TEST',
'lastName'=> 'TEST',
'phoneNumber'=> '666111333',
'company'=> 'Potato Corp.',
'countryCode'=> $countryCode,
'provinceCode'=> null,
'provinceName'=> null,
'street'=> 'Top secret',
'city'=> 'Nebraska',
'postcode'=> '12343'
'firstName' => 'TEST',
'lastName' => 'TEST',
'phoneNumber' => '666111333',
'company' => 'Potato Corp.',
'countryCode' => $countryCode,
'provinceCode' => $provinceCode,
'provinceName' => $provinceName,
'street' => 'Top secret',
'city' => 'Nebraska',
'postcode' => '12343',
];
}
}