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); }
/** * {@inheritdoc} */ public function validate($instruction, Constraint $constraint) { if (null === $instruction->getCodeLength() || null === $instruction->getAmount()) { return; } if (!$this->generationPolicy->isGenerationPossible($instruction)) { $this->context->addViolation($constraint->message, array('%expectedAmount%' => $instruction->getAmount(), '%codeLength%' => $instruction->getCodeLength(), '%possibleAmount%' => $this->generationPolicy->getPossibleGenerationAmount($instruction))); } }
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_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]); }
/** * {@inheritdoc} */ public function validate($instruction, Constraint $constraint) { if (null === $instruction->getCodeLength() || null === $instruction->getAmount()) { return; } /** @var PromotionCouponGeneratorInstructionInterface $value */ Assert::isInstanceOf($instruction, PromotionCouponGeneratorInstructionInterface::class); /** @var CouponPossibleGenerationAmount $constraint */ Assert::isInstanceOf($constraint, CouponPossibleGenerationAmount::class); if (!$this->generationPolicy->isGenerationPossible($instruction)) { $this->context->addViolation($constraint->message, array('%expectedAmount%' => $instruction->getAmount(), '%codeLength%' => $instruction->getCodeLength(), '%possibleAmount%' => $this->generationPolicy->getPossibleGenerationAmount($instruction))); } }
/** * @param InstructionInterface $instruction * * @throws FailedGenerationException */ private function assertGenerationIsPossible(InstructionInterface $instruction) { if (!$this->generationPolicy->isGenerationPossible($instruction)) { throw new FailedGenerationException($instruction); } }