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

Example mocking Mage getModel

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

Mocking - Mage::getModel()

Code you want to test

<?php
class MyCompany_MyModule_Model_Example_Mocking
{
    public function doSomething()
    {
        /** @var Mage_Sales_Model_Order $order */
        $order = Mage::getModel('sales/order');
        $order->save();
    }
}

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::getModel() method with the correct key
        $order = $this->buildMock('Mage_Sales_Model_Order');
        $this->addMageModel('sales/order', $order);

        // Method 'save' must be invoked one time
        $order->expects($this->once())
            ->method('save');

        // Execute 'doSomething' method
        $this->_instance->doSomething();
    }
}