Skip to content

Commit

Permalink
[Promotion] Return PHP_INT_MAX value as possible generation amount of…
Browse files Browse the repository at this point in the history
… coupons when the code length is too large
  • Loading branch information
GSadee committed Feb 6, 2020
1 parent eab2397 commit ef1dcb8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions features/promotion/managing_coupons/generating_coupon.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ private function calculatePossibleGenerationAmount(PromotionCouponGeneratorInstr
$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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ function it_returns_possible_generation_amount(
$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
Expand Down

0 comments on commit ef1dcb8

Please sign in to comment.