Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.14'
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Apr 14, 2022
2 parents dbfccb0 + 37d4c47 commit 354d8fd
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
22 changes: 22 additions & 0 deletions spec/ValueHandler/GenericPropertyValueHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,28 @@ public function it_throws_if_provided_property_path_is_not_writeable_on_both_pro
);
}

public function it_does_not_throw_if_provided_property_path_is_not_writeable_on_both_product_and_variant_but_the_value_is_not_related_to_any_channels(
PropertyAccessorInterface $propertyAccessor,
ProductVariantInterface $productVariant,
ProductInterface $product
): void {
$propertyAccessor->isWritable($productVariant, self::PROPERTY_PATH)->willReturn(false);
$propertyAccessor->isWritable($product, self::PROPERTY_PATH)->willReturn(false);

$value = [
[
'scope' => 'other_ecommerce',
'locale' => 'it_IT',
'data' => 'New value other',
],
];

$this->handle($productVariant, self::AKENEO_ATTRIBUTE_CODE, $value);

$propertyAccessor->setValue($productVariant, self::PROPERTY_PATH, 'New value other')->shouldNotHaveBeenCalled();
$propertyAccessor->setValue($product, self::PROPERTY_PATH, 'New value other')->shouldNotHaveBeenCalled();
}

public function it_skips_values_related_to_channels_that_are_not_associated_to_the_product(
PropertyAccessorInterface $propertyAccessor,
ProductVariantInterface $productVariant,
Expand Down
21 changes: 21 additions & 0 deletions spec/ValueHandler/MetricPropertyValueHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,27 @@ public function it_throws_if_provided_property_path_is_not_writeable_on_both_pro
);
}

public function it_does_not_throw_if_provided_property_path_is_not_writeable_on_both_product_and_variant_but_the_value_is_not_related_to_any_channels(
PropertyAccessorInterface $propertyAccessor,
ProductVariantInterface $productVariant,
ProductInterface $product
): void {
$propertyAccessor->isWritable($productVariant, self::PROPERTY_PATH)->willReturn(false);
$propertyAccessor->isWritable($product, self::PROPERTY_PATH)->willReturn(false);

$value = [
[
'scope' => 'other_ecommerce',
'locale' => 'it_IT',
'data' => ['amount' => '21.0000', 'unit' => 'KILOGRAM'],
],
];
$this->handle($productVariant, self::AKENEO_ATTRIBUTE_CODE, $value);

$propertyAccessor->setValue($productVariant, self::PROPERTY_PATH, 21.0)->shouldNotHaveBeenCalled();
$propertyAccessor->setValue($product, self::PROPERTY_PATH, 21.0)->shouldNotHaveBeenCalled();
}

public function it_skips_values_related_to_channels_that_are_not_associated_to_the_product(
PropertyAccessorInterface $propertyAccessor,
ProductVariantInterface $productVariant,
Expand Down
4 changes: 3 additions & 1 deletion src/ValueHandler/GenericPropertyValueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function handle($subject, string $attribute, array $value): void
}

$hasBeenSet = false;
$hasAnyValueApplicable = false;
$productVariant = $subject;
Assert::isInstanceOf($productVariant, ProductVariantInterface::class);
$product = $productVariant->getProduct();
Expand All @@ -63,6 +64,7 @@ public function handle($subject, string $attribute, array $value): void
if ($valueData['scope'] !== null && !in_array($valueData['scope'], $productChannelCodes, true)) {
continue;
}
$hasAnyValueApplicable = true;

/** @psalm-suppress MixedAssignment */
$valueToSet = $valueData['data'];
Expand All @@ -83,7 +85,7 @@ public function handle($subject, string $attribute, array $value): void
}
}

if (!$hasBeenSet) {
if (!$hasBeenSet && $hasAnyValueApplicable) {
throw new RuntimeException(
sprintf(
'Property path "%s" is not writable on both %s and %s but it should be for at least once.',
Expand Down
4 changes: 3 additions & 1 deletion src/ValueHandler/MetricPropertyValueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function handle($subject, string $attribute, array $value): void
}

$hasBeenSet = false;
$hasAnyValueApplicable = false;
$productVariant = $subject;
Assert::isInstanceOf($productVariant, ProductVariantInterface::class);
$product = $productVariant->getProduct();
Expand All @@ -75,6 +76,7 @@ public function handle($subject, string $attribute, array $value): void
if ($valueData['scope'] !== null && !in_array($valueData['scope'], $productChannelCodes, true)) {
continue;
}
$hasAnyValueApplicable = true;

/** @psalm-suppress MixedAssignment */
$data = $valueData['data'];
Expand All @@ -100,7 +102,7 @@ public function handle($subject, string $attribute, array $value): void
}
}

if (!$hasBeenSet) {
if (!$hasBeenSet && $hasAnyValueApplicable) {
throw new RuntimeException(
sprintf(
'Property path "%s" is not writable on both %s and %s but it should be for at least once.',
Expand Down

0 comments on commit 354d8fd

Please sign in to comment.