代码之家  ›  专栏  ›  技术社区  ›  takeshin

ZF、ZFDoctrine和phpunit设置

  •  2
  • takeshin  · 技术社区  · 16 年前

    这里有人使用Zend框架吗? 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 回复  |  直到 15 年前
        1
  •  0
  •   Max Gordon    15 年前

    我没用过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"));
    

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

    推荐文章