diff options
25 files changed, 489 insertions, 34 deletions
diff --git a/.travis.yml b/.travis.yml index 84029b964..97ea0422d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,14 +8,17 @@ env: - DB=mysql - DB=pgsql - DB=sqlite + - DB=pdo/mysql + - DB=pdo/pgsql + - DB=pdo/sqlite before_script: - pyrus channel-discover pear.php-tools.net - pyrus install http://pear.php-tools.net/get/vfsStream-0.11.2.tgz - phpenv rehash - - sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'DROP DATABASE IF EXISTS ci_test;' -U postgres; fi" - - sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'create database ci_test;' -U postgres; fi" - - sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'create database IF NOT EXISTS ci_test;'; fi" + - sh -c "if [ '$DB' = 'pgsql' ] || [ '$DB' = 'pdo/pgsql' ]; then psql -c 'DROP DATABASE IF EXISTS ci_test;' -U postgres; fi" + - sh -c "if [ '$DB' = 'pgsql' ] || [ '$DB' = 'pdo/pgsql' ]; then psql -c 'create database ci_test;' -U postgres; fi" + - sh -c "if [ '$DB' = 'mysql' ] || [ '$DB' = 'pdo/mysql' ]; then mysql -e 'create database IF NOT EXISTS ci_test;'; fi" script: phpunit --configuration tests/travis/$DB.phpunit.xml diff --git a/tests/codeigniter/database/DB_driver_test.php b/tests/codeigniter/database/DB_driver_test.php new file mode 100644 index 000000000..fb40f0608 --- /dev/null +++ b/tests/codeigniter/database/DB_driver_test.php @@ -0,0 +1,36 @@ +<?php + +class DB_driver_test extends CI_TestCase { + + // ------------------------------------------------------------------------ + + public function test_initialize() + { + $config = Mock_Database_DB::config(DB_DRIVER); + $driver_name = current(explode('/', DB_DRIVER)); + $driver = $this->$driver_name($config[DB_DRIVER]); + + $this->assertTrue($driver->initialize()); + } + + protected function pdo($config) + { + return new Mock_Database_Drivers_PDO($config); + } + + protected function mysql($config) + { + return new Mock_Database_Drivers_Mysql($config); + } + + protected function sqlite($config) + { + return new Mock_Database_Drivers_Sqlite($config); + } + + protected function pgsql($config) + { + return new Mock_Database_Drivers_Postgre($config); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/.gitkeep b/tests/codeigniter/database/query_builder/.gitkeep deleted file mode 100644 index e69de29bb..000000000 --- a/tests/codeigniter/database/query_builder/.gitkeep +++ /dev/null diff --git a/tests/codeigniter/database/query_builder/get_test.php b/tests/codeigniter/database/query_builder/get_test.php new file mode 100644 index 000000000..e6fe319c5 --- /dev/null +++ b/tests/codeigniter/database/query_builder/get_test.php @@ -0,0 +1,48 @@ +<?php + +class Get_test extends CI_TestCase { + + /** + * @var object Database/Query Builder holder + */ + protected $query_builder; + + public function set_up() + { + $config = Mock_Database_DB::config(DB_DRIVER); + $connection = new Mock_Database_DB($config); + $db = Mock_Database_DB::DB($connection->set_dsn(DB_DRIVER), TRUE); + + $this->ci_instance_var('db', $db); + + $loader = new Mock_Core_Loader(); + $loader->dbforge(); + + $forge = $this->ci_instance->dbforge; + + Mock_Database_Schema_Skeleton::create_tables($forge, DB_DRIVER); + Mock_Database_Schema_Skeleton::create_data($db); + + $this->query_builder = $db; + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_get_simple() + { + $jobs = $this->query_builder->get('job')->result_array(); + + // Dummy jobs contain 4 rows + $this->assertCount(4, $jobs); + + // Check rows item + $this->assertEquals('Developer', $jobs[0]['name']); + $this->assertEquals('Politician', $jobs[1]['name']); + $this->assertEquals('Accountant', $jobs[2]['name']); + $this->assertEquals('Musician', $jobs[3]['name']); + } + +}
\ No newline at end of file diff --git a/tests/mocks/autoloader.php b/tests/mocks/autoloader.php index dd5929206..f1bdb5d6f 100644 --- a/tests/mocks/autoloader.php +++ b/tests/mocks/autoloader.php @@ -53,13 +53,24 @@ function autoload($class) $dir = BASEPATH.'libraries'.DIRECTORY_SEPARATOR; $class = $subclass; } + 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; + $file = $dir.$m[1].'_'.$m[2].'.php'; + } + elseif (strpos($class, 'CI_DB') === 0) + { + $dir = BASEPATH.'database'.DIRECTORY_SEPARATOR; + $file = $dir.str_replace(array('CI_DB','active_record'), array('DB', 'active_rec'), $subclass).'.php'; + } else { $class = strtolower($class); } } - $file = $dir.$class.'.php'; + $file = (isset($file)) ? $file : $dir.$class.'.php'; if ( ! file_exists($file)) { @@ -71,7 +82,7 @@ function autoload($class) { return FALSE; } - + throw new InvalidArgumentException("Unable to load $class."); } diff --git a/tests/mocks/database/ci_test.sqlite b/tests/mocks/database/ci_test.sqlite Binary files differindex 37ce4f870..86d868af2 100755 --- a/tests/mocks/database/ci_test.sqlite +++ b/tests/mocks/database/ci_test.sqlite diff --git a/tests/mocks/database/config/pdo/mysql.php b/tests/mocks/database/config/pdo/mysql.php new file mode 100644 index 000000000..cefb6b008 --- /dev/null +++ b/tests/mocks/database/config/pdo/mysql.php @@ -0,0 +1,37 @@ +<?php + +return array( + + // Typical Database configuration + 'pdo/mysql' => array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'travis', + 'password' => '', + 'database' => 'ci_test', + 'dbdriver' => 'pdo', + 'pdodriver' => 'mysql', + ), + + // Database configuration with failover + 'pdo/mysql_failover' => array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'not_travis', + 'password' => 'wrong password', + 'database' => 'not_ci_test', + 'dbdriver' => 'pdo', + 'pdodriver' => 'mysql', + 'failover' => array( + array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'travis', + 'password' => '', + 'database' => 'ci_test', + 'dbdriver' => 'pdo', + '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 new file mode 100644 index 000000000..5196e9ad9 --- /dev/null +++ b/tests/mocks/database/config/pdo/pgsql.php @@ -0,0 +1,37 @@ +<?php + +return array( + + // Typical Database configuration + 'pdo/pgsql' => array( + 'dsn' => 'pgsql:host=localhost;port=5432;dbname=ci_test;', + 'hostname' => 'localhost', + 'username' => 'postgres', + 'password' => '', + 'database' => 'ci_test', + 'dbdriver' => 'pdo', + 'pdodriver' => 'pgsql', + ), + + // Database configuration with failover + 'pdo/pgsql_failover' => array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'not_travis', + 'password' => 'wrong password', + 'database' => 'not_ci_test', + 'dbdriver' => 'pdo', + 'pdodriver' => 'pgsql', + 'failover' => array( + array( + 'dsn' => 'pgsql:host=localhost;port=5432;dbname=ci_test;', + 'hostname' => 'localhost', + 'username' => 'postgres', + 'password' => '', + 'database' => 'ci_test', + 'dbdriver' => 'pdo', + '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 new file mode 100644 index 000000000..c68b4b213 --- /dev/null +++ b/tests/mocks/database/config/pdo/sqlite.php @@ -0,0 +1,37 @@ +<?php + +return array( + + // Typical Database configuration + 'pdo/sqlite' => array( + 'dsn' => 'sqlite:/'.realpath(__DIR__.'/../..').'/ci_test.sqlite', + 'hostname' => 'localhost', + 'username' => 'sqlite', + 'password' => 'sqlite', + 'database' => 'sqlite', + 'dbdriver' => 'pdo', + 'pdodriver' => 'sqlite', + ), + + // Database configuration with failover + 'pdo/sqlite_failover' => array( + 'dsn' => 'sqlite:not_exists.sqlite', + 'hostname' => 'localhost', + 'username' => 'sqlite', + 'password' => 'sqlite', + 'database' => 'sqlite', + 'dbdriver' => 'pdo', + 'pdodriver' => 'sqlite', + 'failover' => array( + array( + 'dsn' => 'sqlite:/'.realpath(__DIR__.'/../..').'/ci_test.sqlite', + 'hostname' => 'localhost', + 'username' => 'sqlite', + 'password' => 'sqlite', + 'database' => 'sqlite', + 'dbdriver' => 'pdo', + 'pdodriver' => 'sqlite', + ), + ), + ), +);
\ No newline at end of file diff --git a/tests/mocks/database/config/sqlite.php b/tests/mocks/database/config/sqlite.php index 8665e208d..755ce2a3a 100644 --- a/tests/mocks/database/config/sqlite.php +++ b/tests/mocks/database/config/sqlite.php @@ -1,5 +1,4 @@ <?php -$dbdriver = is_php('5.4') ? 'sqlite3' : 'sqlite'; return array( @@ -10,7 +9,7 @@ return array( 'username' => 'sqlite', 'password' => 'sqlite', 'database' => realpath(__DIR__.'/..').'/ci_test.sqlite', - 'dbdriver' => $dbdriver, + 'dbdriver' => 'sqlite3', ), // Database configuration with failover @@ -20,15 +19,15 @@ return array( 'username' => 'sqlite', 'password' => 'sqlite', 'database' => '../not_exists.sqlite', - 'dbdriver' => $dbdriver, + 'dbdriver' => 'sqlite3', 'failover' => array( array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'sqlite', 'password' => 'sqlite', - 'database' => realpath(__DIR__.'/..').'/ci_testf.sqlite', - 'dbdriver' => $dbdriver, + 'database' => realpath(__DIR__.'/..').'/ci_test.sqlite', + 'dbdriver' => 'sqlite3', ), ), ), diff --git a/tests/mocks/database/db.php b/tests/mocks/database/db.php index 43a0d391f..59028ed9c 100644 --- a/tests/mocks/database/db.php +++ b/tests/mocks/database/db.php @@ -45,19 +45,18 @@ class Mock_Database_DB { ); $config = array_merge($this->config[$group], $params); + $dsnstring = ( ! empty($config['dsn'])) ? $config['dsn'] : FALSE; + $pdodriver = ( ! empty($config['pdodriver'])) ? $config['pdodriver'] : FALSE; + $failover = ( ! empty($config['failover'])) ? $config['failover'] : FALSE; - if ( ! empty($config['dsn'])) - { - $dsn = $config['dsn']; - } - else - { - $dsn = $config['dbdriver'].'://'.$config['username'].':'.$config['password'] + $dsn = $config['dbdriver'].'://'.$config['username'].':'.$config['password'] .'@'.$config['hostname'].'/'.$config['database']; - } - + // Build the parameter $other_params = array_slice($config, 6); + if ($dsnstring) $other_params['dsn'] = $dsnstring; + if ($pdodriver) $other_params['pdodriver'] = $pdodriver; + if ($failover) $other_params['failover'] = $failover; return $dsn.'?'.http_build_query($other_params); } diff --git a/tests/mocks/database/db/driver.php b/tests/mocks/database/db/driver.php new file mode 100644 index 000000000..cb1820277 --- /dev/null +++ b/tests/mocks/database/db/driver.php @@ -0,0 +1,36 @@ +<?php + +class Mock_Database_DB_Driver extends CI_DB_driver { + + /** + * @var object The actual Driver + */ + protected $ci_db_driver; + + /** + * Instantiate the database driver + * + * @param string DB Driver class name + * @param array DB configuration to set + * @return void + */ + public function __construct($driver_class, $config = array()) + { + if (is_string($driver_class)) $this->ci_db_driver = new $driver_class($config); + } + + /** + * Overloading method, emulate the actual driver method (multiple inheritance workaround) + */ + public function __call($method, $arguments) + { + if ( ! is_callable(array($this->ci_db_driver, $method))) + { + throw new BadMethodCallException($method. ' not exists or not implemented'); + } + + return call_user_func_array(array($this->ci_db_driver, $method), $arguments); + } +} + +class CI_DB extends Mock_Database_DB_QueryBuilder {}
\ No newline at end of file diff --git a/tests/mocks/database/db/querybuilder.php b/tests/mocks/database/db/querybuilder.php new file mode 100644 index 000000000..1b95c92af --- /dev/null +++ b/tests/mocks/database/db/querybuilder.php @@ -0,0 +1,10 @@ +<?php + +if ( ! class_exists('CI_DB_query_builder')) +{ + class Mock_Database_DB_QueryBuilder extends CI_DB_active_record {} +} +else +{ + class Mock_Database_DB_QueryBuilder extends CI_DB_query_builder {} +} diff --git a/tests/mocks/database/drivers/mysql.php b/tests/mocks/database/drivers/mysql.php new file mode 100644 index 000000000..34a74e2bf --- /dev/null +++ b/tests/mocks/database/drivers/mysql.php @@ -0,0 +1,16 @@ +<?php + +class Mock_Database_Drivers_Mysql extends Mock_Database_DB_Driver { + + /** + * Instantiate the database driver + * + * @param string DB Driver class name + * @param array DB configuration to set + * @return void + */ + public function __construct($config = array()) + { + parent::__construct('CI_DB_mysql_driver', $config); + } +}
\ No newline at end of file diff --git a/tests/mocks/database/drivers/pdo.php b/tests/mocks/database/drivers/pdo.php new file mode 100644 index 000000000..590e19552 --- /dev/null +++ b/tests/mocks/database/drivers/pdo.php @@ -0,0 +1,16 @@ +<?php + +class Mock_Database_Drivers_PDO extends Mock_Database_DB_Driver { + + /** + * Instantiate the database driver + * + * @param string DB Driver class name + * @param array DB configuration to set + * @return void + */ + public function __construct($config = array()) + { + parent::__construct('CI_DB_pdo_driver', $config); + } +}
\ No newline at end of file diff --git a/tests/mocks/database/drivers/postgre.php b/tests/mocks/database/drivers/postgre.php new file mode 100644 index 000000000..0df905963 --- /dev/null +++ b/tests/mocks/database/drivers/postgre.php @@ -0,0 +1,16 @@ +<?php + +class Mock_Database_Drivers_Postgre extends Mock_Database_DB_Driver { + + /** + * Instantiate the database driver + * + * @param string DB Driver class name + * @param array DB configuration to set + * @return void + */ + public function __construct($config = array()) + { + parent::__construct('CI_DB_postgre_driver', $config); + } +}
\ No newline at end of file diff --git a/tests/mocks/database/drivers/sqlite.php b/tests/mocks/database/drivers/sqlite.php new file mode 100644 index 000000000..76a182cbf --- /dev/null +++ b/tests/mocks/database/drivers/sqlite.php @@ -0,0 +1,16 @@ +<?php + +class Mock_Database_Drivers_Sqlite extends Mock_Database_DB_Driver { + + /** + * Instantiate the database driver + * + * @param string DB Driver class name + * @param array DB configuration to set + * @return void + */ + public function __construct($config = array()) + { + parent::__construct('CI_DB_sqlite3_driver', $config); + } +}
\ No newline at end of file diff --git a/tests/mocks/database/schema/.gitkeep b/tests/mocks/database/schema/.gitkeep deleted file mode 100644 index e69de29bb..000000000 --- a/tests/mocks/database/schema/.gitkeep +++ /dev/null diff --git a/tests/mocks/database/schema/skeleton.php b/tests/mocks/database/schema/skeleton.php new file mode 100644 index 000000000..6610d6b59 --- /dev/null +++ b/tests/mocks/database/schema/skeleton.php @@ -0,0 +1,57 @@ +<?php + +class Mock_Database_Schema_Skeleton { + + /** + * Create the dummy tables + * + * @return void + */ + public static function create_tables($forge, $driver) + { + // Job Table + $forge->add_field(array( + 'id' => array( + 'type' => 'INTEGER', + 'constraint' => 3, + ), + 'name' => array( + 'type' => 'VARCHAR', + 'constraint' => 40, + ), + 'description' => array( + 'type' => 'TEXT', + ), + )); + $forge->add_key('id', TRUE); + $forge->create_table('job', (strpos($driver, 'pgsql') === FALSE)); + } + + /** + * Create the dummy datas + * + * @return void + */ + public static function create_data($db) + { + // Job Data + $data = array( + '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'), + ), + ); + + foreach ($data as $table => $dummy_data) + { + $db->truncate($table); + + foreach ($dummy_data as $single_dummy_data) + { + $db->insert($table, $single_dummy_data); + } + } + } +}
\ No newline at end of file diff --git a/tests/travis/mysql.phpunit.xml b/tests/travis/mysql.phpunit.xml index 44d6d6ed9..e9556f758 100644 --- a/tests/travis/mysql.phpunit.xml +++ b/tests/travis/mysql.phpunit.xml @@ -28,10 +28,5 @@ <directory suffix=".php">PROJECT_BASE.'tests'</directory> <directory suffix=".php">'../../system/core/CodeIgniter.php'</directory> </blacklist> - <whitelist> - <!-- - <directory suffix=".php">'../system/core'</directory> - --> - </whitelist> </filters> </phpunit>
\ No newline at end of file diff --git a/tests/travis/pdo/mysql.phpunit.xml b/tests/travis/pdo/mysql.phpunit.xml new file mode 100644 index 000000000..69eece24f --- /dev/null +++ b/tests/travis/pdo/mysql.phpunit.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<phpunit + bootstrap="../../Bootstrap.php" + colors="true" + convertNoticesToExceptions="true" + convertWarningsToExceptions="true" + stopOnError="false" + stopOnFailure="false" + stopOnIncomplete="false" + stopOnSkipped="false"> + <php> + <const name="DB_DRIVER" value="pdo/mysql"/> + </php> + <testsuites> + <testsuite name="CodeIgniter Core Test Suite"> + <file>../../codeigniter/Setup_test.php</file> + <directory suffix="test.php">../../codeigniter/core</directory> + <directory suffix="test.php">../../codeigniter/helpers</directory> + <directory suffix="test.php">../../codeigniter/libraries</directory> + <directory suffix="test.php">../../codeigniter/database</directory> + </testsuite> + </testsuites> + <filters> + <blacklist> + <directory suffix=".php">PEAR_INSTALL_DIR</directory> + <directory suffix=".php">PHP_LIBDIR</directory> + <directory suffix=".php">PROJECT_BASE.'tests'</directory> + <directory suffix=".php">'../../../system/core/CodeIgniter.php'</directory> + </blacklist> + </filters> +</phpunit>
\ No newline at end of file diff --git a/tests/travis/pdo/pgsql.phpunit.xml b/tests/travis/pdo/pgsql.phpunit.xml new file mode 100644 index 000000000..e68c3e028 --- /dev/null +++ b/tests/travis/pdo/pgsql.phpunit.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<phpunit + bootstrap="../../Bootstrap.php" + colors="true" + convertNoticesToExceptions="true" + convertWarningsToExceptions="true" + stopOnError="false" + stopOnFailure="false" + stopOnIncomplete="false" + stopOnSkipped="false"> + <php> + <const name="DB_DRIVER" value="pdo/pgsql"/> + </php> + <testsuites> + <testsuite name="CodeIgniter Core Test Suite"> + <file>../../codeigniter/Setup_test.php</file> + <directory suffix="test.php">../../codeigniter/core</directory> + <directory suffix="test.php">../../codeigniter/helpers</directory> + <directory suffix="test.php">../../codeigniter/libraries</directory> + <directory suffix="test.php">../../codeigniter/database</directory> + </testsuite> + </testsuites> + <filters> + <blacklist> + <directory suffix=".php">PEAR_INSTALL_DIR</directory> + <directory suffix=".php">PHP_LIBDIR</directory> + <directory suffix=".php">PROJECT_BASE.'tests'</directory> + <directory suffix=".php">'../../../system/core/CodeIgniter.php'</directory> + </blacklist> + </filters> +</phpunit>
\ No newline at end of file diff --git a/tests/travis/pdo/sqlite.phpunit.xml b/tests/travis/pdo/sqlite.phpunit.xml new file mode 100644 index 000000000..1871f6221 --- /dev/null +++ b/tests/travis/pdo/sqlite.phpunit.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<phpunit + bootstrap="../../Bootstrap.php" + colors="true" + convertNoticesToExceptions="true" + convertWarningsToExceptions="true" + stopOnError="false" + stopOnFailure="false" + stopOnIncomplete="false" + stopOnSkipped="false"> + <php> + <const name="DB_DRIVER" value="pdo/sqlite"/> + </php> + <testsuites> + <testsuite name="CodeIgniter Core Test Suite"> + <file>../../codeigniter/Setup_test.php</file> + <directory suffix="test.php">../../codeigniter/core</directory> + <directory suffix="test.php">../../codeigniter/helpers</directory> + <directory suffix="test.php">../../codeigniter/libraries</directory> + <directory suffix="test.php">../../codeigniter/database</directory> + </testsuite> + </testsuites> + <filters> + <blacklist> + <directory suffix=".php">PEAR_INSTALL_DIR</directory> + <directory suffix=".php">PHP_LIBDIR</directory> + <directory suffix=".php">PROJECT_BASE.'tests'</directory> + <directory suffix=".php">'../../../system/core/CodeIgniter.php'</directory> + </blacklist> + </filters> +</phpunit>
\ No newline at end of file diff --git a/tests/travis/pgsql.phpunit.xml b/tests/travis/pgsql.phpunit.xml index 9f52b40ae..ad8aeded2 100644 --- a/tests/travis/pgsql.phpunit.xml +++ b/tests/travis/pgsql.phpunit.xml @@ -28,10 +28,5 @@ <directory suffix=".php">PROJECT_BASE.'tests'</directory> <directory suffix=".php">'../../system/core/CodeIgniter.php'</directory> </blacklist> - <whitelist> - <!-- - <directory suffix=".php">'../system/core'</directory> - --> - </whitelist> </filters> </phpunit>
\ No newline at end of file diff --git a/tests/travis/sqlite.phpunit.xml b/tests/travis/sqlite.phpunit.xml index 74ebb482b..628370e93 100644 --- a/tests/travis/sqlite.phpunit.xml +++ b/tests/travis/sqlite.phpunit.xml @@ -28,10 +28,5 @@ <directory suffix=".php">PROJECT_BASE.'tests'</directory> <directory suffix=".php">'../../system/core/CodeIgniter.php'</directory> </blacklist> - <whitelist> - <!-- - <directory suffix=".php">'../system/core'</directory> - --> - </whitelist> </filters> </phpunit>
\ No newline at end of file |