summaryrefslogtreecommitdiffstats
path: root/tests/mocks
diff options
context:
space:
mode:
authorAndrey Andreev <narf@bofh.bg>2012-06-07 21:34:38 +0200
committerAndrey Andreev <narf@bofh.bg>2012-06-07 21:34:38 +0200
commitfeec9db5e6514f242e17b4504536d746660fd02c (patch)
treecec0ce356bd58892b9b3af50d14c2eb70c9b5b48 /tests/mocks
parentbd738c80a623700b3b11e876abb764ca6d57769d (diff)
parent25c08390bd93ba27d89a8682b37c8c6184a53c07 (diff)
Merge upstream branch
Diffstat (limited to 'tests/mocks')
-rw-r--r--tests/mocks/autoloader.php17
-rw-r--r--tests/mocks/core/benchmark.php3
-rw-r--r--tests/mocks/core/common.php167
-rw-r--r--tests/mocks/core/input.php31
-rw-r--r--tests/mocks/core/security.php30
-rw-r--r--tests/mocks/core/utf8.php27
-rwxr-xr-xtests/mocks/database/ci_test.sqlitebin17408 -> 19456 bytes
-rw-r--r--tests/mocks/database/drivers/sqlite.php2
-rw-r--r--tests/mocks/database/schema/skeleton.php49
-rw-r--r--tests/mocks/libraries/table.php2
10 files changed, 253 insertions, 75 deletions
diff --git a/tests/mocks/autoloader.php b/tests/mocks/autoloader.php
index f1bdb5d6f..90aabcbe6 100644
--- a/tests/mocks/autoloader.php
+++ b/tests/mocks/autoloader.php
@@ -6,11 +6,10 @@
//
// Prototype :
//
-// include_once('Mock_Core_Loader') // Will load ./mocks/core/loader.php
// $mock_table = new Mock_Libraries_Table(); // Will load ./mocks/libraries/table.php
-// $mock_database_driver = new Mock_Database_Driver(); // Will load ./mocks/database/driver.php
+// $mock_database_driver = new Mock_Database_Driver(); // Will load ./mocks/database/driver.php
// and so on...
-function autoload($class)
+function autoload($class)
{
$dir = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR;
@@ -23,7 +22,7 @@ function autoload($class)
);
$ci_libraries = array(
- 'Calendar', 'Cart', 'Driver',
+ 'Calendar', 'Cart', 'Driver_Library',
'Email', 'Encrypt', 'Form_validation',
'Ftp', 'Image_lib', 'Javascript',
'Log', 'Migration', 'Pagination',
@@ -51,9 +50,9 @@ function autoload($class)
elseif (in_array($subclass, $ci_libraries))
{
$dir = BASEPATH.'libraries'.DIRECTORY_SEPARATOR;
- $class = $subclass;
+ $class = ($subclass === 'Driver_Library') ? 'Driver' : $subclass;
}
- elseif (preg_match('/^CI_DB_(.+)_(driver|forge|result|utility)$/', $class, $m) && count($m) == 3)
+ elseif (preg_match('/^CI_DB_(.+)_(driver|forge|result|utility)$/', $class, $m) && count($m) === 3)
{
$driver_path = BASEPATH.'database'.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR;
$dir = $driver_path.$m[1].DIRECTORY_SEPARATOR;
@@ -76,13 +75,13 @@ function autoload($class)
{
$trace = debug_backtrace();
- // If the autoload call came from `class_exists` or `file_exists`,
+ // If the autoload call came from `class_exists` or `file_exists`,
// we skipped and return FALSE
- if ($trace[2]['function'] == 'class_exists' OR $trace[2]['function'] == 'file_exists')
+ if ($trace[2]['function'] === 'class_exists' OR $trace[2]['function'] === 'file_exists')
{
return FALSE;
}
-
+
throw new InvalidArgumentException("Unable to load $class.");
}
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
index fc94d7fff..e1c493aa0 100644
--- a/tests/mocks/core/common.php
+++ b/tests/mocks/core/common.php
@@ -2,53 +2,65 @@
// Set up the global CI functions in their most minimal core representation
-function &get_instance()
+if ( ! function_exists('get_instance'))
{
- $test = CI_TestCase::instance();
- $instance = $test->ci_instance();
- return $instance;
+ function &get_instance()
+ {
+ $test = CI_TestCase::instance();
+ $instance = $test->ci_instance();
+ return $instance;
+ }
}
// --------------------------------------------------------------------
-function &get_config() {
- $test = CI_TestCase::instance();
- $config = $test->ci_get_config();
-
- return $config;
+if ( ! function_exists('get_config'))
+{
+ function &get_config() {
+ $test = CI_TestCase::instance();
+ $config = $test->ci_get_config();
+
+ return $config;
+ }
}
-function config_item($item)
+if ( ! function_exists('config_item'))
{
- $config =& get_config();
-
- if ( ! isset($config[$item]))
+ function config_item($item)
{
- return FALSE;
+ $config =& get_config();
+
+ if ( ! isset($config[$item]))
+ {
+ return FALSE;
+ }
+
+ return $config[$item];
}
-
- return $config[$item];
}
// --------------------------------------------------------------------
-function load_class($class, $directory = 'libraries', $prefix = 'CI_')
+if ( ! function_exists('load_class'))
{
- 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))
+ function load_class($class, $directory = 'libraries', $prefix = 'CI_')
{
- throw new Exception('Bad Isolation: Use ci_set_core_class to set '.$class.'');
+ 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;
}
-
- return $obj;
}
// This is sort of meh. Should probably be mocked up with
@@ -57,76 +69,103 @@ function load_class($class, $directory = 'libraries', $prefix = 'CI_')
// bootstrap testsuite.
// --------------------------------------------------------------------
-function remove_invisible_characters($str, $url_encoded = TRUE)
+if ( ! function_exists('remove_invisible_characters'))
{
- $non_displayables = array();
-
- // every control character except newline (dec 10)
- // carriage return (dec 13), and horizontal tab (dec 09)
-
- if ($url_encoded)
+ function remove_invisible_characters($str, $url_encoded = TRUE)
{
- $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
+ $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);
+ do
+ {
+ $str = preg_replace($non_displayables, '', $str, -1, $count);
+ }
+ while ($count);
- return $str;
+ return $str;
+ }
}
// Clean up error messages
// --------------------------------------------------------------------
-function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
+if ( ! function_exists('show_error'))
{
- throw new RuntimeException('CI Error: '.$message);
+ function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
+ {
+ throw new RuntimeException('CI Error: '.$message);
+ }
}
-function show_404($page = '', $log_error = TRUE)
+if ( ! function_exists('show_404'))
{
- throw new RuntimeException('CI Error: 404');
+ function show_404($page = '', $log_error = TRUE)
+ {
+ throw new RuntimeException('CI Error: 404');
+ }
}
-function _exception_handler($severity, $message, $filepath, $line)
+if ( ! function_exists('_exception_handler'))
{
- throw new RuntimeException('CI Exception: '.$message.' | '.$filepath.' | '.$line);
+ function _exception_handler($severity, $message, $filepath, $line)
+ {
+ throw new RuntimeException('CI Exception: '.$message.' | '.$filepath.' | '.$line);
+ }
}
// We assume a few things about our environment ...
// --------------------------------------------------------------------
-function is_php($version = '5.0.0')
+if ( ! function_exists('is_php'))
{
- return ! (version_compare(PHP_VERSION, $version) < 0);
+ function is_php($version = '5.0.0')
+ {
+ return ! (version_compare(PHP_VERSION, $version) < 0);
+ }
}
-function is_really_writable($file)
+if ( ! function_exists('is_really_writable'))
{
- return is_writable($file);
+ function is_really_writable($file)
+ {
+ return is_writable($file);
+ }
}
-function is_loaded()
+if ( ! function_exists('is_loaded'))
{
- throw new Exception('Bad Isolation: mock up environment');
+ function is_loaded()
+ {
+ throw new Exception('Bad Isolation: mock up environment');
+ }
}
-function log_message($level = 'error', $message, $php_error = FALSE)
+if ( ! function_exists('log_message'))
{
- return TRUE;
+ function log_message($level = 'error', $message, $php_error = FALSE)
+ {
+ return TRUE;
+ }
}
-function set_status_header($code = 200, $text = '')
+if ( ! function_exists('set_status_header'))
{
- return TRUE;
+ 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/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/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
diff --git a/tests/mocks/database/ci_test.sqlite b/tests/mocks/database/ci_test.sqlite
index 86d868af2..44dcef9ec 100755
--- a/tests/mocks/database/ci_test.sqlite
+++ b/tests/mocks/database/ci_test.sqlite
Binary files differ
diff --git a/tests/mocks/database/drivers/sqlite.php b/tests/mocks/database/drivers/sqlite.php
index 76a182cbf..15cefbf53 100644
--- a/tests/mocks/database/drivers/sqlite.php
+++ b/tests/mocks/database/drivers/sqlite.php
@@ -1,7 +1,7 @@
<?php
class Mock_Database_Drivers_Sqlite extends Mock_Database_DB_Driver {
-
+
/**
* Instantiate the database driver
*
diff --git a/tests/mocks/database/schema/skeleton.php b/tests/mocks/database/schema/skeleton.php
index a3d5bac65..05499f82f 100644
--- a/tests/mocks/database/schema/skeleton.php
+++ b/tests/mocks/database/schema/skeleton.php
@@ -50,6 +50,28 @@ class Mock_Database_Schema_Skeleton {
*/
public static function create_tables()
{
+ // User Table
+ static::$forge->add_field(array(
+ 'id' => array(
+ 'type' => 'INTEGER',
+ 'constraint' => 3,
+ ),
+ 'name' => array(
+ 'type' => 'VARCHAR',
+ 'constraint' => 40,
+ ),
+ 'email' => array(
+ 'type' => 'VARCHAR',
+ 'constraint' => 100,
+ ),
+ 'country' => array(
+ 'type' => 'VARCHAR',
+ 'constraint' => 40,
+ ),
+ ));
+ static::$forge->add_key('id', TRUE);
+ static::$forge->create_table('user', (strpos(static::$driver, 'pgsql') === FALSE));
+
// Job Table
static::$forge->add_field(array(
'id' => array(
@@ -66,6 +88,23 @@ class Mock_Database_Schema_Skeleton {
));
static::$forge->add_key('id', TRUE);
static::$forge->create_table('job', (strpos(static::$driver, 'pgsql') === FALSE));
+
+ // Misc Table
+ static::$forge->add_field(array(
+ 'id' => array(
+ 'type' => 'INTEGER',
+ 'constraint' => 3,
+ ),
+ 'key' => array(
+ 'type' => 'VARCHAR',
+ 'constraint' => 40,
+ ),
+ 'value' => array(
+ 'type' => 'TEXT',
+ ),
+ ));
+ static::$forge->add_key('id', TRUE);
+ static::$forge->create_table('misc', (strpos(static::$driver, 'pgsql') === FALSE));
}
/**
@@ -77,12 +116,22 @@ class Mock_Database_Schema_Skeleton {
{
// Job Data
$data = array(
+ 'user' => array(
+ array('id' => 1, 'name' => 'Derek Jones', 'email' => 'derek@world.com', 'country' => 'US'),
+ array('id' => 2, 'name' => 'Ahmadinejad', 'email' => 'ahmadinejad@world.com', 'country' => 'Iran'),
+ array('id' => 3, 'name' => 'Richard A Causey', 'email' => 'richard@world.com', 'country' => 'US'),
+ array('id' => 4, 'name' => 'Chris Martin', 'email' => 'chris@world.com', 'country' => 'UK'),
+ ),
'job' => array(
array('id' => 1, 'name' => 'Developer', 'description' => 'Awesome job, but sometimes makes you bored'),
array('id' => 2, 'name' => 'Politician', 'description' => 'This is not really a job'),
array('id' => 3, 'name' => 'Accountant', 'description' => 'Boring job, but you will get free snack at lunch'),
array('id' => 4, 'name' => 'Musician', 'description' => 'Only Coldplay can actually called Musician'),
),
+ 'misc' => array(
+ array('id' => 1, 'key' => '\\xxxfoo456', 'value' => 'Entry with \\xxx'),
+ array('id' => 2, 'key' => '\\%foo456', 'value' => 'Entry with \\%'),
+ ),
);
foreach ($data as $table => $dummy_data)
diff --git a/tests/mocks/libraries/table.php b/tests/mocks/libraries/table.php
index 1a6ff8d35..97fbb30bd 100644
--- a/tests/mocks/libraries/table.php
+++ b/tests/mocks/libraries/table.php
@@ -2,7 +2,7 @@
class Mock_Libraries_Table extends CI_Table {
- // Overide inaccesible private or protected method
+ // Overide inaccesible protected method
public function __call($method, $params)
{
if (is_callable(array($this, '_'.$method)))