我如何在 PHP 中重新创建我的数据库(例如用于单元测试)
我如何从 PHP 重新创建我的数据库,也许插入默认数据。目前,我打算将该行为用于单元测试。
我正在使用 Doctrine 2、Zend Framework 1.11、Zend_Test
进行单元测试
我可以使用 CLI
doctrine orm:schema-tool:update --force
或者
doctrine orm:schema-tool:drop --force
doctrine orm:schema-tool:create
我正在寻找 PHP 替代品,到目前为止发现 this
但它看起来像
$tool = new \Doctrine\ORM\Tools\SchemaTool($em);
$classes = array(
$em->getClassMetadata('Entities\User'),
$em->getClassMetadata('Entities\Profile')
);
$tool->dropSchema($classes, \Doctrine\ORM\Tools\SchemaTool::DROP_DATABASE);
$tool->createSchema($classes);
我真的不想指定模型类,尤其是在开发中,当它们可以改变时。它应该只读取...下面指定的所有类...就像使用 CLI 一样,您不需要指定您想要的类?
$driverImpl = $config->newDefaultAnnotationDriver(array(realpath('../models')));
$config->setMetadataDriverImpl($driverImpl);
How can I from PHP, re-create my Database, maybe inserting default data. Currently, I am intending to use the behavior for Unit Tests.
I am using Doctrine 2, Zend Framework 1.11, Zend_Test
for unit tests
I could use the CLI
doctrine orm:schema-tool:update --force
Or
doctrine orm:schema-tool:drop --force
doctrine orm:schema-tool:create
I am looking for a PHP replacement, so far found this
but it will look something like
$tool = new \Doctrine\ORM\Tools\SchemaTool($em);
$classes = array(
$em->getClassMetadata('Entities\User'),
$em->getClassMetadata('Entities\Profile')
);
$tool->dropSchema($classes, \Doctrine\ORM\Tools\SchemaTool::DROP_DATABASE);
$tool->createSchema($classes);
And I don't really want having to specify the model classes, esp in development when they can change. It should just read from the all classes specified in ... below ... just like with the CLI, you don't need to specify the classes you want?
$driverImpl = $config->newDefaultAnnotationDriver(array(realpath('../models')));
$config->setMetadataDriverImpl($driverImpl);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
自述文件中有一些示例,其中包括一个演示如何动态创建数据库架构的示例。 Benjamin Eberlei 是 Doctrine 2 的核心贡献者。
另请参阅 B. Eberlei 的 PHPUnit 数据库测试终极指南
You can use
There is some examples in the Readme, including an example that shows how to create the database schema on the fly. Benjamin Eberlei's is a Doctrine 2 core contributor.
Also see B. Eberlei's Ultimate Guide to DB Testing with PHPUnit
因为您在设置 EntityManager 时设置了模型的路径,所以您可以在代码中创建架构,而无需重新声明此路径(并且无需声明每个类)。为此,您需要获取对已配置的 EntityManager 实例的引用,并获取对 ClassMetadataFactory 的引用,然后您可以从中调用 ClassMetadataFactory#getAllMedata() 。
这是一个示例,其中我有一个静态 Bootstrap 类,它允许我从任何地方获取对 EntityManager 的引用,并在单元测试中的 setUp() 调用上重新创建架构:
Because you have set the path to the models when setting up the EntityManager you can create the schema in code without having to redeclare this path (and without having to declare each class). To do this you need to get a reference to your configured instance of EntityManager and get a reference to ClassMetadataFactory which you can then call ClassMetadataFactory#getAllMedata() from.
Here is an example where I have a static Bootstrap class that allows me to get a reference to the EntityManager from anywhere and I recreate the schema on the setUp() call in the unit tests:
Doctrine 支持
fixture
。尝试一下。
Doctrine supports
fixtures
. Try it out.