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>

アプリ/コード/コミュニティ/マイ/シング/ヘルパー/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);
    }
}

アプリ/コード/コミュニティ/マイ/モノ/テスト/ヘルパー/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()
        );
    }
}

アプリ/コード/コミュニティ/マイ/モノ/テスト/ヘルパー/DataTest/testGetArbitrary.yaml:

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

上記のテストが不自然であることは承知していますが、ポイントは、「任意の」値を「何か」に設定するフィクスチャを作成して、「何かの値がデータベースにあります。何故ですか?キャッシュやconfig.xmlのデフォルトではなく、テストデータベースからデータを取得するために必要なphpunitまたはEcomDevへの他のトリックはありますか?

ちなみに、phpunitテスト データベースの値を常にポーリングしながら実行すると、適切な場所に挿入されることわかります。somethingテストはそこから値を取得しません。

4

0 に答える 0