How do you test setting and getting a session variable in PHPUnit/EComDev? This is what I've so far in my test method:

$checkoutSession = $this->getModelMock('checkout/session');
$checkoutSession->expects($this->any())->method('getData')->with('number')->will($this->returnCallback(array($this, 'getNumber')));
$checkoutSession->expects($this->any())->method('setData')->will($this->returnCallback(array($this, 'setNumber')));

In the test class, I've defined the following:

protected $_number = 5;

public function getNumber()
    return $this->_number;

public function setNumber()
    $args = func_get_args();
    $key = $args[0];
    $value = $args[1];

    $this->_number = $value;

This works in the code being tested:

$checkoutSession = Mage::getSingleton('checkout/session');
$number = $checkoutSession->getData('number'); //5
$checkoutSession->setData('number', $number+1);
$number2 = $checkoutSession->getData('number'); //6

However, getting the variable with Magento's magic method does not:

$number = Mage::getSingleton('checkout/session')->getNumber();//this is null

How does one mock Magento's magic methods?

1 Answer 1


In the given example, all methods are replaced by mock implementations. No existing Mage_Checkout_Model_Session method ever gets called, including getNumber() and __call().

There is no return value defined for getNumber() so you get null.

To create a stub that returns configured values for getData and setData only (while executing all other methods normally), add these method names as second parameter:

$checkoutSession = $this->getModelMock(
    array('init', 'getData', 'setData')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.