function it_throws_an_invalid_argument_exception_when_expecte_code_length_is_null(PromotionCouponGeneratorInstructionInterface $instruction) { $instruction->getAmount()->willReturn(18); $instruction->getCodeLength()->willReturn(null); $this->shouldThrow(\InvalidArgumentException::class)->during('isGenerationPossible', [$instruction]); $this->shouldThrow(\InvalidArgumentException::class)->during('getPossibleGenerationAmount', [$instruction]); }
/** * @param PromotionCouponGeneratorInstructionInterface $instruction * * @return int * * @throws \InvalidArgumentException */ private function calculatePossibleGenerationAmount(PromotionCouponGeneratorInstructionInterface $instruction) { $expectedAmount = $instruction->getAmount(); $expectedCodeLength = $instruction->getCodeLength(); Assert::allNotNull([$expectedAmount, $expectedCodeLength], 'Code length or amount cannot be null.'); $generatedAmount = $this->couponRepository->countByCodeLength($expectedCodeLength); return floor(pow(16, $expectedCodeLength) * $this->ratio - $generatedAmount); }
function it_does_not_add_violation(ExecutionContextInterface $context, PromotionCouponGeneratorInstructionInterface $instruction, GenerationPolicyInterface $generationPolicy) { $constraint = new CouponPossibleGenerationAmount(); $instruction->getAmount()->willReturn(5); $instruction->getCodeLength()->willReturn(1); $generationPolicy->isGenerationPossible($instruction)->willReturn(true); $generationPolicy->getPossibleGenerationAmount($instruction)->shouldNotBeCalled(); $context->addViolation($constraint->message, Argument::any())->shouldNotBeCalled(); $this->validate($instruction, $constraint); }
function it_throws_an_invalid_argument_exception_when_code_length_is_not_between_one_and_forty(PromotionCouponInterface $promotionCoupon, FactoryInterface $promotionCouponFactory, GenerationPolicyInterface $generationPolicy, PromotionInterface $promotion, PromotionCouponGeneratorInstructionInterface $instruction) { $instruction->getAmount()->willReturn(16); $instruction->getCodeLength()->willReturn(-1); $generationPolicy->isGenerationPossible($instruction)->willReturn(true); $promotionCouponFactory->createNew()->willReturn($promotionCoupon); $this->shouldThrow(\InvalidArgumentException::class)->during('generate', [$promotion, $instruction]); $instruction->getCodeLength()->willReturn(45); $this->shouldThrow(\InvalidArgumentException::class)->during('generate', [$promotion, $instruction]); }
function let(PromotionCouponGeneratorInstructionInterface $instruction, \InvalidArgumentException $previousException) { $instruction->getAmount()->willReturn(17); $instruction->getCodeLength()->willReturn(1); $this->beConstructedWith($instruction, 0, $previousException); }
/** * {@inheritdoc} */ public function __construct(PromotionCouponGeneratorInstructionInterface $instruction, $exceptionCode = 0, \Exception $previousException = null) { $message = sprintf('Invalid coupon code length or coupons amount. It is not possible to generate %d unique coupons with %d code length', $instruction->getAmount(), $instruction->getCodeLength()); parent::__construct($message, $exceptionCode, $previousException); }