summaryrefslogtreecommitdiffstats
path: root/tests/mocks
diff options
context:
space:
mode:
Diffstat (limited to 'tests/mocks')
-rw-r--r--tests/mocks/autoloader.php43
-rw-r--r--tests/mocks/ci_testcase.php203
-rw-r--r--tests/mocks/ci_testconfig.php20
-rw-r--r--tests/mocks/core/common.php29
-rw-r--r--tests/mocks/core/input.php10
-rw-r--r--tests/mocks/core/lang.php15
-rw-r--r--tests/mocks/core/loader.php31
-rw-r--r--tests/mocks/database/db.php45
-rw-r--r--tests/mocks/database/schema/skeleton.php5
-rw-r--r--tests/mocks/libraries/calendar.php25
-rw-r--r--tests/mocks/libraries/session.php43
-rw-r--r--tests/mocks/libraries/upload.php3
-rw-r--r--tests/mocks/uploads/ci_logo.gifbin0 -> 3270 bytes
13 files changed, 414 insertions, 58 deletions
diff --git a/tests/mocks/autoloader.php b/tests/mocks/autoloader.php
index be1c2220c..4fc9c63b6 100644
--- a/tests/mocks/autoloader.php
+++ b/tests/mocks/autoloader.php
@@ -26,41 +26,58 @@ function autoload($class)
'Email', 'Encrypt', 'Form_validation',
'Ftp', 'Image_lib', 'Javascript',
'Log', 'Migration', 'Pagination',
- 'Parser', 'Profiler', 'Session',
- 'Table', 'Trackback', 'Typography',
- 'Unit_test', 'Upload', 'User_agent',
- 'Xmlrpc', 'Zip',
+ 'Parser', 'Profiler', 'Table',
+ 'Trackback', 'Typography', 'Unit_test',
+ 'Upload', 'User_agent', 'Xmlrpc',
+ 'Zip',
+ );
+
+ $ci_drivers = array(
+ 'Session',
);
if (strpos($class, 'Mock_') === 0)
{
- $class = str_replace(array('Mock_', '_'), array('', DIRECTORY_SEPARATOR), $class);
- $class = strtolower($class);
+ $class = strtolower(str_replace(array('Mock_', '_'), array('', DIRECTORY_SEPARATOR), $class));
}
elseif (strpos($class, 'CI_') === 0)
{
- $fragments = explode('_', $class, 2);
- $subclass = next($fragments);
+ $subclass = substr($class, 3);
if (in_array($subclass, $ci_core))
{
- $dir = BASEPATH.'core'.DIRECTORY_SEPARATOR;
+ $dir = SYSTEM_PATH.'core'.DIRECTORY_SEPARATOR;
$class = $subclass;
}
elseif (in_array($subclass, $ci_libraries))
{
- $dir = BASEPATH.'libraries'.DIRECTORY_SEPARATOR;
+ $dir = SYSTEM_PATH.'libraries'.DIRECTORY_SEPARATOR;
$class = ($subclass === 'Driver_Library') ? 'Driver' : $subclass;
}
+ elseif (in_array($subclass, $ci_drivers))
+ {
+ $dir = SYSTEM_PATH.'libraries'.DIRECTORY_SEPARATOR.$subclass.DIRECTORY_SEPARATOR;
+ $class = $subclass;
+ }
+ elseif (in_array(($parent = strtok($subclass, '_')), $ci_drivers)) {
+ $dir = SYSTEM_PATH.'libraries'.DIRECTORY_SEPARATOR.$parent.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR;
+ $class = $subclass;
+ }
+ elseif (preg_match('/^CI_DB_(.+)_(.+)_(driver|forge|result|utility)$/', $class, $m) && count($m) === 4)
+ {
+ $driver_path = SYSTEM_PATH.'database'.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR;
+ $dir = $driver_path.$m[1].DIRECTORY_SEPARATOR.'subdrivers'.DIRECTORY_SEPARATOR;
+ $file = $dir.$m[1].'_'.$m[2].'_'.$m[3].'.php';
+ }
elseif (preg_match('/^CI_DB_(.+)_(driver|forge|result|utility)$/', $class, $m) && count($m) === 3)
{
- $driver_path = BASEPATH.'database'.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR;
+ $driver_path = SYSTEM_PATH.'database'.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR;
$dir = $driver_path.$m[1].DIRECTORY_SEPARATOR;
$file = $dir.$m[1].'_'.$m[2].'.php';
}
elseif (strpos($class, 'CI_DB') === 0)
{
- $dir = BASEPATH.'database'.DIRECTORY_SEPARATOR;
+ $dir = SYSTEM_PATH.'database'.DIRECTORY_SEPARATOR;
$file = $dir.str_replace(array('CI_DB','active_record'), array('DB', 'active_rec'), $subclass).'.php';
}
else
@@ -69,7 +86,7 @@ function autoload($class)
}
}
- $file = (isset($file)) ? $file : $dir.$class.'.php';
+ $file = isset($file) ? $file : $dir.$class.'.php';
if ( ! file_exists($file))
{
diff --git a/tests/mocks/ci_testcase.php b/tests/mocks/ci_testcase.php
index eda9e1b60..f16492945 100644
--- a/tests/mocks/ci_testcase.php
+++ b/tests/mocks/ci_testcase.php
@@ -2,7 +2,9 @@
class CI_TestCase extends PHPUnit_Framework_TestCase {
- protected $ci_config;
+ public $ci_vfs_root;
+ public $ci_app_root;
+ public $ci_base_root;
protected $ci_instance;
protected static $ci_test_instance;
@@ -25,13 +27,19 @@ class CI_TestCase extends PHPUnit_Framework_TestCase {
public function __construct()
{
parent::__construct();
- $this->ci_config = array();
+ $this->ci_instance = new StdClass();
}
// --------------------------------------------------------------------
public function setUp()
{
+ // Setup VFS with base directories
+ $this->ci_vfs_root = vfsStream::setup();
+ $this->ci_app_root = vfsStream::newDirectory('application')->at($this->ci_vfs_root);
+ $this->ci_base_root = vfsStream::newDirectory('system')->at($this->ci_vfs_root);
+ $this->ci_view_root = vfsStream::newDirectory('views')->at($this->ci_app_root);
+
if (method_exists($this, 'set_up'))
{
$this->set_up();
@@ -57,15 +65,27 @@ class CI_TestCase extends PHPUnit_Framework_TestCase {
// --------------------------------------------------------------------
- public function ci_set_config($key, $val = '')
+ public function ci_set_config($key = '', $val = '')
{
+ // Add test config
+ if ( ! isset($this->ci_instance->config))
+ {
+ $this->ci_instance->config = new CI_TestConfig();
+ }
+
+ // Empty key means just do setup above
+ if ($key === '')
+ {
+ return;
+ }
+
if (is_array($key))
{
- $this->ci_config = $key;
+ $this->ci_instance->config->config = $key;
}
else
{
- $this->ci_config[$key] = $val;
+ $this->ci_instance->config->config[$key] = $val;
}
}
@@ -73,7 +93,7 @@ class CI_TestCase extends PHPUnit_Framework_TestCase {
public function ci_get_config()
{
- return $this->ci_config;
+ return isset($this->ci_instance->config) ? $this->ci_instance->config->config : array();
}
// --------------------------------------------------------------------
@@ -132,7 +152,7 @@ class CI_TestCase extends PHPUnit_Framework_TestCase {
if ( ! class_exists('CI_'.$class_name))
{
- require_once BASEPATH.'core/'.$class_name.'.php';
+ require_once SYSTEM_PATH.'core/'.$class_name.'.php';
}
$GLOBALS[strtoupper($global_name)] = 'CI_'.$class_name;
@@ -148,6 +168,165 @@ class CI_TestCase extends PHPUnit_Framework_TestCase {
$orig = $obj;
}
+ /**
+ * Create VFS directory
+ *
+ * @param string Directory name
+ * @param object Optional root to create in
+ * @return object New directory object
+ */
+ public function ci_vfs_mkdir($name, $root = NULL)
+ {
+ // Check for root
+ if ( ! $root)
+ {
+ $root = $this->ci_vfs_root;
+ }
+
+ // Return new directory object
+ return vfsStream::newDirectory($name)->at($root);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Create VFS content
+ *
+ * @param string File name
+ * @param string File content
+ * @param object VFS directory object
+ * @param mixed Optional subdirectory path or array of subs
+ * @return void
+ */
+ public function ci_vfs_create($file, $content = '', $root = NULL, $path = NULL)
+ {
+ // Check for array
+ if (is_array($file))
+ {
+ foreach ($file as $name => $content)
+ {
+ $this->ci_vfs_create($name, $content, $root, $path);
+ }
+ return;
+ }
+
+ // Assert .php extension if none given
+ if (pathinfo($file, PATHINFO_EXTENSION) == '')
+ {
+ $file .= '.php';
+ }
+
+ // Build content
+ $tree = array($file => $content);
+
+ // Check for path
+ $subs = array();
+ if ($path)
+ {
+ // Explode if not array
+ $subs = is_array($path) ? $path : explode('/', trim($path, '/'));
+ }
+
+ // Check for root
+ if ( ! $root)
+ {
+ // Use base VFS root
+ $root = $this->ci_vfs_root;
+ }
+
+ // Handle subdirectories
+ while (($dir = array_shift($subs)))
+ {
+ // See if subdir exists under current root
+ $dir_root = $root->getChild($dir);
+ if ($dir_root)
+ {
+ // Yes - recurse into subdir
+ $root = $dir_root;
+ }
+ else
+ {
+ // No - put subdirectory back and quit
+ array_unshift($subs, $dir);
+ break;
+ }
+ }
+
+ // Create any remaining subdirectories
+ if ($subs)
+ {
+ foreach (array_reverse($subs) as $dir)
+ {
+ // Wrap content in subdirectory for creation
+ $tree = array($dir => $tree);
+ }
+ }
+
+ // Create tree
+ vfsStream::create($tree, $root);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Clone a real file into VFS
+ *
+ * @param string Path from base directory
+ * @return bool TRUE on success, otherwise FALSE
+ */
+ public function ci_vfs_clone($path)
+ {
+ // Check for array
+ if (is_array($path))
+ {
+ foreach ($path as $file)
+ {
+ $this->ci_vfs_clone($file);
+ }
+ return;
+ }
+
+ // Get real file contents
+ $content = file_get_contents(PROJECT_BASE.$path);
+ if ($content === FALSE)
+ {
+ // Couldn't find file to clone
+ return FALSE;
+ }
+
+ $this->ci_vfs_create(basename($path), $content, NULL, dirname($path));
+ return TRUE;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Helper to get a VFS URL path
+ *
+ * @param string Path
+ * @param string Optional base path
+ * @return string Path URL
+ */
+ public function ci_vfs_path($path, $base = '')
+ {
+ // Check for base path
+ if ($base)
+ {
+ // Prepend to path
+ $path = rtrim($base, '/').'/'.ltrim($path, '/');
+
+ // Is it already in URL form?
+ if (strpos($path, '://') !== FALSE)
+ {
+ // Done - return path
+ return $path;
+ }
+ }
+
+ // Trim leading slash and return URL
+ return vfsStream::url(ltrim($path, '/'));
+ }
+
// --------------------------------------------------------------------
// Internals
// --------------------------------------------------------------------
@@ -171,7 +350,15 @@ class CI_TestCase extends PHPUnit_Framework_TestCase {
public function helper($name)
{
- require_once(BASEPATH.'helpers/'.$name.'_helper.php');
+ require_once(SYSTEM_PATH.'helpers/'.$name.'_helper.php');
+ }
+
+ // --------------------------------------------------------------------
+
+ public function lang($name)
+ {
+ require(SYSTEM_PATH.'language/english/'.$name.'_lang.php');
+ return $lang;
}
// --------------------------------------------------------------------
diff --git a/tests/mocks/ci_testconfig.php b/tests/mocks/ci_testconfig.php
new file mode 100644
index 000000000..0c52bb984
--- /dev/null
+++ b/tests/mocks/ci_testconfig.php
@@ -0,0 +1,20 @@
+<?php
+
+class CI_TestConfig {
+
+ public $config = array();
+ public $_config_paths = array(APPPATH);
+ public $loaded = array();
+
+ public function item($key)
+ {
+ return isset($this->config[$key]) ? $this->config[$key] : FALSE;
+ }
+
+ public function load($file, $arg2 = FALSE, $arg3 = FALSE)
+ {
+ $this->loaded[] = $file;
+ return TRUE;
+ }
+
+}
diff --git a/tests/mocks/core/common.php b/tests/mocks/core/common.php
index a655ee1db..9289b2716 100644
--- a/tests/mocks/core/common.php
+++ b/tests/mocks/core/common.php
@@ -39,6 +39,30 @@ if ( ! function_exists('config_item'))
}
}
+if ( ! function_exists('get_mimes'))
+{
+ /**
+ * Returns the MIME types array from config/mimes.php
+ *
+ * @return array
+ */
+ function &get_mimes()
+ {
+ static $_mimes = array();
+
+ if (empty($_mimes))
+ {
+ $path = realpath(PROJECT_BASE.'application/config/mimes.php');
+ if (is_file($path))
+ {
+ $_mimes = include($path);
+ }
+ }
+
+ return $_mimes;
+ }
+}
+
// --------------------------------------------------------------------
if ( ! function_exists('load_class'))
@@ -146,9 +170,10 @@ if ( ! function_exists('is_really_writable'))
if ( ! function_exists('is_loaded'))
{
- function is_loaded()
+ function &is_loaded()
{
- throw new Exception('Bad Isolation: mock up environment');
+ $loaded = array();
+ return $loaded;
}
}
diff --git a/tests/mocks/core/input.php b/tests/mocks/core/input.php
index 2a4aa4997..0d1873849 100644
--- a/tests/mocks/core/input.php
+++ b/tests/mocks/core/input.php
@@ -28,4 +28,14 @@ class Mock_Core_Input extends CI_Input {
return parent::_fetch_from_array($array, $index, $xss_clean);
}
+ /**
+ * Lie about being a CLI request
+ *
+ * We take advantage of this in libraries/Session_test
+ */
+ public function is_cli_request()
+ {
+ return FALSE;
+ }
+
} \ No newline at end of file
diff --git a/tests/mocks/core/lang.php b/tests/mocks/core/lang.php
new file mode 100644
index 000000000..27ea3faba
--- /dev/null
+++ b/tests/mocks/core/lang.php
@@ -0,0 +1,15 @@
+<?php
+
+class Mock_Core_Lang extends CI_Lang {
+
+ public function line($line = '')
+ {
+ return FALSE;
+ }
+
+ public function load($langfile, $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '')
+ {
+ return;
+ }
+
+} \ No newline at end of file
diff --git a/tests/mocks/core/loader.php b/tests/mocks/core/loader.php
deleted file mode 100644
index 53d88d55b..000000000
--- a/tests/mocks/core/loader.php
+++ /dev/null
@@ -1,31 +0,0 @@
-<?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/database/db.php b/tests/mocks/database/db.php
index 75658530b..7e0030e15 100644
--- a/tests/mocks/database/db.php
+++ b/tests/mocks/database/db.php
@@ -8,6 +8,16 @@ class Mock_Database_DB {
private $config = array();
/**
+ * @var string DB driver name
+ */
+ private static $dbdriver = '';
+
+ /**
+ * @var string DB sub-driver name
+ */
+ private static $subdriver = '';
+
+ /**
* Prepare database configuration skeleton
*
* @param array DB configuration to set
@@ -31,6 +41,12 @@ class Mock_Database_DB {
throw new InvalidArgumentException('Group '.$group.' not exists');
}
+ self::$dbdriver = $this->config[$group]['dbdriver'];
+ if (isset($this->config[$group]['subdriver']))
+ {
+ self::$subdriver = $this->config[$group]['subdriver'];
+ }
+
$params = array(
'dbprefix' => '',
'pconnect' => FALSE,
@@ -50,7 +66,7 @@ class Mock_Database_DB {
$failover = empty($config['failover']) ? FALSE : $config['failover'];
$dsn = $config['dbdriver'].'://'.$config['username'].':'.$config['password']
- .'@'.$config['hostname'].'/'.$config['database'];
+ .'@'.$config['hostname'].'/'.$config['database'];
// Build the parameter
$other_params = array_slice($config, 6);
@@ -83,7 +99,32 @@ class Mock_Database_DB {
*/
public static function DB($group, $query_builder = FALSE)
{
- include_once(BASEPATH.'database/DB.php');
+ // Create dummy driver and builder files to "load" - the mocks have
+ // already triggered autoloading of the real files
+ $case = CI_TestCase::instance();
+ $driver = self::$dbdriver;
+ $subdriver = self::$subdriver;
+ $case->ci_vfs_create(array(
+ 'DB_driver.php' => '',
+ 'DB_forge.php' => '',
+ 'DB_query_builder.php' => ''
+ ), '', $case->ci_base_root, 'database');
+ if (file_exists(SYSTEM_PATH.'database/drivers/'.$driver.'/'.$driver.'_driver.php'))
+ {
+ $case->ci_vfs_create(array(
+ $driver.'_driver.php' => '',
+ $driver.'_forge.php' => ''
+ ), '', $case->ci_base_root, 'database/drivers/'.$driver);
+ }
+ if ($subdriver)
+ {
+ $case->ci_vfs_create(array(
+ $driver.'_'.$subdriver.'_driver.php' => '',
+ $driver.'_'.$subdriver.'_forge.php' => ''
+ ), '', $case->ci_base_root, 'database/drivers/'.$driver.'/subdrivers');
+ }
+
+ include_once(SYSTEM_PATH.'database/DB.php');
try
{
diff --git a/tests/mocks/database/schema/skeleton.php b/tests/mocks/database/schema/skeleton.php
index 18e1ddd4d..d72244528 100644
--- a/tests/mocks/database/schema/skeleton.php
+++ b/tests/mocks/database/schema/skeleton.php
@@ -30,7 +30,7 @@ class Mock_Database_Schema_Skeleton {
CI_TestCase::instance()->ci_instance_var('db', $db);
- $loader = new Mock_Core_Loader();
+ $loader = new CI_Loader();
$loader->dbforge();
$forge = CI_TestCase::instance()->ci_instance_var('dbforge');
@@ -129,7 +129,8 @@ class Mock_Database_Schema_Skeleton {
),
'misc' => array(
array('id' => 1, 'key' => '\\xxxfoo456', 'value' => 'Entry with \\xxx'),
- array('id' => 2, 'key' => '\\%foo456', 'value' => 'Entry with \\%')
+ array('id' => 2, 'key' => '\\%foo456', 'value' => 'Entry with \\%'),
+ array('id' => 3, 'key' => 'spaces and tabs', 'value' => ' One two three tab')
)
);
diff --git a/tests/mocks/libraries/calendar.php b/tests/mocks/libraries/calendar.php
new file mode 100644
index 000000000..8fee5365e
--- /dev/null
+++ b/tests/mocks/libraries/calendar.php
@@ -0,0 +1,25 @@
+<?php
+
+class Mock_Libraries_Calendar extends CI_Calendar {
+
+ public function __construct($config = array())
+ {
+ $this->CI = new stdClass;
+ $this->CI->lang = new Mock_Core_Lang();
+
+ if ( ! in_array('calendar_lang.php', $this->CI->lang->is_loaded, TRUE))
+ {
+ $this->CI->lang->load('calendar');
+ }
+
+ $this->local_time = time();
+
+ if (count($config) > 0)
+ {
+ $this->initialize($config);
+ }
+
+ log_message('debug', 'Calendar Class Initialized');
+ }
+
+} \ No newline at end of file
diff --git a/tests/mocks/libraries/session.php b/tests/mocks/libraries/session.php
new file mode 100644
index 000000000..c6e194f58
--- /dev/null
+++ b/tests/mocks/libraries/session.php
@@ -0,0 +1,43 @@
+<?php
+
+/**
+ * Mock library to add testing features to Session driver library
+ */
+class Mock_Libraries_Session extends CI_Session {
+
+ /**
+ * Simulate new page load
+ */
+ public function reload()
+ {
+ $this->_flashdata_sweep();
+ $this->_flashdata_mark();
+ $this->_tempdata_sweep();
+ }
+}
+
+/**
+ * Mock cookie driver to overload cookie setting
+ */
+class Mock_Libraries_Session_cookie extends CI_Session_cookie {
+
+ /**
+ * Overload _setcookie to manage $_COOKIE values, since actual cookies can't be set in unit testing
+ */
+ protected function _setcookie($name, $value = '', $expire = 0, $path = '', $domain = '', $secure = FALSE, $httponly = FALSE)
+ {
+ if (empty($value) OR $expire <= time())
+ {
+ unset($_COOKIE[$name]);
+ }
+ else
+ {
+ $_COOKIE[$name] = $value;
+ }
+ }
+}
+
+/**
+ * Mock native driver (just for consistency in loading)
+ */
+class Mock_Libraries_Session_native extends CI_Session_native { } \ No newline at end of file
diff --git a/tests/mocks/libraries/upload.php b/tests/mocks/libraries/upload.php
new file mode 100644
index 000000000..988723e45
--- /dev/null
+++ b/tests/mocks/libraries/upload.php
@@ -0,0 +1,3 @@
+<?php
+
+class Mock_Libraries_Upload extends CI_Upload {} \ No newline at end of file
diff --git a/tests/mocks/uploads/ci_logo.gif b/tests/mocks/uploads/ci_logo.gif
new file mode 100644
index 000000000..073ec14b4
--- /dev/null
+++ b/tests/mocks/uploads/ci_logo.gif
Binary files differ