Moving from SimpleTest to PHPUnit

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

    Three key points were highlighted where there are differences between SimpleTest and PHPUnit:
    • 1. Running tests
    • 2. Verification methods used and inheritance
    • 3. Processing test results



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

    1. Running tests


    We take into account 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. Verification methods used and inheritance


    Each framework requires that test cases be inherited from the specific base classes.
    This difference 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) : All project test cases 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 subsequent processing of the results (saving to the database, sending letters, if problems are identified, etc.), standard
    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 {
    }
    }






    The htmlReporter from SimpleTest is cast to the PHPUnit_Framework_TestResult interface from PHPUnit. Now tests can be run and processed both in SimpleTest mode and in PHPUnit mode.
    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;
         }
    };



    Also popular now: