[Behat][API] Add the billing and shipping addresses validation

This commit is contained in:
Rafikooo 2023-11-22 13:11:45 +01:00
parent 3d48a074dc
commit da27211d02
No known key found for this signature in database
GPG key ID: 4A26D8327BC2442B
3 changed files with 75 additions and 15 deletions

View file

@ -16,7 +16,7 @@ Feature: Modifying a customer's billing address validation
And the customer chose "Free" shipping method with "Cash on Delivery" payment
And I am logged in as an administrator
@ui @no-api
@api @ui
Scenario: Attempt to save an order with incomplete billing address
When I view the summary of the order "#00000001"
And I want to modify a customer's billing address of this order

View file

@ -16,12 +16,12 @@ Feature: Modifying a customer's shipping address validation
And the customer chose "Free" shipping method with "Cash on Delivery" payment
And I am logged in as an administrator
@ui @no-api
@api @ui
Scenario: Attempt to save an order with incomplete shipping address
When I view the summary of the order "#00000001"
And I want to modify a customer's shipping address of this order
And I clear the shipping address information
But I do not specify new information
And I try to save my changes
Then I should be notified that all mandatory billing address details are incomplete
Then I should be notified that all mandatory shipping address details are incomplete
And this order should still be shipped to "Mike Ross", "350 5th Ave", "10118", "New York", "United States"

View file

@ -24,6 +24,16 @@ use Webmozart\Assert\Assert;
final class ManagingPlacedOrderAddressesContext implements Context
{
/** @var array<string, string> */
private array $addressProperties = [
'firstName' => 'getFirstName',
'lastName' => 'getLastName',
'street' => 'getStreet',
'postcode' => 'getPostcode',
'city' => 'getCity',
'countryCode' => 'getCountryCode',
];
public function __construct(
private ApiClientInterface $client,
private ResponseCheckerInterface $responseChecker,
@ -43,9 +53,9 @@ final class ManagingPlacedOrderAddressesContext implements Context
}
/**
* @When /^I want to modify a customer's shipping address of this order$/
* @When I want to modify a customer's shipping address of this order
*/
public function iWantToModifyCustomerAddress(): void
public function iWantToModifyCustomerShippingAddress(): void
{
$this->client->buildUpdateRequest(
Resources::ADDRESSES,
@ -53,6 +63,22 @@ final class ManagingPlacedOrderAddressesContext implements Context
);
}
/**
* @When /^I clear the (?:billing|shipping) address information$/
*/
public function iClearTheAddressInformation(): void
{
$this->client->updateRequestData(array_fill_keys(array_keys($this->addressProperties), ''));
}
/**
* @When /^I do not specify new information$/
*/
public function iDoNotSpecifyNewInformation(): void
{
// Intentionally left blank to fulfill context expectation
}
/**
* @When /^I specify their (?:|new )(?:billing|shipping) (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/
*/
@ -79,7 +105,7 @@ final class ManagingPlacedOrderAddressesContext implements Context
}
/**
* @Then /^this order should(?:|still ) (be shipped to "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)")$/
* @Then /^this order should(?:| still) (be shipped to "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)")$/
*/
public function itShouldBeShippedTo(AddressInterface $address): void
{
@ -91,19 +117,53 @@ final class ManagingPlacedOrderAddressesContext implements Context
$this->assertAddressResponseProperties($response, $address);
}
private function assertAddressResponseProperties(Response $response, AddressInterface $exceptedAddress): void
/**
* @Then /^I should be notified that all mandatory (?:shipping|billing) address details are incomplete$/
*/
public function iShouldBeNotifiedThatAllMandatoryAddressDetailsAreIncomplete(): void
{
$addressProperties = [
'firstName' => 'getFirstName',
'lastName' => 'getLastName',
'street' => 'getStreet',
'postcode' => 'getPostcode',
'city' => 'getCity',
'countryCode' => 'getCountryCode',
/** @var array<string, array<string, string>> $mandatoryAddressProperties */
$mandatoryAddressProperties = [
'firstName' => ['minLength' => '2 characters'],
'lastName' => ['minLength' => '2 characters'],
'street' => ['minLength' => '2 characters'],
'city' => ['minLength' => '2 characters'],
'postcode' => ['minLength' => '1 character'],
];
foreach ($addressProperties as $property => $getter) {
$violations = $this->responseChecker->getError($this->client->getLastResponse());
foreach ($mandatoryAddressProperties as $property => $constraints) {
Assert::contains(
$violations,
sprintf('%s: Please enter %s.', $property, $this->camelCaseToSpaces($property)),
'Not found violation for ' . $property . ' property.',
);
$formattedProperty = ucfirst($this->camelCaseToSpaces($property));
Assert::contains(
$violations,
sprintf('%s: %s must be at least %s long.', $property, $formattedProperty, $constraints['minLength']),
'Not found violation for ' . $property . ' property.',
);
}
Assert::contains(
$violations,
'countryCode: Please select country.',
'Not found violation for countryCode property.',
);
}
private function assertAddressResponseProperties(Response $response, AddressInterface $exceptedAddress): void
{
foreach ($this->addressProperties as $property => $getter) {
Assert::same($this->responseChecker->getValue($response, $property), $exceptedAddress->$getter());
}
}
private function camelCaseToSpaces(string $string): string
{
return strtolower(preg_replace('/(?<!^)[A-Z]/', ' $0', $string));
}
}