Skip to content

Instantly share code, notes, and snippets.

@dovidezra
Forked from tinkertim/fakeigniter.php
Created September 10, 2019 06:48
Show Gist options
  • Save dovidezra/7834681d662aa4b2af4d41e2e9009372 to your computer and use it in GitHub Desktop.
Save dovidezra/7834681d662aa4b2af4d41e2e9009372 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 <tpost@ezp.net> 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;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment