add country to Api Platform

This commit is contained in:
Olivier ALLAIN 2020-03-23 09:15:30 +01:00
parent d69b0169c3
commit 45cf126d65
10 changed files with 310 additions and 5 deletions

View file

@ -7,7 +7,7 @@ Feature: Adding a new country
Background:
Given I am logged in as an administrator
@ui
@ui @api
Scenario: Adding country
When I want to add a new country
And I choose "United States"

View file

@ -8,7 +8,7 @@ Feature: Country unique code validation
Given the store operates in "Norway"
And I am logged in as an administrator
@ui
@ui @api
Scenario: Trying to add a new country with used code
When I want to add a new country
Then I should not be able to choose "Norway"

View file

@ -7,7 +7,7 @@ Feature: Editing country
Background:
Given I am logged in as an administrator
@ui
@ui @api
Scenario: Disabling country
Given the store has country "United States"
When I want to edit this country
@ -16,7 +16,7 @@ Feature: Editing country
Then I should be notified that it has been successfully edited
And this country should be disabled
@ui
@ui @api
Scenario: Enabling country
Given the store has disabled country "United States"
When I want to edit this country
@ -25,7 +25,7 @@ Feature: Editing country
Then I should be notified that it has been successfully edited
And this country should be enabled
@ui
@ui @api
Scenario: Seeing disabled code field while editing country
Given the store has country "United States"
When I want to edit this country

View file

@ -0,0 +1,193 @@
<?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\Behat\Context\Api\Admin;
use Behat\Behat\Context\Context;
use Sylius\Behat\Client\ApiClientInterface;
use Sylius\Behat\Client\ResponseCheckerInterface;
use Sylius\Component\Addressing\Model\CountryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\Intl\Countries;
use Symfony\Component\Intl\Intl;
use Symfony\Component\Serializer\SerializerInterface;
use Webmozart\Assert\Assert;
final class ManagingCountriesContext implements Context
{
/** @var ApiClientInterface */
private $client;
/** @var ResponseCheckerInterface */
private $responseChecker;
/** @var SerializerInterface */
private $serializer;
/** @var RepositoryInterface */
private $countryRepository;
public function __construct(
ApiClientInterface $client,
ResponseCheckerInterface $responseChecker,
SerializerInterface $serializer,
RepositoryInterface $countryRepository
) {
$this->client = $client;
$this->responseChecker = $responseChecker;
$this->serializer = $serializer;
$this->countryRepository = $countryRepository;
}
/**
* @When I want to add a new country
*/
public function iWantToAddANewCountry(): void
{
$this->client->buildCreateRequest();
}
/**
* @When I choose :countryName
*/
public function iChoose(string $countryName): void
{
$this->client->addRequestData('code', $this->getCountryCodeByName($countryName));
}
/**
* @When I add it
*/
public function iAddIt(): void
{
$this->client->create();
}
/**
* @Then I should be notified that it has been successfully created
*/
public function iShouldBeNotifiedThatItHasBeenSuccessfullyCreated(): void
{
Assert::true(
$this->responseChecker->isCreationSuccessful($this->client->getLastResponse()),
'Country could not be created'
);
}
/**
* @Then the country :countryName should appear in the store
*/
public function theCountryShouldAppearInTheStore(string $countryName): void
{
Assert::true(
$this->responseChecker->hasItemWithValue($this->client->index(), 'name', $countryName),
sprintf('There is no country with name "%s"', $countryName)
);
}
/**
* @Then I should not be able to choose :countryName
*/
public function iShouldNotBeAbleToChoose(string $countryName): void
{
$this->client->addRequestData('code', $this->getCountryCodeByName($countryName));
$response = $this->client->create();
Assert::false(
$this->responseChecker->isCreationSuccessful($response),
'Country has been created successfully, but it should not'
);
Assert::same($this->responseChecker->getError($response), 'code: Country ISO code must be unique.');
}
/**
* @When /^I want to edit (this country)$/
*/
public function iWantToEditThisCountry(CountryInterface $country): void
{
$this->client->buildUpdateRequest($country->getCode());
}
/**
* @When I enable it
*/
public function iEnableIt(): void
{
$this->client->addRequestData('enabled', true);
}
/**
* @When I disable it
*/
public function iDisableIt(): void
{
$this->client->addRequestData('enabled', false);
}
/**
* @When I save my changes
*/
public function iSaveMyChanges(): void
{
$this->client->update();
}
/**
* @Then I should be notified that it has been successfully edited
*/
public function iShouldBeNotifiedThatItHasBeenSuccessfullyEdited(): void
{
Assert::true(
$this->responseChecker->isUpdateSuccessful($this->client->getLastResponse()),
'Country could not be edited'
);
}
/**
* @Then /^(this country) should be ([^"]+)$/
*/
public function thisCountryShouldBeDisabled(CountryInterface $country, string $enabled): void
{
Assert::true(
$this->responseChecker->hasValue(
$this->client->show($country->getCode()),
'enabled',
$enabled === 'enabled' ? true : false
),
'Country is not disabled'
);
}
/**
* @Then the code field should be disabled
*/
public function theCodeFieldShouldBeDisabled(): void
{
$countryUpdateSerialised = $this->serializer->serialize(
$this->countryRepository->findOneBy([]),
'json', ['groups' => 'country:update']
);
Assert::keyNotExists(\json_decode($countryUpdateSerialised, true), 'code');
}
private function getCountryCodeByName(string $countryName): string
{
$countryList = array_flip(Countries::getNames());
Assert::keyExists(
$countryList,
$countryName,
sprintf('The country with name "%s" not found', $countryName)
);
return $countryList[$countryName];
}
}

View file

@ -25,6 +25,12 @@
<argument>avatar-images</argument>
</service>
<service id="sylius.behat.api_platform_client.country" class="Sylius\Behat\Client\ApiPlatformClient">
<argument type="service" id="test.client" />
<argument type="service" id="sylius.behat.shared_storage" />
<argument>countries</argument>
</service>
<service id="sylius.behat.api_platform_client.currency" class="Sylius\Behat\Client\ApiPlatformClient">
<argument type="service" id="test.client" />
<argument type="service" id="sylius.behat.shared_storage" />

View file

@ -31,6 +31,13 @@
<argument type="service" id="behat.mink.parameters" />
</service>
<service id="sylius.behat.context.api.admin.managing_countries" class="Sylius\Behat\Context\Api\Admin\ManagingCountriesContext">
<argument type="service" id="sylius.behat.api_platform_client.country" />
<argument type="service" id="Sylius\Behat\Client\ResponseCheckerInterface" />
<argument type="service" id="serializer" />
<argument type="service" id="sylius.repository.country" />
</service>
<service id="sylius.behat.context.api.admin.managing_currencies" class="Sylius\Behat\Context\Api\Admin\ManagingCurrenciesContext">
<argument type="service" id="sylius.behat.api_platform_client.currency" />
<argument type="service" id="Sylius\Behat\Client\ResponseCheckerInterface" />

View file

@ -2,6 +2,7 @@
# (c) Paweł Jędrzejewski
imports:
- suites/api/addressing/managing_countries.yml
- suites/api/account/customer_registration.yml
- suites/api/account/login.yml
- suites/api/admin/login.yml

View file

@ -0,0 +1,21 @@
# This file is part of the Sylius package.
# (c) Paweł Jędrzejewski
default:
suites:
api_managing_countries:
contexts:
- sylius.behat.context.hook.doctrine_orm
- sylius.behat.context.transform.country
- sylius.behat.context.transform.shared_storage
- sylius.behat.context.transform.lexical
- sylius.behat.context.setup.admin_api_security
- sylius.behat.context.setup.geographical
- sylius.behat.context.api.admin.managing_countries
filters:
tags: "@managing_countries && @api"
javascript: false

View file

@ -0,0 +1,41 @@
<?xml version="1.0" ?>
<!--
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.
-->
<resources xmlns="https://api-platform.com/schema/metadata"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://api-platform.com/schema/metadata https://api-platform.com/schema/metadata/metadata-2.0.xsd"
>
<resource class="%sylius.model.country.class%" shortName="Country">
<attribute name="validation_groups">sylius</attribute>
<collectionOperations>
<collectionOperation name="get" />
<collectionOperation name="post" />
</collectionOperations>
<itemOperations>
<itemOperation name="get" />
<itemOperation name="put">
<attribute name="denormalization_context">
<attribute name="groups">country:update</attribute>
</attribute>
</itemOperation>
</itemOperations>
<property name="id" identifier="false" writable="false" />
<property name="code" identifier="true" required="true" />
<property name="enabled" writable="true" />
<property name="createdAt" writable="false" />
<property name="updatedAt" writable="false" />
</resource>
</resources>

View file

@ -0,0 +1,36 @@
<?xml version="1.0" ?>
<!--
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.
-->
<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>country:read</group>
</attribute>
<attribute name="createdAt">
<group>country:read</group>
</attribute>
<attribute name="updatedAt">
<group>country:read</group>
</attribute>
<attribute name="code">
<group>country:read</group>
</attribute>
<attribute name="enabled">
<group>country:read</group>
<group>country:update</group>
</attribute>
</class>
</serializer>