Skip to content
This repository has been archived by the owner on Nov 26, 2017. It is now read-only.

Latest commit

 

History

History
66 lines (55 loc) · 1.87 KB

testing.md

File metadata and controls

66 lines (55 loc) · 1.87 KB

Testing

This document is about creating tests for the Joomla Framework.

PHP Unit Tests

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.

Accessing and Invoking Protected Properties and Methods

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"'
	);
}