This document is about creating tests for the Joomla Framework.
This section provides some useful information about creating PHP unit tests.
Note
Unit tests should always observe the Joomla Framework Coding Standards. Ensure the code is well formatted and that full DocBlocks are provided. Most importantly, ensure the @since tag reflects the version the test was added. (Not necessary for tests stubs that are marked as incomplete.
Starting in PHP 5 there are facilities for reverse engineering classes and objects which now make it much easier to test protected methods and to modify and verify the values of protected properties and methods. The Joomla! test infrastructure includes a helper class that can be used to read, modify and invoke protected and private properties and methods.
/**
* Tests the SomeClass::__construct method
*
* @return void
*
* @since 11.3
*/
public function test__construct()
{
$object = new SomeClass('foo');
// For the purposes of this example, we are assuming that 'name' is a protected class property
// that is set by an argument passed into the class constructor.
$this->assertThat(
TestReflection::getValue($object, 'name'),
$this->equalTo('foo'),
'Tests that the protected name property is set by the constructor.'
);
}
To gain access to a protected method you might add a method like the
following example to your inspector class:
/**
* Test that calls a protected method called 'hidden'
*
* @return void
*
* @since 11.3
*/
public function hidden()
{
$object = new SomeClass('foo');
$this->assertThat(
TestReflection::invoke($object, 'hidden', 'arg1'),
$this->equalTo('result1'),
'This test asserts that $object->hidden("arg1") will return "result1"'
);
}