This repository has been archived by the owner on Apr 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Example mocking Mage getSingleton
Vadim Justus edited this page Jun 30, 2014
·
5 revisions
Code you want to test
<?php
class MyCompany_MyModule_Model_Example_Mocking
{
public function doSomething()
{
/** @var MyCompany_MyModule_Model_Example $singleton */
$singleton = Mage::getSingleton('mycompany_mymodule/example')
$singleton->setSomething('foobar');
return $singleton;
}
}
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::getSingleton() method with the correct key
$singleton = $this->buildMock('MyCompany_MyModule_Model_Example');
$this->addMageSingleton('mycompany_mymodule/example', $singleton);
// Method 'setSomething' must be invoked one time
$singleton->expects($this->once())
->method('setSomething');
// Method should return the singleton
$result = $this->_instance->doSomething();
$this->assertSame($singleton, $result);
}
}