Skip to content

Commit

Permalink
fix code generation with too long code length
Browse files Browse the repository at this point in the history
  • Loading branch information
oallain authored and GSadee committed Feb 6, 2020
1 parent b3d94ed commit eab2397
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
9 changes: 9 additions & 0 deletions features/promotion/managing_coupons/generating_coupon.feature
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,12 @@ Feature: Generating new coupons
And I generate these coupons
Then I should be notified that they have been successfully generated
And there should be 5 coupons related to this promotion

@ui
Scenario: Generating new coupons with too long code length
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 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
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">40</option>
<option name="max">15</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,6 +68,11 @@ private function calculatePossibleGenerationAmount(PromotionCouponGeneratorInstr
$instruction->getSuffix()
);

return (int) floor((16 ** $expectedCodeLength) * $this->ratio - $generatedAmount);
$codeCombination = 16 ** $expectedCodeLength;
if ($codeCombination > PHP_INT_MAX) {
$codeCombination = 0;
}

return (int) floor($codeCombination * $this->ratio - $generatedAmount);
}
}

0 comments on commit eab2397

Please sign in to comment.