[Promotion] Return PHP_INT_MAX value as possible generation amount of coupons when the code length is too large

This commit is contained in:
Grzegorz Sadowski 2020-02-05 10:23:28 +01:00
parent eab239702e
commit ef1dcb8af3
No known key found for this signature in database
GPG key ID: EF87FF4E6E3BD364
5 changed files with 24 additions and 11 deletions

View file

@ -22,7 +22,7 @@ Feature: Coupon generate instruction validation
And there should be 0 coupon related to this promotion
@ui
Scenario: Trying to generate a new coupons without specifying their code length
Scenario: Trying to generate new coupons without specifying their code length
When I want to generate new coupons for this promotion
And I do not specify their code length
And I choose the amount of 4 coupons to be generated

View file

@ -32,10 +32,10 @@ Feature: Generating new coupons
And there should be 5 coupons related to this promotion
@ui
Scenario: Generating new coupons with too long code length
Scenario: Generating new coupons with a large long code length value
When I want to generate new coupons for this promotion
And I choose the amount of 5 coupons to be generated
And I specify their code length as 16
And I choose the amount of 10 coupons to be generated
And I specify their code length as 40
And I generate it
Then I should be notified that generating 5 coupons with code length equal to 16 is not possible
And there should be 0 coupon related to this promotion
Then I should be notified that they have been successfully generated
And there should be 10 coupons related to this promotion

View file

@ -33,7 +33,7 @@
<constraint name="Range">
<option name="min">1</option>
<option name="minMessage">sylius.promotion_coupon_generator_instruction.code_length.min</option>
<option name="max">15</option>
<option name="max">40</option>
<option name="maxMessage">sylius.promotion_coupon_generator_instruction.code_length.max</option>
</constraint>
<constraint name="NotBlank">

View file

@ -68,11 +68,11 @@ final class PercentageGenerationPolicy implements GenerationPolicyInterface
$instruction->getSuffix()
);
$codeCombination = 16 ** $expectedCodeLength;
if ($codeCombination > PHP_INT_MAX) {
$codeCombination = 0;
$codeCombination = 16 ** $expectedCodeLength * $this->ratio;
if ($codeCombination >= PHP_INT_MAX) {
return PHP_INT_MAX - $generatedAmount;
}
return (int) floor($codeCombination * $this->ratio - $generatedAmount);
return (int) $codeCombination - $generatedAmount;
}
}

View file

@ -72,6 +72,19 @@ final class PercentageGenerationPolicySpec extends ObjectBehavior
$this->getPossibleGenerationAmount($instruction)->shouldReturn(7);
}
function it_returns_php_int_max_value_as_possible_generation_amount_when_code_length_is_too_large(
PromotionCouponGeneratorInstructionInterface $instruction,
PromotionCouponRepositoryInterface $couponRepository
): void {
$instruction->getAmount()->willReturn(1000);
$instruction->getCodeLength()->willReturn(40);
$instruction->getPrefix()->willReturn(null);
$instruction->getSuffix()->willReturn(null);
$couponRepository->countByCodeLength(40, null, null)->willReturn(0);
$this->getPossibleGenerationAmount($instruction)->shouldReturn(PHP_INT_MAX);
}
function it_returns_possible_generation_amount_with_prefix_and_suffix(
PromotionCouponGeneratorInstructionInterface $instruction,
PromotionCouponRepositoryInterface $couponRepository