mirror of
https://github.com/Sylius/Sylius.git
synced 2026-07-14 17:10:53 +00:00
[Promotion] Return PHP_INT_MAX value as possible generation amount of coupons when the code length is too large
This commit is contained in:
parent
eab239702e
commit
ef1dcb8af3
5 changed files with 24 additions and 11 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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">
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue