Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if (!defined('BASEPATH'))
- exit('No direct script access allowed');
- /**
- * Unit test controller
- *
- * @package SimpleHostingPanel
- * @subpackage Controllers
- * @author kimse
- * @property $shp_users Shp_users
- */
- class Unit_test extends CI_Controller {
- var $classes = array(
- 'Please select a class',
- 'Shp_auth',
- 'Shp_authz',
- 'Shp_base',
- 'Shp_groups',
- 'Shp_msg',
- 'Shp_profile',
- 'Shp_users'
- );
- var $class_methods = array(
- 'Please select a class method'
- );
- var $class_method_params = array();
- var $selected_class = 0;
- var $selected_method = 0;
- var $test_result = "";
- /**
- * Loads selected class
- */
- public function __construct() {
- parent::__construct();
- $this->load->helper(array('form', 'url'));
- $this->load->library(array('unit_test', 'form_validation', 'session'));
- $this->selected_class = (int) $this->input->post('selected_class'); // Select the class
- $this->selected_method = (int) $this->input->post('selected_method'); // Select the class method
- // Save the class and method selection in a flash session variable
- $this->session->set_flashdata('last_selected_class', $this->selected_class);
- $this->session->set_flashdata('last_selected_method', $this->selected_method);
- // Reset method selection, if selected class changes
- if ($this->session->flashdata('last_selected_class') != $this->selected_class)
- $this->selected_method = 0; // class changed, reset method selection
- // Get the selected class methods
- if ($this->selected_class > 0)
- $this->_set_class_methods($this->selected_class);
- // Get the selected class's methods
- if ($this->selected_class > 0 && $this->selected_method > 0)
- $this->_set_method_params($this->selected_class, $this->selected_method);
- }
- /**
- * Update method list, with selected class methods
- * @param string $class
- */
- public function _set_class_methods($class) {
- $class_methods = get_class_methods($this->classes[$class]);
- $exclude = array('__construct', $this->class_methods[0]);
- $methods = array();
- foreach ($class_methods as $value) {
- if (!in_array($value, $exclude)) {
- $methods[] = $value;
- }
- }
- $this->class_methods = array_merge($this->class_methods, $methods);
- }
- /**
- * Update method params list, with selected method
- * @param string $class
- * @param string $method
- */
- public function _set_method_params($class, $method) {
- $params = array();
- $r = new ReflectionMethod($this->classes[$class], $this->class_methods[$method]);
- foreach ($r->getParameters() as $param) {
- //$param is an instance of ReflectionParameter
- $params[] = array(
- 'name' => $param->getName(),
- 'optional' => $param->isOptional(),
- 'type' => ($param->isArray() ? 'array' : 'string')
- );
- }
- $this->class_method_params = array_merge($this->class_method_params, $params);
- }
- /**
- * Unit testing UI
- */
- public function index() {
- if ($this->input->post('expected_result') != "" || $this->input->post('expected_types') != "") {
- // Get expected result
- if ($this->input->post('expected_result') != "")
- $expected_result = $this->input->post('expected_result');
- else
- $expected_result = $this->input->post('expected_types');
- // Get class names
- $class_name = $this->classes[$this->input->post('selected_class')];
- $method_name = $this->class_methods[$this->input->post('selected_method')];
- $test_name = $this->input->post('test_name') != "" ? $this->input->post('test_name') : 'Test: ' . $class_name . '::' . $method_name;
- // Get method parameters
- $class_method_params = $this->class_method_params;
- $input_params = array();
- foreach ($class_method_params as $class_method_param) {
- $input_params[] = $this->input->post($class_method_param['name']);
- }
- // Call method and get result
- $method_return = call_user_func_array(array(new $class_name(), $method_name), $input_params);
- // Run unit test
- $this->test_result = $this->unit->run($method_return, $expected_result, $test_name);
- }
- $this->load->view('unit_test/unit_test_index', $this);
- }
- }
- /* End of file unit_test.php */
- /* Location: ./application/controllers/unit_test.php */
Add Comment
Please, Sign In to add comment