ZF,ZFDoctrine和PHPUnit设置

时间:2010-08-19 12:11:13

标签: unit-testing zend-framework doctrine phpunit zfdoctrine

这里有没有人一起使用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);

如果有人知道更好的方法,请纠正我。

1 个答案:

答案 0 :(得分:0)

我没有使用ZFDoctrine,只是简单的Doctrine 1.2。我不知道我的解决方案是否更好,但我想如果any1感兴趣我发帖,这是我的测试文件夹中的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"));

这里的关键实际上是重建所有数据库的最后一行,为每次测试创建一个干净的环境。