Guest User

Untitled

a guest
Jun 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.80 KB | None | 0 0
  1. <?php
  2.  
  3. if (!defined('BASEPATH'))
  4.     exit('No direct script access allowed');
  5.  
  6. /**
  7.  * Unit test controller
  8.  *
  9.  * @package SimpleHostingPanel
  10.  * @subpackage Controllers
  11.  * @author kimse
  12.  * @property $shp_users Shp_users
  13.  */
  14. class Unit_test extends CI_Controller {
  15.  
  16.     var $classes = array(
  17.         'Please select a class',
  18.         'Shp_auth',
  19.         'Shp_authz',
  20.         'Shp_base',
  21.         'Shp_groups',
  22.         'Shp_msg',
  23.         'Shp_profile',
  24.         'Shp_users'
  25.     );
  26.     var $class_methods = array(
  27.         'Please select a class method'
  28.     );
  29.     var $class_method_params = array();
  30.     var $selected_class = 0;
  31.     var $selected_method = 0;
  32.     var $test_result = "";
  33.  
  34.     /**
  35.      * Loads selected class
  36.      */
  37.     public function __construct() {
  38.         parent::__construct();
  39.         $this->load->helper(array('form', 'url'));
  40.         $this->load->library(array('unit_test', 'form_validation', 'session'));
  41.  
  42.         $this->selected_class = (int) $this->input->post('selected_class'); // Select the class
  43.         $this->selected_method = (int) $this->input->post('selected_method'); // Select the class method
  44.         // Save the class and method selection in a flash session variable
  45.         $this->session->set_flashdata('last_selected_class', $this->selected_class);
  46.         $this->session->set_flashdata('last_selected_method', $this->selected_method);
  47.  
  48.         // Reset method selection, if selected class changes
  49.         if ($this->session->flashdata('last_selected_class') != $this->selected_class)
  50.             $this->selected_method = 0; // class changed, reset method selection
  51.            
  52.         // Get the selected class methods
  53.         if ($this->selected_class > 0)
  54.             $this->_set_class_methods($this->selected_class);
  55.         // Get the selected class's methods
  56.         if ($this->selected_class > 0 && $this->selected_method > 0)
  57.             $this->_set_method_params($this->selected_class, $this->selected_method);
  58.     }
  59.  
  60.     /**
  61.      * Update method list, with selected class methods
  62.      * @param string $class
  63.      */
  64.     public function _set_class_methods($class) {
  65.         $class_methods = get_class_methods($this->classes[$class]);
  66.         $exclude = array('__construct', $this->class_methods[0]);
  67.         $methods = array();
  68.         foreach ($class_methods as $value) {
  69.             if (!in_array($value, $exclude)) {
  70.                 $methods[] = $value;
  71.             }
  72.         }
  73.         $this->class_methods = array_merge($this->class_methods, $methods);
  74.     }
  75.  
  76.     /**
  77.      * Update method params list, with selected method
  78.      * @param string $class
  79.      * @param string $method
  80.      */
  81.     public function _set_method_params($class, $method) {
  82.         $params = array();
  83.         $r = new ReflectionMethod($this->classes[$class], $this->class_methods[$method]);
  84.         foreach ($r->getParameters() as $param) {
  85.             //$param is an instance of ReflectionParameter
  86.             $params[] = array(
  87.                 'name' => $param->getName(),
  88.                 'optional' => $param->isOptional(),
  89.                 'type' => ($param->isArray() ? 'array' : 'string')
  90.             );
  91.         }
  92.         $this->class_method_params = array_merge($this->class_method_params, $params);
  93.     }
  94.  
  95.     /**
  96.      * Unit testing UI
  97.      */
  98.     public function index() {
  99.  
  100.         if ($this->input->post('expected_result') != "" || $this->input->post('expected_types') != "") {
  101.  
  102.             // Get expected result
  103.             if ($this->input->post('expected_result') != "")
  104.                 $expected_result = $this->input->post('expected_result');
  105.             else
  106.                 $expected_result = $this->input->post('expected_types');
  107.  
  108.  
  109.             // Get class names
  110.             $class_name = $this->classes[$this->input->post('selected_class')];
  111.             $method_name = $this->class_methods[$this->input->post('selected_method')];
  112.  
  113.  
  114.             $test_name = $this->input->post('test_name') != "" ? $this->input->post('test_name') : 'Test: ' . $class_name . '::' . $method_name;
  115.  
  116.             // Get method parameters
  117.             $class_method_params = $this->class_method_params;
  118.             $input_params = array();
  119.             foreach ($class_method_params as $class_method_param) {
  120.                 $input_params[] = $this->input->post($class_method_param['name']);
  121.             }
  122.  
  123.             // Call method and get result
  124.             $method_return = call_user_func_array(array(new $class_name(), $method_name), $input_params);
  125.  
  126.             // Run unit test
  127.             $this->test_result = $this->unit->run($method_return, $expected_result, $test_name);
  128.         }
  129.  
  130.  
  131.         $this->load->view('unit_test/unit_test_index', $this);
  132.     }
  133.  
  134. }
  135.  
  136. /* End of file unit_test.php */
  137. /* Location: ./application/controllers/unit_test.php */
Add Comment
Please, Sign In to add comment