/** * Test existing user modification */ public function testOnFlushUpdateUser() { $args = new OnFlushEventArgs($this->em); $user = new User(); $org = new Organization(); $org->setId(1); $org->setName('test'); $user->addOrganization($org); $newCalendar = new Calendar(); $newCalendar->setOwner($user); $newCalendar->setOrganization($org); $newConnection = new CalendarConnection($newCalendar); $newCalendar->addConnection($newConnection); $calendarMetadata = new ClassMetadata(get_class($newCalendar)); $connectionMetadata = new ClassMetadata(get_class($newConnection)); $this->em->expects($this->any())->method('getClassMetadata')->will($this->returnValueMap([['Oro\\Bundle\\CalendarBundle\\Entity\\Calendar', $calendarMetadata], ['Oro\\Bundle\\CalendarBundle\\Entity\\CalendarConnection', $connectionMetadata]])); $calendarRepo = $this->getMockBuilder('\\Doctrine\\ORM\\EntityRepository')->disableOriginalConstructor()->getMock(); $calendarRepo->expects($this->any())->method('findDefaultCalendar')->will($this->returnValue(false)); $this->em->expects($this->once())->method('getUnitOfWork')->will($this->returnValue($this->uow)); $this->uow->expects($this->once())->method('getScheduledEntityInsertions')->will($this->returnValue([])); $this->uow->expects($this->once())->method('getScheduledEntityUpdates')->will($this->returnValue([$user])); $this->em->expects($this->any())->method('getRepository')->with('OroCalendarBundle:Calendar')->will($this->returnValue($calendarRepo)); $this->em->expects($this->at(2))->method('persist')->with($this->equalTo($newCalendar)); $this->em->expects($this->at(3))->method('persist')->with($this->equalTo($newConnection)); $this->uow->expects($this->at(2))->method('computeChangeSet')->with($calendarMetadata, $newCalendar); $this->uow->expects($this->at(3))->method('computeChangeSet')->with($connectionMetadata, $newConnection); $this->listener->onFlush($args); }
/** * @expectedException \Oro\Bundle\EntityBundle\Exception\InvalidEntityException */ public function testAddConnectionShouldNotAllowConnectEmpty() { $obj = new Calendar(); $connection = new CalendarConnection(new Calendar()); ReflectionUtil::setPrivateProperty($connection, 'connectedCalendar', null); $obj->addConnection($connection); }
/** * @param EntityManager $em * @param UnitOfWork $uow * @param User $entity * @param Organization $organization */ protected function createCalendar($em, $uow, $entity, $organization) { // create a default calendar for assigned organization $calendar = new Calendar(); $calendar->setOwner($entity); $calendar->setOrganization($organization); // connect the calendar to itself $calendarConnection = new CalendarConnection($calendar); $calendar->addConnection($calendarConnection); $em->persist($calendar); $em->persist($calendarConnection); $uow->computeChangeSet($this->getClassMetadata($calendar, $em), $calendar); $uow->computeChangeSet($this->getClassMetadata($calendarConnection, $em), $calendarConnection); }
/** * @param OnFlushEventArgs $event */ public function onFlush(OnFlushEventArgs $event) { $em = $event->getEntityManager(); $uow = $em->getUnitOfWork(); foreach ($uow->getScheduledEntityInsertions() as $entity) { if ($entity instanceof User) { // create a default calendar to a new user $calendar = new Calendar(); $calendar->setOwner($entity); // connect the calendar to itself $calendarConnection = new CalendarConnection($calendar); $calendar->addConnection($calendarConnection); $em->persist($calendar); $em->persist($calendarConnection); // can't inject entity manager through constructor because of circular dependency $uow->computeChangeSet($this->getCalendarMetadata($em), $calendar); $uow->computeChangeSet($this->getCalendarConnectionMetadata($em), $calendarConnection); } } }