From 6a8324bd0ef1febc5b521916b854793ed08e9a82 Mon Sep 17 00:00:00 2001 From: Olivier ALLAIN Date: Sun, 19 Apr 2020 21:24:07 +0200 Subject: [PATCH 01/12] adding zone --- .../managing_zones/adding_zone.feature | 8 +- .../Api/Admin/ManagingZonesContext.php | 168 ++++++++++++++++++ .../Behat/Context/Transform/ZoneContext.php | 2 + .../Behat/Resources/config/services/api.xml | 6 + .../config/services/contexts/api/admin.xml | 5 + src/Sylius/Behat/Resources/config/suites.yml | 1 + .../suites/api/addressing/managing_zones.yml | 24 +++ .../config/api_resources/ZoneMember.xml | 45 +++++ .../Resources/config/serialization/Zone.xml | 50 ++++++ .../config/serialization/ZoneMember.xml | 29 +++ 10 files changed, 334 insertions(+), 4 deletions(-) create mode 100644 src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php create mode 100644 src/Sylius/Behat/Resources/config/suites/api/addressing/managing_zones.yml create mode 100644 src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/ZoneMember.xml create mode 100644 src/Sylius/Bundle/ApiBundle/Resources/config/serialization/Zone.xml create mode 100644 src/Sylius/Bundle/ApiBundle/Resources/config/serialization/ZoneMember.xml diff --git a/features/addressing/managing_zones/adding_zone.feature b/features/addressing/managing_zones/adding_zone.feature index 6307422308..f603441a74 100644 --- a/features/addressing/managing_zones/adding_zone.feature +++ b/features/addressing/managing_zones/adding_zone.feature @@ -11,7 +11,7 @@ Feature: Adding a new zone with country type members And the store has a zone "North America" with code "NA" And I am logged in as an administrator - @ui @javascript + @ui @javascript @api Scenario: Adding a zone with a country type member When I want to create a new zone consisting of country And I name it "European Union" @@ -21,7 +21,7 @@ Feature: Adding a new zone with country type members Then I should be notified that it has been successfully created And the zone named "European Union" with the "France" country member should appear in the registry - @ui @javascript + @ui @javascript @api Scenario: Adding a zone with province type member When I want to create a new zone consisting of province And I name it "United States" @@ -31,7 +31,7 @@ Feature: Adding a new zone with country type members Then I should be notified that it has been successfully created And the zone named "United States" with the "Alabama" province member should appear in the registry - @ui @javascript + @ui @javascript @api Scenario: Adding a zone with zone type member When I want to create a new zone consisting of zone And I name it "America" @@ -41,7 +41,7 @@ Feature: Adding a new zone with country type members Then I should be notified that it has been successfully created And the zone named "America" with the "North America" zone member should appear in the registry - @ui @javascript + @ui @javascript @api Scenario: Adding a zone with a country type member and a shipping scope When I want to create a new zone consisting of country And I name it "European Union" diff --git a/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php b/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php new file mode 100644 index 0000000000..67f29a81c4 --- /dev/null +++ b/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php @@ -0,0 +1,168 @@ +client = $client; + $this->responseChecker = $responseChecker; + } + + /** + * @When I want to create a new zone consisting of :memberType + */ + public function iWantToCreateANewZoneConsistingOfCountry(string $memberType): void + { + $this->client->buildCreateRequest(); + $this->client->addRequestData('type', $memberType); + } + + /** + * @When I name it :name + */ + public function iNameIt(string $name): void + { + $this->client->addRequestData('name', $name); + } + + /** + * @When I specify its code as :code + */ + public function iSpecifyItsCodeAs(string $code): void + { + $this->client->addRequestData('code', $code); + } + + /** + * @When I add a country :country + */ + public function iAddACountry(CountryInterface $country): void + { + $this->client->addSubResourceData('members', [ + 'code' => $country->getCode() + ]); + } + + /** + * @When I add a province :province + */ + public function iAddAProvince(ProvinceInterface $province): void + { + $this->client->addSubResourceData('members', [ + 'code' => $province->getCode() + ]); + } + + /** + * @When I add a zone :zoneName + */ + public function iAddAZone(ZoneInterface $zoneName): void + { + $this->client->addSubResourceData('members', [ + 'code' => $zoneName->getCode() + ]); + } + + /** + * @When I select its scope as :scope + */ + public function iSelectItsScopeAs(string $scope): void + { + $this->client->addRequestData('scope', $scope); + } + + /** + * @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()), + 'Zone could not be created' + ); + } + + /** + * @Then the zone named :zoneName with the :country country member should appear in the registry + */ + public function theZoneNamedWithTheCountryMemberShouldAppearInTheRegistry(ZoneInterface $zoneName, CountryInterface $country): void + { + Assert::true($this->responseChecker->hasItemWithValue( + $this->client->subResourceIndex('members', $zoneName->getCode()), + 'code', + $country->getCode() + )); + } + + /** + * @Then the zone named :zoneName with the :province province member should appear in the registry + */ + public function theZoneNamedWithTheProvinceMemberShouldAppearInTheRegistry(ZoneInterface $zoneName, ProvinceInterface $province): void + { + Assert::true($this->responseChecker->hasItemWithValue( + $this->client->subResourceIndex('members', $zoneName->getCode()), + 'code', + $province->getCode() + )); + } + + /** + * @Then the zone named :zoneNamed with the :zoneName zone member should appear in the registry + */ + public function theZoneNamedWithTheZoneMemberShouldAppearInTheRegistry(ZoneInterface $zoneNamed, ZoneInterface $zoneName): void + { + Assert::true($this->responseChecker->hasItemWithValue( + $this->client->subResourceIndex('members', $zoneNamed->getCode()), + 'code', + $zoneName->getCode() + )); + } + + /** + * @Then its scope should be :scope + */ + public function itsScopeShouldBe(string $scope): void + { + Assert::true( + $this->responseChecker->hasValue($this->client->show('EU'), 'scope', $scope), + sprintf('Its Zone does not have %s scope', $scope) + ); + } +} diff --git a/src/Sylius/Behat/Context/Transform/ZoneContext.php b/src/Sylius/Behat/Context/Transform/ZoneContext.php index 489733c4c5..02162c231b 100644 --- a/src/Sylius/Behat/Context/Transform/ZoneContext.php +++ b/src/Sylius/Behat/Context/Transform/ZoneContext.php @@ -40,6 +40,8 @@ final class ZoneContext implements Context /** * @Transform /^zone named "([^"]+)"$/ + * @Transform :zoneName + * @Transform :zoneNamed */ public function getZoneByName(string $name): ZoneInterface { diff --git a/src/Sylius/Behat/Resources/config/services/api.xml b/src/Sylius/Behat/Resources/config/services/api.xml index 6d3dd02eb7..960de765a8 100644 --- a/src/Sylius/Behat/Resources/config/services/api.xml +++ b/src/Sylius/Behat/Resources/config/services/api.xml @@ -121,6 +121,12 @@ taxons + + + + zones + + diff --git a/src/Sylius/Behat/Resources/config/services/contexts/api/admin.xml b/src/Sylius/Behat/Resources/config/services/contexts/api/admin.xml index cf01cde160..268b4eac04 100644 --- a/src/Sylius/Behat/Resources/config/services/contexts/api/admin.xml +++ b/src/Sylius/Behat/Resources/config/services/contexts/api/admin.xml @@ -133,5 +133,10 @@ + + + + + diff --git a/src/Sylius/Behat/Resources/config/suites.yml b/src/Sylius/Behat/Resources/config/suites.yml index 91dfde4077..3f8539a69e 100644 --- a/src/Sylius/Behat/Resources/config/suites.yml +++ b/src/Sylius/Behat/Resources/config/suites.yml @@ -5,6 +5,7 @@ imports: - suites/api/addressing/managing_countries.yml - suites/api/account/customer_registration.yml - suites/api/account/login.yml + - suites/api/addressing/managing_zones.yml - suites/api/admin/login.yml - suites/api/currency/managing_currencies.yml - suites/api/currency/managing_exchange_rates.yml diff --git a/src/Sylius/Behat/Resources/config/suites/api/addressing/managing_zones.yml b/src/Sylius/Behat/Resources/config/suites/api/addressing/managing_zones.yml new file mode 100644 index 0000000000..9ca411d437 --- /dev/null +++ b/src/Sylius/Behat/Resources/config/suites/api/addressing/managing_zones.yml @@ -0,0 +1,24 @@ +# This file is part of the Sylius package. +# (c) Paweł Jędrzejewski + +default: + suites: + api_managing_zones: + contexts: + - sylius.behat.context.hook.doctrine_orm + + - sylius.behat.context.transform.country + - sylius.behat.context.transform.province + - sylius.behat.context.transform.shared_storage + - sylius.behat.context.transform.zone_member + - sylius.behat.context.transform.zone + + - sylius.behat.context.setup.admin_api_security + - sylius.behat.context.setup.geographical + - sylius.behat.context.setup.zone + + - sylius.behat.context.api.admin.managing_zones + + filters: + tags: "@managing_zones && @api" + javascript: false diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/ZoneMember.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/ZoneMember.xml new file mode 100644 index 0000000000..caa7e214f4 --- /dev/null +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/ZoneMember.xml @@ -0,0 +1,45 @@ + + + + + + + admin + + + + zone_member:read + + + + + + zone_member:write + + + + sylius + + + + + + + + + + + + diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/Zone.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/Zone.xml new file mode 100644 index 0000000000..a95add33ab --- /dev/null +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/Zone.xml @@ -0,0 +1,50 @@ + + + + + + + + zone:read + + + zone:read + zone:create + + + zone:read + zone:create + + + zone:read + zone:create + + + zone:read + zone:create + + + zone:read + + + zone:read + + + zone:read + zone:create + zone:update + + + diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/ZoneMember.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/ZoneMember.xml new file mode 100644 index 0000000000..ca2329cb6a --- /dev/null +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/ZoneMember.xml @@ -0,0 +1,29 @@ + + + + + + + + zone_member:read + + + zone:create + zone:update + zone_member:read + zone_member:write + + + From 3ddbcbf90b876b1bfec1480c4e462481ce0407dc Mon Sep 17 00:00:00 2001 From: Olivier ALLAIN Date: Sun, 19 Apr 2020 22:00:50 +0200 Subject: [PATCH 02/12] browsing zone --- .../managing_zones/browsing_zone.feature | 2 +- .../Api/Admin/ManagingZonesContext.php | 33 +++++++++++++++++-- .../Resources/config/api_resources/Zone.xml | 27 +++++++++++++-- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/features/addressing/managing_zones/browsing_zone.feature b/features/addressing/managing_zones/browsing_zone.feature index 4c4392f4b8..3381c5bdc8 100644 --- a/features/addressing/managing_zones/browsing_zone.feature +++ b/features/addressing/managing_zones/browsing_zone.feature @@ -9,7 +9,7 @@ Feature: Browsing zones And the store also has a zone "South America" with code "SA" And I am logged in as an administrator - @ui + @ui @api Scenario: Browsing zones in store When I want to see all zones in store Then I should see 2 zones in the list diff --git a/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php b/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php index 67f29a81c4..b51972de77 100644 --- a/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php +++ b/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php @@ -68,7 +68,7 @@ final class ManagingZonesContext implements Context public function iAddACountry(CountryInterface $country): void { $this->client->addSubResourceData('members', [ - 'code' => $country->getCode() + 'code' => $country->getCode(), ]); } @@ -78,7 +78,7 @@ final class ManagingZonesContext implements Context public function iAddAProvince(ProvinceInterface $province): void { $this->client->addSubResourceData('members', [ - 'code' => $province->getCode() + 'code' => $province->getCode(), ]); } @@ -88,7 +88,7 @@ final class ManagingZonesContext implements Context public function iAddAZone(ZoneInterface $zoneName): void { $this->client->addSubResourceData('members', [ - 'code' => $zoneName->getCode() + 'code' => $zoneName->getCode(), ]); } @@ -108,6 +108,14 @@ final class ManagingZonesContext implements Context $this->client->create(); } + /** + * @When I want to see all zones in store + */ + public function iWantToSeeAllZonesInStore(): void + { + $this->client->index(); + } + /** * @Then I should be notified that it has been successfully created */ @@ -165,4 +173,23 @@ final class ManagingZonesContext implements Context sprintf('Its Zone does not have %s scope', $scope) ); } + + /** + * @Then I should see :count zones in the list + */ + public function iShouldSeeZonesInTheList(int $count): void + { + Assert::same($this->responseChecker->countCollectionItems($this->client->getLastResponse()), $count); + } + + /** + * @Then I should see the zone named :name in the list + */ + public function iShouldSeeTheZoneNamedInTheList(string $name): void + { + Assert::true( + $this->responseChecker->hasItemWithValue($this->client->getLastResponse(), 'name', $name), + sprintf('There is no zone with name "%s"', $name) + ); + } } diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Zone.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Zone.xml index 389b2d75de..b7ab3d59fc 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Zone.xml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Zone.xml @@ -22,18 +22,41 @@ - + sylius + + + + is_granted('ROLE_API_ACCESS') + + + is_granted('ROLE_API_ACCESS') + + zone:create + + + is_granted('ROLE_API_ACCESS') + + is_granted('ROLE_API_ACCESS') + + zone:update + + - + + + + + + From e2c514f4beb6e7ccd4e42054b2e188cebc711477 Mon Sep 17 00:00:00 2001 From: Olivier ALLAIN Date: Sun, 19 Apr 2020 22:45:23 +0200 Subject: [PATCH 03/12] deleting zone --- .../managing_zones/deleting_zone.feature | 4 +- .../Api/Admin/ManagingZonesContext.php | 44 ++++++++++++++++++- .../suites/api/addressing/managing_zones.yml | 1 + .../Resources/config/api_resources/Zone.xml | 1 + 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/features/addressing/managing_zones/deleting_zone.feature b/features/addressing/managing_zones/deleting_zone.feature index 497e153ef1..8e35b7ea25 100644 --- a/features/addressing/managing_zones/deleting_zone.feature +++ b/features/addressing/managing_zones/deleting_zone.feature @@ -11,13 +11,13 @@ Feature: Deleting a zone And the store has "Sales Tax" tax rate of 20% for "Sports gear" within the "SA" zone And I am logged in as an administrator - @ui + @ui @api Scenario: Deleted zone should disappear from the registry When I delete zone named "North America" Then I should be notified that it has been successfully deleted And the zone named "North America" should no longer exist in the registry - @ui + @ui @api Scenario: Deleting zone with associated tax rates should not be possible When I delete zone named "South America" Then I should be notified that this zone cannot be deleted diff --git a/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php b/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php index b51972de77..384a973c8d 100644 --- a/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php +++ b/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php @@ -116,6 +116,14 @@ final class ManagingZonesContext implements Context $this->client->index(); } + /** + * @When I delete zone named :zoneName + */ + public function iDeleteZoneNamed(ZoneInterface $zoneName): void + { + $this->client->delete($zoneName->getCode()); + } + /** * @Then I should be notified that it has been successfully created */ @@ -184,12 +192,46 @@ final class ManagingZonesContext implements Context /** * @Then I should see the zone named :name in the list + * @Then I should still see the zone named :name in the list */ public function iShouldSeeTheZoneNamedInTheList(string $name): void { Assert::true( - $this->responseChecker->hasItemWithValue($this->client->getLastResponse(), 'name', $name), + $this->responseChecker->hasItemWithValue($this->client->index(), 'name', $name), sprintf('There is no zone with name "%s"', $name) ); } + + /** + * @Then I should be notified that it has been successfully deleted + */ + public function iShouldBeNotifiedThatItHasBeenSuccessfullyDeleted(): void + { + Assert::true($this->responseChecker->isDeletionSuccessful( + $this->client->getLastResponse()), + 'Zone could not be deleted' + ); + } + + /** + * @Then the zone named :name should no longer exist in the registry + */ + public function theZoneNamedShouldNoLongerExistInTheRegistry(string $name): void + { + Assert::false( + $this->responseChecker->hasItemWithValue($this->client->index(), 'name', $name), + sprintf('Zone with name %s exist', $name) + ); + } + + /** + * @Then I should be notified that this zone cannot be deleted + */ + public function iShouldBeNotifiedThatThisZoneCannotBeDeleted(): void + { + Assert::false( + $this->responseChecker->isDeletionSuccessful($this->client->getLastResponse()), + 'Zone can be deleted, but it should not' + ); + } } diff --git a/src/Sylius/Behat/Resources/config/suites/api/addressing/managing_zones.yml b/src/Sylius/Behat/Resources/config/suites/api/addressing/managing_zones.yml index 9ca411d437..65d39d8196 100644 --- a/src/Sylius/Behat/Resources/config/suites/api/addressing/managing_zones.yml +++ b/src/Sylius/Behat/Resources/config/suites/api/addressing/managing_zones.yml @@ -15,6 +15,7 @@ default: - sylius.behat.context.setup.admin_api_security - sylius.behat.context.setup.geographical + - sylius.behat.context.setup.taxation - sylius.behat.context.setup.zone - sylius.behat.context.api.admin.managing_zones diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Zone.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Zone.xml index b7ab3d59fc..efe2966540 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Zone.xml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Zone.xml @@ -46,6 +46,7 @@ zone:update + From 28ae5d484b702fd1351c6577b791d02697de08c5 Mon Sep 17 00:00:00 2001 From: Olivier ALLAIN Date: Mon, 20 Apr 2020 22:09:29 +0200 Subject: [PATCH 04/12] deleting multiple zones --- .../deleting_multiple_zones.feature | 4 +- .../Api/Admin/ManagingZonesContext.php | 39 +++++++++++++++++-- .../Context/Ui/Admin/ManagingZonesContext.php | 8 ---- .../config/services/contexts/api/admin.xml | 1 + 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/features/addressing/managing_zones/deleting_multiple_zones.feature b/features/addressing/managing_zones/deleting_multiple_zones.feature index 5154590c02..eb67831121 100644 --- a/features/addressing/managing_zones/deleting_multiple_zones.feature +++ b/features/addressing/managing_zones/deleting_multiple_zones.feature @@ -8,7 +8,7 @@ Feature: Deleting multiple zones Given the store has zones "North America", "South America" and "Europe" And I am logged in as an administrator - @ui + @ui @api Scenario: Deleting multiple zones at once When I browse zones And I check the "North America" zone @@ -16,4 +16,4 @@ Feature: Deleting multiple zones And I delete them Then I should be notified that they have been successfully deleted And I should see a single zone in the list - And I should see the zone "Europe" in the list + And I should see the zone named "Europe" in the list diff --git a/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php b/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php index 384a973c8d..dabdfc87ef 100644 --- a/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php +++ b/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php @@ -16,6 +16,7 @@ namespace Sylius\Behat\Context\Api\Admin; use Behat\Behat\Context\Context; use Sylius\Behat\Client\ApiClientInterface; use Sylius\Behat\Client\ResponseCheckerInterface; +use Sylius\Behat\Service\SharedStorageInterface; use Sylius\Component\Addressing\Model\CountryInterface; use Sylius\Component\Addressing\Model\ProvinceInterface; use Sylius\Component\Addressing\Model\ZoneInterface; @@ -29,12 +30,17 @@ final class ManagingZonesContext implements Context /** @var ResponseCheckerInterface */ private $responseChecker; + /** @var SharedStorageInterface */ + private $sharedStorage; + public function __construct( ApiClientInterface $client, - ResponseCheckerInterface $responseChecker + ResponseCheckerInterface $responseChecker, + SharedStorageInterface $sharedStorage ) { $this->client = $client; $this->responseChecker = $responseChecker; + $this->sharedStorage = $sharedStorage; } /** @@ -110,6 +116,7 @@ final class ManagingZonesContext implements Context /** * @When I want to see all zones in store + * @When I browse zones */ public function iWantToSeeAllZonesInStore(): void { @@ -124,6 +131,30 @@ final class ManagingZonesContext implements Context $this->client->delete($zoneName->getCode()); } + /** + * @When I check the :zoneName zone + * @When I check also the :zoneName zone + */ + public function iCheckTheZone(ZoneInterface $zoneName): void + { + $ZoneToDelete = []; + if ($this->sharedStorage->has('zone_to_delete')) { + $ZoneToDelete = $this->sharedStorage->get('zone_to_delete'); + } + $ZoneToDelete[] = $zoneName->getCode(); + $this->sharedStorage->set('zone_to_delete', $ZoneToDelete); + } + + /** + * @When I delete them + */ + public function iDeleteThem(): void + { + foreach ($this->sharedStorage->get('zone_to_delete') as $code) { + $this->client->delete($code); + } + } + /** * @Then I should be notified that it has been successfully created */ @@ -184,10 +215,11 @@ final class ManagingZonesContext implements Context /** * @Then I should see :count zones in the list + * @Then I should see a single zone in the list */ - public function iShouldSeeZonesInTheList(int $count): void + public function iShouldSeeZonesInTheList(int $count = 1): void { - Assert::same($this->responseChecker->countCollectionItems($this->client->getLastResponse()), $count); + Assert::same($this->responseChecker->countCollectionItems($this->client->index()), $count); } /** @@ -204,6 +236,7 @@ final class ManagingZonesContext implements Context /** * @Then I should be notified that it has been successfully deleted + * @Then I should be notified that they have been successfully deleted */ public function iShouldBeNotifiedThatItHasBeenSuccessfullyDeleted(): void { diff --git a/src/Sylius/Behat/Context/Ui/Admin/ManagingZonesContext.php b/src/Sylius/Behat/Context/Ui/Admin/ManagingZonesContext.php index 4bbe29a9fe..c8fc926094 100644 --- a/src/Sylius/Behat/Context/Ui/Admin/ManagingZonesContext.php +++ b/src/Sylius/Behat/Context/Ui/Admin/ManagingZonesContext.php @@ -329,14 +329,6 @@ final class ManagingZonesContext implements Context Assert::true($this->indexPage->isSingleResourceOnPage(['code' => $zone->getCode(), 'name' => $zone->getName()])); } - /** - * @Then I should see the zone :zoneName in the list - */ - public function iShouldSeeTheZoneInTheList(string $zoneName): void - { - Assert::true($this->indexPage->isSingleResourceOnPage(['name' => $zoneName])); - } - /** * @Then I should be notified that this zone cannot be deleted */ diff --git a/src/Sylius/Behat/Resources/config/services/contexts/api/admin.xml b/src/Sylius/Behat/Resources/config/services/contexts/api/admin.xml index 268b4eac04..fd5b563664 100644 --- a/src/Sylius/Behat/Resources/config/services/contexts/api/admin.xml +++ b/src/Sylius/Behat/Resources/config/services/contexts/api/admin.xml @@ -137,6 +137,7 @@ + From 88cf3236ee9141d446cb1d0dc2025a9971159df8 Mon Sep 17 00:00:00 2001 From: Olivier ALLAIN Date: Mon, 27 Apr 2020 09:52:59 +0200 Subject: [PATCH 05/12] editing zone --- .../managing_zones/editing_zone.feature | 2 +- .../Api/Admin/ManagingZonesContext.php | 101 ++++++++++++++---- .../config/services/contexts/api/admin.xml | 1 + 3 files changed, 85 insertions(+), 19 deletions(-) diff --git a/features/addressing/managing_zones/editing_zone.feature b/features/addressing/managing_zones/editing_zone.feature index 9292d0ce3a..d17315e234 100644 --- a/features/addressing/managing_zones/editing_zone.feature +++ b/features/addressing/managing_zones/editing_zone.feature @@ -13,7 +13,7 @@ Feature: Editing a zone And the store has a zone "South America" with code "SA" And I am logged in as an administrator - @ui @javascript + @api Scenario: Removing a country from a zone Given the store has a zone "European Union" with code "EU" And it has the "France" country member diff --git a/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php b/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php index dabdfc87ef..44d06d5a78 100644 --- a/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php +++ b/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php @@ -13,6 +13,7 @@ declare(strict_types=1); namespace Sylius\Behat\Context\Api\Admin; +use ApiPlatform\Core\Api\IriConverterInterface; use Behat\Behat\Context\Context; use Sylius\Behat\Client\ApiClientInterface; use Sylius\Behat\Client\ResponseCheckerInterface; @@ -20,6 +21,7 @@ use Sylius\Behat\Service\SharedStorageInterface; use Sylius\Component\Addressing\Model\CountryInterface; use Sylius\Component\Addressing\Model\ProvinceInterface; use Sylius\Component\Addressing\Model\ZoneInterface; +use Sylius\Component\Addressing\Model\ZoneMemberInterface; use Webmozart\Assert\Assert; final class ManagingZonesContext implements Context @@ -33,14 +35,19 @@ final class ManagingZonesContext implements Context /** @var SharedStorageInterface */ private $sharedStorage; + /** @var IriConverterInterface */ + private $iriConverter; + public function __construct( ApiClientInterface $client, ResponseCheckerInterface $responseChecker, - SharedStorageInterface $sharedStorage + SharedStorageInterface $sharedStorage, + IriConverterInterface $iriConverter ) { $this->client = $client; $this->responseChecker = $responseChecker; $this->sharedStorage = $sharedStorage; + $this->iriConverter = $iriConverter; } /** @@ -156,14 +163,36 @@ final class ManagingZonesContext implements Context } /** - * @Then I should be notified that it has been successfully created + * @When I want to modify the zone named :zoneName */ - public function iShouldBeNotifiedThatItHasBeenSuccessfullyCreated(): void + public function iWantToModifyTheZoneNamed(ZoneInterface $zoneName) { - Assert::true( - $this->responseChecker->isCreationSuccessful($this->client->getLastResponse()), - 'Zone could not be created' - ); + $this->client->buildUpdateRequest($zoneName->getCode()); + } + + /** + * @When I remove the :country country member + */ + public function iRemoveTheCountryMember(CountryInterface $country) + { + /** @var ZoneInterface $zone */ + $zone = $this->sharedStorage->get('zone'); + $countryIri = $this->iriConverter->getIriFromItem($country); + + $members = $this->responseChecker->getValue($this->client->show($zone->getCode(), 'members')); + foreach ($members as $key => $member) { + if ($countryIri === $member) { + $this->client->removeSubResource('members', $member); + } + } + } + + /** + * @When I save my changes + */ + public function iSaveMyChanges() + { + $this->client->update(); } /** @@ -234,6 +263,53 @@ final class ManagingZonesContext implements Context ); } + /** + * @Then the zone named :name should no longer exist in the registry + */ + public function theZoneNamedShouldNoLongerExistInTheRegistry(string $name): void + { + Assert::false( + $this->responseChecker->hasItemWithValue($this->client->index(), 'name', $name), + sprintf('Zone with name %s exist', $name) + ); + } + + /** + * @Then /^(this zone) should have only (the "([^"]*)" (?:country|province|zone) member)$/ + */ + public function thisZoneShouldHaveOnlyTheProvinceMember(ZoneInterface $zone, ZoneMemberInterface $zoneMember) + { + Assert::true($this->responseChecker->hasItemWithValue( + $this->client->subResourceIndex('members', $zone->getCode()), + 'code', + $zoneMember->getCode() + )); + + Assert::same($this->responseChecker->countCollectionItems($this->client->index()), 1); + } + + /** + * @Then I should be notified that it has been successfully created + */ + public function iShouldBeNotifiedThatItHasBeenSuccessfullyCreated(): void + { + Assert::true( + $this->responseChecker->isCreationSuccessful($this->client->getLastResponse()), + 'Zone could not be created' + ); + } + + /** + * @Then I should be notified that it has been successfully edited + */ + public function iShouldBeNotifiedThatItHasBeenSuccessfullyEdited() + { + Assert::true( + $this->responseChecker->isUpdateSuccessful($this->client->getLastResponse()), + 'Zone could not be edited' + ); + } + /** * @Then I should be notified that it has been successfully deleted * @Then I should be notified that they have been successfully deleted @@ -246,17 +322,6 @@ final class ManagingZonesContext implements Context ); } - /** - * @Then the zone named :name should no longer exist in the registry - */ - public function theZoneNamedShouldNoLongerExistInTheRegistry(string $name): void - { - Assert::false( - $this->responseChecker->hasItemWithValue($this->client->index(), 'name', $name), - sprintf('Zone with name %s exist', $name) - ); - } - /** * @Then I should be notified that this zone cannot be deleted */ diff --git a/src/Sylius/Behat/Resources/config/services/contexts/api/admin.xml b/src/Sylius/Behat/Resources/config/services/contexts/api/admin.xml index fd5b563664..2897cb936b 100644 --- a/src/Sylius/Behat/Resources/config/services/contexts/api/admin.xml +++ b/src/Sylius/Behat/Resources/config/services/contexts/api/admin.xml @@ -138,6 +138,7 @@ + From ba3b88f960f421df735aa7c8b8459c149d1e48be Mon Sep 17 00:00:00 2001 From: Olivier ALLAIN Date: Mon, 27 Apr 2020 11:22:05 +0200 Subject: [PATCH 06/12] zone unique code validation --- .../managing_zones/editing_zone.feature | 2 +- .../zone_unique_code_validation.feature | 2 +- .../Api/Admin/ManagingZonesContext.php | 34 ++++++++++++++++--- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/features/addressing/managing_zones/editing_zone.feature b/features/addressing/managing_zones/editing_zone.feature index d17315e234..9292d0ce3a 100644 --- a/features/addressing/managing_zones/editing_zone.feature +++ b/features/addressing/managing_zones/editing_zone.feature @@ -13,7 +13,7 @@ Feature: Editing a zone And the store has a zone "South America" with code "SA" And I am logged in as an administrator - @api + @ui @javascript Scenario: Removing a country from a zone Given the store has a zone "European Union" with code "EU" And it has the "France" country member diff --git a/features/addressing/managing_zones/zone_unique_code_validation.feature b/features/addressing/managing_zones/zone_unique_code_validation.feature index dcb354795d..a999372625 100644 --- a/features/addressing/managing_zones/zone_unique_code_validation.feature +++ b/features/addressing/managing_zones/zone_unique_code_validation.feature @@ -10,7 +10,7 @@ Feature: Zone unique code validation And this zone has the "United States" country member And I am logged in as an administrator - @ui + @ui @api Scenario: Trying to add zone with taken code When I want to create a new zone consisting of country And I specify its code as "US" diff --git a/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php b/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php index 44d06d5a78..c9962a81db 100644 --- a/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php +++ b/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php @@ -115,6 +115,7 @@ final class ManagingZonesContext implements Context /** * @When I add it + * @When I try to add it */ public function iAddIt(): void { @@ -165,7 +166,7 @@ final class ManagingZonesContext implements Context /** * @When I want to modify the zone named :zoneName */ - public function iWantToModifyTheZoneNamed(ZoneInterface $zoneName) + public function iWantToModifyTheZoneNamed(ZoneInterface $zoneName): void { $this->client->buildUpdateRequest($zoneName->getCode()); } @@ -173,7 +174,7 @@ final class ManagingZonesContext implements Context /** * @When I remove the :country country member */ - public function iRemoveTheCountryMember(CountryInterface $country) + public function iRemoveTheCountryMember(CountryInterface $country): void { /** @var ZoneInterface $zone */ $zone = $this->sharedStorage->get('zone'); @@ -190,7 +191,7 @@ final class ManagingZonesContext implements Context /** * @When I save my changes */ - public function iSaveMyChanges() + public function iSaveMyChanges(): void { $this->client->update(); } @@ -263,6 +264,18 @@ final class ManagingZonesContext implements Context ); } + /** + * @Then there should still be only one zone with code :code + */ + public function thereShouldStillBeOnlyOneZoneWithCode(string $code): void + { + Assert::count( + $this->responseChecker->getCollectionItemsWithValue($this->client->index(), 'code', $code), + 1, + sprintf('There should be only one zone with code "%s"', $code) + ); + } + /** * @Then the zone named :name should no longer exist in the registry */ @@ -277,7 +290,7 @@ final class ManagingZonesContext implements Context /** * @Then /^(this zone) should have only (the "([^"]*)" (?:country|province|zone) member)$/ */ - public function thisZoneShouldHaveOnlyTheProvinceMember(ZoneInterface $zone, ZoneMemberInterface $zoneMember) + public function thisZoneShouldHaveOnlyTheProvinceMember(ZoneInterface $zone, ZoneMemberInterface $zoneMember): void { Assert::true($this->responseChecker->hasItemWithValue( $this->client->subResourceIndex('members', $zone->getCode()), @@ -302,7 +315,7 @@ final class ManagingZonesContext implements Context /** * @Then I should be notified that it has been successfully edited */ - public function iShouldBeNotifiedThatItHasBeenSuccessfullyEdited() + public function iShouldBeNotifiedThatItHasBeenSuccessfullyEdited(): void { Assert::true( $this->responseChecker->isUpdateSuccessful($this->client->getLastResponse()), @@ -332,4 +345,15 @@ final class ManagingZonesContext implements Context 'Zone can be deleted, but it should not' ); } + + /** + * @Then I should be notified that zone with this code already exists + */ + public function iShouldBeNotifiedThatZoneWithThisCodeAlreadyExists(): void + { + Assert::contains( + $this->responseChecker->getError($this->client->getLastResponse()), + 'code: Zone code must be unique.' + ); + } } From e757ce8f62722990ab1039e8e08ab89491939599 Mon Sep 17 00:00:00 2001 From: Olivier ALLAIN Date: Mon, 27 Apr 2020 16:09:02 +0200 Subject: [PATCH 07/12] zone validation --- .../managing_zones/zone_validation.feature | 16 ++--- .../Behat/Client/ApiClientInterface.php | 3 + src/Sylius/Behat/Client/ApiPlatformClient.php | 6 ++ src/Sylius/Behat/Client/Request.php | 6 ++ src/Sylius/Behat/Client/RequestInterface.php | 2 + .../Api/Admin/ManagingZonesContext.php | 62 +++++++++++++++++++ .../Context/Ui/Admin/ManagingZonesContext.php | 4 +- 7 files changed, 89 insertions(+), 10 deletions(-) diff --git a/features/addressing/managing_zones/zone_validation.feature b/features/addressing/managing_zones/zone_validation.feature index 733d750802..a03bed1793 100644 --- a/features/addressing/managing_zones/zone_validation.feature +++ b/features/addressing/managing_zones/zone_validation.feature @@ -8,16 +8,16 @@ Feature: Zone validation Given the store has country "United States" And I am logged in as an administrator - @ui + @ui @api Scenario: Trying to add a zone without specifying its code When I want to create a new zone consisting of country And I name it "European Union" But I do not specify its code And I try to add it Then I should be notified that code is required - And zone with code "European Union" should not be added + And zone with name "European Union" should not be added - @ui + @ui @api Scenario: Trying to add a zone without specifying its name When I want to create a new zone consisting of country And I specify its code as "EU" @@ -26,7 +26,7 @@ Feature: Zone validation Then I should be notified that name is required And zone with code "EU" should not be added - @ui + @ui @api Scenario: Trying to add a zone without any countries When I want to create a new zone consisting of country And I name it "European Union" @@ -36,14 +36,14 @@ Feature: Zone validation Then I should be notified that at least one zone member is required And zone with name "European Union" should not be added - @ui + @ui @api Scenario: Seeing a disabled type field when adding country type zone When I want to create a new zone consisting of country - Then the type field should be disabled + Then I should not be able to edit its type And it should be of country type - @ui + @ui @api Scenario: Seeing a disabled type field when adding province type zone When I want to create a new zone consisting of province - Then the type field should be disabled + Then I should not be able to edit its type And it should be of province type diff --git a/src/Sylius/Behat/Client/ApiClientInterface.php b/src/Sylius/Behat/Client/ApiClientInterface.php index 173d175cc7..ede72cbe3d 100644 --- a/src/Sylius/Behat/Client/ApiClientInterface.php +++ b/src/Sylius/Behat/Client/ApiClientInterface.php @@ -61,6 +61,9 @@ interface ApiClientInterface /** @param string|int|array $value */ public function addRequestData(string $key, $value): void; + /** @return string|int|array */ + public function getRequestData(string $key); + public function addSubResourceData(string $key, array $data): void; public function removeSubResource(string $subResource, string $id): void; diff --git a/src/Sylius/Behat/Client/ApiPlatformClient.php b/src/Sylius/Behat/Client/ApiPlatformClient.php index ec58c48bfa..92c992a0c9 100644 --- a/src/Sylius/Behat/Client/ApiPlatformClient.php +++ b/src/Sylius/Behat/Client/ApiPlatformClient.php @@ -157,6 +157,12 @@ final class ApiPlatformClient implements ApiClientInterface $this->request->updateContent([$key => $value]); } + /** @return string|int|array */ + public function getRequestData(string $key) + { + return $this->request->getData($key); + } + public function updateRequestData(array $data): void { $this->request->updateContent($data); diff --git a/src/Sylius/Behat/Client/Request.php b/src/Sylius/Behat/Client/Request.php index 05e8c3410c..743d932e79 100644 --- a/src/Sylius/Behat/Client/Request.php +++ b/src/Sylius/Behat/Client/Request.php @@ -154,6 +154,12 @@ final class Request implements RequestInterface $this->content = $this->mergeArraysUniquely($this->content, $newValues); } + /** @return string|int|array */ + public function getData(string $key) + { + return $this->content[$key]; + } + public function parameters(): array { return $this->parameters; diff --git a/src/Sylius/Behat/Client/RequestInterface.php b/src/Sylius/Behat/Client/RequestInterface.php index fc7291991e..fb3e352bd2 100644 --- a/src/Sylius/Behat/Client/RequestInterface.php +++ b/src/Sylius/Behat/Client/RequestInterface.php @@ -45,6 +45,8 @@ interface RequestInterface public function updateContent(array $newValues): void; + public function getData(string $key); + public function parameters(): array; public function updateParameters(array $newParameters): void; diff --git a/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php b/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php index c9962a81db..ccc9a97149 100644 --- a/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php +++ b/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php @@ -75,6 +75,15 @@ final class ManagingZonesContext implements Context $this->client->addRequestData('code', $code); } + /** + * @When I do not specify its :type + * @When I do not add a country member + */ + public function iDoNotSpecifyItsField(): void + { + // Intentionally left blank + } + /** * @When I add a country :country */ @@ -287,6 +296,17 @@ final class ManagingZonesContext implements Context ); } + /** + * @Then /^zone with (code|name) "([^"]*)" should not be added$/ + */ + public function zoneShouldNotBeAdded(string $field, string $value): void + { + Assert::false( + $this->responseChecker->hasItemWithValue($this->client->index(), $field, $value), + sprintf('Zone with %s %s exist', $field, $value) + ); + } + /** * @Then /^(this zone) should have only (the "([^"]*)" (?:country|province|zone) member)$/ */ @@ -301,6 +321,26 @@ final class ManagingZonesContext implements Context Assert::same($this->responseChecker->countCollectionItems($this->client->index()), 1); } + /** + * @Then I should not be able to edit its type + */ + public function iShouldNotBeAbleToEditItsType(): void + { + // Intentionally left blank, because in API context we can do that + } + + /** + * @Then /^it should be of (country|province) type$/ + */ + public function itShouldBeOfType(string $type): void + { + Assert::same( + $type, + $this->client->getRequestData('type'), + sprintf('Zone should be of type %s.', $type) + ); + } + /** * @Then I should be notified that it has been successfully created */ @@ -356,4 +396,26 @@ final class ManagingZonesContext implements Context 'code: Zone code must be unique.' ); } + + /** + * @Then /^I should be notified that (code|name) is required$/ + */ + public function iShouldBeNotifiedThatIsRequired(string $element): void + { + Assert::contains( + $this->responseChecker->getError($this->client->getLastResponse()), + sprintf('Please enter zone %s.', $element) + ); + } + + /** + * @Then I should be notified that at least one zone member is required + */ + public function iShouldBeNotifiedThatAtLeastOneZoneMemberIsRequired(): void + { + Assert::contains( + $this->responseChecker->getError($this->client->getLastResponse()), + 'members: Please add at least 1 zone member.' + ); + } } diff --git a/src/Sylius/Behat/Context/Ui/Admin/ManagingZonesContext.php b/src/Sylius/Behat/Context/Ui/Admin/ManagingZonesContext.php index c8fc926094..46e0f09775 100644 --- a/src/Sylius/Behat/Context/Ui/Admin/ManagingZonesContext.php +++ b/src/Sylius/Behat/Context/Ui/Admin/ManagingZonesContext.php @@ -289,9 +289,9 @@ final class ManagingZonesContext implements Context } /** - * @Then the type field should be disabled + * @Then I should not be able to edit its type */ - public function theTypeFieldShouldBeDisabled() + public function iShouldNotBeAbleToEditItsType() { Assert::true($this->createPage->isTypeFieldDisabled()); } From dc6949201721677ca9d1d0448cfda67e3095f86f Mon Sep 17 00:00:00 2001 From: Olivier ALLAIN Date: Tue, 28 Apr 2020 22:56:36 +0200 Subject: [PATCH 08/12] editing zone --- .../managing_zones/editing_zone.feature | 15 ++-- .../Api/Admin/ManagingZonesContext.php | 85 ++++++++++++++++--- .../Context/Ui/Admin/ManagingZonesContext.php | 4 +- .../Resources/config/serialization/Zone.xml | 1 + .../ApiBundle/Resources/config/services.xml | 7 +- .../Resources/config/services/validator.xml | 29 +++++++ .../Resources/config/validation/Zone.xml | 25 ++++++ .../Constraints/ZoneCannotContainItself.php | 27 ++++++ .../ZoneCannotContainItselfValidator.php | 39 +++++++++ .../ZoneCannotContainItselfValidatorSpec.php | 79 +++++++++++++++++ 10 files changed, 286 insertions(+), 25 deletions(-) create mode 100644 src/Sylius/Bundle/ApiBundle/Resources/config/services/validator.xml create mode 100644 src/Sylius/Bundle/ApiBundle/Resources/config/validation/Zone.xml create mode 100644 src/Sylius/Bundle/ApiBundle/Validator/Constraints/ZoneCannotContainItself.php create mode 100644 src/Sylius/Bundle/ApiBundle/Validator/Constraints/ZoneCannotContainItselfValidator.php create mode 100644 src/Sylius/Bundle/ApiBundle/spec/Validator/Constraints/ZoneCannotContainItselfValidatorSpec.php diff --git a/features/addressing/managing_zones/editing_zone.feature b/features/addressing/managing_zones/editing_zone.feature index 9292d0ce3a..0671053f0c 100644 --- a/features/addressing/managing_zones/editing_zone.feature +++ b/features/addressing/managing_zones/editing_zone.feature @@ -13,7 +13,7 @@ Feature: Editing a zone And the store has a zone "South America" with code "SA" And I am logged in as an administrator - @ui @javascript + @ui @javascript @api Scenario: Removing a country from a zone Given the store has a zone "European Union" with code "EU" And it has the "France" country member @@ -24,7 +24,7 @@ Feature: Editing a zone Then I should be notified that it has been successfully edited And this zone should have only the "France" country member - @ui @javascript + @ui @javascript @api Scenario: Removing a province from a zone Given the store has a zone "United States" with code "USA" And it has the "Alabama" province member @@ -35,7 +35,7 @@ Feature: Editing a zone Then I should be notified that it has been successfully edited And this zone should have only the "Alabama" province member - @ui @javascript + @ui @javascript @api Scenario: Removing a zone from a zone Given the store has a zone "America" with code "AM" And it has the zone named "North America" @@ -46,7 +46,7 @@ Feature: Editing a zone Then I should be notified that it has been successfully edited And this zone should have only the "South America" zone member - @ui + @ui @api Scenario: Renaming a zone Given the store has a zone "European Union" with code "EU" And it has the "France" country member @@ -56,13 +56,14 @@ Feature: Editing a zone Then I should be notified that it has been successfully edited And this zone name should be "EU" - @ui + @ui @api Scenario: Seeing a disabled code field when editing a zone Given the store has a zone "European Union" with code "EU" + And it has the "France" country member When I want to modify the zone named "European Union" - Then the code field should be disabled + Then I should not be able to edit its code - @ui + @ui @api Scenario: Not seeing zone itself in member select when editing a zone of type zone Given the store has a zone "European Union" with code "EU" When I want to modify the zone named "European Union" diff --git a/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php b/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php index ccc9a97149..5e5662de17 100644 --- a/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php +++ b/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php @@ -21,6 +21,7 @@ use Sylius\Behat\Service\SharedStorageInterface; use Sylius\Component\Addressing\Model\CountryInterface; use Sylius\Component\Addressing\Model\ProvinceInterface; use Sylius\Component\Addressing\Model\ZoneInterface; +use Sylius\Component\Addressing\Model\ZoneMember; use Sylius\Component\Addressing\Model\ZoneMemberInterface; use Webmozart\Assert\Assert; @@ -61,6 +62,7 @@ final class ManagingZonesContext implements Context /** * @When I name it :name + * @When I rename it to :name */ public function iNameIt(string $name): void { @@ -185,16 +187,23 @@ final class ManagingZonesContext implements Context */ public function iRemoveTheCountryMember(CountryInterface $country): void { - /** @var ZoneInterface $zone */ - $zone = $this->sharedStorage->get('zone'); - $countryIri = $this->iriConverter->getIriFromItem($country); + $this->removeZoneMember($country); + } - $members = $this->responseChecker->getValue($this->client->show($zone->getCode(), 'members')); - foreach ($members as $key => $member) { - if ($countryIri === $member) { - $this->client->removeSubResource('members', $member); - } - } + /** + * @When I remove the :province province member + */ + public function iRemoveTheProvinceMember(ProvinceInterface $province): void + { + $this->removeZoneMember($province); + } + + /** + * @When I remove the :zoneName zone member + */ + public function iRemoveTheZoneMember(ZoneInterface $zoneName): void + { + $this->removeZoneMember($zoneName); } /** @@ -217,6 +226,34 @@ final class ManagingZonesContext implements Context )); } + /** + * @Then I should not be able to edit its code + */ + public function iShouldNotBeAbleToEditItsCode(): void + { + $this->client->addRequestData('code', 'NEW_CODE'); + + Assert::false( + $this->responseChecker->hasValue($this->client->update(), 'code', 'NEW_CODE'), + 'The code field with value NEW_CODE exist' + ); + } + + /** + * @Then I can not add a zone :zoneName + */ + public function iCanNotAddAZone(ZoneInterface $zoneName): void + { + $this->client->addSubResourceData('members', [ + 'code' => $zoneName->getCode(), + ]); + + Assert::contains( + $this->responseChecker->getError($this->client->update()), + 'members: Chosen zone member is already in this zone.' + ); + } + /** * @Then the zone named :zoneName with the :province province member should appear in the registry */ @@ -318,7 +355,18 @@ final class ManagingZonesContext implements Context $zoneMember->getCode() )); - Assert::same($this->responseChecker->countCollectionItems($this->client->index()), 1); + Assert::same($this->responseChecker->countCollectionItems($this->client->subResourceIndex('members', $zone->getCode())), 1); + } + + /** + * @Then /^(this zone) name should be "([^"]*)"$/ + */ + public function thisZoneNameShouldBe(ZoneInterface $zone, string $name): void + { + Assert::true( + $this->responseChecker->hasValue($this->client->show($zone->getCode()), 'name', $name), + sprintf('Its Zone does not have name %s.', $name) + ); } /** @@ -418,4 +466,21 @@ final class ManagingZonesContext implements Context 'members: Please add at least 1 zone member.' ); } + + /** + * @param CountryInterface|ZoneInterface|ProvinceInterface $objectToRemove + */ + private function removeZoneMember($objectToRemove): void + { + /** @var ZoneInterface $zone */ + $zone = $this->sharedStorage->get('zone'); + $iri = $this->iriConverter->getItemIriFromResourceClass(ZoneMember::class, ['code' => $objectToRemove->getCode()]); + + $members = $this->responseChecker->getValue($this->client->show($zone->getCode()), 'members'); + foreach ($members as $key => $member) { + if ($iri === $member) { + $this->client->removeSubResource('members', $member); + } + } + } } diff --git a/src/Sylius/Behat/Context/Ui/Admin/ManagingZonesContext.php b/src/Sylius/Behat/Context/Ui/Admin/ManagingZonesContext.php index 46e0f09775..564e974180 100644 --- a/src/Sylius/Behat/Context/Ui/Admin/ManagingZonesContext.php +++ b/src/Sylius/Behat/Context/Ui/Admin/ManagingZonesContext.php @@ -231,9 +231,9 @@ final class ManagingZonesContext implements Context } /** - * @Then /^the code field should be disabled$/ + * @Then I should not be able to edit its code */ - public function theCodeFieldShouldBeDisabled() + public function iShouldNotBeAbleToEditItsCode() { Assert::true($this->updatePage->isCodeDisabled()); } diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/Zone.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/Zone.xml index a95add33ab..fadeb82d9b 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/Zone.xml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/Zone.xml @@ -26,6 +26,7 @@ zone:read zone:create + zone:update zone:read diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/services.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/services.xml index 969685363f..e7a3d00673 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/services.xml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/services.xml @@ -24,6 +24,7 @@ + @@ -60,11 +61,5 @@ > - - - - - - diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/services/validator.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/services/validator.xml new file mode 100644 index 0000000000..bc0f645bc7 --- /dev/null +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/services/validator.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/validation/Zone.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/validation/Zone.xml new file mode 100644 index 0000000000..259824f1d1 --- /dev/null +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/validation/Zone.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + diff --git a/src/Sylius/Bundle/ApiBundle/Validator/Constraints/ZoneCannotContainItself.php b/src/Sylius/Bundle/ApiBundle/Validator/Constraints/ZoneCannotContainItself.php new file mode 100644 index 0000000000..1b22a682c5 --- /dev/null +++ b/src/Sylius/Bundle/ApiBundle/Validator/Constraints/ZoneCannotContainItself.php @@ -0,0 +1,27 @@ +getCode() === $zoneMember->getBelongsTo()->getCode()) { + $this->context->addViolation($constraint->message); + } + } + } +} diff --git a/src/Sylius/Bundle/ApiBundle/spec/Validator/Constraints/ZoneCannotContainItselfValidatorSpec.php b/src/Sylius/Bundle/ApiBundle/spec/Validator/Constraints/ZoneCannotContainItselfValidatorSpec.php new file mode 100644 index 0000000000..8288524068 --- /dev/null +++ b/src/Sylius/Bundle/ApiBundle/spec/Validator/Constraints/ZoneCannotContainItselfValidatorSpec.php @@ -0,0 +1,79 @@ +beConstructedWith(); + $this->initialize($executionContext); + } + + function it_is_a_constraint_validator(): void + { + $this->shouldImplement(ConstraintValidatorInterface::class); + } + + function is_does_nothing_if_value_is_null(ExecutionContextInterface $executionContext): void + { + $executionContext->addViolation(Argument::cetera())->shouldNotBeCalled(); + + $this->validate(null, new ZoneCannotContainItself()); + } + + function it_throws_an_exception_if_constraint_is_not_of_expected_type(): void + { + $this->shouldThrow(\InvalidArgumentException::class)->during('validate', ['', new class() extends Constraint { + }]); + } + + function it_does_not_add_violation_if_zone_not_contain_itself_in_members( + ZoneInterface $zone, + ZoneMemberInterface $zoneMember, + ExecutionContextInterface $executionContext + ): void { + $zone->getCode()->willReturn('WORLD'); + $zoneMember->getCode()->willReturn('EU'); + $zoneMember->getBelongsTo()->willReturn($zone); + + $executionContext->addViolation(Argument::cetera())->shouldNotBeCalled(); + + $this->validate([$zoneMember], new ZoneCannotContainItself()); + } + + function it_adds_violation_if_zone_contain_itself_in_members( + ZoneInterface $zone, + ZoneMemberInterface $zoneMember, + ExecutionContextInterface $executionContext + ): void { + $zone->getCode()->willReturn('EU'); + $zoneMember->getCode()->willReturn('EU'); + $zoneMember->getBelongsTo()->willReturn($zone); + + $executionContext->addViolation(Argument::cetera())->shouldBeCalled(); + + $this->validate([$zoneMember], new ZoneCannotContainItself()); + } +} From faa9da12005948ebd79ca558f3443e583fdadcf2 Mon Sep 17 00:00:00 2001 From: Olivier ALLAIN Date: Thu, 30 Apr 2020 10:38:23 +0200 Subject: [PATCH 09/12] rebase and update after merge #11397 --- .../ApiBundle/Resources/config/api_resources/ZoneMember.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/ZoneMember.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/ZoneMember.xml index caa7e214f4..80f7b52c1f 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/ZoneMember.xml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/ZoneMember.xml @@ -16,8 +16,6 @@ xsi:schemaLocation="https://api-platform.com/schema/metadata https://api-platform.com/schema/metadata/metadata-2.0.xsd" > - admin - zone_member:read @@ -35,7 +33,9 @@ - + + is_granted('ROLE_API_ACCESS') + From d19fca5cbabe86eb5f77f1f3fcf0db01df8f0846 Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Fri, 8 May 2020 16:41:25 +0200 Subject: [PATCH 10/12] [API][Zone] Minor fixes after review --- .../Api/Admin/ManagingZonesContext.php | 90 ++++++++++--------- .../Behat/Context/Transform/ZoneContext.php | 38 ++++---- .../Context/Ui/Admin/ManagingZonesContext.php | 4 +- .../Checkout/CheckoutAddressingContext.php | 4 +- .../Resources/config/api_resources/Zone.xml | 4 +- .../ApiBundle/Resources/config/services.xml | 8 +- 6 files changed, 72 insertions(+), 76 deletions(-) diff --git a/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php b/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php index 5e5662de17..aed211cac1 100644 --- a/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php +++ b/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php @@ -107,12 +107,12 @@ final class ManagingZonesContext implements Context } /** - * @When I add a zone :zoneName + * @When I add a zone :zone */ - public function iAddAZone(ZoneInterface $zoneName): void + public function iAddAZone(ZoneInterface $zone): void { $this->client->addSubResourceData('members', [ - 'code' => $zoneName->getCode(), + 'code' => $zone->getCode(), ]); } @@ -143,24 +143,24 @@ final class ManagingZonesContext implements Context } /** - * @When I delete zone named :zoneName + * @When I delete zone named :zone */ - public function iDeleteZoneNamed(ZoneInterface $zoneName): void + public function iDeleteZoneNamed(ZoneInterface $zone): void { - $this->client->delete($zoneName->getCode()); + $this->client->delete($zone->getCode()); } /** - * @When I check the :zoneName zone - * @When I check also the :zoneName zone + * @When I check the :zone zone + * @When I check also the :zone zone */ - public function iCheckTheZone(ZoneInterface $zoneName): void + public function iCheckTheZone(ZoneInterface $zone): void { $ZoneToDelete = []; if ($this->sharedStorage->has('zone_to_delete')) { $ZoneToDelete = $this->sharedStorage->get('zone_to_delete'); } - $ZoneToDelete[] = $zoneName->getCode(); + $ZoneToDelete[] = $zone->getCode(); $this->sharedStorage->set('zone_to_delete', $ZoneToDelete); } @@ -175,11 +175,11 @@ final class ManagingZonesContext implements Context } /** - * @When I want to modify the zone named :zoneName + * @When I want to modify the zone named :zone */ - public function iWantToModifyTheZoneNamed(ZoneInterface $zoneName): void + public function iWantToModifyTheZoneNamed(ZoneInterface $zone): void { - $this->client->buildUpdateRequest($zoneName->getCode()); + $this->client->buildUpdateRequest($zone->getCode()); } /** @@ -199,11 +199,11 @@ final class ManagingZonesContext implements Context } /** - * @When I remove the :zoneName zone member + * @When I remove the :zone zone member */ - public function iRemoveTheZoneMember(ZoneInterface $zoneName): void + public function iRemoveTheZoneMember(ZoneInterface $zone): void { - $this->removeZoneMember($zoneName); + $this->removeZoneMember($zone); } /** @@ -215,12 +215,14 @@ final class ManagingZonesContext implements Context } /** - * @Then the zone named :zoneName with the :country country member should appear in the registry + * @Then the zone named :zone with the :country country member should appear in the registry */ - public function theZoneNamedWithTheCountryMemberShouldAppearInTheRegistry(ZoneInterface $zoneName, CountryInterface $country): void - { + public function theZoneNamedWithTheCountryMemberShouldAppearInTheRegistry( + ZoneInterface $zone, + CountryInterface $country + ): void { Assert::true($this->responseChecker->hasItemWithValue( - $this->client->subResourceIndex('members', $zoneName->getCode()), + $this->client->subResourceIndex('members', $zone->getCode()), 'code', $country->getCode() )); @@ -235,17 +237,17 @@ final class ManagingZonesContext implements Context Assert::false( $this->responseChecker->hasValue($this->client->update(), 'code', 'NEW_CODE'), - 'The code field with value NEW_CODE exist' + 'The code field with value NEW_CODE exists' ); } /** - * @Then I can not add a zone :zoneName + * @Then I can not add a zone :zone */ - public function iCanNotAddAZone(ZoneInterface $zoneName): void + public function iCanNotAddAZone(ZoneInterface $zone): void { $this->client->addSubResourceData('members', [ - 'code' => $zoneName->getCode(), + 'code' => $zone->getCode(), ]); Assert::contains( @@ -255,26 +257,30 @@ final class ManagingZonesContext implements Context } /** - * @Then the zone named :zoneName with the :province province member should appear in the registry + * @Then the zone named :zone with the :province province member should appear in the registry */ - public function theZoneNamedWithTheProvinceMemberShouldAppearInTheRegistry(ZoneInterface $zoneName, ProvinceInterface $province): void - { + public function theZoneNamedWithTheProvinceMemberShouldAppearInTheRegistry( + ZoneInterface $zone, + ProvinceInterface $province + ): void { Assert::true($this->responseChecker->hasItemWithValue( - $this->client->subResourceIndex('members', $zoneName->getCode()), + $this->client->subResourceIndex('members', $zone->getCode()), 'code', $province->getCode() )); } /** - * @Then the zone named :zoneNamed with the :zoneName zone member should appear in the registry + * @Then the zone named :zone with the :otherZone zone member should appear in the registry */ - public function theZoneNamedWithTheZoneMemberShouldAppearInTheRegistry(ZoneInterface $zoneNamed, ZoneInterface $zoneName): void - { + public function theZoneNamedWithTheZoneMemberShouldAppearInTheRegistry( + ZoneInterface $zone, + ZoneInterface $otherZone + ): void { Assert::true($this->responseChecker->hasItemWithValue( - $this->client->subResourceIndex('members', $zoneNamed->getCode()), + $this->client->subResourceIndex('members', $zone->getCode()), 'code', - $zoneName->getCode() + $otherZone->getCode() )); } @@ -329,7 +335,7 @@ final class ManagingZonesContext implements Context { Assert::false( $this->responseChecker->hasItemWithValue($this->client->index(), 'name', $name), - sprintf('Zone with name %s exist', $name) + sprintf('Zone with name %s exists', $name) ); } @@ -340,7 +346,7 @@ final class ManagingZonesContext implements Context { Assert::false( $this->responseChecker->hasItemWithValue($this->client->index(), $field, $value), - sprintf('Zone with %s %s exist', $field, $value) + sprintf('Zone with %s %s exists', $field, $value) ); } @@ -355,7 +361,10 @@ final class ManagingZonesContext implements Context $zoneMember->getCode() )); - Assert::same($this->responseChecker->countCollectionItems($this->client->subResourceIndex('members', $zone->getCode())), 1); + Assert::same( + $this->responseChecker->countCollectionItems($this->client->subResourceIndex('members', $zone->getCode())), + 1 + ); } /** @@ -472,15 +481,8 @@ final class ManagingZonesContext implements Context */ private function removeZoneMember($objectToRemove): void { - /** @var ZoneInterface $zone */ - $zone = $this->sharedStorage->get('zone'); $iri = $this->iriConverter->getItemIriFromResourceClass(ZoneMember::class, ['code' => $objectToRemove->getCode()]); - $members = $this->responseChecker->getValue($this->client->show($zone->getCode()), 'members'); - foreach ($members as $key => $member) { - if ($iri === $member) { - $this->client->removeSubResource('members', $member); - } - } + $this->client->removeSubResource('members', $iri); } } diff --git a/src/Sylius/Behat/Context/Transform/ZoneContext.php b/src/Sylius/Behat/Context/Transform/ZoneContext.php index 02162c231b..848240452d 100644 --- a/src/Sylius/Behat/Context/Transform/ZoneContext.php +++ b/src/Sylius/Behat/Context/Transform/ZoneContext.php @@ -31,21 +31,24 @@ final class ZoneContext implements Context /** * @Transform /^"([^"]+)" zone$/ * @Transform /^zone "([^"]+)"$/ - * @Transform :zone - */ - public function getZoneByCode(string $code): ZoneInterface - { - return $this->getZoneBy(['code' => $code]); - } - - /** * @Transform /^zone named "([^"]+)"$/ - * @Transform :zoneName - * @Transform :zoneNamed + * @Transform :zone + * @Transform :otherZone */ - public function getZoneByName(string $name): ZoneInterface + public function getZone(string $codeOrName): ZoneInterface { - return $this->getZoneBy(['name' => $name]); + $zone = $this->zoneRepository->findOneBy(['code' => $codeOrName]); + if (null !== $zone) { + return $zone; + } + + $zone = $this->zoneRepository->findOneBy(['name' => $codeOrName]); + Assert::notNull( + $zone, + 'Zone does not exist.' + ); + + return $zone; } /** @@ -61,15 +64,4 @@ final class ZoneContext implements Context return $zone; } - - private function getZoneBy(array $parameters): ZoneInterface - { - $existingZone = $this->zoneRepository->findOneBy($parameters); - Assert::notNull( - $existingZone, - 'Zone does not exist.' - ); - - return $existingZone; - } } diff --git a/src/Sylius/Behat/Context/Ui/Admin/ManagingZonesContext.php b/src/Sylius/Behat/Context/Ui/Admin/ManagingZonesContext.php index 564e974180..e4aab5ecfc 100644 --- a/src/Sylius/Behat/Context/Ui/Admin/ManagingZonesContext.php +++ b/src/Sylius/Behat/Context/Ui/Admin/ManagingZonesContext.php @@ -233,7 +233,7 @@ final class ManagingZonesContext implements Context /** * @Then I should not be able to edit its code */ - public function iShouldNotBeAbleToEditItsCode() + public function iShouldNotBeAbleToEditItsCode(): void { Assert::true($this->updatePage->isCodeDisabled()); } @@ -291,7 +291,7 @@ final class ManagingZonesContext implements Context /** * @Then I should not be able to edit its type */ - public function iShouldNotBeAbleToEditItsType() + public function iShouldNotBeAbleToEditItsType(): void { Assert::true($this->createPage->isTypeFieldDisabled()); } diff --git a/src/Sylius/Behat/Context/Ui/Shop/Checkout/CheckoutAddressingContext.php b/src/Sylius/Behat/Context/Ui/Shop/Checkout/CheckoutAddressingContext.php index 36ebfbaa6c..d6c9e454a9 100644 --- a/src/Sylius/Behat/Context/Ui/Shop/Checkout/CheckoutAddressingContext.php +++ b/src/Sylius/Behat/Context/Ui/Shop/Checkout/CheckoutAddressingContext.php @@ -150,7 +150,7 @@ final class CheckoutAddressingContext implements Context /** * @When I specify shipping country province as :provinceName */ - public function iSpecifyShippingCountryProvinceAs($provinceName) + public function iSpecifyShippingCountryProvinceAs(string $provinceName): void { $this->addressPage->selectShippingAddressProvince($provinceName); } @@ -158,7 +158,7 @@ final class CheckoutAddressingContext implements Context /** * @When I specify billing country province as :provinceName */ - public function iSpecifyBillingCountryProvinceAs($provinceName) + public function iSpecifyBillingCountryProvinceAs(string $provinceName): void { $this->addressPage->selectBillingAddressProvince($provinceName); } diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Zone.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Zone.xml index efe2966540..4c0a7a6540 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Zone.xml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Zone.xml @@ -46,7 +46,9 @@ zone:update - + + is_granted('ROLE_API_ACCESS') + diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/services.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/services.xml index e7a3d00673..cb469b7488 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/services.xml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/services.xml @@ -21,10 +21,10 @@ - - - - + + + + From b9df4a5f60dd7260c76e13ebfc2666c611180259 Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Sat, 9 May 2020 12:22:52 +0200 Subject: [PATCH 11/12] [API][Zone] Revert implementation of seeing disable type field scenarios in api context --- .../managing_zones/editing_zone.feature | 13 ------------ .../managing_zones/zone_validation.feature | 18 +++++++++++++++-- .../Behat/Client/ApiClientInterface.php | 3 --- src/Sylius/Behat/Client/ApiPlatformClient.php | 6 ------ src/Sylius/Behat/Client/Request.php | 6 ------ src/Sylius/Behat/Client/RequestInterface.php | 2 -- .../Api/Admin/ManagingZonesContext.php | 20 ------------------- 7 files changed, 16 insertions(+), 52 deletions(-) diff --git a/features/addressing/managing_zones/editing_zone.feature b/features/addressing/managing_zones/editing_zone.feature index 0671053f0c..8beef82fb4 100644 --- a/features/addressing/managing_zones/editing_zone.feature +++ b/features/addressing/managing_zones/editing_zone.feature @@ -55,16 +55,3 @@ Feature: Editing a zone And I save my changes Then I should be notified that it has been successfully edited And this zone name should be "EU" - - @ui @api - Scenario: Seeing a disabled code field when editing a zone - Given the store has a zone "European Union" with code "EU" - And it has the "France" country member - When I want to modify the zone named "European Union" - Then I should not be able to edit its code - - @ui @api - Scenario: Not seeing zone itself in member select when editing a zone of type zone - Given the store has a zone "European Union" with code "EU" - When I want to modify the zone named "European Union" - Then I can not add a zone "European Union" diff --git a/features/addressing/managing_zones/zone_validation.feature b/features/addressing/managing_zones/zone_validation.feature index a03bed1793..dc3471c928 100644 --- a/features/addressing/managing_zones/zone_validation.feature +++ b/features/addressing/managing_zones/zone_validation.feature @@ -5,7 +5,8 @@ Feature: Zone validation I want to be prevented from adding it without specifying required fields Background: - Given the store has country "United States" + Given the store operates in "France" and "Germany" + And the store has country "United States" And I am logged in as an administrator @ui @api @@ -37,12 +38,25 @@ Feature: Zone validation And zone with name "European Union" should not be added @ui @api + Scenario: Being unable to edit code of an existing zone + Given the store has a zone "European Union" with code "EU" + And it has the "France" country member + When I want to modify the zone named "European Union" + Then I should not be able to edit its code + + @ui @api + Scenario: Not seeing zone itself in member select when editing a zone of type zone + Given the store has a zone "European Union" with code "EU" + When I want to modify the zone named "European Union" + Then I can not add a zone "European Union" + + @ui Scenario: Seeing a disabled type field when adding country type zone When I want to create a new zone consisting of country Then I should not be able to edit its type And it should be of country type - @ui @api + @ui Scenario: Seeing a disabled type field when adding province type zone When I want to create a new zone consisting of province Then I should not be able to edit its type diff --git a/src/Sylius/Behat/Client/ApiClientInterface.php b/src/Sylius/Behat/Client/ApiClientInterface.php index ede72cbe3d..173d175cc7 100644 --- a/src/Sylius/Behat/Client/ApiClientInterface.php +++ b/src/Sylius/Behat/Client/ApiClientInterface.php @@ -61,9 +61,6 @@ interface ApiClientInterface /** @param string|int|array $value */ public function addRequestData(string $key, $value): void; - /** @return string|int|array */ - public function getRequestData(string $key); - public function addSubResourceData(string $key, array $data): void; public function removeSubResource(string $subResource, string $id): void; diff --git a/src/Sylius/Behat/Client/ApiPlatformClient.php b/src/Sylius/Behat/Client/ApiPlatformClient.php index 92c992a0c9..ec58c48bfa 100644 --- a/src/Sylius/Behat/Client/ApiPlatformClient.php +++ b/src/Sylius/Behat/Client/ApiPlatformClient.php @@ -157,12 +157,6 @@ final class ApiPlatformClient implements ApiClientInterface $this->request->updateContent([$key => $value]); } - /** @return string|int|array */ - public function getRequestData(string $key) - { - return $this->request->getData($key); - } - public function updateRequestData(array $data): void { $this->request->updateContent($data); diff --git a/src/Sylius/Behat/Client/Request.php b/src/Sylius/Behat/Client/Request.php index 743d932e79..05e8c3410c 100644 --- a/src/Sylius/Behat/Client/Request.php +++ b/src/Sylius/Behat/Client/Request.php @@ -154,12 +154,6 @@ final class Request implements RequestInterface $this->content = $this->mergeArraysUniquely($this->content, $newValues); } - /** @return string|int|array */ - public function getData(string $key) - { - return $this->content[$key]; - } - public function parameters(): array { return $this->parameters; diff --git a/src/Sylius/Behat/Client/RequestInterface.php b/src/Sylius/Behat/Client/RequestInterface.php index fb3e352bd2..fc7291991e 100644 --- a/src/Sylius/Behat/Client/RequestInterface.php +++ b/src/Sylius/Behat/Client/RequestInterface.php @@ -45,8 +45,6 @@ interface RequestInterface public function updateContent(array $newValues): void; - public function getData(string $key); - public function parameters(): array; public function updateParameters(array $newParameters): void; diff --git a/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php b/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php index aed211cac1..60eab207e7 100644 --- a/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php +++ b/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php @@ -378,26 +378,6 @@ final class ManagingZonesContext implements Context ); } - /** - * @Then I should not be able to edit its type - */ - public function iShouldNotBeAbleToEditItsType(): void - { - // Intentionally left blank, because in API context we can do that - } - - /** - * @Then /^it should be of (country|province) type$/ - */ - public function itShouldBeOfType(string $type): void - { - Assert::same( - $type, - $this->client->getRequestData('type'), - sprintf('Zone should be of type %s.', $type) - ); - } - /** * @Then I should be notified that it has been successfully created */ From 788c2ea47b794f718c782e6a5465e73993164eee Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Sun, 10 May 2020 20:33:15 +0200 Subject: [PATCH 12/12] [Zone] Move zone validator from ApiBundle to AddressingBundle --- .../managing_zones/zone_validation.feature | 2 +- .../Api/Admin/ManagingZonesContext.php | 2 +- .../Resources/config/services.xml | 4 +++ .../Resources/config/validation/Zone.xml | 6 ++++ .../Resources/translations/validators.en.yml | 1 + .../Constraints/ZoneCannotContainItself.php | 6 ++-- .../ZoneCannotContainItselfValidator.php | 2 +- .../ZoneCannotContainItselfValidatorSpec.php | 29 ++++++++++--------- .../Resources/config/services/validator.xml | 4 --- .../Resources/config/validation/Zone.xml | 25 ---------------- 10 files changed, 32 insertions(+), 49 deletions(-) rename src/Sylius/Bundle/{ApiBundle => AddressingBundle}/Validator/Constraints/ZoneCannotContainItself.php (68%) rename src/Sylius/Bundle/{ApiBundle => AddressingBundle}/Validator/Constraints/ZoneCannotContainItselfValidator.php (94%) rename src/Sylius/Bundle/{ApiBundle => AddressingBundle}/spec/Validator/Constraints/ZoneCannotContainItselfValidatorSpec.php (70%) delete mode 100644 src/Sylius/Bundle/ApiBundle/Resources/config/validation/Zone.xml diff --git a/features/addressing/managing_zones/zone_validation.feature b/features/addressing/managing_zones/zone_validation.feature index dc3471c928..dbac8fa8d9 100644 --- a/features/addressing/managing_zones/zone_validation.feature +++ b/features/addressing/managing_zones/zone_validation.feature @@ -45,7 +45,7 @@ Feature: Zone validation Then I should not be able to edit its code @ui @api - Scenario: Not seeing zone itself in member select when editing a zone of type zone + Scenario: Being unable to add itself to members during editing an existing zone Given the store has a zone "European Union" with code "EU" When I want to modify the zone named "European Union" Then I can not add a zone "European Union" diff --git a/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php b/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php index 60eab207e7..0cc0d05282 100644 --- a/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php +++ b/src/Sylius/Behat/Context/Api/Admin/ManagingZonesContext.php @@ -252,7 +252,7 @@ final class ManagingZonesContext implements Context Assert::contains( $this->responseChecker->getError($this->client->update()), - 'members: Chosen zone member is already in this zone.' + 'members: Zone member cannot be the same as a zone.' ); } diff --git a/src/Sylius/Bundle/AddressingBundle/Resources/config/services.xml b/src/Sylius/Bundle/AddressingBundle/Resources/config/services.xml index 1ae51d3018..d4f3c0ecc2 100644 --- a/src/Sylius/Bundle/AddressingBundle/Resources/config/services.xml +++ b/src/Sylius/Bundle/AddressingBundle/Resources/config/services.xml @@ -56,5 +56,9 @@ + + + + diff --git a/src/Sylius/Bundle/AddressingBundle/Resources/config/validation/Zone.xml b/src/Sylius/Bundle/AddressingBundle/Resources/config/validation/Zone.xml index 49b457ed95..c69a38e4bc 100644 --- a/src/Sylius/Bundle/AddressingBundle/Resources/config/validation/Zone.xml +++ b/src/Sylius/Bundle/AddressingBundle/Resources/config/validation/Zone.xml @@ -53,6 +53,12 @@ + + + + diff --git a/src/Sylius/Bundle/AddressingBundle/Resources/translations/validators.en.yml b/src/Sylius/Bundle/AddressingBundle/Resources/translations/validators.en.yml index db2b927d66..5de2018be9 100644 --- a/src/Sylius/Bundle/AddressingBundle/Resources/translations/validators.en.yml +++ b/src/Sylius/Bundle/AddressingBundle/Resources/translations/validators.en.yml @@ -56,6 +56,7 @@ sylius: min_length: Zone name must be at least {{ limit }} characters long|Zone name must be at least {{ limit }} characters long. not_blank: Please enter zone name. zone_member: + cannot_be_the_same_as_zone: Zone member cannot be the same as a zone. code: not_blank: Please choose a zone member. regex: Zone member code can only be comprised of letters, numbers, dashes and underscores. diff --git a/src/Sylius/Bundle/ApiBundle/Validator/Constraints/ZoneCannotContainItself.php b/src/Sylius/Bundle/AddressingBundle/Validator/Constraints/ZoneCannotContainItself.php similarity index 68% rename from src/Sylius/Bundle/ApiBundle/Validator/Constraints/ZoneCannotContainItself.php rename to src/Sylius/Bundle/AddressingBundle/Validator/Constraints/ZoneCannotContainItself.php index 1b22a682c5..f13768211b 100644 --- a/src/Sylius/Bundle/ApiBundle/Validator/Constraints/ZoneCannotContainItself.php +++ b/src/Sylius/Bundle/AddressingBundle/Validator/Constraints/ZoneCannotContainItself.php @@ -11,17 +11,17 @@ declare(strict_types=1); -namespace Sylius\Bundle\ApiBundle\Validator\Constraints; +namespace Sylius\Bundle\AddressingBundle\Validator\Constraints; use Symfony\Component\Validator\Constraint; final class ZoneCannotContainItself extends Constraint { /** @var string */ - public $message = 'sylius.zone_member.unique'; + public $message = 'sylius.zone_member.cannot_be_the_same_as_zone'; public function validatedBy(): string { - return 'sylius.validator.zone_cannot_contain_itself'; + return 'sylius_zone_cannot_contain_itself_validator'; } } diff --git a/src/Sylius/Bundle/ApiBundle/Validator/Constraints/ZoneCannotContainItselfValidator.php b/src/Sylius/Bundle/AddressingBundle/Validator/Constraints/ZoneCannotContainItselfValidator.php similarity index 94% rename from src/Sylius/Bundle/ApiBundle/Validator/Constraints/ZoneCannotContainItselfValidator.php rename to src/Sylius/Bundle/AddressingBundle/Validator/Constraints/ZoneCannotContainItselfValidator.php index c5f0b90466..30233ac65d 100644 --- a/src/Sylius/Bundle/ApiBundle/Validator/Constraints/ZoneCannotContainItselfValidator.php +++ b/src/Sylius/Bundle/AddressingBundle/Validator/Constraints/ZoneCannotContainItselfValidator.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace Sylius\Bundle\ApiBundle\Validator\Constraints; +namespace Sylius\Bundle\AddressingBundle\Validator\Constraints; use Sylius\Component\Addressing\Model\ZoneMemberInterface; use Symfony\Component\Validator\Constraint; diff --git a/src/Sylius/Bundle/ApiBundle/spec/Validator/Constraints/ZoneCannotContainItselfValidatorSpec.php b/src/Sylius/Bundle/AddressingBundle/spec/Validator/Constraints/ZoneCannotContainItselfValidatorSpec.php similarity index 70% rename from src/Sylius/Bundle/ApiBundle/spec/Validator/Constraints/ZoneCannotContainItselfValidatorSpec.php rename to src/Sylius/Bundle/AddressingBundle/spec/Validator/Constraints/ZoneCannotContainItselfValidatorSpec.php index 8288524068..dd7837b218 100644 --- a/src/Sylius/Bundle/ApiBundle/spec/Validator/Constraints/ZoneCannotContainItselfValidatorSpec.php +++ b/src/Sylius/Bundle/AddressingBundle/spec/Validator/Constraints/ZoneCannotContainItselfValidatorSpec.php @@ -11,11 +11,11 @@ declare(strict_types=1); -namespace spec\Sylius\Bundle\ApiBundle\Validator\Constraints; +namespace spec\Sylius\Bundle\AddressingBundle\Validator\Constraints; use PhpSpec\ObjectBehavior; use Prophecy\Argument; -use Sylius\Bundle\ApiBundle\Validator\Constraints\ZoneCannotContainItself; +use Sylius\Bundle\AddressingBundle\Validator\Constraints\ZoneCannotContainItself; use Sylius\Component\Addressing\Model\ZoneInterface; use Sylius\Component\Addressing\Model\ZoneMemberInterface; use Symfony\Component\Validator\Constraint; @@ -24,9 +24,8 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface; final class ZoneCannotContainItselfValidatorSpec extends ObjectBehavior { - function let( - ExecutionContextInterface $executionContext - ): void { + function let(ExecutionContextInterface $executionContext): void + { $this->beConstructedWith(); $this->initialize($executionContext); } @@ -36,7 +35,7 @@ final class ZoneCannotContainItselfValidatorSpec extends ObjectBehavior $this->shouldImplement(ConstraintValidatorInterface::class); } - function is_does_nothing_if_value_is_null(ExecutionContextInterface $executionContext): void + function it_does_nothing_if_value_is_null(ExecutionContextInterface $executionContext): void { $executionContext->addViolation(Argument::cetera())->shouldNotBeCalled(); @@ -45,14 +44,16 @@ final class ZoneCannotContainItselfValidatorSpec extends ObjectBehavior function it_throws_an_exception_if_constraint_is_not_of_expected_type(): void { - $this->shouldThrow(\InvalidArgumentException::class)->during('validate', ['', new class() extends Constraint { - }]); + $this + ->shouldThrow(\InvalidArgumentException::class) + ->during('validate', ['', new class() extends Constraint {}]) + ; } - function it_does_not_add_violation_if_zone_not_contain_itself_in_members( + function it_does_not_add_violation_if_zone_does_not_contain_itself_in_members( + ExecutionContextInterface $executionContext, ZoneInterface $zone, - ZoneMemberInterface $zoneMember, - ExecutionContextInterface $executionContext + ZoneMemberInterface $zoneMember ): void { $zone->getCode()->willReturn('WORLD'); $zoneMember->getCode()->willReturn('EU'); @@ -63,10 +64,10 @@ final class ZoneCannotContainItselfValidatorSpec extends ObjectBehavior $this->validate([$zoneMember], new ZoneCannotContainItself()); } - function it_adds_violation_if_zone_contain_itself_in_members( + function it_adds_violation_if_zone_contains_itself_in_members( + ExecutionContextInterface $executionContext, ZoneInterface $zone, - ZoneMemberInterface $zoneMember, - ExecutionContextInterface $executionContext + ZoneMemberInterface $zoneMember ): void { $zone->getCode()->willReturn('EU'); $zoneMember->getCode()->willReturn('EU'); diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/services/validator.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/services/validator.xml index bc0f645bc7..db1a64638c 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/services/validator.xml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/services/validator.xml @@ -21,9 +21,5 @@ - - - - diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/validation/Zone.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/validation/Zone.xml deleted file mode 100644 index 259824f1d1..0000000000 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/validation/Zone.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - - - - - - - - -