3

I'm trying to mock this method: getCurrentCurrency() from "core/store" (Mage_Core_Model_Store) in my unit test. Just a simple stub, nothing more.

private function _stubGetCurrentCurrency() {
    $mock = $this->getModelMock(
        'core/store',
        array(
            'getCurrentCurrency'
        )
    );
    $mock
        ->expects($this->any())
        ->method('getCurrentCurrency')
        ->will($this->returnCallback(array($this, 'signal')));
    $this->replaceByMock(
        'singleton',
        'core/store',
        $mock
    );
}

function signal() {
    echo "\n- Signal invoked -\n";
}

However it doesn't seem to work for me - when I do $this->_stubGetCurrentCurrency(); I don't see the signal() is being called at all. I've ascertained that the original method is being invoked instead. Looks like I'm missing something, but I can't find what's wrong.

I also have stubs for "sales/order_invoice" canCapture() and "salesrule/observer" addProductAttributes() in that test - they are instantiated in the same manner and both work perfectly fine.

Can anyone point out my miss? Thank you.

  • Apparently, the problem is core/store object is instantiated by store_collection by real class name (not by alias) for performance. So the mocking with aliases does not work. But this is not the only problem it seems, you have to re-init the store as well, and this brings another problem with discarding your fixture data after test. I'll post when I have the 100% working solution. – Thanks Nov 29 '13 at 10:01
  • The type parameter of replaceByMock() should be 'model', not 'singleton'. However, I couldn't get this to work neither, even with $this->app()->reinitStores(). Did you find a solution? – Fabian Schmengler Jun 15 '14 at 13:30
1

This is how I ended up mocking core/store, it may work for you!

class Mage_Core_Model_Store_Stub extends Mage_Core_Model_Store
{
    public function setCurrentCurrencyCode($currency)
    {
        return $this->setData('current_currency_code', $currency);
    }
    public function getCurrentCurrencyCode()
    {
        return $this->getData('current_currency_code');
    }
}

$coreStore = new Mage_Core_Model_Store_Stub();
$coreStore->setCurrentCurrencyCode($currency);
Mage::app()->getStore($coreStore)->getCurrentCurrencyCode();
|improve this answer|||||

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.