-
Notifications
You must be signed in to change notification settings - Fork 35
How to write a test
The correct location for your tests is in the directory dev/tests
. There you will find some folders and the phpunit-phar
file. Create a new testsuite with your own namespace within the test type directory depending on the kind of test you
want to write.
Example:
dev/tests/unit/testsuite/MyCompany/MyModule
We recommend to use an identical folder structure within your testsuite as within the codepool of your module. A possible structure for our example module could be the following:
src
|-- app
| `-- code
| `-- community
| `-- MyCompany
| `-- MyModule
| |-- Model
| | |-- Sales
| | | `-- Order.php
| | `-- Observer.php
| `-- Helper
| |-- Config.php
| `-- Data.php
`-- dev
`-- tests
`-- unit
`-- testsuite
`-- MyCompany
`-- MyModule
|-- Model
| |-- Sales
| | `-- OrderTest.php
| `-- ObserverTest.php
`-- Helper
|-- ConfigTest.php
`-- DataTest.php
That way it is easier to maintain your tests and keep them up to date.
Create a new php file. As shown in the example above the name of your test case file should be the same as the class you want to test suffixed by the word "Test". According to the file name you have to suffix also the class name.
To get access to the magic which the module provides you have to extend your test class from
a test case class. All given test case classes are located at
src/dev/tests/unit/framework/TechDivision/MagentoUnitTesting/TestCase
.
There are already some test cases which support you writing your tests and we are working hard to extend the
framework. Feel free to fork the repository, implement new test cases and submit a pull request!
Code for the OrderTest.php
class MyCompany_MyModule_Unit_Model_Sales_OrderTest
extends TechDivision_MagentoUnitTesting_TestCase_Model
{
}
The abstract test case will init a mock of the static Mage class and some other Magento classes you need to simulate your code which runs inside a Magento instance. In order to accomplish this the test has to implement some properties and in some cases methods.
If the class you want to tests hasn't a constructor method you just need to implement the $_testClassName
property
which defines the name of the class. The framework will automatically init the class before each execution of a test
method.
Optional you could overwrite the $_instance
property to let your IDE know the instance the test case works with.
class MyCompany_MyModule_Unit_Model_Sales_OrderTest
extends TechDivision_MagentoUnitTesting_TestCase_Model
{
/**
* @var string
*/
protected $_testClassName = 'MyCompany_MyModule_Model_Sales_Order';
/**
* @var MyCompany_MyModule_Model_Sales_Order
*/
protected $_instance;
}
If the class you want to test has a method invocation or object initialization within the constructor you may need to
register some mocks in the mocked Mage class before the initialization of the class. In order to do that there is a
protected method _beforeInitInstance
you have to override in your test.
Here is an example implementation which initializes a mock of a helper and registers it to the Mage mock as 'sales' helper:
/**
* Override that method to define environment for instance construction
* e.g. Mage::helper in __construct method of your class
*
* @return void
*/
protected function _beforeInitInstance()
{
$salesHelperMock = $this->buildMock('Mage_Sales_Helper_Data');
$this->addMageHelper('sales', $salesHelperMock);
}
With the $_testClassInitArguments
property you can define an array with arguments which will be passed to the
constructor. It depends on the class you extend by your test how the arguments will be passed to the instance. In
Test Cases documentation you will find more information.
In some cases it could be necessary to disable the initialization of the class you want to test by default. You can do
that by setting the $_initInstanceInTestSetup
property to false
. With the _initInstance()
method you can invoke
the initialization manually or manipulate the process by overwriting the method.