【问题标题】:ZF, ZFDoctrine and PHPUnit setupZF、ZFDoctrine 和 PHPUnit 设置
【发布时间】:2011-04-01 02:23:03
【问题描述】:

这里有人同时使用 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);

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

【问题讨论】:

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


    【解决方案1】:

    我没有使用过 ZFDoctrine,而只是简单的 Doctrine 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"));
    

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

    【讨论】:

      猜你喜欢
      • 2011-07-03
      • 1970-01-01
      • 2015-07-13
      • 2014-04-28
      • 1970-01-01
      • 1970-01-01
      • 2012-12-13
      • 1970-01-01
      • 2016-02-24
      相关资源
      最近更新 更多