Skip to content

Commit b7158f3

Browse files
committed
feat: implement NormalizedKeyAwareTrait for key normalization across contexts
1 parent 1484d67 commit b7158f3

24 files changed

Lines changed: 502 additions & 235 deletions

src/Sylius/Behat/Context/Api/Admin/BrowsingCatalogPromotionProductVariantsContext.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,24 @@
1717
use Behat\Behat\Context\Context;
1818
use Sylius\Behat\Client\ApiClientInterface;
1919
use Sylius\Behat\Client\ResponseCheckerInterface;
20+
use Sylius\Behat\Context\Api\NormalizedKeyAwareTrait;
2021
use Sylius\Behat\Context\Api\Resources;
2122
use Sylius\Component\Core\Model\CatalogPromotionInterface;
2223
use Sylius\Component\Core\Model\ChannelInterface;
2324
use Sylius\Component\Core\Model\ProductInterface;
2425
use Sylius\Component\Core\Model\ProductVariantInterface;
26+
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
2527
use Webmozart\Assert\Assert;
2628

2729
final class BrowsingCatalogPromotionProductVariantsContext implements Context
2830
{
31+
use NormalizedKeyAwareTrait;
32+
2933
public function __construct(
3034
private ApiClientInterface $client,
3135
private ResponseCheckerInterface $responseChecker,
3236
private IriConverterInterface $iriConverter,
37+
private ?NameConverterInterface $nameConverter,
3338
) {
3439
}
3540

@@ -145,9 +150,13 @@ private function variantHasCatalogPromotionInChannel(
145150
): bool {
146151
$variantData = $this->getDataOfVariantWithCode($variant->getCode());
147152

148-
$promotions = $variantData['channelPricings'][$channel->getCode()]['appliedPromotions'] ?? [];
153+
$channelPricingsKey = $this->getNormalizedKey('channelPricings');
154+
$appliedPromotionsKey = $this->getNormalizedKey('appliedPromotions');
155+
$codeKey = $this->getNormalizedKey('code');
156+
157+
$promotions = $variantData[$channelPricingsKey][$channel->getCode()][$appliedPromotionsKey] ?? [];
149158
foreach ($promotions as $promotion) {
150-
if ($promotion['code'] === $catalogPromotion->getCode()) {
159+
if ($promotion[$codeKey] === $catalogPromotion->getCode()) {
151160
return true;
152161
}
153162
}
@@ -158,8 +167,10 @@ private function variantHasCatalogPromotionInChannel(
158167
private function getDataOfVariantWithCode(string $code): array
159168
{
160169
$variantsData = $this->responseChecker->getCollection($this->client->getLastResponse());
170+
$codeKey = $this->getNormalizedKey('code');
171+
161172
foreach ($variantsData as $variantData) {
162-
if ($variantData['code'] === $code) {
173+
if ($variantData[$codeKey] === $code) {
163174
return $variantData;
164175
}
165176
}

src/Sylius/Behat/Context/Api/Admin/ChannelPricingLogEntryContext.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,22 @@
1616
use Behat\Behat\Context\Context;
1717
use Sylius\Behat\Client\ApiClientInterface;
1818
use Sylius\Behat\Client\ResponseCheckerInterface;
19+
use Sylius\Behat\Context\Api\NormalizedKeyAwareTrait;
1920
use Sylius\Behat\Context\Api\Resources;
2021
use Sylius\Behat\Service\SharedStorageInterface;
2122
use Sylius\Component\Core\Model\ProductVariantInterface;
23+
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
2224
use Webmozart\Assert\Assert;
2325

2426
final class ChannelPricingLogEntryContext implements Context
2527
{
28+
use NormalizedKeyAwareTrait;
29+
2630
public function __construct(
2731
private ApiClientInterface $client,
2832
private ResponseCheckerInterface $responseChecker,
2933
private SharedStorageInterface $sharedStorage,
34+
private ?NameConverterInterface $nameConverter,
3035
) {
3136
}
3237

@@ -69,8 +74,8 @@ public function thereShouldBeALogEntryOnThePositionWithTheSellingPriceOriginalPr
6974

7075
$logEntry = $this->responseChecker->getCollection($this->client->getLastResponse())[$position - 1];
7176

72-
Assert::same($logEntry['price'], $price);
73-
Assert::same($logEntry['originalPrice'], $originalPrice);
77+
Assert::same($logEntry[$this->getNormalizedKey('price')], $price);
78+
Assert::same($logEntry[$this->getNormalizedKey('originalPrice')], $originalPrice);
7479
Assert::keyExists($logEntry, 'loggedAt');
7580
}
7681

src/Sylius/Behat/Context/Api/Admin/ManagingChannelsBillingDataContext.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,21 @@
1616
use Behat\Behat\Context\Context;
1717
use Sylius\Behat\Client\ApiClientInterface;
1818
use Sylius\Behat\Client\ResponseCheckerInterface;
19+
use Sylius\Behat\Context\Api\NormalizedKeyAwareTrait;
1920
use Sylius\Behat\Context\Api\Resources;
2021
use Sylius\Component\Addressing\Model\CountryInterface;
2122
use Sylius\Component\Core\Model\ChannelInterface;
23+
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
2224
use Webmozart\Assert\Assert;
2325

2426
final readonly class ManagingChannelsBillingDataContext implements Context
2527
{
28+
use NormalizedKeyAwareTrait;
29+
2630
public function __construct(
2731
private ApiClientInterface $client,
2832
private ResponseCheckerInterface $responseChecker,
33+
private ?NameConverterInterface $nameConverter,
2934
) {
3035
}
3136

@@ -36,7 +41,7 @@ public function thisChannelCompanyShouldBe(ChannelInterface $channel, string $co
3641
{
3742
$shopBillingData = $this->getShopBillingDataFromChannel($channel);
3843

39-
Assert::same($shopBillingData['company'], $company);
44+
Assert::same($shopBillingData[$this->getNormalizedKey('company')], $company);
4045
}
4146

4247
/**
@@ -46,7 +51,7 @@ public function thisChannelTaxIdShouldBe(ChannelInterface $channel, string $taxI
4651
{
4752
$shopBillingData = $this->getShopBillingDataFromChannel($channel);
4853

49-
Assert::same($shopBillingData['taxId'], $taxId);
54+
Assert::same($shopBillingData[$this->getNormalizedKey('taxId')], $taxId);
5055
}
5156

5257
/**
@@ -62,10 +67,10 @@ public function thisChannelShopBillingAddressShouldBe(
6267
): void {
6368
$shopBillingData = $this->getShopBillingDataFromChannel($channel);
6469

65-
Assert::same($shopBillingData['street'], $street);
66-
Assert::same($shopBillingData['postcode'], $postcode);
67-
Assert::same($shopBillingData['city'], $city);
68-
Assert::same($shopBillingData['countryCode'], $country->getCode());
70+
Assert::same($shopBillingData[$this->getNormalizedKey('street')], $street);
71+
Assert::same($shopBillingData[$this->getNormalizedKey('postcode')], $postcode);
72+
Assert::same($shopBillingData[$this->getNormalizedKey('city')], $city);
73+
Assert::same($shopBillingData[$this->getNormalizedKey('countryCode')], $country->getCode());
6974
}
7075

7176
/**

src/Sylius/Behat/Context/Api/Admin/ManagingExchangeRatesContext.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,24 @@
1616
use Behat\Behat\Context\Context;
1717
use Sylius\Behat\Client\ApiClientInterface;
1818
use Sylius\Behat\Client\ResponseCheckerInterface;
19+
use Sylius\Behat\Context\Api\NormalizedKeyAwareTrait;
1920
use Sylius\Behat\Context\Api\Resources;
2021
use Sylius\Behat\Service\SharedStorageInterface;
2122
use Sylius\Component\Currency\Model\CurrencyInterface;
2223
use Sylius\Component\Currency\Model\ExchangeRateInterface;
24+
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
2325
use Webmozart\Assert\Assert;
2426

2527
final readonly class ManagingExchangeRatesContext implements Context
2628
{
29+
use NormalizedKeyAwareTrait;
30+
2731
public function __construct(
2832
private ApiClientInterface $client,
2933
private ResponseCheckerInterface $responseChecker,
3034
private SharedStorageInterface $sharedStorage,
3135
private string $apiUrlPrefix,
36+
private ?NameConverterInterface $nameConverter,
3237
) {
3338
}
3439

@@ -237,7 +242,7 @@ public function thisExchangeRateShouldHaveARatioOf(ExchangeRateInterface $exchan
237242
$exchangeRate->getTargetCurrency(),
238243
);
239244

240-
Assert::same($exchangeRate['ratio'], $ratio);
245+
Assert::same($exchangeRate[$this->getNormalizedKey('ratio')], $ratio);
241246
}
242247

243248
/**
@@ -361,8 +366,8 @@ private function getExchangeRateFromResponse(
361366
/** @var array $item */
362367
foreach ($this->responseChecker->getCollection($this->client->index(Resources::EXCHANGE_RATES)) as $item) {
363368
if (
364-
$item['sourceCurrency'] === sprintf('%s/admin/currencies/%s', $this->apiUrlPrefix, $sourceCurrency->getCode()) &&
365-
$item['targetCurrency'] === sprintf('%s/admin/currencies/%s', $this->apiUrlPrefix, $targetCurrency->getCode())
369+
$item[$this->getNormalizedKey('sourceCurrency')] === sprintf('%s/admin/currencies/%s', $this->apiUrlPrefix, $sourceCurrency->getCode()) &&
370+
$item[$this->getNormalizedKey('targetCurrency')] === sprintf('%s/admin/currencies/%s', $this->apiUrlPrefix, $targetCurrency->getCode())
366371
) {
367372
return $item;
368373
}
@@ -382,6 +387,6 @@ private function responseHasExchangeRate(
382387
return false;
383388
}
384389

385-
return $exchangeRateResponse['ratio'] === $ratio;
390+
return $exchangeRateResponse[$this->getNormalizedKey('ratio')] === $ratio;
386391
}
387392
}

src/Sylius/Behat/Context/Api/Admin/ManagingOrdersContext.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ public function iCheckData(string $itemName): void
327327
/** @var string $lastResponseContent */
328328
$lastResponseContent = $this->client->getLastResponse()->getContent();
329329
/** @var array{productName: string}[] $items */
330-
$items = json_decode($lastResponseContent, true)['items'];
330+
$items = json_decode($lastResponseContent, true)[$this->getNormalizedKey('items')];
331331

332332
$productNameKey = $this->getNormalizedKey('productName');
333333
foreach ($items as $item) {
@@ -730,7 +730,7 @@ public function theFirstOrderShouldHaveNumber(string $number): void
730730
$items = $this->responseChecker->getValue($this->client->getLastResponse(), 'hydra:member');
731731
$firstItem = $items[0];
732732

733-
Assert::same($firstItem['number'], str_replace('#', '', $number));
733+
Assert::same($firstItem[$this->getNormalizedKey('number')], str_replace('#', '', $number));
734734
}
735735

736736
/**
@@ -745,7 +745,7 @@ public function iShouldSeeTheOrderWithTotal(string $orderNumber, int $total): vo
745745
)[0];
746746

747747
Assert::same(
748-
$order['total'],
748+
$order[$this->getNormalizedKey('total')],
749749
$total,
750750
);
751751
}
@@ -773,7 +773,7 @@ public function theAdministratorShouldSeeTheOrderWithTotalInOrderList(string $to
773773
$firstItem = array_pop($itemsWithCurrency);
774774

775775
Assert::notEmpty($firstItem);
776-
Assert::same($firstItem['total'], $total);
776+
Assert::same($firstItem[$this->getNormalizedKey('total')], $total);
777777
}
778778

779779
/**
@@ -793,7 +793,7 @@ public function itShouldHaveBeenPlacedByTheCustomer(CustomerInterface $customer)
793793
public function itShouldBeShippedViaTheShippingMethod(ShippingMethodInterface $shippingMethod): void
794794
{
795795
Assert::same(
796-
$this->responseChecker->getValue($this->client->getLastResponse(), 'shipments')[0]['method'],
796+
$this->responseChecker->getValue($this->client->getLastResponse(), 'shipments')[0][$this->getNormalizedKey('method')],
797797
$this->iriConverter->getIriFromResource($shippingMethod),
798798
);
799799
}
@@ -804,7 +804,7 @@ public function itShouldBeShippedViaTheShippingMethod(ShippingMethodInterface $s
804804
public function itShouldBePaidWith(PaymentMethodInterface $paymentMethod): void
805805
{
806806
Assert::same(
807-
$this->responseChecker->getValue($this->client->getLastResponse(), 'payments')[0]['method'],
807+
$this->responseChecker->getValue($this->client->getLastResponse(), 'payments')[0][$this->getNormalizedKey('method')],
808808
$this->iriConverter->getIriFromResource($paymentMethod),
809809
);
810810
}
@@ -912,23 +912,23 @@ public function itemUnitPriceShouldBe(array $orderItem, string $unitPrice): void
912912
*/
913913
public function itemTotalShouldBe(array $orderItem, string $total): void
914914
{
915-
Assert::same($this->getTotalAsInt($total), $orderItem['total']);
915+
Assert::same($this->getTotalAsInt($total), $orderItem[$this->getNormalizedKey('total')]);
916916
}
917917

918918
/**
919919
* @Then /^(its) code should be "([^"]+)"$/
920920
*/
921921
public function itemCodeShouldBe(array $orderItem, string $code): void
922922
{
923-
Assert::endsWith($orderItem['variant'], $code);
923+
Assert::endsWith($orderItem[$this->getNormalizedKey('variant')], $code);
924924
}
925925

926926
/**
927927
* @Then /^(its) quantity should be ([^"]+)$/
928928
*/
929929
public function itemQuantityShouldBe(array $orderItem, int $quantity): void
930930
{
931-
Assert::same($quantity, $orderItem['quantity']);
931+
Assert::same($quantity, $orderItem[$this->getNormalizedKey('quantity')]);
932932
}
933933

934934
/**
@@ -1007,8 +1007,8 @@ public function itsTaxIncludedInPriceShouldBe(string $tax): void
10071007
$totalTax = 0;
10081008

10091009
foreach ($unitPromotionAdjustments as $unitPromotionAdjustment) {
1010-
if (true === $unitPromotionAdjustment['neutral']) {
1011-
$totalTax += $unitPromotionAdjustment['amount'];
1010+
if (true === $unitPromotionAdjustment[$this->getNormalizedKey('neutral')]) {
1011+
$totalTax += $unitPromotionAdjustment[$this->getNormalizedKey('amount')];
10121012
}
10131013
}
10141014

src/Sylius/Behat/Context/Api/Admin/ManagingProductVariantsContext.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Sylius\Behat\Client\ApiClientInterface;
1818
use Sylius\Behat\Client\ResponseCheckerInterface;
1919
use Sylius\Behat\Context\Api\Admin\Helper\ValidationTrait;
20+
use Sylius\Behat\Context\Api\NormalizedKeyAwareTrait;
2021
use Sylius\Behat\Context\Api\Resources;
2122
use Sylius\Behat\Service\Converter\IriConverterInterface;
2223
use Sylius\Component\Core\Formatter\StringInflector;
@@ -27,11 +28,13 @@
2728
use Sylius\Component\Product\Model\ProductOptionValueInterface;
2829
use Sylius\Component\Product\Resolver\ProductVariantResolverInterface;
2930
use Sylius\Component\Shipping\Model\ShippingCategoryInterface;
31+
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
3032
use Webmozart\Assert\Assert;
3133

3234
final class ManagingProductVariantsContext implements Context
3335
{
3436
use ValidationTrait;
37+
use NormalizedKeyAwareTrait;
3538

3639
private const FIRST_COLLECTION_ITEM = 0;
3740

@@ -42,6 +45,7 @@ public function __construct(
4245
private ApiClientInterface $client,
4346
private ResponseCheckerInterface $responseChecker,
4447
private IriConverterInterface $iriConverter,
48+
private ?NameConverterInterface $nameConverter,
4549
) {
4650
}
4751

@@ -374,12 +378,15 @@ public function theVariantWithCodeShouldBeNamedIn(string $name, string $localeCo
374378
'name' => $name,
375379
];
376380

377-
$translationInLocale = $response[self::FIRST_COLLECTION_ITEM]['translations'][$localeCode];
381+
$translationsKey = $this->getNormalizedKey('translations');
382+
$nameKey = $this->getNormalizedKey('name');
383+
384+
$translationInLocale = $response[self::FIRST_COLLECTION_ITEM][$translationsKey][$localeCode];
378385

379386
Assert::allInArray(
380387
$expectedTranslation,
381388
$translationInLocale,
382-
sprintf('Expected translation %s, got %s', $expectedTranslation['name'], $translationInLocale['name']),
389+
sprintf('Expected translation %s, got %s', $expectedTranslation['name'], $translationInLocale[$nameKey]),
383390
);
384391
}
385392

@@ -394,7 +401,10 @@ public function theVariantWithCodeShouldBePricedAtForChannel(
394401
): void {
395402
$response = $this->responseChecker->getCollection($this->client->index(Resources::PRODUCT_VARIANTS));
396403

397-
Assert::same($response[self::FIRST_COLLECTION_ITEM]['channelPricings'][$channel->getCode()]['price'], $price);
404+
$channelPricingsKey = $this->getNormalizedKey('channelPricings');
405+
$priceKey = $this->getNormalizedKey('price');
406+
407+
Assert::same($response[self::FIRST_COLLECTION_ITEM][$channelPricingsKey][$channel->getCode()][$priceKey], $price);
398408
}
399409

400410
/**
@@ -408,7 +418,10 @@ public function theVariantWithCodeShouldHaveAnOriginalPriceOfForChannel(
408418
): void {
409419
$response = $this->responseChecker->getCollection($this->client->index(Resources::PRODUCT_VARIANTS));
410420

411-
Assert::same($response[self::FIRST_COLLECTION_ITEM]['channelPricings'][$channel->getCode()]['originalPrice'], $originalPrice);
421+
$channelPricingsKey = $this->getNormalizedKey('channelPricings');
422+
$originalPriceKey = $this->getNormalizedKey('originalPrice');
423+
424+
Assert::same($response[self::FIRST_COLLECTION_ITEM][$channelPricingsKey][$channel->getCode()][$originalPriceKey], $originalPrice);
412425
}
413426

414427
/**
@@ -428,7 +441,10 @@ public function theVariantWithCodeShouldHaveMinimumPriceForChannel(ProductVarian
428441
{
429442
$response = $this->responseChecker->getCollection($this->client->index(Resources::PRODUCT_VARIANTS));
430443

431-
Assert::same($response[self::FIRST_COLLECTION_ITEM]['channelPricings'][$channel->getCode()]['minimumPrice'], $minimumPrice);
444+
$channelPricingsKey = $this->getNormalizedKey('channelPricings');
445+
$minimumPriceKey = $this->getNormalizedKey('minimumPrice');
446+
447+
Assert::same($response[self::FIRST_COLLECTION_ITEM][$channelPricingsKey][$channel->getCode()][$minimumPriceKey], $minimumPrice);
432448
}
433449

434450
/**

0 commit comments

Comments
 (0)