DIR : /home/kozerus/public_html/dgs/scripts/tests/DgsUnitTest.php

/home/kozerus/public_html/dgs/scripts/tests

<?php
/*
Dragon Go Server
Copyright (C) 2001-  Jens-Uwe Gaspar

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/* Author: Jens-Uwe Gaspar */

require_once 'DgsUnitTest.php';

define('DUT_LOG_OK', 0x01);


/*!
 * \brief Base class for unit tests.
 *
 * \note replacement for phpunit not working for PHP 5.4
 */
class DgsUnitTest {

   private $test_class;
   private $test_methods;

   public function __construct() {
      $this->test_class = new ReflectionClass( $this );
      $this->test_methods = array();
      $reflection_methods = $this->test_class->getMethods( ReflectionMethod::IS_PUBLIC );
      foreach ( $reflection_methods as $test_method ) {
         if ( strpos($test_method->name, 'test_') !== false )
            array_push( $this->test_methods, $test_method );
      }
   }

   /**
    * \param $flags flags to control test run: DUT_LOG_OK
    */
   public function runTests( $flags = 0 ) {
      $count_tests = 0;
      $count_ok = 0;
      $class_name = $this->test_class->getName();

      printf("\n[%s] start tests:\n", $class_name);

      foreach ( $this->test_methods as $test_method ) {
         $test_name = $test_method->name;
         $count_tests++;

         try {
            $this->setUp();
            $test_method->invoke( $this );
            $count_ok++;
            if ( $flags & DUT_LOG_OK )
               printf("    OK   - %s\n", $test_name);
         } catch (Exception $ex) {
            printf("    FAIL - %s: %s\n", $test_name, $ex->getMessage());
         }
      }

      $success = ($count_ok == $count_tests);
      $test_pass = ($success) ? 'PASS' : 'FAIL';
      printf("[%s] %s/%s tests ok -> %s\n", $class_name, $count_ok, $count_tests, $test_pass);

      return array( $success, $count_ok, $count_tests );
   }//runTests

   /** Sets up the fixture. */
   protected function setUp() {
   }

   protected function assertEquals( $expected, $actual, $msg = '' ) {
      $asserted = $this->equals( $expected, $actual );
      $this->handle_assertion( $asserted, $expected, $actual, $msg );
   }

   protected function assertNotEquals( $expected, $actual, $msg = '' ) {
      $asserted = !$this->equals( $expected, $actual );
      $this->handle_assertion( $asserted, $expected, $actual, $msg );
   }

   protected function assertSame( $expected, $actual, $msg = '' ) {
      $asserted = ( $expected === $actual );
      $this->handle_assertion( $asserted, $expected, $actual, $msg );
   }

   protected function assertTrue( $actual, $msg = '' ) {
      $asserted = $this->equals( true, $actual );
      $this->handle_assertion( $asserted, true, $actual, $msg );
   }

   protected function assertFalse( $actual, $msg = '' ) {
      $asserted = $this->equals( false, $actual );
      $this->handle_assertion( $asserted, false, $actual, $msg );
   }

   protected function assertNull( $actual, $msg = '' ) {
      $asserted = $this->equals( null, $actual );
      $this->handle_assertion( $asserted, null, $actual, $msg );
   }

   protected function assertNotNull( $actual, $msg = '' ) {
      $asserted = !$this->equals( null, $actual );
      $this->handle_assertion( $asserted, '!null', $actual, $msg );
   }

   private function handle_assertion( $assertion_true, $expected, $actual, $msg ) {
      if ( is_array($expected) )
         $expected = print_r($expected, true);
      if ( is_array($actual) )
         $actual = print_r($actual, true);
      if ( !$assertion_true ) {
         throw new Exception(
            sprintf("%sAssertion failed: expected [%s], actual [%s]",
               ($msg ? "$msg: " : ''), $expected, $actual) );
      }
   }

   private function equals( $expected, $actual ) {
      if ( is_null($expected) )
         $result = is_null($actual);
      elseif ( is_numeric($expected) )
         $result = ( $expected == $actual );
      elseif ( is_string($expected) )
         $result = ( strcmp($expected, $actual) == 0 );
      elseif ( is_bool($expected) )
         $result = ( (bool)$expected == (bool)$actual );
      else // array | object
         $result = ( strcmp( print_r($expected, true), print_r($actual, true) ) == 0 );

      return $result;
   }

}// end of 'DgsUnitTest'

?>
//


koh5_pano



Your browser does not support the HTML5 canvas element.


Drag mouse to navigate.

Navigation





17.Aug.2010, Martin Wengenmayer