forked from techdivision/TechDivision_MagentoUnitTesting
-
Notifications
You must be signed in to change notification settings - Fork 0
Example mocking Mage helper
Vadim Justus edited this page Jun 30, 2014
·
4 revisions
Code you want to test
<?php
class MyCompany_MyModule_Model_Example_Mocking
{
public function doSomething()
{
/** @var MyCompany_MyModule_Helper_Config $configHelper */
$configHelper = Mage::helper('mycompany_mymodule/config')
return $configHelper->getAnyConfigurationValue();
}
}
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::helper() method with the correct key
$helper = $this->buildMock('MyCompany_MyModule_Helper_Config');
$this->addMageHelper('mycompany_mymodule/config', $helper);
// define configuration value
$configurationValue = 'unittest value';
// Method 'getAnyConfigurationValue
// must be invoked one time
// and return the value '$configurationValue'
$helper->expects($this->once())
->method('getAnyConfigurationValue')
->will($this->returnValue($configurationValue));
// Method must return the configuration value
$result = $this->_instance->doSomething();
$this->assertSame($configurationValue, $result);
}
}