1

我们正在使用IvanChepurnyi 的 EcomDev_PHPUnit,它似乎有一些很好的特性,可以用 Magento 编写单元和集成测试,包括关于从数据库加载数据的声明特性。只是,我只能让它工作半途而废:我可以使用夹具将数据获取数据库,但我的测试总是从数据库之外的意外位置获取数据,例如 config.xml。

例如,假设我创建了一个带有管理配置的简单模型:

应用程序/代码/社区/我的/事物/etc/config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <My_Thing>
            <version>0.0.1</version>
        </My_Thing>
    </modules>
    <global>
        <helpers>
            <thing>
                <class>My_Thing_Helper</class>
            </thing>
        </helpers>
    </global>
    <default>
        <thing>
            <it>
                <arbitrary>value</arbitrary>
            </it>
        </thing>
    </default>
    <phpunit>
        <suite>
            <modules>
                <My_Thing/>
            </modules>
        </suite>
    </phpunit>
</config>

应用程序/代码/社区/我的/事物/etc/settings.xml:

<?xml version="1.0"?>
<config>
    <tabs>
        <thing translate="label" module="my_thing">
            <label>Thing</label>
        </thing>
    </tabs>
    <sections>
        <thing translate="label">
            <label>Thing Settings</label>
            <tab>thing</tab>
            <frontend_type>text</frontend_type>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
            <groups>
                <it translate="label">
                    <label>It</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>120</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
                        <arbitrary translate="label">
                            <label>Arbitrary</label>
                            <frontend_type>text</frontend_type>
                            <validate>required-entry</validate>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </arbitrary>
                  </fields>
                </it>
            </groups>
        </thing>
    </sections>
</config>

应用程序/代码/社区/My/Thing/Helper/Data.php:

<?php
class My_Thing_Helper_Data extends Mage_Core_Helper_Abstract
{
    const CONFIG_IT_ARBITRARY = 'thing/it/arbitrary';

    /**
     * Get the arbitrary value.
     *
     * @return string
     */
    public static function getArbitrary()
    {
        return Mage::getStoreConfig(self::CONFIG_IT_ARBITRARY);
    }
}

app/code/community/My/Thing/Test/Helper/DataTest.php:

<?php
class My_Thing_Helper_DataTest extends EcomDev_PHPUnit_Test_Case_Controller
{
    /**
     * The helper we're testing.
     */
    public $target = null;

    /**
     * Set up configuration for testing
     */
    public function setUp()
    {
        parent::setUp();
        $this->target = Mage::helper('thing');
    }

    /**
     * Test that paths are joined correctly.
     * 
     * @loadFixture
     */
    public function testGetArbitrary()
    {
        $this->assertEquals(
            'something',
            $this->target->getArbitrary()
        );
    }
}

app/code/community/My/Thing/Test/Helper/DataTest/testGetArbitrary.yaml:

tables:
  core/config_data:
    - path: thing/it/arbitrary
      value: something

我意识到上面的测试是人为的,但关键是即使我创建一个夹具来将“任意”值设置为“某物”,测试也会失败,因为它从 config.xml 默认值中获取值,即使'某些东西的价值在数据库中。这是为什么?为了从测试数据库中获取数据,而不是缓存或 config.xml 默认值,我需要对 phpunit 或 EcomDev 执行其他一些技巧吗?

顺便说一句,如果我phpunit在不断轮询测试数据库的值的同时运行,我确实看到something插入到了正确的位置。测试只是没有从那里获得价值。

4

0 回答 0