Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.36 KB | None | 0 0
  1. <?php
  2. /**
  3.  * System exceptions
  4.  */
  5. class Assert_Exception extends Exception {}
  6. class Php_Error_Exception extends Exception {}
  7. /**
  8.  * Parent class for test's classes
  9.  */
  10. class TestCase {
  11.   protected function assertEq($tested, $sample, $mode = true) {
  12.     if ($mode) {
  13.       if ($tested !== $sample) {
  14.         throw new Assert_Exception;
  15.       } else {
  16.         return true;
  17.       }
  18.     } else {
  19.       return $tested === $sample;
  20.     }
  21.   }
  22. }
  23. /**
  24.  * Tests class with public methods, names must started from 'test'
  25.  * Constructor not tested in this version
  26.  */
  27. class SomeTests extends TestCase {
  28.   public function testOne() {
  29.     $this->assertEq(1,2);
  30.   }
  31.   public function testTwo() {
  32.     $this->assertEq(1,1);
  33.   }
  34.   public function testThree() {
  35.     $a[] = $a['dsd'];
  36.   }
  37. }
  38. /**
  39.  * Test engine and test-results storage
  40.  * Return array with results or message
  41.  */
  42. class Test_Runner {
  43.   private $_results = array();
  44.   private $_ready   = false;
  45.   private function _test_method($instance, $method_name) {
  46.     try {
  47.       set_error_handler(function($c,$m) {throw new Php_Error_Exception($m,$c);}, E_ALL);
  48.       $instance->{$method_name}();
  49.       restore_error_handler();
  50.       return array('test' => 'Passed');
  51.     } catch (Php_Error_Exception $e) {
  52.       return array('test' => 'Failed on php error', 'exception' => $e);
  53.     } catch (Assert_Exception $e) {
  54.       return array('test' => 'Failed on assertion', 'exception' => $e);
  55.     } catch (Exception $e) {
  56.       return array('test' => 'Failed on unexpected exception', 'exception' => $e);
  57.     }
  58.   }
  59.   public function getResults() {
  60.     if ($this->_ready) {
  61.       return $this->_results;
  62.     } else {
  63.       return 'Not ready';
  64.     }
  65.   }
  66.   function __construct($instance) {
  67.     $methods = get_class_methods($instance);
  68.     if (!empty($methods)) {
  69.      $filtered = array_filter($methods, function($meth) {return preg_match("/^test.*/U", $meth) ? true : false;});
  70.       if (!empty($filtered)) {
  71.         foreach ($filtered as $method) {
  72.           $this->_results[$method] = $this->_test_method($instance, $method);
  73.         }
  74.         $this->_ready = true;
  75.         unset($instance);
  76.       } else {
  77.         $this->_results = 'No valid test names';
  78.       }
  79.     } else {
  80.        $this->_results = 'No methods to test';
  81.     }
  82.   }
  83. }
  84. $run = new Test_Runner(new SomeTests());
  85. print_r($run->getResults());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement