如何动态更改数据库连接以进行PhpUnit测试

时间:2010-09-15 15:34:29

标签: php unit-testing magento phpunit

我正在研究Magento 1.4.1项目,我想用PhpUnit来测试我的模型。

我可以使用默认连接运行我的PhpUnit测试,但是我想使用与我用来测试界面的数据库连接不同的数据库连接。

我想知道的是什么(如果可能的话):

  1. 有没有办法选择不同的 在我之前为我的模特连接 运行我的所有测试;
  2. 我可以添加一个 像我这样的local.xml连接 这样:

        <phpunit_setup>
            <connection>
                <host><![CDATA[localhost]]></host>
                <username><![CDATA[username]]></username>
                <password><![CDATA[password]]></password>
                <dbname><![CDATA[dbname]]></dbname>
                <active>1</active>
            </connection>
        </phpunit_setup>
    

    如果是,我该如何访问它。

  3. 感谢。

2 个答案:

答案 0 :(得分:2)

也许有另一种解决方案,但我发现当我们申请时,我们可以更改“etc_dir”。

  1. 我将“app / etc / local.xml”和“app / etc / config.xml”复制到新创建的文件夹“tests / etc /”;
  2. 我将此数据库配置更改为我需要的内容;
  3. 我在“tests / etc /”中创建了一个符号链接,指向“app / etc / modules”(不建议使用副本);
  4. 最后,我将默认参数和“etc_dir”传递给文件“tests / helper.php”中的“Mage :: app()”方法,该文件被执行以设置我的测试(包括路径,代码的白名单)覆盖)。
  5. 看起来像这样。

    在 “tests / helper.php”

    ...
    // Start Magento application
    Mage::app();
    ...
    

    在 “tests / helper.php”

    ...
    // Start Magento application
    Mage::app('default', 'store', '/path/to/test/etc');
    ...
    

    我的app文件夹

    alt text

    我的测试文件夹

    alt text

    希望这可以帮助某人。

答案 1 :(得分:1)

您可以创建自己的local.xml,例如:

<?xml version="1.0"?>
<config>
    <global>
        <resources>
            <default_setup>
                <connection>
                    <host><![CDATA[localhost]]></host>
                    <username><![CDATA[root]]></username>
                    <password></password>
                    <dbname><![CDATA[magento_test]]></dbname>
                    <active>1</active>
                </connection>
            </default_setup>
        </resources>
    </global>
</config>

并将其应用于testCase setUp方法:

$test_config = new Mage_Core_Model_Config('test/local.xml');
Mage::getConfig()->extend($test_config);
相关问题