Integrate name converter for key normalization in ManagingOrdersContext

This commit is contained in:
Francis Hilaire 2026-05-28 18:16:17 +02:00
parent 64c1f29314
commit cd07d06f99
No known key found for this signature in database
GPG key ID: 3392F830BF33D421
2 changed files with 65 additions and 25 deletions

View file

@ -39,6 +39,7 @@ use Sylius\Component\Shipping\ShipmentTransitions;
use Symfony\Component\HttpFoundation\Request as HttpRequest; use Symfony\Component\HttpFoundation\Request as HttpRequest;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Intl\Countries; use Symfony\Component\Intl\Countries;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Webmozart\Assert\Assert; use Webmozart\Assert\Assert;
final readonly class ManagingOrdersContext implements Context final readonly class ManagingOrdersContext implements Context
@ -50,6 +51,7 @@ final readonly class ManagingOrdersContext implements Context
private SecurityServiceInterface $adminSecurityService, private SecurityServiceInterface $adminSecurityService,
private SharedStorageInterface $sharedStorage, private SharedStorageInterface $sharedStorage,
private SharedSecurityServiceInterface $sharedSecurityService, private SharedSecurityServiceInterface $sharedSecurityService,
private ?NameConverterInterface $nameConverter
) { ) {
} }
@ -327,8 +329,9 @@ final readonly class ManagingOrdersContext implements Context
/** @var array{productName: string}[] $items */ /** @var array{productName: string}[] $items */
$items = json_decode($lastResponseContent, true)['items']; $items = json_decode($lastResponseContent, true)['items'];
$productNameKey = $this->getNormalizedKey('productName');
foreach ($items as $item) { foreach ($items as $item) {
if ($item['productName'] === $itemName) { if ($item[$productNameKey] === $itemName) {
$this->sharedStorage->set('item', $item); $this->sharedStorage->set('item', $item);
return; return;
@ -480,10 +483,12 @@ final readonly class ManagingOrdersContext implements Context
{ {
$order = $this->responseChecker->getCollection($this->client->getLastResponse())[0]; $order = $this->responseChecker->getCollection($this->client->getLastResponse())[0];
$paymentStateKey = $this->getNormalizedKey('paymentState');
$tokenValueKey = $this->getNormalizedKey('tokenValue');
Assert::same( Assert::same(
$order['paymentState'], $order[$paymentStateKey],
strtolower($orderPaymentState), strtolower($orderPaymentState),
sprintf('Order "%s" does not have "%s" payment state', $order['tokenValue'], $orderPaymentState), sprintf('Order "%s" does not have "%s" payment state', $order[$tokenValueKey], $orderPaymentState),
); );
} }
@ -502,8 +507,9 @@ final readonly class ManagingOrdersContext implements Context
{ {
$items = $this->responseChecker->getValue($this->client->getLastResponse(), 'items'); $items = $this->responseChecker->getValue($this->client->getLastResponse(), 'items');
$productNameKey = $this->getNormalizedKey('productName');
foreach ($items as $item) { foreach ($items as $item) {
if ($item['productName'] === $productName) { if ($item[$productNameKey] === $productName) {
return; return;
} }
} }
@ -860,8 +866,9 @@ final readonly class ManagingOrdersContext implements Context
*/ */
public function iShouldSeeAsProvinceInTheShippingAddress(string $provinceName): void public function iShouldSeeAsProvinceInTheShippingAddress(string $provinceName): void
{ {
$provinceNameKey = $this->getNormalizedKey('provinceName');
Assert::same( Assert::same(
$this->responseChecker->getValue($this->client->getLastResponse(), 'shippingAddress')['provinceName'], $this->responseChecker->getValue($this->client->getLastResponse(), 'shippingAddress')[$provinceNameKey],
$provinceName, $provinceName,
); );
} }
@ -871,8 +878,9 @@ final readonly class ManagingOrdersContext implements Context
*/ */
public function iShouldSeeAsProvinceInTheBillingAddress(string $provinceName): void public function iShouldSeeAsProvinceInTheBillingAddress(string $provinceName): void
{ {
$provinceNameKey = $this->getNormalizedKey('provinceName');
Assert::same( Assert::same(
$this->responseChecker->getValue($this->client->getLastResponse(), 'billingAddress')['provinceName'], $this->responseChecker->getValue($this->client->getLastResponse(), 'billingAddress')[$provinceNameKey],
$provinceName, $provinceName,
); );
} }
@ -895,7 +903,8 @@ final readonly class ManagingOrdersContext implements Context
*/ */
public function itemUnitPriceShouldBe(array $orderItem, string $unitPrice): void public function itemUnitPriceShouldBe(array $orderItem, string $unitPrice): void
{ {
Assert::same($this->getTotalAsInt($unitPrice), $orderItem['unitPrice']); $unitPriceKey = $this->getNormalizedKey('unitPrice');
Assert::same($this->getTotalAsInt($unitPrice), $orderItem[$unitPriceKey]);
} }
/** /**
@ -943,14 +952,18 @@ final readonly class ManagingOrdersContext implements Context
{ {
$orderItem = $this->sharedStorage->get('item'); $orderItem = $this->sharedStorage->get('item');
$typeKey = $this->getNormalizedKey('type');
$amountKey = $this->getNormalizedKey('amount');
$unitPriceKey = $this->getNormalizedKey('unitPrice');
$quantityKey = $this->getNormalizedKey('quantity');
$unitPromotionAdjustments = 0; $unitPromotionAdjustments = 0;
foreach ($this->responseChecker->getCollection($this->client->getLastResponse()) as $item) { foreach ($this->responseChecker->getCollection($this->client->getLastResponse()) as $item) {
if (in_array($item['type'], [AdjustmentInterface::ORDER_UNIT_PROMOTION_ADJUSTMENT, AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT])) { if (in_array($item[$typeKey], [AdjustmentInterface::ORDER_UNIT_PROMOTION_ADJUSTMENT, AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT])) {
$unitPromotionAdjustments += $item['amount']; $unitPromotionAdjustments += $item[$amountKey];
} }
} }
Assert::same($this->getTotalAsInt($subtotal), $orderItem['unitPrice'] * $orderItem['quantity'] + $unitPromotionAdjustments); Assert::same($this->getTotalAsInt($subtotal), $orderItem[$unitPriceKey] * $orderItem[$quantityKey] + $unitPromotionAdjustments);
} }
/** /**
@ -1041,9 +1054,10 @@ final readonly class ManagingOrdersContext implements Context
*/ */
public function productUnitPriceShouldBe(string $productName, string $price): void public function productUnitPriceShouldBe(string $productName, string $price): void
{ {
$unitPriceKey = $this->getNormalizedKey('unitPrice');
$this->iCheckData($productName); $this->iCheckData($productName);
$orderItem = $this->sharedStorage->get('item'); $orderItem = $this->sharedStorage->get('item');
Assert::same($this->getTotalAsInt($price), $orderItem['unitPrice']); Assert::same($this->getTotalAsInt($price), $orderItem[$unitPriceKey]);
} }
/** /**
@ -1052,7 +1066,8 @@ final readonly class ManagingOrdersContext implements Context
public function productDiscountedUnitPriceShouldBe(string $productName, string $price): void public function productDiscountedUnitPriceShouldBe(string $productName, string $price): void
{ {
$orderItem = $this->sharedStorage->get('item'); $orderItem = $this->sharedStorage->get('item');
Assert::same($this->getTotalAsInt($price), $orderItem['fullDiscountedUnitPrice']); $fullDiscountedUnitPriceKey = $this->getNormalizedKey('fullDiscountedUnitPrice');
Assert::same($this->getTotalAsInt($price), $orderItem[$fullDiscountedUnitPriceKey]);
} }
/** /**
@ -1061,6 +1076,7 @@ final readonly class ManagingOrdersContext implements Context
public function productQuantityShouldBe(string $productName, int $quantity): void public function productQuantityShouldBe(string $productName, int $quantity): void
{ {
$orderItem = $this->sharedStorage->get('item'); $orderItem = $this->sharedStorage->get('item');
$quantityKey = $this->getNormalizedKey('quantity');
Assert::same($quantity, $orderItem['quantity']); Assert::same($quantity, $orderItem['quantity']);
} }
@ -1077,9 +1093,12 @@ final readonly class ManagingOrdersContext implements Context
AdjustmentInterface::ORDER_UNIT_PROMOTION_ADJUSTMENT, AdjustmentInterface::ORDER_UNIT_PROMOTION_ADJUSTMENT,
); );
$orderItemUnitsKey = $this->getNormalizedKey('orderItemUnits');
$unitsKey = $this->getNormalizedKey('units');
$amountKey = $this->getNormalizedKey('amount');
foreach ($adjustments as $adjustment) { foreach ($adjustments as $adjustment) {
if (in_array($adjustment['orderItemUnit'], $orderItem['units'])) { if (in_array($adjustment[$orderItemUnitsKey], $orderItem[$unitsKey])) {
Assert::same($this->getTotalAsInt($price), $adjustment['amount']); Assert::same($this->getTotalAsInt($price), $adjustment[$amountKey]);
return; return;
} }
@ -1099,9 +1118,12 @@ final readonly class ManagingOrdersContext implements Context
AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT, AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT,
); );
$orderItemUnitsKey = $this->getNormalizedKey('orderItemUnits');
$unitsKey = $this->getNormalizedKey('units');
$amountKey = $this->getNormalizedKey('amount');
foreach ($adjustments as $adjustment) { foreach ($adjustments as $adjustment) {
if (in_array($adjustment['orderItemUnit'], $orderItem['units'])) { if (in_array($adjustment[$orderItemUnitsKey], $orderItem[$unitsKey])) {
Assert::same($this->getTotalAsInt(trim($price, ' ~')), $adjustment['amount']); Assert::same($this->getTotalAsInt(trim($price, ' ~')), $adjustment[$amountKey]);
return; return;
} }
@ -1116,16 +1138,22 @@ final readonly class ManagingOrdersContext implements Context
$orderItem = $this->sharedStorage->get('item'); $orderItem = $this->sharedStorage->get('item');
$response = $this->getAdjustmentsResponseForOrder(true); $response = $this->getAdjustmentsResponseForOrder(true);
$typeKey = $this->getNormalizedKey('type');
$orderItemUnitsKey = $this->getNormalizedKey('orderItemUnits');
$unitsKey = $this->getNormalizedKey('units');
$amountKey = $this->getNormalizedKey('amount');
$unitPromotionAdjustments = 0; $unitPromotionAdjustments = 0;
foreach ($this->responseChecker->getCollection($response) as $adjustment) { foreach ($this->responseChecker->getCollection($response) as $adjustment) {
if (in_array($adjustment['type'], [AdjustmentInterface::ORDER_UNIT_PROMOTION_ADJUSTMENT, AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT])) { if (in_array($adjustment[$typeKey], [AdjustmentInterface::ORDER_UNIT_PROMOTION_ADJUSTMENT, AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT])) {
if (in_array($adjustment['orderItemUnit'], $orderItem['units'])) { if (in_array($adjustment[$orderItemUnitsKey], $orderItem[$unitsKey])) {
$unitPromotionAdjustments += $adjustment['amount']; $unitPromotionAdjustments += $adjustment[$amountKey];
} }
} }
} }
Assert::same($this->getTotalAsInt($subTotal), $orderItem['unitPrice'] * $orderItem['quantity'] + $unitPromotionAdjustments); $quantityKey = $this->getNormalizedKey('quantity');
$unitPriceKey = $this->getNormalizedKey('unitPrice');
Assert::same($this->getTotalAsInt($subTotal), $orderItem[$unitPriceKey] * $orderItem[$quantityKey] + $unitPromotionAdjustments);
} }
/** /**
@ -1305,11 +1333,17 @@ final readonly class ManagingOrdersContext implements Context
string $city, string $city,
string $countryName, string $countryName,
): void { ): void {
Assert::same($address['firstName'] . ' ' . $address['lastName'], $customerName); $firstNameKey = $this->getNormalizedKey('firstName');
Assert::same($address['street'], $street); $lastNameKey = $this->getNormalizedKey('lastName');
Assert::same($address['postcode'], $postcode); $streetKey = $this->getNormalizedKey('street');
Assert::same($address['city'], $city); $postcodeKey = $this->getNormalizedKey('postcode');
Assert::same($address['countryCode'], $this->getCountryCodeFromName($countryName)); $cityKey = $this->getNormalizedKey('city');
$countryCodeKey = $this->getNormalizedKey('countryCode');
Assert::same($address[$firstNameKey] . ' ' . $address[$lastNameKey], $customerName);
Assert::same($address[$streetKey], $street);
Assert::same($address[$postcodeKey], $postcode);
Assert::same($address[$cityKey], $city);
Assert::same($address[$countryCodeKey], $this->getCountryCodeFromName($countryName));
} }
private function getCountryCodeFromName(string $name): string private function getCountryCodeFromName(string $name): string
@ -1352,4 +1386,9 @@ final readonly class ManagingOrdersContext implements Context
forgetResponse: $forgetResponse, forgetResponse: $forgetResponse,
); );
} }
private function getNormalizedKey(string $key): string
{
return $this->nameConverter ? $this->nameConverter->normalize($key) : $key;
}
} }

View file

@ -208,6 +208,7 @@
<argument type="service" id="sylius.behat.api_admin_security" /> <argument type="service" id="sylius.behat.api_admin_security" />
<argument type="service" id="sylius.behat.shared_storage" /> <argument type="service" id="sylius.behat.shared_storage" />
<argument type="service" id="sylius.behat.api.shared_security" /> <argument type="service" id="sylius.behat.api.shared_security" />
<argument type="service" id="api_platform.name_converter" on-invalid="ignore" />
</service> </service>
<service id="sylius.behat.context.api.admin.managing_payment_methods" class="Sylius\Behat\Context\Api\Admin\ManagingPaymentMethodsContext"> <service id="sylius.behat.context.api.admin.managing_payment_methods" class="Sylius\Behat\Context\Api\Admin\ManagingPaymentMethodsContext">