function it_returns_possible_generation_amount(PromotionCouponGeneratorInstructionInterface $instruction, PromotionCouponRepositoryInterface $couponRepository)
 {
     $instruction->getAmount()->willReturn(17);
     $instruction->getCodeLength()->willReturn(1);
     $couponRepository->countByCodeLength(1)->willReturn(1);
     $this->isGenerationPossible($instruction)->shouldReturn(false);
     $this->getPossibleGenerationAmount($instruction)->shouldReturn(7.0);
 }
Exemplo n.º 2
0
 /**
  * @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);
 }
Exemplo n.º 3
0
 /**
  * @param string $code
  * @param array $generatedCoupons
  *
  * @return bool
  */
 private function isUsedCode($code, array $generatedCoupons)
 {
     if (isset($generatedCoupons[$code])) {
         return true;
     }
     return null !== $this->couponRepository->findOneBy(['code' => $code]);
 }
 function it_generates_coupons_according_to_an_instruction(FactoryInterface $promotionCouponFactory, PromotionCouponRepositoryInterface $promotionCouponRepository, ObjectManager $objectManager, PromotionInterface $promotion, PromotionCouponInterface $promotionCoupon, PromotionCouponGeneratorInstructionInterface $instruction, GenerationPolicyInterface $generationPolicy)
 {
     $instruction->getAmount()->willReturn(1);
     $instruction->getUsageLimit()->willReturn(null);
     $instruction->getExpiresAt()->willReturn(null);
     $instruction->getCodeLength()->willReturn(6);
     $generationPolicy->isGenerationPossible($instruction)->willReturn(true);
     $promotionCouponFactory->createNew()->willReturn($promotionCoupon);
     $promotionCouponRepository->findOneBy(Argument::any())->willReturn(null);
     $promotionCoupon->setPromotion($promotion)->shouldBeCalled();
     $promotionCoupon->setCode(Argument::any())->shouldBeCalled();
     $promotionCoupon->setUsageLimit(null)->shouldBeCalled();
     $promotionCoupon->setExpiresAt(null)->shouldBeCalled();
     $objectManager->persist($promotionCoupon)->shouldBeCalled();
     $objectManager->flush()->shouldBeCalled();
     $this->generate($promotion, $instruction);
 }
 /**
  * @Then /^([^"]+) should still exist in the registry$/
  */
 public function couponShouldStillExistInTheRegistry(PromotionCouponInterface $coupon)
 {
     Assert::notNull($this->couponRepository->find($coupon->getId()), sprintf('The coupon with id %s should exist', $coupon->getId()));
 }