PHPUnit – Abstrakte Testhelper Klasse

Momentan beschäftige ich mich unter anderem mal wieder mit PHPUnit. Und nach mehreren Tests, wurde es mal wieder Zeit für ne abstrakte Testhelper Klasse als Basis für meine Tests. Hoffe es hilft.

[code]
_object = new $this->_className;
}

/* TEST HELPER METHODS */
/**
* helps to doing getter and setter tests
*
* @param string $methodNamesTail
* @param mixed $testValue
*/
protected function _getSetTestWrapper($methodNamesTail, $testValue)
{
$getterName = ‚get‘ . $methodNamesTail;
$setterName = ’set‘ . $methodNamesTail;

$this->_object->{$setterName}($testValue);
$this->assertEquals($testValue, $this->_object->{$getterName}());
}

/* CUSTOM ASSERTION METHODS */
/**
* custom assert instance of method
*
* @param string $classToCheckAgainst
* @param object $instanceToCheck
*/
public function assertInstanceOf($classToCheckAgainst, $instanceToCheck)
{
if (function_exists(‚parent::assertInstanceOf‘)) {
parent::assertInstanceOf($classToCheckAgainst, $instanceToCheck);
} else if (!($instanceToCheck instanceof $classToCheckAgainst)) {
$this->fail(‚Failed asserting that <' . get_class($instanceToCheck) . '> is an instance of class „‚ .
$classToCheckAgainst
. ‚“.‘);
}
}
}

class ChildTest extends TestCaseAbstract
{
/**
* @see TestCaseAbstract::_className
*/
protected $_className = ‚ChildTest‘;

/* TEST METHODS */
/**
* test getter and setter of genuine id
*/
public function testGetSetGenuineId()
{
$this->_getSetTestWrapper(‚GenuineId‘, 1);
}

/**
* test getter and setter of matched id
*/
public function testGetSetMatchedId()
{
$this->_getSetTestWrapper(‚MatchedId‘, 1);
}
}
[/code]

GD Star Rating
loading...

Kommentar verfassen