使用SQlite数据库进行Zend PHPUnit测试

时间:2011-08-01 15:46:28

标签: php zend-framework sqlite phpunit

尝试在我的ZF应用程序中执行测试时遇到问题:

我的测试用例具有以下设置功能:

$this->bootstrap = new Zend_Application(
        'testing',
        APPLICATION_PATH . '/configs/application.ini'
    );

我的testing环境会扩展development,所以我的slite数据库文件应该以相同的方式加载,我是对的吗? 但是当我尝试在测试中执行任何db操作时,我得到以下错误:

SQLSTATE[HY000]: General error: 11 malformed database schema - near "NO": syntax error

我的数据库在development环境下运行正常,所以我猜这不是数据库问题...谢谢!

以下是我application.ini的内容:

[production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 1
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
resources.view[] = 

resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "root"
resources.db.params.password = ""
resources.db.params.dbname = ""
resources.db.params.charset = "utf8"

phpSettings.date.timezone = "America/Argentina/Cordoba"
phpSettings.upload_max_filesize = "10M"
phpSettings.post_max_size = "10M"

logger.path = "/logs"

[staging : production]

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

resources.db.adapter = "pdo_sqlite"
resources.db.params.dbname = APPLICATION_PATH "/../database/mydb.db"

[testing : development]

1 个答案:

答案 0 :(得分:1)

我的配置:

应用/ bootstrap.php中

protected function _initAutoload()
{
    $autoloader = new Zend_Application_Module_Autoloader(array('namespace' => '', 'basePath' => APPLICATION_PATH));
    return $autoloader;
}

/**
 * initialize doctrine library
 */
protected function _initDoctrine()
{
    // retrieve options
    $doctrine = $this->getOption('doctrine');
    // push doctrine model autoloader
    $this->getApplication()->getAutoloader()->pushAutoloader(array('Doctrine_Core', 'autoload'));
    // push doctrine model autoloader
    $this->getApplication()->getAutoloader()->pushAutoloader(array('Doctrine_Core', 'modelsAutoload'));
    // setup doctrine attributes
    $manager = Doctrine_Manager::getInstance();
    $manager->setAttribute(Doctrine_Core::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
    $manager->setAttribute(Doctrine_Core::ATTR_MODEL_LOADING, Doctrine_Core::MODEL_LOADING_CONSERVATIVE);
    $manager->setAttribute(Doctrine_Core::ATTR_AUTOLOAD_TABLE_CLASSES, true);
    $manager->setAttribute(Doctrine_Core::ATTR_QUOTE_IDENTIFIER, true);
    $manager->setAttribute(Doctrine_Core::ATTR_USE_NATIVE_ENUM, true);
    // set default encoding
    $manager->setCharset('utf8');
    $manager->setCollate('utf8_unicode_ci');
    // autoloading models
    Doctrine_Core::loadModels(APPLICATION_PATH . '/models');
    // creating connection
    $conn = Doctrine_Manager::connection($doctrine['dsn'], 'doctrine');
    return $conn;
}

/tests/application/bootstrap.php

// Define path to application directory
if (!defined('APPLICATION_PATH')) {
    define('APPLICATION_PATH',
            realpath(dirname(__FILE__) . '/../../application'));
}

// Define application environment
define('APPLICATION_ENV', 'testing');

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(APPLICATION_ENV,
        APPLICATION_PATH . '/configs/application.ini');
$application->bootstrap();

$cli = new Doctrine_Cli($application->getOption('doctrine'));

$cli->run(array("doctrine","create-db","force"));
$cli->run(array("doctrine","create-tables","force"));
$cli->run(array("doctrine","load-data","force"));

/application/configs/application.ini

[production]
; doctrine settings
doctrine.dsn                = "mysql://root:root@localhost/dbname"
doctrine.data_fixtures_path = APPLICATION_PATH "/../doctrine/data/fixtures/data.yml"
doctrine.sql_path           = APPLICATION_PATH "/../doctrine/data/sql"
doctrine.migrations_path    = APPLICATION_PATH "/../doctrine/migrations"
doctrine.yaml_schema_path   = APPLICATION_PATH "/../doctrine/schema"
doctrine.models_path        = APPLICATION_PATH "/models"
doctrine.generate_models_options.generateTableClasses = true
doctrine.generate_models_options.phpDocName = "Name Firstname"
doctrine.generate_models_options.phpDocEmail = "user@email.com"

[development : production]
; doctrine settings
doctrine.dsn                = "mysql://root:root@localhost/dbname"
doctrine.data_fixtures_path = APPLICATION_PATH "/../doctrine/data/fixtures/data.development.yml"

[testing : development]
; uses sqlite for testing
doctrine.dsn                = "sqlite::memory:"
doctrine.data_fixtures_path = APPLICATION_PATH "/../doctrine/data/fixtures/data.testing.yml"

测试/ phpunit.xml

<phpunit bootstrap="./application/bootstrap.php" colors="true">
    <testsuites>
        <testsuite name="Forms">
            <directory>application/forms/</directory>
        </testsuite><testsuite name="Models">
            <directory>application/models/</directory>
        </testsuite>
        <testsuite name="Acls">
            <directory>application/acls/</directory>
        </testsuite>
        <testsuite name="Library">
            <directory>library/</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist>
            <directory suffix=".php">../application</directory>
            <directory suffix=".php">../library</directory>
            <exclude>
                <directory suffix=".php">../library</directory>
                <directory suffix=".phtml">../application</directory>
                <file>../application/Bootstrap.php</file>
                <file>../doctrine/doctrine.php</file>
            </exclude>
        </whitelist>
    </filter>
</phpunit>

我希望看看帮助!