From f243ce13b4baf5bf8bebf36586514bb243dfc355 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sat, 9 Jun 2012 23:34:21 +0300 Subject: Cleanup/optimize tests/mocks/ --- tests/mocks/autoloader.php | 4 +- tests/mocks/ci_testcase.php | 84 +++++++++++++++--------------- tests/mocks/core/common.php | 36 ++++++------- tests/mocks/core/input.php | 6 +-- tests/mocks/core/loader.php | 7 +-- tests/mocks/core/security.php | 2 +- tests/mocks/core/uri.php | 9 ++-- tests/mocks/core/utf8.php | 11 ++-- tests/mocks/database/config/mysql.php | 10 ++-- tests/mocks/database/config/pdo/mysql.php | 12 ++--- tests/mocks/database/config/pdo/pgsql.php | 12 ++--- tests/mocks/database/config/pdo/sqlite.php | 12 ++--- tests/mocks/database/config/pgsql.php | 10 ++-- tests/mocks/database/config/sqlite.php | 10 ++-- tests/mocks/database/db.php | 18 +++---- tests/mocks/database/db/driver.php | 7 ++- tests/mocks/database/db/querybuilder.php | 9 +--- tests/mocks/database/drivers/mysql.php | 9 ++-- tests/mocks/database/drivers/pdo.php | 8 +-- tests/mocks/database/drivers/postgre.php | 9 ++-- tests/mocks/database/drivers/sqlite.php | 7 +-- tests/mocks/database/schema/skeleton.php | 48 ++++++++--------- tests/mocks/libraries/encrypt.php | 21 ++++---- tests/mocks/libraries/table.php | 3 +- 24 files changed, 180 insertions(+), 184 deletions(-) (limited to 'tests/mocks') diff --git a/tests/mocks/autoloader.php b/tests/mocks/autoloader.php index 90aabcbe6..e3ff7a8bd 100644 --- a/tests/mocks/autoloader.php +++ b/tests/mocks/autoloader.php @@ -69,7 +69,7 @@ function autoload($class) } } - $file = (isset($file)) ? $file : $dir.$class.'.php'; + $file = isset($file) ? $file : $dir.$class.'.php'; if ( ! file_exists($file)) { @@ -82,7 +82,7 @@ function autoload($class) return FALSE; } - throw new InvalidArgumentException("Unable to load $class."); + throw new InvalidArgumentException("Unable to load {$class}."); } include_once($file); diff --git a/tests/mocks/ci_testcase.php b/tests/mocks/ci_testcase.php index f327e6b07..eda9e1b60 100644 --- a/tests/mocks/ci_testcase.php +++ b/tests/mocks/ci_testcase.php @@ -1,11 +1,11 @@ 'bm', 'config' => 'cfg', @@ -19,18 +19,17 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { 'loader' => 'load', 'model' => 'model' ); - + // -------------------------------------------------------------------- - + public function __construct() { parent::__construct(); - $this->ci_config = array(); } - + // -------------------------------------------------------------------- - + public function setUp() { if (method_exists($this, 'set_up')) @@ -38,10 +37,10 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { $this->set_up(); } } - + // -------------------------------------------------------------------- - - public function tearDown() + + public function tearDown() { if (method_exists($this, 'tear_down')) { @@ -50,15 +49,15 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { } // -------------------------------------------------------------------- - + public static function instance() { return self::$ci_test_instance; } - + // -------------------------------------------------------------------- - - function ci_set_config($key, $val = '') + + public function ci_set_config($key, $val = '') { if (is_array($key)) { @@ -71,36 +70,36 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { } // -------------------------------------------------------------------- - - function ci_get_config() + + public function ci_get_config() { return $this->ci_config; } - + // -------------------------------------------------------------------- - - function ci_instance($obj = FALSE) + + public function ci_instance($obj = FALSE) { if ( ! is_object($obj)) { return $this->ci_instance; } - + $this->ci_instance = $obj; } - + // -------------------------------------------------------------------- - - function ci_instance_var($name, $obj = FALSE) + + public function ci_instance_var($name, $obj = FALSE) { if ( ! is_object($obj)) { return $this->ci_instance->$name; } - + $this->ci_instance->$name =& $obj; } - + // -------------------------------------------------------------------- /** @@ -112,10 +111,10 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { * test can modify the variable it assigns to and * still maintain the global. */ - function &ci_core_class($name) + public function &ci_core_class($name) { $name = strtolower($name); - + if (isset($this->global_map[$name])) { $class_name = ucfirst($name); @@ -130,29 +129,29 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { { throw new Exception('Not a valid core class.'); } - + if ( ! class_exists('CI_'.$class_name)) { require_once BASEPATH.'core/'.$class_name.'.php'; } - + $GLOBALS[strtoupper($global_name)] = 'CI_'.$class_name; return $GLOBALS[strtoupper($global_name)]; } - + // -------------------------------------------------------------------- - + // convenience function for global mocks - function ci_set_core_class($name, $obj) + public function ci_set_core_class($name, $obj) { $orig =& $this->ci_core_class($name); $orig = $obj; } - + // -------------------------------------------------------------------- // Internals // -------------------------------------------------------------------- - + /** * Overwrite runBare * @@ -169,28 +168,27 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { } // -------------------------------------------------------------------- - - function helper($name) + + public function helper($name) { require_once(BASEPATH.'helpers/'.$name.'_helper.php'); } // -------------------------------------------------------------------- - + /** * This overload is useful to create a stub, that need to have a specific method. */ - function __call($method, $args) + public function __call($method, $args) { - if ($this->{$method} instanceof Closure) + if ($this->{$method} instanceof Closure) { return call_user_func_array($this->{$method},$args); - } - else + } + else { return parent::__call($method, $args); } } -} -// EOF \ No newline at end of file +} \ No newline at end of file diff --git a/tests/mocks/core/common.php b/tests/mocks/core/common.php index e1c493aa0..8466e47f8 100644 --- a/tests/mocks/core/common.php +++ b/tests/mocks/core/common.php @@ -4,11 +4,10 @@ if ( ! function_exists('get_instance')) { - function &get_instance() + function &get_instance() { $test = CI_TestCase::instance(); - $instance = $test->ci_instance(); - return $instance; + return $test->ci_instance(); } } @@ -16,11 +15,10 @@ if ( ! function_exists('get_instance')) if ( ! function_exists('get_config')) { - function &get_config() { + function &get_config() + { $test = CI_TestCase::instance(); - $config = $test->ci_get_config(); - - return $config; + return $test->ci_get_config(); } } @@ -29,12 +27,12 @@ if ( ! function_exists('config_item')) function config_item($item) { $config =& get_config(); - + if ( ! isset($config[$item])) { return FALSE; } - + return $config[$item]; } } @@ -49,16 +47,16 @@ if ( ! function_exists('load_class')) { 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.''); + throw new Exception('Bad Isolation: Use ci_set_core_class to set '.$class); } - + return $obj; } } @@ -74,16 +72,16 @@ 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 @@ -166,6 +164,4 @@ if ( ! function_exists('set_status_header')) { return TRUE; } -} - -// EOF \ No newline at end of file +} \ No newline at end of file diff --git a/tests/mocks/core/input.php b/tests/mocks/core/input.php index 8a337d2ef..2a4aa4997 100644 --- a/tests/mocks/core/input.php +++ b/tests/mocks/core/input.php @@ -1,10 +1,10 @@ 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 index d7ea0e6bd..e19a8b20b 100644 --- a/tests/mocks/core/security.php +++ b/tests/mocks/core/security.php @@ -1,7 +1,7 @@ ci_core_class('cfg'); - + // set predictable config values $test->ci_set_config(array( 'index_page' => 'index.php', @@ -14,12 +14,13 @@ class Mock_Core_URI extends CI_URI { 'subclass_prefix' => 'MY_' )); - $this->config = new $cls; + $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 index b77d717e7..068e74ac1 100644 --- a/tests/mocks/core/utf8.php +++ b/tests/mocks/core/utf8.php @@ -1,27 +1,26 @@ array( 'dsn' => '', @@ -9,7 +9,7 @@ return array( 'username' => 'travis', 'password' => '', 'database' => 'ci_test', - 'dbdriver' => 'mysql', + 'dbdriver' => 'mysql' ), // Database configuration with failover @@ -28,7 +28,7 @@ return array( 'password' => '', 'database' => 'ci_test', 'dbdriver' => 'mysql', - ), - ), - ), + ) + ) + ) ); \ No newline at end of file diff --git a/tests/mocks/database/config/pdo/mysql.php b/tests/mocks/database/config/pdo/mysql.php index cefb6b008..fefe0d624 100644 --- a/tests/mocks/database/config/pdo/mysql.php +++ b/tests/mocks/database/config/pdo/mysql.php @@ -1,7 +1,7 @@ array( 'dsn' => '', @@ -10,7 +10,7 @@ return array( 'password' => '', 'database' => 'ci_test', 'dbdriver' => 'pdo', - 'pdodriver' => 'mysql', + 'pdodriver' => 'mysql' ), // Database configuration with failover @@ -30,8 +30,8 @@ return array( 'password' => '', 'database' => 'ci_test', 'dbdriver' => 'pdo', - 'pdodriver' => 'mysql', - ), - ), - ), + 'pdodriver' => 'mysql' + ) + ) + ) ); \ No newline at end of file diff --git a/tests/mocks/database/config/pdo/pgsql.php b/tests/mocks/database/config/pdo/pgsql.php index 5196e9ad9..ddd638c8a 100644 --- a/tests/mocks/database/config/pdo/pgsql.php +++ b/tests/mocks/database/config/pdo/pgsql.php @@ -1,7 +1,7 @@ array( 'dsn' => 'pgsql:host=localhost;port=5432;dbname=ci_test;', @@ -10,7 +10,7 @@ return array( 'password' => '', 'database' => 'ci_test', 'dbdriver' => 'pdo', - 'pdodriver' => 'pgsql', + 'pdodriver' => 'pgsql' ), // Database configuration with failover @@ -30,8 +30,8 @@ return array( 'password' => '', 'database' => 'ci_test', 'dbdriver' => 'pdo', - 'pdodriver' => 'pgsql', - ), - ), - ), + 'pdodriver' => 'pgsql' + ) + ) + ) ); \ No newline at end of file diff --git a/tests/mocks/database/config/pdo/sqlite.php b/tests/mocks/database/config/pdo/sqlite.php index c68b4b213..36461843d 100644 --- a/tests/mocks/database/config/pdo/sqlite.php +++ b/tests/mocks/database/config/pdo/sqlite.php @@ -10,7 +10,7 @@ return array( 'password' => 'sqlite', 'database' => 'sqlite', 'dbdriver' => 'pdo', - 'pdodriver' => 'sqlite', + 'pdodriver' => 'sqlite' ), // Database configuration with failover @@ -29,9 +29,9 @@ return array( 'username' => 'sqlite', 'password' => 'sqlite', 'database' => 'sqlite', - 'dbdriver' => 'pdo', - 'pdodriver' => 'sqlite', - ), - ), - ), + 'dbdriver' => 'pdo', + 'pdodriver' => 'sqlite' + ) + ) + ) ); \ No newline at end of file diff --git a/tests/mocks/database/config/pgsql.php b/tests/mocks/database/config/pgsql.php index c06af8ce0..1444b0066 100644 --- a/tests/mocks/database/config/pgsql.php +++ b/tests/mocks/database/config/pgsql.php @@ -1,7 +1,7 @@ array( 'dsn' => '', @@ -9,7 +9,7 @@ return array( 'username' => 'postgres', 'password' => '', 'database' => 'ci_test', - 'dbdriver' => 'postgre', + 'dbdriver' => 'postgre' ), // Database configuration with failover @@ -28,7 +28,7 @@ return array( 'password' => '', 'database' => 'ci_test', 'dbdriver' => 'postgre', - ), - ), - ), + ) + ) + ) ); \ No newline at end of file diff --git a/tests/mocks/database/config/sqlite.php b/tests/mocks/database/config/sqlite.php index 755ce2a3a..d37ee4871 100644 --- a/tests/mocks/database/config/sqlite.php +++ b/tests/mocks/database/config/sqlite.php @@ -9,7 +9,7 @@ return array( 'username' => 'sqlite', 'password' => 'sqlite', 'database' => realpath(__DIR__.'/..').'/ci_test.sqlite', - 'dbdriver' => 'sqlite3', + 'dbdriver' => 'sqlite3' ), // Database configuration with failover @@ -27,8 +27,8 @@ return array( 'username' => 'sqlite', 'password' => 'sqlite', 'database' => realpath(__DIR__.'/..').'/ci_test.sqlite', - 'dbdriver' => 'sqlite3', - ), - ), - ), + 'dbdriver' => 'sqlite3' + ) + ) + ) ); \ No newline at end of file diff --git a/tests/mocks/database/db.php b/tests/mocks/database/db.php index 59028ed9c..30504bba6 100644 --- a/tests/mocks/database/db.php +++ b/tests/mocks/database/db.php @@ -6,7 +6,7 @@ class Mock_Database_DB { * @var array DB configuration */ private $config = array(); - + /** * Prepare database configuration skeleton * @@ -21,7 +21,7 @@ class Mock_Database_DB { /** * Build DSN connection string for DB driver instantiate process * - * @param string Group name + * @param string Group name * @return string DSN Connection string */ public function set_dsn($group = 'default') @@ -65,28 +65,27 @@ class Mock_Database_DB { * Return a database config array * * @see ./config - * @param string Driver based configuration - * @return array + * @param string Driver based configuration + * @return array */ public static function config($driver) { $dir = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR; - return include($dir.'config'.DIRECTORY_SEPARATOR.$driver.'.php'); } /** * Main DB method wrapper * - * @param string Group or DSN string - * @param bool - * @return object + * @param string Group or DSN string + * @param bool + * @return object */ public static function DB($group, $query_builder = FALSE) { include_once(BASEPATH.'database/DB.php'); - try + try { $db = DB($group, $query_builder); } @@ -97,4 +96,5 @@ class Mock_Database_DB { return $db; } + } \ No newline at end of file diff --git a/tests/mocks/database/db/driver.php b/tests/mocks/database/db/driver.php index cb1820277..65ac2c4cc 100644 --- a/tests/mocks/database/db/driver.php +++ b/tests/mocks/database/db/driver.php @@ -1,7 +1,7 @@ ci_db_driver = new $driver_class($config); + if (is_string($driver_class)) + { + $this->ci_db_driver = new $driver_class($config); + } } /** diff --git a/tests/mocks/database/db/querybuilder.php b/tests/mocks/database/db/querybuilder.php index 1b95c92af..3f2252622 100644 --- a/tests/mocks/database/db/querybuilder.php +++ b/tests/mocks/database/db/querybuilder.php @@ -1,10 +1,3 @@ add_field(array( 'id' => array( 'type' => 'INTEGER', - 'constraint' => 3, + 'constraint' => 3 ), 'name' => array( 'type' => 'VARCHAR', - 'constraint' => 40, + 'constraint' => 40 ), 'email' => array( 'type' => 'VARCHAR', - 'constraint' => 100, + 'constraint' => 100 ), 'country' => array( 'type' => 'VARCHAR', - 'constraint' => 40, - ), + 'constraint' => 40 + ) )); static::$forge->add_key('id', TRUE); static::$forge->create_table('user', (strpos(static::$driver, 'pgsql') === FALSE)); @@ -76,15 +75,15 @@ class Mock_Database_Schema_Skeleton { static::$forge->add_field(array( 'id' => array( 'type' => 'INTEGER', - 'constraint' => 3, + 'constraint' => 3 ), 'name' => array( 'type' => 'VARCHAR', - 'constraint' => 40, + 'constraint' => 40 ), 'description' => array( - 'type' => 'TEXT', - ), + 'type' => 'TEXT' + ) )); static::$forge->add_key('id', TRUE); static::$forge->create_table('job', (strpos(static::$driver, 'pgsql') === FALSE)); @@ -93,15 +92,15 @@ class Mock_Database_Schema_Skeleton { static::$forge->add_field(array( 'id' => array( 'type' => 'INTEGER', - 'constraint' => 3, + 'constraint' => 3 ), 'key' => array( 'type' => 'VARCHAR', - 'constraint' => 40, + 'constraint' => 40 ), 'value' => array( - 'type' => 'TEXT', - ), + 'type' => 'TEXT' + ) )); static::$forge->add_key('id', TRUE); static::$forge->create_table('misc', (strpos(static::$driver, 'pgsql') === FALSE)); @@ -120,28 +119,29 @@ class Mock_Database_Schema_Skeleton { 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'), + 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' => 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'), + 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 \\%'), - ), + array('id' => 1, 'key' => '\\xxxfoo456', 'value' => 'Entry with \\xxx'), + array('id' => 2, 'key' => '\\%foo456', 'value' => 'Entry with \\%') + ) ); - foreach ($data as $table => $dummy_data) + foreach ($data as $table => $dummy_data) { static::$db->truncate($table); foreach ($dummy_data as $single_dummy_data) { - static::$db->insert($table, $single_dummy_data); + static::$db->insert($table, $single_dummy_data); } } } + } \ No newline at end of file diff --git a/tests/mocks/libraries/encrypt.php b/tests/mocks/libraries/encrypt.php index a9bbaafdc..f1859398f 100644 --- a/tests/mocks/libraries/encrypt.php +++ b/tests/mocks/libraries/encrypt.php @@ -2,14 +2,15 @@ class Mock_Libraries_Encrypt extends CI_Encrypt { - // 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'); - } + // 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/libraries/table.php b/tests/mocks/libraries/table.php index 97fbb30bd..87c278bce 100644 --- a/tests/mocks/libraries/table.php +++ b/tests/mocks/libraries/table.php @@ -1,7 +1,7 @@