function it_throws_invalid_argument_exception_when_expecte_code_length_is_null(InstructionInterface $instruction)
 {
     $instruction->getAmount()->willReturn(18);
     $instruction->getCodeLength()->willReturn(null);
     $this->shouldThrow(\InvalidArgumentException::class)->during('isGenerationPossible', [$instruction]);
     $this->shouldThrow(\InvalidArgumentException::class)->during('getPossibleGenerationAmount', [$instruction]);
 }
 /**
  * @param InstructionInterface $instruction
  *
  * @return int
  */
 private function calculatePossibleGenerationAmount(InstructionInterface $instruction)
 {
     $expectedAmount = $instruction->getAmount();
     $expectedCodeLength = $instruction->getCodeLength();
     Assert::allNotNull([$expectedAmount, $expectedCodeLength], 'Code length or amount cannot be null.');
     $generatedAmount = $this->couponRepository->countCouponsByCodeLength($expectedCodeLength);
     return floor(pow(16, $expectedCodeLength) * $this->ratio - $generatedAmount);
 }
 function it_not_adds_violation(ExecutionContextInterface $context, CouponPossibleGenerationAmount $constraint, InstructionInterface $instruction, GenerationPolicyInterface $generationPolicy)
 {
     $instruction->getAmount()->willReturn(5);
     $instruction->getCodeLength()->willReturn(1);
     $generationPolicy->isGenerationPossible($instruction)->willReturn(true);
     $generationPolicy->getPossibleGenerationAmount($instruction)->shouldNotBeCalled();
     $context->addViolation($constraint->message)->shouldNotBeCalled();
     $this->validate($instruction, $constraint);
 }
Пример #4
0
 function it_throws_invalid_argument_exception_when_code_length_is_not_between_one_and_forty(CouponInterface $coupon, FactoryInterface $couponFactory, GenerationPolicyInterface $generationPolicy, PromotionInterface $promotion, InstructionInterface $instruction)
 {
     $instruction->getAmount()->willReturn(16);
     $instruction->getCodeLength()->willReturn(-1);
     $generationPolicy->isGenerationPossible($instruction)->willReturn(true);
     $couponFactory->createNew()->willReturn($coupon);
     $this->shouldThrow(\InvalidArgumentException::class)->during('generate', [$promotion, $instruction]);
     $instruction->getCodeLength()->willReturn(45);
     $this->shouldThrow(\InvalidArgumentException::class)->during('generate', [$promotion, $instruction]);
 }
 function let(InstructionInterface $instruction, \InvalidArgumentException $previousException)
 {
     $instruction->getAmount()->willReturn(17);
     $instruction->getCodeLength()->willReturn(1);
     $this->beConstructedWith($instruction, 0, $previousException);
 }
 /**
  * {@inheritdoc}
  */
 public function __construct(InstructionInterface $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);
 }