summaryrefslogtreecommitdiffstats
path: root/tests/mocks/core
diff options
context:
space:
mode:
Diffstat (limited to 'tests/mocks/core')
-rw-r--r--tests/mocks/core/benchmark.php3
-rw-r--r--tests/mocks/core/common.php171
-rw-r--r--tests/mocks/core/input.php31
-rw-r--r--tests/mocks/core/loader.php30
-rw-r--r--tests/mocks/core/security.php30
-rw-r--r--tests/mocks/core/uri.php25
-rw-r--r--tests/mocks/core/utf8.php27
7 files changed, 317 insertions, 0 deletions
diff --git a/tests/mocks/core/benchmark.php b/tests/mocks/core/benchmark.php
new file mode 100644
index 000000000..d92be21db
--- /dev/null
+++ b/tests/mocks/core/benchmark.php
@@ -0,0 +1,3 @@
+<?php
+
+class Mock_Core_Benchmark extends CI_Benchmark {} \ No newline at end of file
diff --git a/tests/mocks/core/common.php b/tests/mocks/core/common.php
new file mode 100644
index 000000000..e1c493aa0
--- /dev/null
+++ b/tests/mocks/core/common.php
@@ -0,0 +1,171 @@
+<?php
+
+// Set up the global CI functions in their most minimal core representation
+
+if ( ! function_exists('get_instance'))
+{
+ function &get_instance()
+ {
+ $test = CI_TestCase::instance();
+ $instance = $test->ci_instance();
+ return $instance;
+ }
+}
+
+// --------------------------------------------------------------------
+
+if ( ! function_exists('get_config'))
+{
+ function &get_config() {
+ $test = CI_TestCase::instance();
+ $config = $test->ci_get_config();
+
+ return $config;
+ }
+}
+
+if ( ! function_exists('config_item'))
+{
+ function config_item($item)
+ {
+ $config =& get_config();
+
+ if ( ! isset($config[$item]))
+ {
+ return FALSE;
+ }
+
+ return $config[$item];
+ }
+}
+
+// --------------------------------------------------------------------
+
+if ( ! function_exists('load_class'))
+{
+ function load_class($class, $directory = 'libraries', $prefix = 'CI_')
+ {
+ if ($directory !== 'core' OR $prefix !== 'CI_')
+ {
+ throw new Exception('Not Implemented: Non-core load_class()');
+ }
+
+ $test = CI_TestCase::instance();
+
+ $obj =& $test->ci_core_class($class);
+
+ if (is_string($obj))
+ {
+ throw new Exception('Bad Isolation: Use ci_set_core_class to set '.$class.'');
+ }
+
+ return $obj;
+ }
+}
+
+// This is sort of meh. Should probably be mocked up with
+// controllable output, so that we can test some of our
+// security code. The function itself will be tested in the
+// bootstrap testsuite.
+// --------------------------------------------------------------------
+
+if ( ! function_exists('remove_invisible_characters'))
+{
+ function remove_invisible_characters($str, $url_encoded = TRUE)
+ {
+ $non_displayables = array();
+
+ // every control character except newline (dec 10)
+ // carriage return (dec 13), and horizontal tab (dec 09)
+
+ if ($url_encoded)
+ {
+ $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15
+ $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31
+ }
+
+ $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127
+
+ do
+ {
+ $str = preg_replace($non_displayables, '', $str, -1, $count);
+ }
+ while ($count);
+
+ return $str;
+ }
+}
+
+
+// Clean up error messages
+// --------------------------------------------------------------------
+
+if ( ! function_exists('show_error'))
+{
+ function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
+ {
+ throw new RuntimeException('CI Error: '.$message);
+ }
+}
+
+if ( ! function_exists('show_404'))
+{
+ function show_404($page = '', $log_error = TRUE)
+ {
+ throw new RuntimeException('CI Error: 404');
+ }
+}
+
+if ( ! function_exists('_exception_handler'))
+{
+ function _exception_handler($severity, $message, $filepath, $line)
+ {
+ throw new RuntimeException('CI Exception: '.$message.' | '.$filepath.' | '.$line);
+ }
+}
+
+
+// We assume a few things about our environment ...
+// --------------------------------------------------------------------
+
+if ( ! function_exists('is_php'))
+{
+ function is_php($version = '5.0.0')
+ {
+ return ! (version_compare(PHP_VERSION, $version) < 0);
+ }
+}
+
+if ( ! function_exists('is_really_writable'))
+{
+ function is_really_writable($file)
+ {
+ return is_writable($file);
+ }
+}
+
+if ( ! function_exists('is_loaded'))
+{
+ function is_loaded()
+ {
+ throw new Exception('Bad Isolation: mock up environment');
+ }
+}
+
+if ( ! function_exists('log_message'))
+{
+ function log_message($level = 'error', $message, $php_error = FALSE)
+ {
+ return TRUE;
+ }
+}
+
+if ( ! function_exists('set_status_header'))
+{
+ function set_status_header($code = 200, $text = '')
+ {
+ return TRUE;
+ }
+}
+
+// EOF \ No newline at end of file
diff --git a/tests/mocks/core/input.php b/tests/mocks/core/input.php
new file mode 100644
index 000000000..8a337d2ef
--- /dev/null
+++ b/tests/mocks/core/input.php
@@ -0,0 +1,31 @@
+<?php
+
+class Mock_Core_Input extends CI_Input {
+
+ /**
+ * Since we use GLOBAL to fetch Security and Utf8 classes,
+ * we need to use inversion of control to mock up
+ * the same process within CI_Input class constructor.
+ *
+ * @covers CI_Input::__construct()
+ */
+ public function __construct($security, $utf8)
+ {
+ $this->_allow_get_array = (config_item('allow_get_array') === TRUE);
+ $this->_enable_xss = (config_item('global_xss_filtering') === TRUE);
+ $this->_enable_csrf = (config_item('csrf_protection') === TRUE);
+
+ // Assign Security and Utf8 classes
+ $this->security = $security;
+ $this->uni = $utf8;
+
+ // Sanitize global arrays
+ $this->_sanitize_globals();
+ }
+
+ public function fetch_from_array($array, $index = '', $xss_clean = FALSE)
+ {
+ return parent::_fetch_from_array($array, $index, $xss_clean);
+ }
+
+} \ No newline at end of file
diff --git a/tests/mocks/core/loader.php b/tests/mocks/core/loader.php
new file mode 100644
index 000000000..d4b29bb3d
--- /dev/null
+++ b/tests/mocks/core/loader.php
@@ -0,0 +1,30 @@
+<?php
+
+class Mock_Core_Loader extends CI_Loader {
+
+ /**
+ * Since we use paths to load up models, views, etc, we need the ability to
+ * mock up the file system so when core tests are run, we aren't mucking
+ * in the application directory. this will give finer grained control over
+ * these tests. So yeah, while this looks odd, I need to overwrite protected
+ * class vars in the loader. So here we go...
+ *
+ * @covers CI_Loader::__construct()
+ */
+ public function __construct()
+ {
+ vfsStreamWrapper::register();
+ vfsStreamWrapper::setRoot(new vfsStreamDirectory('application'));
+
+ $this->models_dir = vfsStream::newDirectory('models')->at(vfsStreamWrapper::getRoot());
+ $this->libs_dir = vfsStream::newDirectory('libraries')->at(vfsStreamWrapper::getRoot());
+ $this->helpers_dir = vfsStream::newDirectory('helpers')->at(vfsStreamWrapper::getRoot());
+ $this->views_dir = vfsStream::newDirectory('views')->at(vfsStreamWrapper::getRoot());
+
+ $this->_ci_ob_level = ob_get_level();
+ $this->_ci_library_paths = array(vfsStream::url('application').'/', BASEPATH);
+ $this->_ci_helper_paths = array(vfsStream::url('application').'/', BASEPATH);
+ $this->_ci_model_paths = array(vfsStream::url('application').'/');
+ $this->_ci_view_paths = array(vfsStream::url('application').'/views/' => TRUE);
+ }
+} \ No newline at end of file
diff --git a/tests/mocks/core/security.php b/tests/mocks/core/security.php
new file mode 100644
index 000000000..d7ea0e6bd
--- /dev/null
+++ b/tests/mocks/core/security.php
@@ -0,0 +1,30 @@
+<?php
+
+class Mock_Core_Security extends CI_Security {
+
+ public function csrf_set_cookie()
+ {
+ // We cannot set cookie in CLI mode, so for csrf test, who rely on $_COOKIE,
+ // we superseded set_cookie with directly set the cookie variable,
+ // @see : ./tests/codeigniter/core/Security_test.php, line 8
+ return $this;
+ }
+
+ // Overide inaccesible protected properties
+ public function __get($property)
+ {
+ return isset($this->{'_'.$property}) ? $this->{'_'.$property} : NULL;
+ }
+
+ // Overide inaccesible protected method
+ public function __call($method, $params)
+ {
+ if (is_callable(array($this, '_'.$method)))
+ {
+ return call_user_func_array(array($this, '_'.$method), $params);
+ }
+
+ throw new BadMethodCallException('Method '.$method.' was not found');
+ }
+
+} \ No newline at end of file
diff --git a/tests/mocks/core/uri.php b/tests/mocks/core/uri.php
new file mode 100644
index 000000000..b6946091e
--- /dev/null
+++ b/tests/mocks/core/uri.php
@@ -0,0 +1,25 @@
+<?php
+
+class Mock_Core_URI extends CI_URI {
+
+ public function __construct()
+ {
+ $test = CI_TestCase::instance();
+ $cls =& $test->ci_core_class('cfg');
+
+ // set predictable config values
+ $test->ci_set_config(array(
+ 'index_page' => 'index.php',
+ 'base_url' => 'http://example.com/',
+ 'subclass_prefix' => 'MY_'
+ ));
+
+ $this->config = new $cls;
+
+ }
+
+ protected function _is_cli_request()
+ {
+ return FALSE;
+ }
+} \ No newline at end of file
diff --git a/tests/mocks/core/utf8.php b/tests/mocks/core/utf8.php
new file mode 100644
index 000000000..b77d717e7
--- /dev/null
+++ b/tests/mocks/core/utf8.php
@@ -0,0 +1,27 @@
+<?php
+
+class Mock_Core_Utf8 extends CI_Utf8 {
+
+ /**
+ * We need to define several constants as
+ * the same process within CI_Utf8 class constructor.
+ *
+ * @covers CI_Utf8::__construct()
+ */
+ public function __construct()
+ {
+ defined('UTF8_ENABLED') or define('UTF8_ENABLED', TRUE);
+
+ if (extension_loaded('mbstring'))
+ {
+ defined('MB_ENABLED') or define('MB_ENABLED', TRUE);
+ mb_internal_encoding('UTF-8');
+ }
+ else
+ {
+ defined('MB_ENABLED') or define('MB_ENABLED', FALSE);
+ }
+
+ }
+
+} \ No newline at end of file