/**
  * Add email
  *
  * @param  Email $email
  * @return $this
  */
 public function addEmail(Email $email)
 {
     $this->emails[] = $email;
     $email->setFolder($this);
     return $this;
 }
 public function testFolderGetterAndSetter()
 {
     $folder = $this->getMock('Oro\\Bundle\\EmailBundle\\Entity\\EmailFolder');
     $entity = new Email();
     $entity->setFolder($folder);
     $this->assertTrue($folder === $entity->getFolder());
 }
 /**
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function testPersist()
 {
     $origin = $this->getMockBuilder('Oro\\Bundle\\EmailBundle\\Entity\\EmailOrigin')->getMock();
     $origin->expects($this->any())->method('getId')->will($this->returnValue(1));
     $this->batch->addOrigin($origin);
     $folder = new EmailFolder();
     $folder->setName('Exist');
     $folder->setFullName('Exist');
     $folder->setOrigin($origin);
     $this->batch->addFolder($folder);
     $newFolder = new EmailFolder();
     $newFolder->setName('New');
     $newFolder->setFullName('New');
     $newFolder->setOrigin($origin);
     $this->batch->addFolder($newFolder);
     $dbFolder = new EmailFolder();
     $dbFolder->setName('DbExist');
     $dbFolder->setFullName('DbExist');
     $dbFolder->setOrigin($origin);
     $addr = $this->addrManager->newEmailAddress()->setEmail('Exist');
     $this->batch->addAddress($addr);
     $newAddr = $this->addrManager->newEmailAddress()->setEmail('New');
     $this->batch->addAddress($newAddr);
     $dbAddr = $this->addrManager->newEmailAddress()->setEmail('DbExist');
     $email1 = new Email();
     $email1->setFolder($folder);
     $email1->setFromEmailAddress($addr);
     $email1Recip1 = new EmailRecipient();
     $email1Recip1->setEmailAddress($addr);
     $email1Recip2 = new EmailRecipient();
     $email1Recip2->setEmailAddress($newAddr);
     $email1->addRecipient($email1Recip1);
     $email1->addRecipient($email1Recip2);
     $this->batch->addEmail($email1);
     $email2 = new Email();
     $email2->setFolder($newFolder);
     $email2->setFromEmailAddress($newAddr);
     $email2Recip1 = new EmailRecipient();
     $email2Recip1->setEmailAddress($addr);
     $email2Recip2 = new EmailRecipient();
     $email2Recip2->setEmailAddress($newAddr);
     $email2->addRecipient($email2Recip1);
     $email2->addRecipient($email2Recip2);
     $this->batch->addEmail($email2);
     $em = $this->getMockBuilder('Doctrine\\ORM\\EntityManager')->disableOriginalConstructor()->getMock();
     $folderRepo = $this->getMockBuilder('Doctrine\\ORM\\EntityRepository')->disableOriginalConstructor()->getMock();
     $addrRepo = $this->getMockBuilder('Doctrine\\ORM\\EntityRepository')->disableOriginalConstructor()->getMock();
     $em->expects($this->exactly(2))->method('getRepository')->will($this->returnValueMap(array(array('OroEmailBundle:EmailFolder', $folderRepo), array('Oro\\Bundle\\EmailBundle\\Tests\\Unit\\Entity\\TestFixtures\\TestEmailAddressProxy', $addrRepo))));
     $folderRepo->expects($this->exactly(2))->method('findOneBy')->will($this->returnCallback(function ($c) use(&$dbFolder) {
         return $c['fullName'] === 'Exist' ? $dbFolder : null;
     }));
     $addrRepo->expects($this->exactly(2))->method('findOneBy')->will($this->returnCallback(function ($c) use(&$dbAddr) {
         return $c['email'] === 'Exist' ? $dbAddr : null;
     }));
     $em->expects($this->exactly(4))->method('persist')->with($this->logicalOr($this->identicalTo($newFolder), $this->identicalTo($newAddr), $this->identicalTo($email1), $this->identicalTo($email2)));
     $owner = $this->getMock('Oro\\Bundle\\EmailBundle\\Entity\\EmailOwnerInterface');
     $this->ownerProvider->expects($this->any())->method('findEmailOwner')->will($this->returnValue($owner));
     $this->batch->persist($em);
     $this->assertTrue($origin === $email1->getFolder()->getOrigin());
     $this->assertTrue($origin === $email2->getFolder()->getOrigin());
     $this->assertTrue($dbFolder === $email1->getFolder());
     $this->assertTrue($newFolder === $email2->getFolder());
     $this->assertTrue($dbAddr === $email1->getFromEmailAddress());
     $this->assertNull($email1->getFromEmailAddress()->getOwner());
     $this->assertTrue($newAddr === $email2->getFromEmailAddress());
     $this->assertTrue($owner === $email2->getFromEmailAddress()->getOwner());
     $email1Recipients = $email1->getRecipients();
     $this->assertTrue($dbAddr === $email1Recipients[0]->getEmailAddress());
     $this->assertNull($email1Recipients[0]->getEmailAddress()->getOwner());
     $this->assertTrue($newAddr === $email1Recipients[1]->getEmailAddress());
     $this->assertTrue($owner === $email1Recipients[1]->getEmailAddress()->getOwner());
     $email2Recipients = $email2->getRecipients();
     $this->assertTrue($dbAddr === $email2Recipients[0]->getEmailAddress());
     $this->assertNull($email2Recipients[0]->getEmailAddress()->getOwner());
     $this->assertTrue($newAddr === $email2Recipients[1]->getEmailAddress());
     $this->assertTrue($owner === $email2Recipients[1]->getEmailAddress()->getOwner());
 }