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