Skip to content
This repository has been archived by the owner on Apr 11, 2022. It is now read-only.

Example mocking Mage getResourceModel

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

Mocking - Mage::getResourceModel()

Code you want to test

<?php
class MyCompany_MyModule_Model_Example_Mocking
{
    public function doSomething()
    {
        /** @var MyCompany_MyModule_Model_Resource_Example $resource */
        $resource = Mage::getResourceModel('mycompany_mymodule/example')
        return $resource;
    }
}

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()
    {
        // Build a mock object and register it for the
        // Mage::getResourceModel() method with the correct key
        $resource = $this->buildMock('MyCompany_MyModule_Model_Resource_Example');
        $this->addMageResourceModel('mycompany_mymodule/example', $resource);

        $result = $this->_instance->doSomething();
        $this->assertSame($resource, $result);
    }
}