Skip to content

Instantly share code, notes, and snippets.

@tinkertim
Created May 3, 2012 13:37
Show Gist options
  • Save tinkertim/2585711 to your computer and use it in GitHub Desktop.
Save tinkertim/2585711 to your computer and use it in GitHub Desktop.
A fake Codeigniter framework for testing codeigniter-redis
<?php
error_reporting(E_ALL);
/**
* A 'fake' Codeigniter framework for testing codeigniter-redis
* I seriously wrote this because it was faster than setting up another CI install, so I can
* work on it without breaking stuff that other people are using.
* Written by Tim Post <[email protected]> I take no responsibility for what comes below!
**/
/* Provide some of the CI common functions */
function get_instance() { return fakeIgniter::$instance; }
function log_message($level, $message) { echo '<p>'.$message.'</p>'; }
function show_error($msg, $status = 500) { die('[Error]: '.$msg); }
/* Little pigs, little pigs, LET ME IN! */
define('BASEPATH', dirname(__FILE__));
/* Provide a fake loader class */
class fakeLoader {
public function config() {}
}
/* Provide a fake config class */
class fakeConfig {
/* Set values you'd expect $this->config->item() to retrieve here */
private $vars = array(
'redis_host' => '127.0.0.1',
'redis_port' => '6379',
'redis_password' => 'swordfish'
);
public function item($key) {
/* Fakes $this->config->item() */
return isset($this->vars[$key]) ? $this->vars[$key] : NULL;
}
}
/* Now provide a fake CI for get_instance() to dereference */
class fakeIgniter {
public static $instance;
public $load;
public $config;
public function __construct() {
$this->load = new fakeLoader();
$this->config = new fakeConfig();
self::$instance =& $this;
}
}
$fakeci = new fakeIgniter();
/*
* That's it, no modifications to the library are needed. It can just call
* get_instance() / etc as usual within it.
*/
require('Redis.php');
/* Now just work with the library */
$redis = new Redis(); /* IT CAN HAZ get_instance() */
$redis->debug = TRUE;
$redis->command('SELECT 12');
$redis->set('foo bar');
$test = $redis->get('foo');
echo 'Test is: '.$test;
?>
@tinkertim
Copy link
Author

I have revised this a tiny bit, but it really took less than five minutes to write. I just wanted a convenient way to work with the library without dirtying dev or production repos, since I'm working on it remotely :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment