保存教义记录而不保留
在我的单元测试中,我希望具有以下行为:
$myNewDoctrineRecord = new Dto_MyRecord();
$myNewDoctrineRecord->pk = 1; //the primary key
...
$myNewDoctrineRecord->save();
现在该记录不应该真正保留到数据库中。但我想做的是
$myFetchedDoctrineRecord = Doctrine::getTable('Dto_MyRecord')->find(1);
//result should be $myFetchedDoctrineRecord === $myNewDoctrineRecord
,我知道这可以通过 DAO 模式来实现,我将 DAO 替换为某种模拟。但也许有某种可能性,所以只是说教义不坚持,而只是“记住”记录。
谢谢你! 马库斯
within my Unit-tests i would like to have the following behavior:
$myNewDoctrineRecord = new Dto_MyRecord();
$myNewDoctrineRecord->pk = 1; //the primary key
...
$myNewDoctrineRecord->save();
Now this record shouldnt really persist to the database. But i would like to do
$myFetchedDoctrineRecord = Doctrine::getTable('Dto_MyRecord')->find(1);
//result should be $myFetchedDoctrineRecord === $myNewDoctrineRecord
I know this could be achived with a DAO pattern, where i replace the DAO to some mock. But maybe there is some possibility so just say Doctrine not to persist, but only to "remember" the records.
Thank you!
Markus
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以启动一个事务,并在测试结束时回滚。这样您仍然可以 find() 在事务中创建的记录,但回滚将确保它们不会被持久化。
You could start a transaction, and at the end of the test rollback. That way you can still find() the records created within the transaction, but the rollback will ensure they're not persisted.
如果您的机器有一些空间,那么您可以将对象放入 $_SESSION 中。但请注意,Doctrine 对象可能会变得相当大。
If your machine has some space then you could put your objects into the $_SESSION. But beware, Doctrine objects can grow pretty large.