minor #16656 [Addressing] Add validation for zone type (Wojdylak)

This PR was merged into the api-platform-3 branch.

Discussion
----------

| Q               | A
|-----------------|-----
| Branch?         | api-platform-3
| Bug fix?        | no
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | n/a
| License         | MIT

Commits
-------
  [Addressing] Add validation for zone type
This commit is contained in:
Jan Góralski 2024-07-31 11:21:07 +02:00 committed by GitHub
commit 4df2e729ce
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 30 additions and 7 deletions

View file

@ -99,3 +99,13 @@ Feature: Zone validation
And I try to add it
Then I should be notified that "NA" is not a valid zone code
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
{
$members = $this->client->getContent()['members'];

View file

@ -34,6 +34,13 @@
<option name="groups">sylius</option>
</constraint>
</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">
<constraint name="NotBlank">
<option name="message">sylius.zone.scope.not_blank</option>

View file

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

View file

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