Skip to content
This repository has been archived by the owner on Apr 11, 2022. It is now read-only.

Test cases

Vadim Justus edited this page Jun 29, 2014 · 4 revisions

TechDivision_MagentoUnitTesting_TestCase_Abstract

Purpose of test cases

The test cases provided by the testing framework support you writing the tests for your Magento modules. It depends on the type of the class you want to test which test case you need to use.

Feel free to fork the project and implement your own test cases. Don't forget to submit a pull request :)

General logic

There is an abstract test case which provides the main logic for all the other test case classes. The core function is to generate a static Mage class which dispatches all static method invocation to a mock of a Mage proxy class. That allows us to define the expectations and returns of each static method in the Mage class.

Further the abstract test case provides an API so that we can easily access the mocked objects and define the code environment for each test.

API

Protected properties

The following protected properties can be overwritten within your test class.

/**
 * Disable/enable test class initiation
 *
 * @var bool
 */
protected $_initInstanceInTestSetup = true;

/**
 * Name of class you want to test. That class will be
 * automaticaly init in the `$_instance` property for each test.
 * Unless the `$_initInstanceInTestSetup` property is set to `false`.
 *
 * @var string
 */
protected $_testClassName = 'Varien_Object';

/**
 * Array of arguments which should be passed to the
 * constructor during the `$_instance` initiation.
 *
 * @var array
 */
protected $_testClassInitArguments = array();

/**
 * That property holds the instance of the class defined
 * in `$_testClassName` property.
 *
 * @var Varien_Object
 */
protected $_instance;

Protected methods

The following protected methods can be overwritten or invoked within your test class.

/**
 * Sets up the fixture, for example, open a network connection.
 * This method is called before a test is executed.
 *
 * @return void
 */
protected function setUp() {}

/**
 * Tears down the fixture, for example, close a network connection.
 * This method is called after a test is executed.
 *
 * @return void
 */
protected function tearDown() {}

/**
 * That method generates the static Mage class, initiates a MageProxy
 * mock and register several callback methods.
 * This method is called within the setUp method by default.
 *
 * @return void
 */
protected function _initMageMock() {}

/**
 * Initiates a mock of `Mage_Core_Model_Config`.
 * This method is called within the setUp method by default.
 *
 * @return void
 */
protected function _initMageConfigMock() {}

/**
 * Initiates a mock of `Mage_Core_Model_App`.
 * This method is called within the setUp method by default.
 *
 * @return void
 */
protected function _initMageAppMock() {}

/**
 * Initiates a mock of `Mage_Core_Controller_Request_Http`.
 * This method is called within the setUp method by default.
 *
 * @return void
 */
protected function _initMageRequestMock() {}

/**
 * If the class defined in `$_testClassName` property need any speific
 * environment preparations before it could be
 * initiated. Thats the place you should implement it.
 * This method is called within the setUp method by default.
 *
 * Override that method to define environment for instance construction
 * e.g. Mage::helper in __construct method of your class
 *
 * @return void
 */
protected function _beforeInitInstance() {}

/**
 * This method is called within the setUp method by default.
 *
 * @return void
 */
protected function _afterInitInstance() {}

/**
 * Initiates the class defined in `$_testClassName` property and
 * set the `$_instance` property.
 * This method is called within the setUp method by default.
 *
 * @return void
 */
protected function _initInstance() {}

Public methods

Add data to the test environment

With following methods you can define your test environment and set data which would be passed to the code when the test is executed.

/**
 * Registers a `$value` which will be returned if
 * `Mage::getSingleton($key)` is called.
 *
 * @param string $key
 * @param mixed  $value
 *
 * @return void
 */
public function addMageSingleton($key, $value) {}

/**
 * Registers a `$value` which will be returned if
 * `Mage::getModel($key)` is called.
 *
 * @param string $key
 * @param mixed  $value
 *
 * @return void
 */
public function addMageModel($key, $value) {}

/**
 * Registers a `$value` which will be returned if
 * `Mage::helper($key)` is called.
 *
 * @param string $key
 * @param mixed  $value
 *
 * @return void
 */
public function addMageHelper($key, $value) {}

/**
 * Registers a `$value` which will be returned if
 * `Mage::getResourceModel($key)` is called.
 *
 * @param string $key
 * @param mixed  $value
 *
 * @return void
 */
public function addMageResourceModel($key, $value) {}

/**
 * Registers a `$value` which will be returned if
 * `Mage::getControllerInstance($key)` is called.
 *
 * @param string $key
 * @param mixed  $value
 *
 * @return void
 */
public function addMageControllerInstance($key, $value) {}

/**
 * Registers a `$value` which will be returned if
 * `Mage::getResourceSingleton($key)` is called.
 *
 * @param string $key
 * @param mixed  $value
 *
 * @return void
 */
public function addMageResourceSingleton($key, $value) {}

/**
 * Registers a `$value` which will be returned if
 * `Mage::getBlockSingleton($key)` is called.
 *
 * @param string $key
 * @param mixed  $value
 *
 * @return void
 */
public function addMageBlockSingleton($key, $value) {}

/**
 * Registers a `$value` which will be returned if
 * `Mage::getResourceHelper($key)` is called.
 *
 * @param string $key
 * @param mixed  $value
 *
 * @return void
 */
public function addMageResourceHelper($key, $value) {}

/**
 * Registers a `$value` which will be returned if
 * `Mage::getStoreConfig($key)` is called.
 *
 * @param string $key
 * @param mixed  $value
 *
 * @return void
 */
public function addMageStoreConfig($key, $value) {}

/**
 * Registers params within the request mock.
 * e.g. `Mage::app()->getRequest()->getParams();
 * e.g. `Mage::app()->getRequest()->getParam($key);
 *
 * @param mixed $value
 *
 * @return void
 */
public function addRequestParams($value) {}

Manipulate the prepared mock objects

With the following methods you get access to the prepared mock objects and can define expectations and so on.

e.g. $this->getMageMock()->expects($this->once())->method('log');

/**
 * Returns mock of the Mage class. You can use that mock
 * same way like any other mock instance.
 *
 * @return PHPUnit_Framework_MockObject_MockObject
 */
public function getMageMock() {}

/**
 * Return the same mock instance of which will be passed
 * to the code when `Mage::getConfig()` is called.
 *
 * @return PHPUnit_Framework_MockObject_MockObject
 */
public function getMageConfigMock() {}

/**
 * Return the same mock instance of which will be passed
 * to the code when `Mage::app()` is called.
 *
 * @return PHPUnit_Framework_MockObject_MockObject
 */
public function getMageAppMock() {}

/**
 * Return the same mock instance of which will be passed
 * to the code when `Mage::app()->getRequest()` is called.
 *
 * @return PHPUnit_Framework_MockObject_MockObject
 */
public function getMageRequestMock() {}

/**
 * Return the same mock instance of which will be passed
 * to the code when `Mage::app()->getStore()` is called.
 *
 * @return PHPUnit_Framework_MockObject_MockObject
 */
public function getMageStoreMock() {}

Invoke private or protected method

If $instance is null the method would use $this->_instance instead.

/**
 * Invokes a method of given instance,
 * even if the method is priate or protected
 *
 * @param string $methodName
 * @param array  $args
 * @param null   $instance
 *
 * @return mixed;
 */
public function callMethod($methodName, array $args = array(), $instance = null) {}

Get private or protected property

If $instance is null the method would use $this->_instance instead.

/**
 * Get value of private or protected property
 *
 * @param string $property
 * @param object $instance
 *
 * @return mixed
 */
public function getPrivateProperty($property, $instance = null) {}

Set private or protected property

If $instance is null the method would use $this->_instance instead.

/**
 * Set value of private or protected property
 *
 * @param string $property
 * @param mixed  $value
 * @param object $instance
 *
 * @return mixed
 */
public function setPrivateProperty($property, $value, $instance = null) {}

Build mock objects

With the following methods you can initiate new mock objects. All initiated mock objects would be destroyed automatically after the test.

/**
 * @param string      $class
 * @param string|null $key
 *
 * @return PHPUnit_Framework_MockObject_MockObject
 */
public function buildMock($class, $key = null) {}

/**
 * @param string $class
 *
 * @return \PHPUnit_Framework_MockObject_MockObject
 */
public function buildMockForAbstractClass($class) {}

Get registered mocks

With the second parameter of the method buildMock you can define a key for each mock object you generate during the test. With the following method you are able to get the object by the defined key.

/**
 * @param string $key
 *
 * @return PHPUnit_Framework_MockObject_MockObject
 * @throws TechDivision_MagentoUnitTesting_Exception_TestCase
 */
public function getRegisteredMock($key) {}

Create a proxy object

In some cases it is necessary to build a proxy for an existing mock object. One of those cases may be a final public method within the class you've mocked. The following method returns an instance of TechDivision_MagentoUnitTesting_Helper_Proxy` which dispatches all method invocations to the mock object you used to generate the proxy instance.

Using the proxy helper you are able to disable the invocation of methods and define callbacks which would be executed before or after the original method. You'll find more information in the Proxy Helper Documentation

/**
 * @param string|PHPUnit_Framework_MockObject_MockObject $mock
 *
 * @return TechDivision_MagentoUnitTesting_Helper_Proxy
 */
public function buildProxy($mock) {}

Testing method invocation within a proxy

With the following method you can test, whether a method was invoked during the test.

/**
 * @param string                                       $method
 * @param TechDivision_MagentoUnitTesting_Helper_Proxy $proxy
 * @param array                                        $arguments
 * @param null                                         $count
 *
 * @return void
 */
public function assertMethodInvocationInProxy(
    $method,
    TechDivision_MagentoUnitTesting_Helper_Proxy $proxy,
    $count = null,
    array $arguments = null
) {}

Add magic methods to a mock object

Magento loves magic methods and the __call method is implemented in a lot of classes. Of course these methods will not be mocked by the mock generator and it is necessary to add them afterwards.

With the following method you can easily register new magic methods in your mock object. The return can be a value or a callable.

/**
 * @param PHPUnit_Framework_MockObject_MockObject $mockObject
 * @param string                                  $methodName
 * @param mixed                                   $return
 * @param string                                  $callInterceptionMethodName
 *
 * @return void
 */
public function addCallInterceptorMethod(
    PHPUnit_Framework_MockObject_MockObject $mockObject,
    $methodName,
    $return,
    $callInterceptionMethodName = '__call'
) {}

Testing magic method invocations

With following method you can test, whether a call interceptor method was invoked during the test.

/**
 * @param PHPUnit_Framework_MockObject_MockObject $mockObject
 * @param                                         $methodName
 *
 * @return int
 */
public function getCallInterceptorInvocations(
    PHPUnit_Framework_MockObject_MockObject $mockObject,
    $methodName
) {}

/**
 * @param PHPUnit_Framework_MockObject_MockObject $mockObject
 * @param string                                  $methodName
 * @param string                                  $expectedCount
 *
 * @return void
 */
public function assertCallInterceptorInvocationCount(
    PHPUnit_Framework_MockObject_MockObject $mockObject,
    $methodName,
    $expectedCount
) {}

Helper methods

/**
 * Convert camel case string to underscored
 *
 * @param string $string
 *
 * @return string
 */
public function decamelize($string) {}

/**
 * Convert underscored string to camel case
 *
 * @param string $string
 *
 * @return mixed
 */
public function camelize($string) {}
Clone this wiki locally