2

I noticed that translations are not working with the EcomDev_PHPUnit Unit Test framework if you replace a helper with a mocked helper, using mockHelper() and replaceByMock(). Shouldn't any call to the not-mocked method __() behave the same way than in the original helper?

4

The original translation method __() indeed gets called, but it involves some "magic". This is the method in Mage_Core_Helper_Abstract:

/**
 * Translate
 *
 * @return string
 */
public function __()
{
    $args = func_get_args();
    $expr = new Mage_Core_Model_Translate_Expr(array_shift($args), $this->_getModuleName());
    array_unshift($args, $expr);
    return Mage::app()->getTranslator()->translate($args);
}

The module name is determined by $this->_getModuleName(), so let's look into this method:

/**
 * Retrieve helper module name
 *
 * @return string
 */
protected function _getModuleName()
{
    if (!$this->_moduleName) {
        $class = get_class($this);
        $this->_moduleName = substr($class, 0, strpos($class, '_Helper'));
    }
    return $this->_moduleName;
}

It determines the module name by stripping anything starting from _Helper off your method (Imagine what bugs a module name starting with "Helper" would cause...)

But if your helper class is Your_Awesome_Helper_Data, the mocked helper class will actually be Mock_Your_Awesome_Helper_Data. As a module named Mock_Your_Awesome doesn't exist, nothing can be translated.

Solution

To make your helpers unit test proof, define _moduleName explicitly:

class Your_Awesome_Helper_Data extends Mage_Core_Helper_Abstract
{
    protected $_moduleName = 'Your_Awesome';
}

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.