Programming Language PHP
Namespace Oro\Bundle\ContactBundle\Entity
Class Contact
Method/Function setUpdatedBy
Total Examples 2
2 code examples of PHP Oro\Bundle\ContactBundle\Entity\Contact::setUpdatedBy extracted from open source projects
/**
* @dataProvider prePersistAndPreUpdateDataProvider
*/
public function testPreUpdate(
Contact $entity,
bool $mockToken = false,
bool $mockUser = false,
bool $detachedUser = null,
bool $reloadUser = null
) {
$oldDate = new \DateTime('2012-12-12 12:12:12');
$oldUser = new User();
$oldUser->setFirstName('oldUser');
$entity->setUpdatedAt($oldDate);
$entity->setUpdatedBy($oldUser);
$newUser = null;
if ($mockUser) {
$newUser = new User();
$newUser->setFirstName('newUser');
}
$this->mockSecurityContext($mockToken, $mockUser, $newUser);
$unitOfWork = $this->createMock(UnitOfWork::class);
$em = $this->createMock(EntityManager::class);
if ($reloadUser) {
$em->expects($this->once())
->method('find')
->with('OroUserBundle:User')
->willReturn($newUser);
} else {
$em->expects($this->never())
->method('find');
}
$em->expects($this->any())
->method('getUnitOfWork')
->willReturn($unitOfWork);
if (null !== $detachedUser) {
$unitOfWork->expects($this->once())
->method('getEntityState')
->with($newUser)
->willReturn($detachedUser ? UnitOfWork::STATE_DETACHED : UnitOfWork::STATE_MANAGED);
}
$unitOfWork->expects($this->exactly(2))
->method('propertyChanged')
->withConsecutive(
[$entity, 'updatedAt', $oldDate, $this->isInstanceOf(\DateTime::class)],
[$entity, 'updatedBy', $oldUser, $newUser]
);
$changeSet = [];
$args = new PreUpdateEventArgs($entity, $em, $changeSet);
$this->contactListener->preUpdate($entity, $args);
$this->assertInstanceOf(\DateTime::class, $entity->getUpdatedAt());
if ($mockToken && $mockUser) {
$this->assertEquals($newUser, $entity->getUpdatedBy());
} else {
$this->assertNull($entity->getUpdatedBy());
}
}
protected function setUpdatedProperties(Contact $contact, EntityManager $entityManager, bool $update = false)
{
$newUpdatedAt = new \DateTime('now', new \DateTimeZone('UTC'));
$newUpdatedBy = $this->getUser($entityManager);
$unitOfWork = $entityManager->getUnitOfWork();
if ($update) {
$unitOfWork->propertyChanged($contact, 'updatedAt', $contact->getUpdatedAt(), $newUpdatedAt);
$unitOfWork->propertyChanged($contact, 'updatedBy', $contact->getUpdatedBy(), $newUpdatedBy);
}
$contact->setUpdatedAt($newUpdatedAt);
$contact->setUpdatedBy($newUpdatedBy);
}