[Addressing] Add validation for zone type

This commit is contained in:
Wojdylak 2024-07-30 18:37:17 +02:00
parent c5dbbf85d4
commit 6607e993ff
No known key found for this signature in database
GPG key ID: 7509E560A6821ABE
5 changed files with 30 additions and 7 deletions

View file

@ -99,3 +99,13 @@ Feature: Zone validation
And I try to add it And I try to add it
Then I should be notified that "NA" is not a valid zone code Then I should be notified that "NA" is not a valid zone code
And zone with name "America" should not be added And zone with name "America" should not be added
@api @no-ui
Scenario: Trying to add a zone with wrong type
When I want to create a new zone consisting of wrong_type
And I name it "European Union"
And I specify its code as "EU"
And I add a country "France"
And I try to add it
Then I should be notified that "wrong_type" is not a valid zone type
And zone with name "European Union" should not be added

View file

@ -485,6 +485,17 @@ final readonly class ManagingZonesContext implements Context
); );
} }
/**
* @Then I should be notified that :type is not a valid zone type
*/
public function iShouldBeNotifiedThatIsNotAValidZoneType(string $type): void
{
Assert::contains(
$this->responseChecker->getError($this->client->getLastResponse()),
sprintf('Type "%s" is invalid. Allowed types are:', $type),
);
}
private function removeZoneMember(CountryInterface|ProvinceInterface|ZoneInterface $objectToRemove): void private function removeZoneMember(CountryInterface|ProvinceInterface|ZoneInterface $objectToRemove): void
{ {
$members = $this->client->getContent()['members']; $members = $this->client->getContent()['members'];

View file

@ -34,6 +34,13 @@
<option name="groups">sylius</option> <option name="groups">sylius</option>
</constraint> </constraint>
</property> </property>
<property name="type">
<constraint name="Choice">
<option name="callback">getTypes</option>
<option name="message">sylius.zone.type.invalid</option>
<option name="groups">sylius</option>
</constraint>
</property>
<property name="scope"> <property name="scope">
<constraint name="NotBlank"> <constraint name="NotBlank">
<option name="message">sylius.zone.scope.not_blank</option> <option name="message">sylius.zone.scope.not_blank</option>

View file

@ -51,6 +51,8 @@ sylius:
not_blank: Please enter zone code. not_blank: Please enter zone code.
regex: Zone code can only be comprised of letters, numbers, dashes and underscores. regex: Zone code can only be comprised of letters, numbers, dashes and underscores.
unique: Zone code must be unique. unique: Zone code must be unique.
type:
invalid: "Type {{ value }} is invalid. Allowed types are: {{ choices }}."
members: members:
min_count: Please add at least {{ limit }} zone member. min_count: Please add at least {{ limit }} zone member.
name: name:

View file

@ -82,15 +82,8 @@ class Zone implements ZoneInterface, \Stringable
return $this->type; return $this->type;
} }
/**
* @throws \InvalidArgumentException
*/
public function setType(?string $type): void public function setType(?string $type): void
{ {
if (!in_array($type, static::getTypes(), true)) {
throw new \InvalidArgumentException('Wrong zone type supplied.');
}
$this->type = $type; $this->type = $type;
} }