/** * Reset CodeIgniter instance */ function reset_instance() { // Reset loaded classes load_class('', '', NULL, TRUE); is_loaded('', TRUE); // Close db connection $CI =& get_instance(); if (isset($CI->db)) { if ($CI->db->dsn !== 'sqlite::memory:' && $CI->db->database !== ':memory:') { $CI->db->close(); $CI->db = null; } else { // Don't close if SQLite in-memory database // If we close it, all tables and stored data will be gone load_class_instance('db', $CI->db); } } // Load core classes load_class('Benchmark', 'core'); load_class('Hooks', 'core'); load_class('Config', 'core'); // load_class('Utf8', 'core'); load_class('URI', 'core'); load_class('Router', 'core'); load_class('Output', 'core'); load_class('Security', 'core'); load_class('Input', 'core'); load_class('Lang', 'core'); CIPHPUnitTest::loadLoader(); // Remove CodeIgniter instance $CI = new CIPHPUnitTestNullCodeIgniter(); }
/** * Get Route including 404 check * * @see core/CodeIgniter.php * * @return array [class, method, pararms] */ public function getRoute() { $RTR =& load_class('Router', 'core'); $URI =& load_class('URI', 'core'); $e404 = FALSE; $class = ucfirst($RTR->class); $method = $RTR->method; if (empty($class) or !file_exists(APPPATH . 'controllers/' . $RTR->directory . $class . '.php')) { $e404 = TRUE; } else { require_once APPPATH . 'controllers/' . $RTR->directory . $class . '.php'; if (!class_exists($class, FALSE) or $method[0] === '_' or method_exists('CI_Controller', $method)) { $e404 = TRUE; } elseif (method_exists($class, '_remap')) { $params = array($method, array_slice($URI->rsegments, 2)); $method = '_remap'; } elseif (!in_array(strtolower($method), array_map('strtolower', get_class_methods($class)), TRUE)) { $e404 = TRUE; } } if ($e404) { // If 404, CodeIgniter instance is not created yet. So create it here. // Because we need CI->output->_status $CI =& get_instance(); if ($CI instanceof CIPHPUnitTestNullCodeIgniter) { CIPHPUnitTest::createCodeIgniterInstance(); } show_404($RTR->directory . $class . '/' . $method . ' is not found'); } if ($method !== '_remap') { $params = array_slice($URI->rsegments, 2); } return [$class, $method, $params]; }
protected static function replaceLoader() { require __DIR__ . '/replacing/core/Loader.php'; $my_loader_file = APPPATH . 'core/' . config_item('subclass_prefix') . 'Loader.php'; if (file_exists($my_loader_file)) { self::$loader_class = config_item('subclass_prefix') . 'Loader'; require $my_loader_file; } self::loadLoader(); }
/** * Reset CodeIgniter instance and assign new CodeIgniter instance as $this->CI */ public function resetInstance() { reset_instance(); if (!CIPHPUnitTest::wiredesignzHmvcInstalled()) { new CI_Controller(); } else { new CI(); new MX_Controller(); } $this->CI =& get_instance(); }
public static function init() { // Fix CLI args $_server_backup = $_SERVER; $_SERVER['argv'] = ['index.php', '_dummy/_dummy']; $_SERVER['argc'] = 2; require __DIR__ . '/CIPHPUnitTestCase.php'; require __DIR__ . '/CIPHPUnitTestRequest.php'; require __DIR__ . '/CIPHPUnitTestDouble.php'; require __DIR__ . '/exceptions/CIPHPUnitTestRedirectException.php'; require __DIR__ . '/exceptions/CIPHPUnitTestShow404Exception.php'; require __DIR__ . '/exceptions/CIPHPUnitTestShowErrorException.php'; // Replace a few Common functions require __DIR__ . '/replacing/core/Common.php'; require BASEPATH . 'core/Common.php'; // Load new functions of CIPHPUnitTest require __DIR__ . '/functions.php'; // Replace Loader require __DIR__ . '/replacing/core/Loader.php'; $my_loader_file = APPPATH . 'core/' . config_item('subclass_prefix') . 'Loader.php'; if (file_exists($my_loader_file)) { self::$loader_class = config_item('subclass_prefix') . 'Loader'; require $my_loader_file; } self::loadLoader(); // Load autoloader for CIPHPUnitTest require __DIR__ . '/autoloader.php'; // Change current directroy chdir(FCPATH); /* * -------------------------------------------------------------------- * LOAD THE BOOTSTRAP FILE * -------------------------------------------------------------------- * * And away we go... */ try { // Request to 404 route // This is needed for not to call Welcome::index() // If controller Welcome is called in bootstrap, we can't test // the same name sub controller Welcome even when we use // `@runInSeparateProcess` and `@preserveGlobalState disabled` ob_start(); require_once BASEPATH . 'core/CodeIgniter.php'; ob_end_clean(); } catch (CIPHPUnitTestShow404Exception $e) { // Catch 404 exception new CI_Controller(); } require APPPATH . '/tests/TestCase.php'; // Restore $_SERVER $_SERVER = $_server_backup; }
/** * Reset CodeIgniter instance */ function reset_instance() { // Reset loaded classes load_class('', '', NULL, TRUE); is_loaded('', TRUE); // Load core classes load_class('Benchmark', 'core'); load_class('Hooks', 'core'); load_class('Config', 'core'); // load_class('Utf8', 'core'); load_class('URI', 'core'); load_class('Router', 'core'); load_class('Output', 'core'); load_class('Security', 'core'); load_class('Input', 'core'); load_class('Lang', 'core'); CIPHPUnitTest::loadLoader(); }
/** * Reset CodeIgniter instance */ function reset_instance() { // Reset loaded classes load_class('', '', NULL, TRUE); is_loaded('', TRUE); // Reset config functions reset_config(); // Close db connection $CI =& get_instance(); if (isset($CI->db)) { if ($CI->db->dsn !== 'sqlite::memory:' && $CI->db->database !== ':memory:') { $CI->db->close(); $CI->db = null; } else { // Don't close if SQLite in-memory database // If we close it, all tables and stored data will be gone load_class_instance('db', $CI->db); } } // Load core classes $BM =& load_class('Benchmark', 'core'); CIPHPUnitTestSuperGlobal::set_Global('BM', $BM); $EXT =& load_class('Hooks', 'core'); CIPHPUnitTestSuperGlobal::set_Global('EXT', $EXT); $CFG =& load_class('Config', 'core'); CIPHPUnitTestSuperGlobal::set_Global('CFG', $CFG); $UNI =& load_class('URI', 'core'); CIPHPUnitTestSuperGlobal::set_Global('UNI', $UNI); // $URI =& load_class('Utf8', 'core'); // CIPHPUnitTestSuperGlobal::set_Global('URI', $URI); $RTR =& load_class('Router', 'core'); CIPHPUnitTestSuperGlobal::set_Global('RTR', $RTR); $OUT =& load_class('Output', 'core'); CIPHPUnitTestSuperGlobal::set_Global('OUT', $OUT); $SEC =& load_class('Security', 'core'); CIPHPUnitTestSuperGlobal::set_Global('SEC', $SEC); $IN =& load_class('Input', 'core'); CIPHPUnitTestSuperGlobal::set_Global('IN', $IN); $LANG =& load_class('Lang', 'core'); CIPHPUnitTestSuperGlobal::set_Global('LANG', $LANG); CIPHPUnitTest::loadLoader(); // Remove CodeIgniter instance $CI = new CIPHPUnitTestNullCodeIgniter(); }
/** * Reset CodeIgniter instance */ function reset_instance() { // Close db connection $CI =& get_instance(); if (isset($CI->db)) { $CI->db->close(); $CI->db = null; } // Reset loaded classes load_class('', '', NULL, TRUE); is_loaded('', TRUE); // Load core classes load_class('Benchmark', 'core'); load_class('Hooks', 'core'); load_class('Config', 'core'); // load_class('Utf8', 'core'); load_class('URI', 'core'); load_class('Router', 'core'); load_class('Output', 'core'); load_class('Security', 'core'); load_class('Input', 'core'); load_class('Lang', 'core'); CIPHPUnitTest::loadLoader(); }
// EXIT_CONFIG } define('APPPATH', BASEPATH . $application_folder . DIRECTORY_SEPARATOR); } // The path to the "views" folder if (!is_dir($view_folder)) { if (!empty($view_folder) && is_dir(APPPATH . $view_folder . DIRECTORY_SEPARATOR)) { $view_folder = APPPATH . $view_folder; } elseif (!is_dir(APPPATH . 'views' . DIRECTORY_SEPARATOR)) { header('HTTP/1.1 503 Service Unavailable.', TRUE, 503); echo 'Your view folder path does not appear to be set correctly. Please open the following file and correct this: ' . SELF; exit(3); // EXIT_CONFIG } else { $view_folder = APPPATH . 'views'; } } if (($_temp = realpath($view_folder)) !== FALSE) { $view_folder = $_temp . DIRECTORY_SEPARATOR; } else { $view_folder = rtrim($view_folder, '/\\') . DIRECTORY_SEPARATOR; } define('VIEWPATH', $view_folder); /* * ------------------------------------------------------------------- * Added for CI PHPUnit Test * ------------------------------------------------------------------- */ require __DIR__ . '/_ci_phpunit_test/CIPHPUnitTest.php'; CIPHPUnitTest::init();
protected function createAndCallController($class, $method, $params) { ob_start(); $this->callHook('pre_controller'); // Run callablePreConstructor if ($this->callablePreConstructors !== []) { foreach ($this->callablePreConstructors as $callable) { $callable(); } } // Create controller if (CIPHPUnitTest::wiredesignzHmvcInstalled()) { new CI(); } $controller = new $class(); $CI =& get_instance(); // Set CodeIgniter instance to TestCase $this->testCase->setCI($CI); // Set default response code 200 set_status_header(200); // Run callable if ($this->callables !== []) { foreach ($this->callables as $callable) { $callable($CI); } } $this->callHook('post_controller_constructor'); // Call controller method call_user_func_array([$controller, $method], $params); $this->callHook('post_controller'); if ($this->callHook('display_override') === false) { $CI->output->_display(); } $output = ob_get_clean(); if ($output == '') { $output = $CI->output->get_output(); } return $output; }
/** * Reference to the CI_Controller method. * * Returns current CI instance object * * @return object * * modified by ci-phpunit-test */ function &get_instance() { if (!CIPHPUnitTest::wiredesignzHmvcInstalled()) { return CI_Controller::get_instance(); } else { return CI::$APP; } }
<?php /** * Part of ci-phpunit-test * * @author Kenji Suzuki <https://github.com/kenjis> * @license MIT License * @copyright 2015 Kenji Suzuki * @link https://github.com/kenjis/ci-phpunit-test */ // Autoloader for ci-phpunit-test require __DIR__ . '/CIPHPUnitTestAutoloader.php'; require __DIR__ . '/CIPHPUnitTestFileCache.php'; $cache = new CIPHPUnitTestFileCache(__DIR__ . '/tmp/cache/autoload.php'); $autoload_dirs = CIPHPUnitTest::getAutoloadDirs(); $autoloader = new CIPHPUnitTestAutoloader($cache, $autoload_dirs); spl_autoload_register([$autoloader, 'load']); // Register CodeIgniter's tests/mocks/autoloader.php define('SYSTEM_PATH', BASEPATH); require APPPATH . 'tests/mocks/autoloader.php'; spl_autoload_register('autoload');
/** * Reset CodeIgniter instance and assign new CodeIgniter instance as $this->CI */ public function resetInstance() { reset_instance(); CIPHPUnitTest::createCodeIgniterInstance(); $this->CI =& get_instance(); }