Ejemplo n.º 1
0
 public function testListenerWithUserCallback()
 {
     $user = new \BehaviorFixtures\ORM\UserEntity();
     $user->setUsername('user');
     $user2 = new \BehaviorFixtures\ORM\UserEntity();
     $user2->setUsername('user2');
     $userCallback = function () use($user) {
         return $user;
     };
     $em = $this->getEntityManager($this->getEventManager(null, $userCallback, get_class($user)));
     $em->persist($user);
     $em->persist($user2);
     $em->flush();
     $entity = new \BehaviorFixtures\ORM\BlameableEntity();
     $em->persist($entity);
     $em->flush();
     $id = $entity->getId();
     $createdBy = $entity->getCreatedBy();
     $this->listener->setUser($user2);
     // switch user for update
     $entity = $em->getRepository('BehaviorFixtures\\ORM\\BlameableEntity')->find($id);
     $entity->setTitle('test');
     // need to modify at least one column to trigger onUpdate
     $em->flush();
     $em->clear();
     $this->assertInstanceOf('BehaviorFixtures\\ORM\\UserEntity', $entity->getCreatedBy(), 'createdBy is a user object');
     $this->assertEquals($createdBy->getUsername(), $entity->getCreatedBy()->getUsername(), 'createdBy is constant');
     $this->assertEquals($user2->getUsername(), $entity->getUpdatedBy()->getUsername());
     $this->assertNotEquals($entity->getCreatedBy(), $entity->getUpdatedBy(), 'createBy and updatedBy have diverged since new update');
 }
 /**
  * @test
  */
 public function should_only_persist_user_entity()
 {
     $user = new \BehaviorFixtures\ORM\UserEntity();
     $user->setUsername('user');
     $userCallback = function () use($user) {
         return $user;
     };
     $em = $this->getEntityManager($this->getEventManager('anon.', $userCallback, get_class($user)));
     $em->persist($user);
     $em->flush();
     $entity = new \BehaviorFixtures\ORM\BlameableEntity();
     $em->persist($entity);
     $em->flush();
     $this->assertNull($entity->getCreatedBy(), 'createdBy is a not updated because not a user entity object');
     $this->assertNull($entity->getUpdatedBy(), 'updatedBy is a not updated because not a user entity object');
 }