Skip to content

Example mocking Mage getStoreConfig

Vadim Justus edited this page Jun 30, 2014 · 2 revisions

Mocking - Mage::getStoreConfig()

Code you want to test

<?php
class MyCompany_MyModule_Model_Example_Mocking
{
    const XML_PATH_GENERAL_ENABLED = 'mycompany_mymodule/general/enabled';

    public function doSomething()
    {
        $configValue = Mage::getStoreConfig(self::XML_PATH_GENERAL_ENABLED)
        return (bool)$configValue;
    }
}

Test implementation

<?php
class MyCompany_MyModule_Unit_Model_Example_MockingTest
    extends TechDivision_MagentoUnitTesting_TestCase_Model
{
    /**
     * @var string
     */
    protected $_testClassName = 'MyCompany_MyModule_Model_Example_Mocking';

    /**
     * @var MyCompany_MyModule_Model_Example_Mocking
     */
    protected $_instance;

    public function testDoSomething_valueIsString()
    {
        $key = MyCompany_MyModule_Model_Example_Mocking::XML_PATH_GENERAL_ENABLED;
        $value = 'unittest value';
        $this->addMageStoreConfig($key, $value);

        $result = $this->_instance->doSomething();
        $this->assertTrue($result);
    }

    public function testDoSomething_valueIsInt()
    {
        $key = MyCompany_MyModule_Model_Example_Mocking::XML_PATH_GENERAL_ENABLED;
        $value = 4563456;
        $this->addMageStoreConfig($key, $value);

        $result = $this->_instance->doSomething();
        $this->assertTrue($result);
    }

    public function testDoSomething_valueIsFalse()
    {
        $key = MyCompany_MyModule_Model_Example_Mocking::XML_PATH_GENERAL_ENABLED;
        $value = false;
        $this->addMageStoreConfig($key, $value);

        $result = $this->_instance->doSomething();
        $this->assertFalse($result);
    }

    public function testDoSomething_valueIsNull()
    {
        $key = MyCompany_MyModule_Model_Example_Mocking::XML_PATH_GENERAL_ENABLED;
        $value = null;
        $this->addMageStoreConfig($key, $value);

        $result = $this->_instance->doSomething();
        $this->assertFalse($result);
    }

    public function testDoSomething_valueIsZero()
    {
        $key = MyCompany_MyModule_Model_Example_Mocking::XML_PATH_GENERAL_ENABLED;
        $value = 0;
        $this->addMageStoreConfig($key, $value);

        $result = $this->_instance->doSomething();
        $this->assertFalse($result);
    }

    public function testDoSomething_valueIsOne()
    {
        $key = MyCompany_MyModule_Model_Example_Mocking::XML_PATH_GENERAL_ENABLED;
        $value = 1;
        $this->addMageStoreConfig($key, $value);

        $result = $this->_instance->doSomething();
        $this->assertTrue($result);
    }
}