【发布时间】:2015-10-27 03:58:57
【问题描述】:
我想对保存 2 个实体的服务类进行单元测试。 只有测试失败,因为人员实体没有从 entityManager 获得 ID,因为它被模拟了。
有没有办法在第一次调用 flush 后更新 person 对象。
class Foo
{
...
public function save()
{
$em = $this->getEntityManager();
$person = new Person();
$person->setName('Dude');
$em->persist($person);
$em->flush();
$user = new User();
$user->setPersonId($person->getId());
$user->setEmail('dude@example.com');
$em->persist($user);
$em->flush();
}
}
class FooTest
{
...
public function testSave_UserIsSaved()
{
$person = new Person();
$person->setName('dude');
$user = new User();
$user->setPersonId(4); // <-- this is where it gets wrong
$user->setEmail('dude@example.com');
$person = array(
'name' => 'Dude',
);
$user = array(
'person_id' => 3,
'email' => 'dude@example.com',
);
$emMock = $this->getMockBuilder('\Doctrine\ORM\EntityManager')
->setMethods(array('persist', 'flush'))
->getMock();
$emMock->expects($this->exactly(2))
->method('persist')
->with(
$this->logicalOr(
$this->equalTo($person),
$this->equalTo($user)
)
);
$emMock->expects($this->exactly(2))
->method('flush');
$foo = new Foo($emMock);
$foo->save();
}
}
【问题讨论】:
标签: php doctrine-orm phpunit