📜 ⬆️ ⬇️

Moving from SimpleTest to PHPUnit

Background: One of the critical parts of the project code is covered by unit tests based on the SimpleTest framework. In connection with the transition to PHPUnit, it was necessary to adapt existing tests for a new test framework.
And it was necessary to leave the running tests in SimpleTest mode, well, and get them to work in PHPUnit. The code for the tests themselves is naturally the same.

Three key points were highlighted where there are differences between SimpleTest and PHPUnit:



Which framework should work is defined very simply, if the constant PHPUNITRUN == true, then PHPUnit works, otherwise SimpleTest
')

1. Run tests


Consider the differences with if and put the results in $ reporter:
if (PHPUNITRUN) { // PHPUnit run
$testSuite = new PHPUnit_Framework_TestSuite( $testCase );
$testSuite->addTestSuite( $testCase );
$reporter = PHPUnit_TextUI_TestRunner::run( $testSuite );
}
else { // SimpleTest run
$testSuite = new TestSuite();
$testSuite->addTestClass( $testCase );
$testSuite->run( $reporter );
}


2. Used methods of checking and inheritance


Each framework requires test cases to be inherited from specific base classes.
This distinction is solved as follows, at the same time the differences between the name of the verification methods are taken into account. For example, in PHPUnit, the assertIsA ($ actual, $ expected, $ message = '') method sounds like assertType ($ expected, $ actual, $ message) :
if ( PHPUNITRUN ) {

class Overload_TestCase extends PHPUnit_Framework_TestCase {

protected $backupGlobals = false;

public static function assertIsA($actual, $expected, $message = '')
{
self::assertType($expected, $actual, $message);
}

public static function assertNotA($actual, $expected, $message = '')
{
self::assertNotType($expected, $actual, $message);
}

public static function assertTrue($condition, $message = '')
{
parent::assertTrue((bool)$condition, $message);
}

public static function assertFalse($condition, $message = '')
{
parent::assertFalse((bool)$condition, $message);
}

public static function assertEqual($expected, $actual, $message = '')
{
self::assertEquals($expected, $actual, $message);
}
}
}
else {
class Overload_TestCase extends UnitTestCase {
}
}


All test cases of the project are inherited from the Overload_TestCase class. Thus, each test case is inherited from the base class required for a particular framework.

3. Processing test results
Test results are stored in the $ reporter object.
For further processing of the results (saving to the database, sending letters, if problems are identified, etc.), the standard HtmlReporter from SimpleTest is brought to the PHPUnit_Framework_TestResult interface from PHPUnit.
class NewHtmlReporter extends HtmlReporter
{
/**
* Gets the number of detected errors.
*
* @return integer
*/
public function errorCount()
{
return $this->_exceptions;
}

/**
* Gets the number of detected failures.
*
* @return integer
*/
public function failureCount()
{
return $this->_fails;
}

/**
* Gets the number of run tests.
*
* @return integer
*/
public function count()
{
return $this->_passes;
}
};


Now tests can be run and processed both in SimpleTest mode and in PHPUnit mode.

Source: https://habr.com/ru/post/55176/


All Articles