/**
  * Tests coupon assignment
  *
  * History:
  * * Given an existing ReferralRule with Register Coupon for referrer and
  *   purchase coupon for invited
  * * Referrer adds invitation
  * * Invited registers in site
  * * Referrer Customer receive coupons
  * * Invited customer purchases and receive his coupon
  * * Both purchases using their coupons
  * * ReferralRule is closed
  */
 public function testCouponAssignmentRegPurch()
 {
     /**
      * Enables right ReferralRule and disable all other
      */
     $this->find('referral_rule', 1)->disable();
     $referralRule = $this->find('referral_rule', 4);
     $this->get('elcodi.referral_rule_manager')->enableReferralRule($referralRule);
     $this->getObjectManager('referral_rule')->clear();
     /**
      * New customer is created with a referral hash in the cookie.
      *
      * @var ReferralHashInterface $referralHash
      */
     $referrer = $this->find('customer', 1);
     $invited = $this->find('customer', 2);
     $referralHash = $this->get('elcodi.referral_hash_manager')->getReferralHashByCustomer($referrer);
     $hash = $referralHash->getHash();
     $invitations = new ArrayCollection();
     $invitation1 = new Invitation();
     $invitation1->setEmail('*****@*****.**')->setName('Invited');
     $invitations->add($invitation1);
     $this->get('elcodi.referral_program_manager')->invite($referrer, $invitations);
     /**
      * * Invited registers in site
      * * Referrer Customer receive coupons
      */
     $referralCouponManager = $this->get('elcodi.referral_coupon_manager');
     $referralCouponManager->checkCouponAssignment($invited, $hash, ElcodiReferralProgramRuleTypes::TYPE_ON_REGISTER);
     $this->getObjectManager('referral_rule')->clear();
     /**
      * @var ReferralLineInterface $referralLine
      */
     $referralLine = $this->getRepository('referral_line')->findOneByInvited($invited);
     $referrerAssignedCoupon = $referralLine->getReferrerAssignedCoupon();
     $this->assertInstanceOf('Elcodi\\Component\\Coupon\\Entity\\Interfaces\\CouponInterface', $referrerAssignedCoupon);
     $this->assertEquals($referrerAssignedCoupon->getCount(), 1);
     $this->assertfalse($referralLine->getReferrerCouponUsed());
     $this->assertEquals($referralLine->getReferralRule()->getReferrerCoupon()->getId(), $referralLine->getReferrerCoupon()->getId());
     $this->assertNotEquals($referrerAssignedCoupon->getId(), $referralLine->getReferrerCoupon()->getId());
     $this->assertNull($referralLine->getInvitedAssignedCoupon());
     $this->assertFalse($referralLine->getInvitedCouponUsed());
     $this->assertFalse($referralLine->isClosed());
     $this->getObjectManager('referral_rule')->clear();
     /**
      * * Invited customer purchases and receive his coupon
      */
     $invited = $this->find('customer', 2);
     $referralLine = $this->getRepository('referral_line')->findOneByInvited($invited);
     $referralCouponManager->checkCouponAssignment($invited, $hash, ElcodiReferralProgramRuleTypes::TYPE_ON_FIRST_PURCHASE);
     $invitedAssignedCoupon = $referralLine->getInvitedAssignedCoupon();
     $this->assertInstanceOf('Elcodi\\Component\\Coupon\\Entity\\Interfaces\\CouponInterface', $invitedAssignedCoupon);
     $this->assertEquals($invitedAssignedCoupon->getCount(), 1);
     $this->assertfalse($referralLine->getInvitedCouponUsed());
     $this->assertEquals($referralLine->getReferralRule()->getInvitedCoupon()->getId(), $referralLine->getInvitedCoupon()->getId());
     $this->assertNotEquals($invitedAssignedCoupon->getId(), $referralLine->getInvitedCoupon()->getId());
     $this->assertFalse($referralLine->isClosed());
     $this->getObjectManager('referral_rule')->clear();
     /**
      * Referrer Customer buys using his coupon
      */
     $referrer = $this->find('customer', 1);
     $invited = $this->find('customer', 2);
     $referralLine = $this->getRepository('referral_line')->findOneByInvited($invited);
     $referralCouponManager->checkCouponAssignment($referrer, $hash, ElcodiReferralProgramRuleTypes::TYPE_ON_FIRST_PURCHASE)->checkCouponsUsed($referrer, new ArrayCollection(array($referralLine->getReferrerAssignedCoupon())));
     $this->assertTrue($referralLine->getReferrerCouponUsed());
     $this->assertFalse($referralLine->isClosed());
     $this->getObjectManager('referral_rule')->clear();
     /**
      * Invited Customer buys using his coupon
      * ReferralLine is closed
      */
     $invited = $this->find('customer', 2);
     $referralLine = $this->getRepository('referral_line')->findOneByInvited($invited);
     $referralCouponManager->checkCouponAssignment($invited, $hash, ElcodiReferralProgramRuleTypes::TYPE_ON_FIRST_PURCHASE)->checkCouponsUsed($invited, new ArrayCollection(array($referralLine->getInvitedAssignedCoupon())));
     $this->assertTrue($referralLine->getInvitedCouponUsed());
     $this->assertTrue($referralLine->isClosed());
 }
 /**
  * Test simple invitation without any enabled ReferralRule
  *
  * History: An email is invited using referralprogram engine.
  * No ReferralRule is enabled
  *
  * @expectedException \Elcodi\Component\ReferralProgram\Exceptions\ReferralProgramRuleNotFoundException
  */
 public function testInviteNoReferralHashEnabled()
 {
     /**
      * @var ObjectManager $referralRuleManager
      */
     $referralRuleManager = $this->getObjectManager('referral_rule');
     $referralProgramManager = $this->get('elcodi.referral_program');
     $referrer = $this->find('customer', 1);
     $referralRules = $this->findAll('referral_rule');
     $referralRules->map(function ($referralRule) {
         /**
          * @var ReferralRuleInterface $referralRule
          */
         $referralRule->setEnabled(false);
     });
     $referralRuleManager->flush();
     $referralRuleManager->clear();
     $invitations = new ArrayCollection();
     $invitation1 = new Invitation();
     $invitation1->setEmail('*****@*****.**')->setName('User Name');
     $invitations->add($invitation1);
     $referralProgramManager->invite($referrer, $invitations);
 }