ZF,ZFDOCTRINE和PHPUNIT设置
-
29-09-2019 - |
题
这里有人使用Zend Framework吗, ZFDOCTRINE 和phpunit在一起?
如何在每个测试运行中重建数据库?如何分开本地/生产/测试环境?
您会分享单元测试设置吗?
我一直在尝试这样的事情:
// /tests/bootstrap.php
// ... setup paths and constants here
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap('doctrine');
$provider = new ZFDoctrine_Tool_DoctrineProvider;
$provider->generateModelsFromYaml();
//$provider->buildProject(true);
但这以:
Notice: Constant APPLICATION_PATH already defined in /home/user/www/library/ZendFramework/1.10.7/library/Zend/Tool/Project/Context/Zf/BootstrapFile.php on line 106
Fatal error: Call to a member function getResponse() on a non-object in /home/user/www/library/zf-doctrine/library/ZFDoctrine/Tool/DoctrineProvider.php on line 271
模型未生成。
我会遇到类似的错误:
$provider->createDatabase();
但是在这种情况下,创建了数据库。
其他提供商命令不起作用。
解决方案:
$provider = new ZFDoctrine_Tool_DoctrineProvider;
$registry = new Zend_Tool_Framework_Registry;
$provider->setRegistry($registry);
@$provider->buildProject(true);
如果有人知道一种更好的方法,请纠正我。
解决方案
我没有使用ZFDOCTRINE,而只是普通学说1.2。我不知道我的解决方案是否更好,但是我想我发布了是否有兴趣,这是我的测试文件夹中的bootstrap.php:
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application'));
// Define application environment
/**
* In the application.ini:
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
doctrine.dsn = "mysql://my_user:passwd@localhost/my_phpunit_test_db"
*/
define('APPLICATION_ENV', 'testing');
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path()
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/../configs/application.ini'
);
$application->getBootstrap()->bootstrap();
// Can run out if too small
ini_set('memory_limit', '512M');
// Get the doctrine settings
$config = $application->getOption('doctrine');
$cli = new Doctrine_Cli($config);
$cli->run(array("doctrine", "build-all-reload","force"));
这里的关键实际上是重建所有数据库的最后一行,为每个测试创建一个干净的环境。
不隶属于 StackOverflow