diff options
Diffstat (limited to 'tests')
85 files changed, 3143 insertions, 390 deletions
diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php index 39c24b219..5216038c6 100644 --- a/tests/Bootstrap.php +++ b/tests/Bootstrap.php @@ -6,16 +6,33 @@ error_reporting(E_ALL | E_STRICT); $dir = realpath(dirname(__FILE__)); - // Path constants -define('PROJECT_BASE', realpath($dir.'/../').'/'); -define('BASEPATH', PROJECT_BASE.'system/'); -define('APPPATH', PROJECT_BASE.'application/'); -define('VIEWPATH', PROJECT_BASE.''); +defined('PROJECT_BASE') OR define('PROJECT_BASE', realpath($dir.'/../').'/'); +defined('BASEPATH') OR define('BASEPATH', PROJECT_BASE.'system/'); +defined('APPPATH') OR define('APPPATH', PROJECT_BASE.'application/'); +defined('VIEWPATH') OR define('VIEWPATH', PROJECT_BASE.''); + +// Get vfsStream either via PEAR or composer +foreach (explode(PATH_SEPARATOR, get_include_path()) as $path) +{ + if (file_exists($path.DIRECTORY_SEPARATOR.'vfsStream/vfsStream.php')) + { + require_once 'vfsStream/vfsStream.php'; + break; + } +} +if ( ! class_exists('vfsStream') && file_exists(PROJECT_BASE.'vendor/autoload.php')) +{ + include_once PROJECT_BASE.'vendor/autoload.php'; + class_alias('org\bovigo\vfs\vfsStream', 'vfsStream'); + class_alias('org\bovigo\vfs\vfsStreamDirectory', 'vfsStreamDirectory'); + class_alias('org\bovigo\vfs\vfsStreamWrapper', 'vfsStreamWrapper'); +} // Prep our test environment -require_once $dir.'/lib/common.php'; -require_once $dir.'/lib/ci_testcase.php'; +include_once $dir.'/mocks/core/common.php'; +include_once $dir.'/mocks/autoloader.php'; +spl_autoload_register('autoload'); unset($dir);
\ No newline at end of file diff --git a/tests/README.md b/tests/README.md index 6d83c34d8..d600951ee 100644 --- a/tests/README.md +++ b/tests/README.md @@ -1,12 +1,6 @@ # CodeIgniter Unit Tests # -Status : [![Build Status](https://secure.travis-ci.org/EllisLab/CodeIgniter.png?branch=feature/unit-tests)](http://travis-ci.org/EllisLab/CodeIgniter) - -*Do not merge to default until these issues have been addressed* - -- Clean up naming conventions -- Figure out config stuff -- Figure out database testing +Status : [![Build Status](https://secure.travis-ci.org/EllisLab/CodeIgniter.png?branch=develop)](http://travis-ci.org/EllisLab/CodeIgniter) ### Introduction: @@ -27,8 +21,8 @@ PHP Unit >= 3.5.6 vfsStream - pear channel-discover pear.php-tools.net - pear install pat/vfsStream-alpha + pear channel-discover pear.bovigo.org + pear install bovigo/vfsStream-beta #### Installation of PEAR and PHPUnit on Ubuntu @@ -43,11 +37,11 @@ vfsStream pear channel-discover pear.phpunit.de pear channel-discover pear.symfony-project.com pear channel-discover components.ez.no - pear channel-discover pear.php-tools.net + pear channel-discover pear.bovigo.org # Finally install PHPUnit and vfsStream (including dependencies) pear install --alldeps phpunit/PHPUnit - pear install --alldeps pat/vfsStream-alpha + pear install --alldeps bovigo/vfsStream-beta # Finally, run 'phpunit' from within the ./tests directory # and you should be on your way! diff --git a/tests/codeigniter/Setup_test.php b/tests/codeigniter/Setup_test.php index 550245f2f..b48e32bfb 100644 --- a/tests/codeigniter/Setup_test.php +++ b/tests/codeigniter/Setup_test.php @@ -2,12 +2,12 @@ class Setup_test extends PHPUnit_Framework_TestCase { - function test_nonsense() + function test_bootstrap_constants() { - $this->markTestIncomplete('not implemented'); - // ensure that our bootstrapped test environment - // is a good representation of an isolated CI install - //die('here'); + $this->assertTrue(defined('PROJECT_BASE')); + $this->assertTrue(defined('BASEPATH')); + $this->assertTrue(defined('APPPATH')); + $this->assertTrue(defined('VIEWPATH')); } }
\ No newline at end of file diff --git a/tests/codeigniter/core/Benchmark_test.php b/tests/codeigniter/core/Benchmark_test.php new file mode 100644 index 000000000..109b38821 --- /dev/null +++ b/tests/codeigniter/core/Benchmark_test.php @@ -0,0 +1,42 @@ +<?php + +class Benchmark_test extends CI_TestCase { + + public function set_up() + { + $this->benchmark = new Mock_Core_Benchmark(); + } + + // -------------------------------------------------------------------- + + public function test_mark() + { + $this->assertEmpty($this->benchmark->marker); + + $this->benchmark->mark('code_start'); + + $this->assertEquals(1, count($this->benchmark->marker)); + $this->assertArrayHasKey('code_start', $this->benchmark->marker); + } + + // -------------------------------------------------------------------- + + public function test_elapsed_time() + { + $this->assertEquals('{elapsed_time}', $this->benchmark->elapsed_time()); + $this->assertEmpty($this->benchmark->elapsed_time('undefined_point')); + + $this->benchmark->mark('code_start'); + sleep(1); + $this->benchmark->mark('code_end'); + + $this->assertEquals('1.0', $this->benchmark->elapsed_time('code_start', 'code_end', 1)); + } + + // -------------------------------------------------------------------- + + public function test_memory_usage() + { + $this->assertEquals('{memory_usage}', $this->benchmark->memory_usage()); + } +}
\ No newline at end of file diff --git a/tests/codeigniter/core/Common_test.php b/tests/codeigniter/core/Common_test.php index cec12982d..dded2e824 100644 --- a/tests/codeigniter/core/Common_test.php +++ b/tests/codeigniter/core/Common_test.php @@ -1,9 +1,6 @@ <?php -require_once(BASEPATH.'helpers/email_helper.php'); - -class Common_test extends CI_TestCase -{ +class Common_test extends CI_TestCase { // ------------------------------------------------------------------------ diff --git a/tests/codeigniter/core/Input_test.php b/tests/codeigniter/core/Input_test.php new file mode 100644 index 000000000..c9322c027 --- /dev/null +++ b/tests/codeigniter/core/Input_test.php @@ -0,0 +1,161 @@ +<?php + +class Input_test extends CI_TestCase { + + public function set_up() + { + // Set server variable to GET as default, since this will leave unset in STDIN env + $_SERVER['REQUEST_METHOD'] = 'GET'; + + // Set config for Input class + $this->ci_set_config('allow_get_array', TRUE); + $this->ci_set_config('global_xss_filtering', FALSE); + $this->ci_set_config('csrf_protection', FALSE); + + $security = new Mock_Core_Security(); + $utf8 = new Mock_Core_Utf8(); + + $this->input = new Mock_Core_Input($security, $utf8); + } + + // -------------------------------------------------------------------- + + public function test_get_not_exists() + { + $this->assertEmpty($this->input->get()); + $this->assertEmpty($this->input->get('foo')); + + $this->assertTrue( ! $this->input->get()); + $this->assertTrue( ! $this->input->get('foo')); + + // Test we're getting empty results + $this->assertTrue($this->input->get() === NULL); + $this->assertTrue($this->input->get('foo') === NULL); + + // Test new 3.0 behaviour for non existant results (used to be FALSE) + $this->assertTrue($this->input->get() === NULL); + $this->assertTrue($this->input->get('foo') === NULL); + } + + // -------------------------------------------------------------------- + + public function test_get_exist() + { + $_SERVER['REQUEST_METHOD'] = 'GET'; + $_GET['foo'] = 'bar'; + + $this->assertArrayHasKey('foo', $this->input->get()); + $this->assertEquals('bar', $this->input->get('foo')); + } + + // -------------------------------------------------------------------- + + public function test_get_exist_with_xss_clean() + { + $_SERVER['REQUEST_METHOD'] = 'GET'; + $_GET['harm'] = "Hello, i try to <script>alert('Hack');</script> your site"; + + $this->assertArrayHasKey('harm', $this->input->get()); + $this->assertEquals("Hello, i try to <script>alert('Hack');</script> your site", $this->input->get('harm')); + $this->assertEquals("Hello, i try to [removed]alert('Hack');[removed] your site", $this->input->get('harm', TRUE)); + } + + // -------------------------------------------------------------------- + + public function test_post_not_exists() + { + $this->assertEmpty($this->input->post()); + $this->assertEmpty($this->input->post('foo')); + + $this->assertTrue( ! $this->input->post()); + $this->assertTrue( ! $this->input->post('foo')); + + $this->assertTrue($this->input->post() === NULL); + $this->assertTrue($this->input->post('foo') === NULL); + + $this->assertTrue($this->input->post() === NULL); + $this->assertTrue($this->input->post('foo') === NULL); + } + + // -------------------------------------------------------------------- + + public function test_post_exist() + { + $_SERVER['REQUEST_METHOD'] = 'POST'; + $_POST['foo'] = 'bar'; + + $this->assertArrayHasKey('foo', $this->input->post()); + $this->assertEquals('bar', $this->input->post('foo')); + } + + // -------------------------------------------------------------------- + + public function test_post_exist_with_xss_clean() + { + $_SERVER['REQUEST_METHOD'] = 'POST'; + $_POST['harm'] = "Hello, i try to <script>alert('Hack');</script> your site"; + + $this->assertArrayHasKey('harm', $this->input->post()); + $this->assertEquals("Hello, i try to <script>alert('Hack');</script> your site", $this->input->post('harm')); + $this->assertEquals("Hello, i try to [removed]alert('Hack');[removed] your site", $this->input->post('harm', TRUE)); + } + + // -------------------------------------------------------------------- + + public function test_get_post() + { + $_SERVER['REQUEST_METHOD'] = 'POST'; + $_POST['foo'] = 'bar'; + + $this->assertEquals('bar', $this->input->get_post('foo')); + } + + // -------------------------------------------------------------------- + + public function test_cookie() + { + $_COOKIE['foo'] = 'bar'; + + $this->assertEquals('bar', $this->input->cookie('foo')); + } + + // -------------------------------------------------------------------- + + public function test_server() + { + $this->assertEquals('GET', $this->input->server('REQUEST_METHOD')); + } + + // -------------------------------------------------------------------- + + public function test_fetch_from_array() + { + $data = array( + 'foo' => 'bar', + 'harm' => 'Hello, i try to <script>alert(\'Hack\');</script> your site', + ); + + $foo = $this->input->fetch_from_array($data, 'foo'); + $harm = $this->input->fetch_from_array($data, 'harm'); + $harmless = $this->input->fetch_from_array($data, 'harm', TRUE); + + $this->assertEquals('bar', $foo); + $this->assertEquals("Hello, i try to <script>alert('Hack');</script> your site", $harm); + $this->assertEquals("Hello, i try to [removed]alert('Hack');[removed] your site", $harmless); + } + + // -------------------------------------------------------------------- + + public function test_valid_ip() + { + $ip_v4 = '192.18.0.1'; + $this->assertTrue($this->input->valid_ip($ip_v4)); + + $ip_v6 = array('2001:0db8:0000:85a3:0000:0000:ac1f:8001', '2001:db8:0:85a3:0:0:ac1f:8001', '2001:db8:0:85a3::ac1f:8001'); + foreach($ip_v6 as $ip) + { + $this->assertTrue($this->input->valid_ip($ip)); + } + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/core/Lang_test.php b/tests/codeigniter/core/Lang_test.php index a414f0ace..874230feb 100644 --- a/tests/codeigniter/core/Lang_test.php +++ b/tests/codeigniter/core/Lang_test.php @@ -18,13 +18,14 @@ class Lang_test extends CI_TestCase { public function test_load() { $this->assertTrue($this->lang->load('profiler', 'english')); + $this->assertEquals('URI STRING', $this->lang->line('profiler_uri_string')); } - - // -------------------------------------------------------------------- - public function test_line() + // -------------------------------------------------------------------- + + public function test_load_with_unspecified_language() { - $this->assertTrue($this->lang->load('profiler', 'english')); + $this->assertTrue($this->lang->load('profiler')); $this->assertEquals('URI STRING', $this->lang->line('profiler_uri_string')); } diff --git a/tests/codeigniter/core/Loader_test.php b/tests/codeigniter/core/Loader_test.php index b86fd3477..43008651e 100644 --- a/tests/codeigniter/core/Loader_test.php +++ b/tests/codeigniter/core/Loader_test.php @@ -1,38 +1,5 @@ <?php -require_once 'vfsStream/vfsStream.php'; -require_once BASEPATH.'/core/Loader.php'; - -class Extended_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); - } -} - - class Loader_test extends CI_TestCase { private $ci_obj; @@ -40,7 +7,7 @@ class Loader_test extends CI_TestCase { public function set_up() { // Instantiate a new loader - $this->load = new Extended_Loader(); + $this->load = new Mock_Core_Loader(); // mock up a ci instance $this->ci_obj = new StdClass; @@ -265,7 +232,4 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- - - - } diff --git a/tests/codeigniter/core/Security_test.php b/tests/codeigniter/core/Security_test.php new file mode 100644 index 000000000..b2f8c69d2 --- /dev/null +++ b/tests/codeigniter/core/Security_test.php @@ -0,0 +1,105 @@ +<?php + +class Security_test extends CI_TestCase { + + public function set_up() + { + // Set cookie for security test + $_COOKIE['ci_csrf_cookie'] = md5(uniqid(rand(), TRUE)); + + // Set config for Security class + $this->ci_set_config('csrf_protection', TRUE); + $this->ci_set_config('csrf_token_name', 'ci_csrf_token'); + $this->ci_set_config('csrf_cookie_name', 'ci_csrf_cookie'); + + $this->security = new Mock_Core_Security(); + } + + // -------------------------------------------------------------------- + + public function test_csrf_verify() + { + $_SERVER['REQUEST_METHOD'] = 'GET'; + + $this->assertInstanceOf('CI_Security', $this->security->csrf_verify()); + } + + // -------------------------------------------------------------------- + + public function test_csrf_verify_invalid() + { + // Without issuing $_POST[csrf_token_name], this request will triggering CSRF error + $_SERVER['REQUEST_METHOD'] = 'POST'; + + $this->setExpectedException('RuntimeException', 'CI Error: The action you have requested is not allowed'); + + $this->security->csrf_verify(); + } + + // -------------------------------------------------------------------- + + public function test_csrf_verify_valid() + { + $_SERVER['REQUEST_METHOD'] = 'POST'; + $_POST[$this->security->csrf_token_name] = $this->security->csrf_hash; + + $this->assertInstanceOf('CI_Security', $this->security->csrf_verify()); + } + + // -------------------------------------------------------------------- + + public function test_get_csrf_hash() + { + $this->assertEquals($this->security->csrf_hash, $this->security->get_csrf_hash()); + } + + // -------------------------------------------------------------------- + + public function test_get_csrf_token_name() + { + $this->assertEquals('ci_csrf_token', $this->security->get_csrf_token_name()); + } + + // -------------------------------------------------------------------- + + public function test_xss_clean() + { + $harm_string = "Hello, i try to <script>alert('Hack');</script> your site"; + + $harmless_string = $this->security->xss_clean($harm_string); + + $this->assertEquals("Hello, i try to [removed]alert('Hack');[removed] your site", $harmless_string); + } + + // -------------------------------------------------------------------- + + public function test_xss_hash() + { + $this->assertEmpty($this->security->xss_hash); + + // Perform hash + $this->security->xss_hash(); + + $this->assertTrue(preg_match('#^[0-9a-f]{32}$#iS', $this->security->xss_hash) === 1); + } + + // -------------------------------------------------------------------- + + public function test_entity_decode() + { + $encoded = '<div>Hello <b>Booya</b></div>'; + $decoded = $this->security->entity_decode($encoded); + + $this->assertEquals('<div>Hello <b>Booya</b></div>', $decoded); + } + + // -------------------------------------------------------------------- + + public function test_sanitize_filename() + { + $filename = './<!--foo-->'; + $safe_filename = $this->security->sanitize_filename($filename); + + $this->assertEquals('foo', $safe_filename); + } +}
\ No newline at end of file diff --git a/tests/codeigniter/core/URI_test.php b/tests/codeigniter/core/URI_test.php index 40252aa14..e340ddf73 100644 --- a/tests/codeigniter/core/URI_test.php +++ b/tests/codeigniter/core/URI_test.php @@ -1,41 +1,10 @@ <?php -require BASEPATH.'core/URI.php'; - -/** - * Extend the URI class - * - override contructor - * - override CLI detection - */ -class URI_extended extends CI_URI { - - public function __construct() - { - $test = CI_TestCase::instance(); - $cls =& $test->ci_core_class('cfg'); - - // set predictable config values - $test->ci_set_config(array( - 'index_page' => 'index.php', - 'base_url' => 'http://example.com/', - 'subclass_prefix' => 'MY_' - )); - - $this->config = new $cls; - - } - - protected function _is_cli_request() - { - return FALSE; - } -} - class URI_test extends CI_TestCase { public function set_up() { - $this->uri = new URI_extended(); + $this->uri = new Mock_Core_URI(); } // -------------------------------------------------------------------- diff --git a/tests/codeigniter/database/DB_driver_test.php b/tests/codeigniter/database/DB_driver_test.php new file mode 100644 index 000000000..9e16e29b4 --- /dev/null +++ b/tests/codeigniter/database/DB_driver_test.php @@ -0,0 +1,34 @@ +<?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/DB_test.php b/tests/codeigniter/database/DB_test.php new file mode 100644 index 000000000..d5c0dea08 --- /dev/null +++ b/tests/codeigniter/database/DB_test.php @@ -0,0 +1,47 @@ +<?php + +class DB_test extends CI_TestCase { + + public function test_db_invalid() + { + $connection = new Mock_Database_DB(array( + 'undefined' => array( + 'dsn' => '', + 'hostname' => 'undefined', + 'username' => 'undefined', + 'password' => 'undefined', + 'database' => 'undefined', + 'dbdriver' => 'undefined', + ), + )); + + $this->setExpectedException('InvalidArgumentException', 'CI Error: Invalid DB driver'); + + Mock_Database_DB::DB($connection->set_dsn('undefined'), TRUE); + } + + // ------------------------------------------------------------------------ + + public function test_db_valid() + { + $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->assertTrue($db instanceof CI_DB); + $this->assertTrue($db instanceof CI_DB_Driver); + } + + // ------------------------------------------------------------------------ + + public function test_db_failover() + { + $config = Mock_Database_DB::config(DB_DRIVER); + $connection = new Mock_Database_DB($config); + $db = Mock_Database_DB::DB($connection->set_dsn(DB_DRIVER.'_failover'), TRUE); + + $this->assertTrue($db instanceof CI_DB); + $this->assertTrue($db instanceof CI_DB_Driver); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/count_test.php b/tests/codeigniter/database/query_builder/count_test.php new file mode 100644 index 000000000..5e691692d --- /dev/null +++ b/tests/codeigniter/database/query_builder/count_test.php @@ -0,0 +1,44 @@ +<?php + +class Count_test extends CI_TestCase { + + /** + * @var object Database/Query Builder holder + */ + protected $db; + + public function set_up() + { + $this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); + + Mock_Database_Schema_Skeleton::create_tables(); + Mock_Database_Schema_Skeleton::create_data(); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_count_all() + { + $job_count = $this->db->count_all('job'); + + // Check the result + $this->assertEquals(4, $job_count); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_count_all_results() + { + $job_count = $this->db->like('name', 'ian') + ->count_all_results('job'); + + // Check the result + $this->assertEquals(2, $job_count); + } +}
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/delete_test.php b/tests/codeigniter/database/query_builder/delete_test.php new file mode 100644 index 000000000..84ea7616f --- /dev/null +++ b/tests/codeigniter/database/query_builder/delete_test.php @@ -0,0 +1,72 @@ +<?php + +class Delete_test extends CI_TestCase { + + /** + * @var object Database/Query Builder holder + */ + protected $db; + + public function set_up() + { + $this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); + + Mock_Database_Schema_Skeleton::create_tables(); + Mock_Database_Schema_Skeleton::create_data(); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_delete() + { + // Check initial record + $job1 = $this->db->where('id', 1) + ->get('job') + ->row(); + + $this->assertEquals('Developer', $job1->name); + + // Do the delete + $this->db->delete('job', array('id' => 1)); + + // Check the record + $job1 = $this->db->where('id', 1) + ->get('job'); + + $this->assertEmpty($job1->result_array()); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_delete_several_tables() + { + // Check initial record + $user4 = $this->db->where('id', 4) + ->get('user') + ->row(); + + $job4 = $this->db->where('id', 4) + ->get('job') + ->row(); + + $this->assertEquals('Musician', $job4->name); + $this->assertEquals('Chris Martin', $user4->name); + + // Do the delete + $this->db->delete(array('job', 'user'), array('id' => 4)); + + // Check the record + $job4 = $this->db->where('id', 4)->get('job'); + $user4 = $this->db->where('id', 4)->get('user'); + + $this->assertEmpty($job4->result_array()); + $this->assertEmpty($user4->result_array()); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/distinct_test.php b/tests/codeigniter/database/query_builder/distinct_test.php new file mode 100644 index 000000000..925eadb19 --- /dev/null +++ b/tests/codeigniter/database/query_builder/distinct_test.php @@ -0,0 +1,34 @@ +<?php + +class Distinct_test extends CI_TestCase { + + /** + * @var object Database/Query Builder holder + */ + protected $db; + + public function set_up() + { + $this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); + + Mock_Database_Schema_Skeleton::create_tables(); + Mock_Database_Schema_Skeleton::create_data(); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_distinct() + { + $users = $this->db->select('country') + ->distinct() + ->get('user') + ->result_array(); + + // Check the result + $this->assertEquals(3, count($users)); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/empty_test.php b/tests/codeigniter/database/query_builder/empty_test.php new file mode 100644 index 000000000..d1f56285f --- /dev/null +++ b/tests/codeigniter/database/query_builder/empty_test.php @@ -0,0 +1,39 @@ +<?php + +class Empty_test extends CI_TestCase { + + /** + * @var object Database/Query Builder holder + */ + protected $db; + + public function set_up() + { + $this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); + + Mock_Database_Schema_Skeleton::create_tables(); + Mock_Database_Schema_Skeleton::create_data(); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_empty_table() + { + // Check initial record + $jobs = $this->db->get('job')->result_array(); + + $this->assertEquals(4, count($jobs)); + + // Do the empty + $this->db->empty_table('job'); + + // Check the record + $jobs = $this->db->get('job'); + + $this->assertEmpty($jobs->result_array()); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/escape_test.php b/tests/codeigniter/database/query_builder/escape_test.php new file mode 100644 index 000000000..5d575a37b --- /dev/null +++ b/tests/codeigniter/database/query_builder/escape_test.php @@ -0,0 +1,67 @@ +<?php + +class Escape_test extends CI_TestCase { + + /** + * @var object Database/Query Builder holder + */ + protected $db; + + public function set_up() + { + $this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); + + Mock_Database_Schema_Skeleton::create_tables(); + Mock_Database_Schema_Skeleton::create_data(); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_escape_like_percent_sign() + { + // Escape the like string + $string = $this->db->escape_like_str('\%foo'); + + if (strpos(DB_DRIVER, 'mysql') !== FALSE) + { + $sql = "SELECT `value` FROM `misc` WHERE `key` LIKE '$string%' ESCAPE '';"; + } + else + { + $sql = 'SELECT "value" FROM "misc" WHERE "key" LIKE \''.$string.'%\' ESCAPE \'!\';'; + } + + $res = $this->db->query($sql)->result_array(); + + // Check the result + $this->assertEquals(1, count($res)); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_escape_like_backslash_sign() + { + // Escape the like string + $string = $this->db->escape_like_str('\\'); + + if (strpos(DB_DRIVER, 'mysql') !== FALSE) + { + $sql = "SELECT `value` FROM `misc` WHERE `key` LIKE '$string%' ESCAPE '';"; + } + else + { + $sql = 'SELECT "value" FROM "misc" WHERE "key" LIKE \''.$string.'%\' ESCAPE \'!\';'; + } + + $res = $this->db->query($sql)->result_array(); + + // Check the result + $this->assertEquals(2, count($res)); + } +}
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/from_test.php b/tests/codeigniter/database/query_builder/from_test.php new file mode 100644 index 000000000..95ae4dfdb --- /dev/null +++ b/tests/codeigniter/database/query_builder/from_test.php @@ -0,0 +1,51 @@ +<?php + +class From_test extends CI_TestCase { + + /** + * @var object Database/Query Builder holder + */ + protected $db; + + public function set_up() + { + $this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); + + Mock_Database_Schema_Skeleton::create_tables(); + Mock_Database_Schema_Skeleton::create_data(); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_from_simple() + { + $jobs = $this->db->from('job') + ->get() + ->result_array(); + + // Check items + $this->assertEquals(4, count($jobs)); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_from_with_where() + { + $job1 = $this->db->from('job') + ->where('id', 1) + ->get() + ->row(); + + // Check the result + $this->assertEquals('1', $job1->id); + $this->assertEquals('Developer', $job1->name); + $this->assertEquals('Awesome job, but sometimes makes you bored', $job1->description); + } + +}
\ No newline at end of file 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..0751c9332 --- /dev/null +++ b/tests/codeigniter/database/query_builder/get_test.php @@ -0,0 +1,53 @@ +<?php + +class Get_test extends CI_TestCase { + + /** + * @var object Database/Query Builder holder + */ + protected $db; + + public function set_up() + { + $this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); + + Mock_Database_Schema_Skeleton::create_tables(); + Mock_Database_Schema_Skeleton::create_data(); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_get_simple() + { + $jobs = $this->db->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']); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_get_where() + { + $job1 = $this->db->get('job', array('id' => 1))->result_array(); + + // Dummy jobs contain 1 rows + $this->assertCount(1, $job1); + + // Check rows item + $this->assertEquals('Developer', $job1[0]['name']); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/group_test.php b/tests/codeigniter/database/query_builder/group_test.php new file mode 100644 index 000000000..7d8abc33f --- /dev/null +++ b/tests/codeigniter/database/query_builder/group_test.php @@ -0,0 +1,53 @@ +<?php + +class Group_test extends CI_TestCase { + + /** + * @var object Database/Query Builder holder + */ + protected $db; + + public function set_up() + { + $this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); + + Mock_Database_Schema_Skeleton::create_tables(); + Mock_Database_Schema_Skeleton::create_data(); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_group_by() + { + $jobs = $this->db->select('name') + ->from('job') + ->group_by('name') + ->get() + ->result_array(); + + // Check the result + $this->assertEquals(4, count($jobs)); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_having_by() + { + $jobs = $this->db->select('name') + ->from('job') + ->group_by('name') + ->having('SUM(id) > 2') + ->get() + ->result_array(); + + // Check the result + $this->assertEquals(2, count($jobs)); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/insert_test.php b/tests/codeigniter/database/query_builder/insert_test.php new file mode 100644 index 000000000..a9aafb66e --- /dev/null +++ b/tests/codeigniter/database/query_builder/insert_test.php @@ -0,0 +1,66 @@ +<?php + +class Insert_test extends CI_TestCase { + + /** + * @var object Database/Query Builder holder + * @see ./mocks/schema/skeleton.php + */ + protected $db; + + public function set_up() + { + $this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); + + Mock_Database_Schema_Skeleton::create_tables(); + + // Truncate the current datas + $this->db->truncate('job'); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_insert() + { + $job_data = array('id' => 1, 'name' => 'Grocery Sales', 'description' => 'Discount!'); + + // Do normal insert + $this->assertTrue($this->db->insert('job', $job_data)); + + $job1 = $this->db->get('job')->row(); + + // Check the result + $this->assertEquals('Grocery Sales', $job1->name); + + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_insert_batch() + { + $job_datas = array( + array('id' => 2, 'name' => 'Commedian', 'description' => 'Theres something in your teeth'), + array('id' => 3, 'name' => 'Cab Driver', 'description' => 'Iam yellow'), + ); + + // Do insert batch except for sqlite driver + if (strpos(DB_DRIVER, 'sqlite') === FALSE) + { + $this->assertTrue($this->db->insert_batch('job', $job_datas)); + + $job_2 = $this->db->where('id', 2)->get('job')->row(); + $job_3 = $this->db->where('id', 3)->get('job')->row(); + + // Check the result + $this->assertEquals('Commedian', $job_2->name); + $this->assertEquals('Cab Driver', $job_3->name); + } + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/join_test.php b/tests/codeigniter/database/query_builder/join_test.php new file mode 100644 index 000000000..e05329d67 --- /dev/null +++ b/tests/codeigniter/database/query_builder/join_test.php @@ -0,0 +1,38 @@ +<?php + +class Join_test extends CI_TestCase { + + /** + * @var object Database/Query Builder holder + */ + protected $db; + + public function set_up() + { + $this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); + + Mock_Database_Schema_Skeleton::create_tables(); + Mock_Database_Schema_Skeleton::create_data(); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_join_simple() + { + $job_user = $this->db->select('job.id as job_id, job.name as job_name, user.id as user_id, user.name as user_name') + ->from('job') + ->join('user', 'user.id = job.id') + ->get() + ->result_array(); + + // Check the result + $this->assertEquals('1', $job_user[0]['job_id']); + $this->assertEquals('1', $job_user[0]['user_id']); + $this->assertEquals('Derek Jones', $job_user[0]['user_name']); + $this->assertEquals('Developer', $job_user[0]['job_name']); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/like_test.php b/tests/codeigniter/database/query_builder/like_test.php new file mode 100644 index 000000000..df98c713f --- /dev/null +++ b/tests/codeigniter/database/query_builder/like_test.php @@ -0,0 +1,90 @@ +<?php + +class Like_test extends CI_TestCase { + + /** + * @var object Database/Query Builder holder + */ + protected $db; + + public function set_up() + { + $this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); + + Mock_Database_Schema_Skeleton::create_tables(); + Mock_Database_Schema_Skeleton::create_data(); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_like() + { + $job1 = $this->db->like('name', 'veloper') + ->get('job') + ->row(); + + // Check the result + $this->assertEquals('1', $job1->id); + $this->assertEquals('Developer', $job1->name); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_or_like() + { + $jobs = $this->db->like('name', 'ian') + ->or_like('name', 'veloper') + ->get('job') + ->result_array(); + + // Check the result + $this->assertEquals(3, count($jobs)); + $this->assertEquals('Developer', $jobs[0]['name']); + $this->assertEquals('Politician', $jobs[1]['name']); + $this->assertEquals('Musician', $jobs[2]['name']); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_not_like() + { + $jobs = $this->db->not_like('name', 'veloper') + ->get('job') + ->result_array(); + + // Check the result + $this->assertEquals(3, count($jobs)); + $this->assertEquals('Politician', $jobs[0]['name']); + $this->assertEquals('Accountant', $jobs[1]['name']); + $this->assertEquals('Musician', $jobs[2]['name']); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_or_not_like() + { + $jobs = $this->db->like('name', 'an') + ->or_not_like('name', 'veloper') + ->get('job') + ->result_array(); + + // Check the result + $this->assertEquals(3, count($jobs)); + $this->assertEquals('Politician', $jobs[0]['name']); + $this->assertEquals('Accountant', $jobs[1]['name']); + $this->assertEquals('Musician', $jobs[2]['name']); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/limit_test.php b/tests/codeigniter/database/query_builder/limit_test.php new file mode 100644 index 000000000..704f3b651 --- /dev/null +++ b/tests/codeigniter/database/query_builder/limit_test.php @@ -0,0 +1,49 @@ +<?php + +class Limit_test extends CI_TestCase { + + /** + * @var object Database/Query Builder holder + */ + protected $db; + + public function set_up() + { + $this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); + + Mock_Database_Schema_Skeleton::create_tables(); + Mock_Database_Schema_Skeleton::create_data(); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_limit() + { + $jobs = $this->db->limit(2) + ->get('job') + ->result_array(); + + // Check the result + $this->assertEquals(2, count($jobs)); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_limit_and_offset() + { + $jobs = $this->db->limit(2, 2) + ->get('job') + ->result_array(); + + // Check the result + $this->assertEquals(2, count($jobs)); + $this->assertEquals('Accountant', $jobs[0]['name']); + $this->assertEquals('Musician', $jobs[1]['name']); + } +}
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/order_test.php b/tests/codeigniter/database/query_builder/order_test.php new file mode 100644 index 000000000..01aa1c2b4 --- /dev/null +++ b/tests/codeigniter/database/query_builder/order_test.php @@ -0,0 +1,55 @@ +<?php + +class Order_test extends CI_TestCase { + + /** + * @var object Database/Query Builder holder + */ + protected $db; + + public function set_up() + { + $this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); + + Mock_Database_Schema_Skeleton::create_tables(); + Mock_Database_Schema_Skeleton::create_data(); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_order_ascending() + { + $jobs = $this->db->order_by('name', 'asc') + ->get('job') + ->result_array(); + + // Check the result + $this->assertEquals(4, count($jobs)); + $this->assertEquals('Accountant', $jobs[0]['name']); + $this->assertEquals('Developer', $jobs[1]['name']); + $this->assertEquals('Musician', $jobs[2]['name']); + $this->assertEquals('Politician', $jobs[3]['name']); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_order_descending() + { + $jobs = $this->db->order_by('name', 'desc') + ->get('job') + ->result_array(); + + // Check the result + $this->assertEquals(4, count($jobs)); + $this->assertEquals('Politician', $jobs[0]['name']); + $this->assertEquals('Musician', $jobs[1]['name']); + $this->assertEquals('Developer', $jobs[2]['name']); + $this->assertEquals('Accountant', $jobs[3]['name']); + } +}
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/select_test.php b/tests/codeigniter/database/query_builder/select_test.php new file mode 100644 index 000000000..0d299ed16 --- /dev/null +++ b/tests/codeigniter/database/query_builder/select_test.php @@ -0,0 +1,95 @@ +<?php + +class Select_test extends CI_TestCase { + + /** + * @var object Database/Query Builder holder + */ + protected $db; + + public function set_up() + { + $this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); + + Mock_Database_Schema_Skeleton::create_tables(); + Mock_Database_Schema_Skeleton::create_data(); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_select_only_one_collumn() + { + $jobs_name = $this->db->select('name') + ->get('job') + ->result_array(); + + // Check rows item + $this->assertArrayHasKey('name',$jobs_name[0]); + $this->assertFalse(array_key_exists('id', $jobs_name[0])); + $this->assertFalse(array_key_exists('description', $jobs_name[0])); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_select_min() + { + $job_min = $this->db->select_min('id') + ->get('job') + ->row(); + + // Minimum id was 1 + $this->assertEquals('1', $job_min->id); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_select_max() + { + $job_max = $this->db->select_max('id') + ->get('job') + ->row(); + + // Maximum id was 4 + $this->assertEquals('4', $job_max->id); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_select_avg() + { + $job_avg = $this->db->select_avg('id') + ->get('job') + ->row(); + + // Average should be 2.5 + $this->assertEquals('2.5', $job_avg->id); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_select_sum() + { + $job_sum = $this->db->select_sum('id') + ->get('job') + ->row(); + + // Sum of ids should be 10 + $this->assertEquals('10', $job_sum->id); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/truncate_test.php b/tests/codeigniter/database/query_builder/truncate_test.php new file mode 100644 index 000000000..2a9c8a91e --- /dev/null +++ b/tests/codeigniter/database/query_builder/truncate_test.php @@ -0,0 +1,61 @@ +<?php + +class Truncate_test extends CI_TestCase { + + /** + * @var object Database/Query Builder holder + */ + protected $db; + + public function set_up() + { + $this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); + + Mock_Database_Schema_Skeleton::create_tables(); + Mock_Database_Schema_Skeleton::create_data(); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_truncate() + { + // Check initial record + $jobs = $this->db->get('job')->result_array(); + + $this->assertEquals(4, count($jobs)); + + // Do the empty + $this->db->truncate('job'); + + // Check the record + $jobs = $this->db->get('job'); + + $this->assertEmpty($jobs->result_array()); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_truncate_with_from() + { + // Check initial record + $users = $this->db->get('user')->result_array(); + + $this->assertEquals(4, count($users)); + + // Do the empty + $this->db->from('user') + ->truncate(); + + // Check the record + $users = $this->db->get('user'); + + $this->assertEmpty($users->result_array()); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/update_test.php b/tests/codeigniter/database/query_builder/update_test.php new file mode 100644 index 000000000..f5bbffd4f --- /dev/null +++ b/tests/codeigniter/database/query_builder/update_test.php @@ -0,0 +1,71 @@ +<?php + +class Update_test extends CI_TestCase { + + /** + * @var object Database/Query Builder holder + */ + protected $db; + + public function set_up() + { + $this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); + + Mock_Database_Schema_Skeleton::create_tables(); + Mock_Database_Schema_Skeleton::create_data(); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_update() + { + // Check initial record + $job1 = $this->db->where('id', 1) + ->get('job') + ->row(); + + $this->assertEquals('Developer', $job1->name); + + // Do the update + $job_data = array('name' => 'Programmer'); + + $this->db->where('id', 1) + ->update('job', $job_data); + + // Check updated record + $job1 = $this->db->where('id', 1) + ->get('job') + ->row(); + + $this->assertEquals('Programmer', $job1->name); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_update_with_set() + { + // Check initial record + $job1 = $this->db->where('id', 4) + ->get('job') + ->row(); + + $this->assertEquals('Musician', $job1->name); + + // Do the update + $this->db->set('name', 'Vocalist'); + $this->db->update('job', NULL, 'id = 4'); + + // Check updated record + $job1 = $this->db->where('id', 4) + ->get('job') + ->row(); + + $this->assertEquals('Vocalist', $job1->name); + } +}
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/where_test.php b/tests/codeigniter/database/query_builder/where_test.php new file mode 100644 index 000000000..607eaa076 --- /dev/null +++ b/tests/codeigniter/database/query_builder/where_test.php @@ -0,0 +1,144 @@ +<?php + +class Where_test extends CI_TestCase { + + /** + * @var object Database/Query Builder holder + */ + protected $db; + + public function set_up() + { + $this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); + + Mock_Database_Schema_Skeleton::create_tables(); + Mock_Database_Schema_Skeleton::create_data(); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_where_simple_key_value() + { + $job1 = $this->db->where('id', 1) + ->get('job') + ->row(); + + // Check the result + $this->assertEquals('1', $job1->id); + $this->assertEquals('Developer', $job1->name); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_where_custom_key_value() + { + $jobs = $this->db->where('id !=', 1) + ->get('job') + ->result_array(); + + // Check the result + $this->assertEquals(3, count($jobs)); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_where_associative_array() + { + $where = array('id >' => 2, 'name !=' => 'Accountant'); + $jobs = $this->db->where($where) + ->get('job') + ->result_array(); + + // Check the result + $this->assertEquals(1, count($jobs)); + + // Should be Musician + $job = current($jobs); + + $this->assertEquals('Musician', $job['name']); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_where_custom_string() + { + $where = "id > 2 AND name != 'Accountant'"; + $jobs = $this->db->where($where) + ->get('job') + ->result_array(); + + // Check the result + $this->assertEquals(1, count($jobs)); + + // Should be Musician + $job = current($jobs); + + $this->assertEquals('Musician', $job['name']); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_where_or() + { + $jobs = $this->db->where('name !=', 'Accountant') + ->or_where('id >', 3) + ->get('job') + ->result_array(); + + // Check the result + $this->assertEquals(3, count($jobs)); + $this->assertEquals('Developer', $jobs[0]['name']); + $this->assertEquals('Politician', $jobs[1]['name']); + $this->assertEquals('Musician', $jobs[2]['name']); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_where_in() + { + $jobs = $this->db->where_in('name', array('Politician', 'Accountant')) + ->get('job') + ->result_array(); + + // Check the result + $this->assertEquals(2, count($jobs)); + $this->assertEquals('Politician', $jobs[0]['name']); + $this->assertEquals('Accountant', $jobs[1]['name']); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_where_not_in() + { + $jobs = $this->db->where_not_in('name', array('Politician', 'Accountant')) + ->get('job') + ->result_array(); + + // Check the result + $this->assertEquals(2, count($jobs)); + $this->assertEquals('Developer', $jobs[0]['name']); + $this->assertEquals('Musician', $jobs[1]['name']); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/helpers/array_helper_test.php b/tests/codeigniter/helpers/array_helper_test.php index 62559de83..9cd15960f 100644 --- a/tests/codeigniter/helpers/array_helper_test.php +++ b/tests/codeigniter/helpers/array_helper_test.php @@ -1,13 +1,11 @@ <?php -// OLD TEST FORMAT: DO NOT COPY - -require_once(BASEPATH.'helpers/array_helper.php'); - -class Array_helper_test extends CI_TestCase -{ +class Array_helper_test extends CI_TestCase { + public function set_up() { + $this->helper('array'); + $this->my_array = array( 'foo' => 'bar', 'sally' => 'jim', diff --git a/tests/codeigniter/helpers/date_helper_test.php b/tests/codeigniter/helpers/date_helper_test.php index 662d16485..17d1ef21e 100644 --- a/tests/codeigniter/helpers/date_helper_test.php +++ b/tests/codeigniter/helpers/date_helper_test.php @@ -1,8 +1,12 @@ <?php -require_once BASEPATH.'helpers/date_helper.php'; -class Date_helper_test extends CI_TestCase -{ +class Date_helper_test extends CI_TestCase { + + public function set_up() + { + $this->helper('date'); + } + // ------------------------------------------------------------------------ public function test_now_local() diff --git a/tests/codeigniter/helpers/directory_helper_test.php b/tests/codeigniter/helpers/directory_helper_test.php index 3fae81b82..3937d2913 100644 --- a/tests/codeigniter/helpers/directory_helper_test.php +++ b/tests/codeigniter/helpers/directory_helper_test.php @@ -1,12 +1,11 @@ <?php -require_once 'vfsStream/vfsStream.php'; -require BASEPATH.'helpers/directory_helper.php'; - -class Directory_helper_test extends CI_TestCase -{ +class Directory_helper_test extends CI_TestCase { + public function set_up() { + $this->helper('directory'); + vfsStreamWrapper::register(); vfsStreamWrapper::setRoot(new vfsStreamDirectory('testDir')); diff --git a/tests/codeigniter/helpers/email_helper_test.php b/tests/codeigniter/helpers/email_helper_test.php index 7324e8109..a01f3d5af 100644 --- a/tests/codeigniter/helpers/email_helper_test.php +++ b/tests/codeigniter/helpers/email_helper_test.php @@ -1,10 +1,12 @@ <?php -require_once(BASEPATH.'helpers/email_helper.php'); +class Email_helper_test extends CI_TestCase { + + public function set_up() + { + $this->helper('email'); + } -class Email_helper_test extends CI_TestCase -{ - public function test_valid_email() { $this->assertEquals(FALSE, valid_email('test')); diff --git a/tests/codeigniter/helpers/file_helper_test.php b/tests/codeigniter/helpers/file_helper_test.php index a596a0375..4b9c29485 100644 --- a/tests/codeigniter/helpers/file_helper_test.php +++ b/tests/codeigniter/helpers/file_helper_test.php @@ -1,12 +1,11 @@ <?php -require_once 'vfsStream/vfsStream.php'; -require BASEPATH.'helpers/file_helper.php'; +class File_helper_Test extends CI_TestCase { -class File_helper_Test extends CI_TestCase -{ public function set_up() { + $this->helper('file'); + vfsStreamWrapper::register(); vfsStreamWrapper::setRoot(new vfsStreamDirectory('testDir')); diff --git a/tests/codeigniter/helpers/form_helper_test.php b/tests/codeigniter/helpers/form_helper_test.php new file mode 100644 index 000000000..80bace9d1 --- /dev/null +++ b/tests/codeigniter/helpers/form_helper_test.php @@ -0,0 +1,252 @@ +<?php + +require BASEPATH . 'core/Common.php'; +require BASEPATH . 'helpers/form_helper.php'; + +class Form_helper_test extends CI_TestCase +{ + public function test_form_hidden() + { + $expected = <<<EOH + +<input type="hidden" name="username" value="johndoe" /> + +EOH; + + $this->assertEquals($expected, form_hidden('username', 'johndoe')); + } + + public function test_form_input() + { + $expected = <<<EOH +<input type="text" name="username" value="johndoe" id="username" maxlength="100" size="50" style="width:50%" /> + +EOH; + + $data = array( + 'name' => 'username', + 'id' => 'username', + 'value' => 'johndoe', + 'maxlength' => '100', + 'size' => '50', + 'style' => 'width:50%', + ); + + $this->assertEquals($expected, form_input($data)); + } + + public function test_form_password() + { + $expected = <<<EOH +<input type="password" name="password" value="" /> + +EOH; + + $this->assertEquals($expected, form_password('password')); + } + + public function test_form_upload() + { + $expected = <<<EOH +<input type="file" name="attachment" value="" /> + +EOH; + + $this->assertEquals($expected, form_upload('attachment')); + } + + public function test_form_textarea() + { + $expected = <<<EOH +<textarea name="notes" cols="40" rows="10" >Notes</textarea> + +EOH; + + $this->assertEquals($expected, form_textarea('notes', 'Notes')); + } + + public function test_form_dropdown() + { + $expected = <<<EOH +<select name="shirts"> +<option value="small">Small Shirt</option> +<option value="med">Medium Shirt</option> +<option value="large" selected="selected">Large Shirt</option> +<option value="xlarge">Extra Large Shirt</option> +</select> + +EOH; + + $options = array( + 'small' => 'Small Shirt', + 'med' => 'Medium Shirt', + 'large' => 'Large Shirt', + 'xlarge' => 'Extra Large Shirt', + ); + + $this->assertEquals($expected, form_dropdown('shirts', $options, 'large')); + + $expected = <<<EOH +<select name="shirts" multiple="multiple"> +<option value="small" selected="selected">Small Shirt</option> +<option value="med">Medium Shirt</option> +<option value="large" selected="selected">Large Shirt</option> +<option value="xlarge">Extra Large Shirt</option> +</select> + +EOH; + + $shirts_on_sale = array('small', 'large'); + + $this->assertEquals($expected, form_dropdown('shirts', $options, $shirts_on_sale)); + + $options = array( + 'Swedish Cars' => array( + 'volvo' => 'Volvo', + 'saab' => 'Saab' + ), + 'German Cars' => array( + 'mercedes' => 'Mercedes', + 'audi' => 'Audi' + ) + ); + + $expected = <<<EOH +<select name="cars" multiple="multiple"> +<optgroup label="Swedish Cars"> +<option value="volvo" selected="selected">Volvo</option> +<option value="saab">Saab</option> +</optgroup> +<optgroup label="German Cars"> +<option value="mercedes">Mercedes</option> +<option value="audi" selected="selected">Audi</option> +</optgroup> +</select> + +EOH; + + $cars_on_sale = array('volvo', 'audi'); + + $this->assertEquals($expected, form_dropdown('cars', $options, $cars_on_sale)); + + } + + public function test_form_multiselect() + { + $expected = <<<EOH +<select name="shirts[]" multiple="multiple"> +<option value="small">Small Shirt</option> +<option value="med" selected="selected">Medium Shirt</option> +<option value="large" selected="selected">Large Shirt</option> +<option value="xlarge">Extra Large Shirt</option> +</select> + +EOH; + + $options = array( + 'small' => 'Small Shirt', + 'med' => 'Medium Shirt', + 'large' => 'Large Shirt', + 'xlarge' => 'Extra Large Shirt', + ); + + $this->assertEquals($expected, form_multiselect('shirts[]', $options, array('med', 'large'))); + } + + public function test_form_fieldset() + { + $expected = <<<EOH +<fieldset> +<legend>Address Information</legend> + +EOH; + + $this->assertEquals($expected, form_fieldset('Address Information')); + } + + public function test_form_fieldset_close() + { + $expected = <<<EOH +</fieldset></div></div> +EOH; + + $this->assertEquals($expected, form_fieldset_close('</div></div>')); + } + + public function test_form_checkbox() + { + $expected = <<<EOH +<input type="checkbox" name="newsletter" value="accept" checked="checked" /> + +EOH; + + $this->assertEquals($expected, form_checkbox('newsletter', 'accept', TRUE)); + } + + public function test_form_radio() + { + $expected = <<<EOH +<input type="radio" name="newsletter" value="accept" checked="checked" /> + +EOH; + + $this->assertEquals($expected, form_radio('newsletter', 'accept', TRUE)); + } + + public function test_form_submit() + { + $expected = <<<EOH +<input type="submit" name="mysubmit" value="Submit Post!" /> + +EOH; + + $this->assertEquals($expected, form_submit('mysubmit', 'Submit Post!')); + } + + public function test_form_label() + { + $expected = <<<EOH +<label for="username">What is your Name</label> +EOH; + + $this->assertEquals($expected, form_label('What is your Name', 'username')); + } + + public function test_form_reset() + { + $expected = <<<EOH +<input type="reset" name="myreset" value="Reset" /> + +EOH; + + $this->assertEquals($expected, form_reset('myreset', 'Reset')); + } + + public function test_form_button() + { + $expected = <<<EOH +<button name="name" type="button" >content</button> + +EOH; + + $this->assertEquals($expected, form_button('name','content')); + } + + public function test_form_close() + { + $expected = <<<EOH +</form></div></div> +EOH; + + $this->assertEquals($expected, form_close('</div></div>')); + } + + public function test_form_prep() + { + $expected = "Here is a string containing "quoted" text."; + + $this->assertEquals($expected, form_prep('Here is a string containing "quoted" text.')); + } +} + +/* End of file form_helper_test.php */
\ No newline at end of file diff --git a/tests/codeigniter/helpers/html_helper_test.php b/tests/codeigniter/helpers/html_helper_test.php index 553fc2bb1..28974b0f8 100644 --- a/tests/codeigniter/helpers/html_helper_test.php +++ b/tests/codeigniter/helpers/html_helper_test.php @@ -1,9 +1,11 @@ <?php -require_once(BASEPATH.'helpers/html_helper.php'); +class Html_helper_test extends CI_TestCase { -class Html_helper_test extends CI_TestCase -{ + public function set_up() + { + $this->helper('html'); + } // ------------------------------------------------------------------------ diff --git a/tests/codeigniter/helpers/inflector_helper_test.php b/tests/codeigniter/helpers/inflector_helper_test.php index 472e28adb..9e9478711 100644 --- a/tests/codeigniter/helpers/inflector_helper_test.php +++ b/tests/codeigniter/helpers/inflector_helper_test.php @@ -1,9 +1,11 @@ <?php -require_once(BASEPATH.'helpers/inflector_helper.php'); - class Inflector_helper_test extends CI_TestCase { + public function set_up() + { + $this->helper('inflector'); + } public function test_singular() { diff --git a/tests/codeigniter/helpers/number_helper_test.php b/tests/codeigniter/helpers/number_helper_test.php index 3322b2475..4bb9a918a 100644 --- a/tests/codeigniter/helpers/number_helper_test.php +++ b/tests/codeigniter/helpers/number_helper_test.php @@ -1,12 +1,11 @@ <?php -require_once BASEPATH.'helpers/number_helper.php'; - -class Number_helper_test extends CI_TestCase -{ +class Number_helper_test extends CI_TestCase { public function set_up() { + $this->helper('number'); + // Grab the core lang class $lang_cls = $this->ci_core_class('lang'); diff --git a/tests/codeigniter/helpers/path_helper_test.php b/tests/codeigniter/helpers/path_helper_test.php index 2e6cc6391..632f57501 100644 --- a/tests/codeigniter/helpers/path_helper_test.php +++ b/tests/codeigniter/helpers/path_helper_test.php @@ -1,9 +1,12 @@ <?php -require BASEPATH . 'helpers/path_helper.php'; +class Path_helper_test extends CI_TestCase { + + public function set_up() + { + $this->helper('path'); + } -class Path_helper_test extends CI_TestCase -{ public function test_set_realpath() { $expected = getcwd() . DIRECTORY_SEPARATOR; diff --git a/tests/codeigniter/helpers/string_helper_test.php b/tests/codeigniter/helpers/string_helper_test.php index a884d6284..29c3d6594 100644 --- a/tests/codeigniter/helpers/string_helper_test.php +++ b/tests/codeigniter/helpers/string_helper_test.php @@ -1,9 +1,12 @@ <?php -require_once(BASEPATH.'helpers/string_helper.php'); +class String_helper_test extends CI_TestCase { + + public function set_up() + { + $this->helper('string'); + } -class String_helper_test extends CI_TestCase -{ public function test_strip_slashes() { $expected = array( diff --git a/tests/codeigniter/helpers/text_helper_test.php b/tests/codeigniter/helpers/text_helper_test.php index a0866e638..73e2b9429 100644 --- a/tests/codeigniter/helpers/text_helper_test.php +++ b/tests/codeigniter/helpers/text_helper_test.php @@ -1,13 +1,13 @@ <?php -require_once(BASEPATH.'helpers/text_helper.php'); +class Text_helper_test extends CI_TestCase { -class Text_helper_test extends CI_TestCase -{ private $_long_string; public function set_up() { + $this->helper('text'); + $this->_long_string = 'Once upon a time, a framework had no tests. It sad. So some nice people began to write tests. The more time that went on, the happier it became. Everyone was happy.'; } @@ -122,7 +122,7 @@ class Text_helper_test extends CI_TestCase // ------------------------------------------------------------------------ - public function test_ellipsizing() + public function test_ellipsize() { $strs = array( '0' => array( @@ -156,4 +156,20 @@ class Text_helper_test extends CI_TestCase // ------------------------------------------------------------------------ + public function test_word_wrap() + { + $string = "Here is a simple string of text that will help us demonstrate this function."; + $word_wrapped = word_wrap($string, 25); + $this->assertEquals(substr_count($word_wrapped, "\n"), 4); + } + + // ------------------------------------------------------------------------ + + public function test_default_word_wrap_charlim() + { + $string = "Here is a longer string of text that will help us demonstrate the default charlim of this function."; + $word_wrapped = word_wrap($string); + $this->assertEquals(strpos($word_wrapped, "\n"), 73); + } + }
\ No newline at end of file diff --git a/tests/codeigniter/helpers/url_helper_test.php b/tests/codeigniter/helpers/url_helper_test.php index 51a8cc7c0..c561809ce 100644 --- a/tests/codeigniter/helpers/url_helper_test.php +++ b/tests/codeigniter/helpers/url_helper_test.php @@ -1,9 +1,12 @@ <?php -require_once(BASEPATH.'helpers/url_helper.php'); +class Url_helper_test extends CI_TestCase { + + public function set_up() + { + $this->helper('url'); + } -class Url_helper_test extends CI_TestCase -{ public function test_url_title() { $words = array( diff --git a/tests/codeigniter/helpers/xml_helper_test.php b/tests/codeigniter/helpers/xml_helper_test.php index 49f49e166..a83fef91e 100644 --- a/tests/codeigniter/helpers/xml_helper_test.php +++ b/tests/codeigniter/helpers/xml_helper_test.php @@ -1,9 +1,11 @@ <?php -require_once(BASEPATH.'helpers/xml_helper.php'); +class Xml_helper_test extends CI_TestCase { -class Xml_helper_test extends CI_TestCase -{ + public function set_up() + { + $this->helper('xml'); + } public function test_xml_convert() { diff --git a/tests/codeigniter/libraries/Encrypt_test.php b/tests/codeigniter/libraries/Encrypt_test.php new file mode 100644 index 000000000..066990186 --- /dev/null +++ b/tests/codeigniter/libraries/Encrypt_test.php @@ -0,0 +1,71 @@ +<?php + +class Encrypt_test extends CI_TestCase { + + public function set_up() + { + $obj = new StdClass; + $obj->encrypt = new Mock_Libraries_Encrypt(); + + $this->ci_instance($obj); + $this->encrypt = $obj->encrypt; + + $this->ci_set_config('encryption_key', "Encryptin'glike@boss!"); + $this->msg = 'My secret message'; + } + + // -------------------------------------------------------------------- + + public function test_encode() + { + $this->assertNotEquals($this->msg, $this->encrypt->encode($this->msg)); + } + + // -------------------------------------------------------------------- + + public function test_decode() + { + $encoded_msg = $this->encrypt->encode($this->msg); + $this->assertEquals($this->msg, $this->encrypt->decode($encoded_msg)); + } + + // -------------------------------------------------------------------- + + public function test_optional_key() + { + $key = 'Ohai!ù0129°03182%HD1892P0'; + $encoded_msg = $this->encrypt->encode($this->msg, $key); + $this->assertEquals($this->msg, $this->encrypt->decode($encoded_msg, $key)); + } + + // -------------------------------------------------------------------- + + public function test_default_cipher() + { + $this->assertEquals('rijndael-256', $this->encrypt->get_cipher()); + } + + // -------------------------------------------------------------------- + + public function test_set_cipher() + { + $this->encrypt->set_cipher(MCRYPT_BLOWFISH); + $this->assertEquals('blowfish', $this->encrypt->get_cipher()); + } + + // -------------------------------------------------------------------- + + public function test_default_mode() + { + $this->assertEquals('cbc', $this->encrypt->get_mode()); + } + + // -------------------------------------------------------------------- + + public function test_set_mode() + { + $this->encrypt->set_mode(MCRYPT_MODE_CFB); + $this->assertEquals('cfb', $this->encrypt->get_mode()); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/libraries/Parser_test.php b/tests/codeigniter/libraries/Parser_test.php index b4580a4b1..c3d88fa85 100644 --- a/tests/codeigniter/libraries/Parser_test.php +++ b/tests/codeigniter/libraries/Parser_test.php @@ -1,14 +1,11 @@ <?php -require BASEPATH.'libraries/Parser.php'; - -class Parser_test extends CI_TestCase -{ +class Parser_test extends CI_TestCase { public function set_up() { $obj = new StdClass; - $obj->parser = new CI_Parser(); + $obj->parser = new Mock_Libraries_Parser(); $this->ci_instance($obj); diff --git a/tests/codeigniter/libraries/Table_test.php b/tests/codeigniter/libraries/Table_test.php index 0208a465a..f5133de1e 100644 --- a/tests/codeigniter/libraries/Table_test.php +++ b/tests/codeigniter/libraries/Table_test.php @@ -1,14 +1,11 @@ <?php -require BASEPATH.'libraries/Table.php'; - -class Table_test extends CI_TestCase -{ +class Table_test extends CI_TestCase { public function set_up() { $obj = new StdClass; - $obj->table = new CI_table(); + $obj->table = new Mock_Libraries_Table(); $this->ci_instance($obj); @@ -103,53 +100,23 @@ class Table_test extends CI_TestCase array('data' => 'size') ); - // test what would be discreet args, - // basically means a single array as the calling method - // will use func_get_args() - - $reflectionOfTable = new ReflectionClass($this->table); - $method = $reflectionOfTable->getMethod('_prep_args'); - - $method->setAccessible(true); - - $this->assertEquals( - $expected, - $method->invokeArgs( - $this->table, array(array('name', 'color', 'size'), 'discreet') - ) - ); - - // test what would be a single array argument. Again, nested - // due to func_get_args on calling methods $this->assertEquals( $expected, - $method->invokeArgs( - $this->table, array(array('name', 'color', 'size'), 'array') - ) + $this->table->prep_args(array('name', 'color', 'size')) ); - - + // with cell attributes - // need to add that new argument row to our expected outcome $expected[] = array('data' => 'weight', 'class' => 'awesome'); $this->assertEquals( $expected, - $method->invokeArgs( - $this->table, array(array('name', 'color', 'size', array('data' => 'weight', 'class' => 'awesome')), 'attributes') - ) + $this->table->prep_args(array('name', 'color', 'size', array('data' => 'weight', 'class' => 'awesome'))) ); } public function test_default_template_keys() { - $reflectionOfTable = new ReflectionClass($this->table); - $method = $reflectionOfTable->getMethod('_default_template'); - - $method->setAccessible(true); - - $deft_template = $method->invoke($this->table); $keys = array( 'table_open', 'thead_open', 'thead_close', @@ -162,29 +129,24 @@ class Table_test extends CI_TestCase foreach ($keys as $key) { - $this->assertArrayHasKey($key, $deft_template); + $this->assertArrayHasKey($key, $this->table->default_template()); } } public function test_compile_template() { - $reflectionOfTable = new ReflectionClass($this->table); - $method = $reflectionOfTable->getMethod('_compile_template'); - - $method->setAccessible(true); - $this->assertFalse($this->table->set_template('invalid_junk')); // non default key $this->table->set_template(array('nonsense' => 'foo')); - $method->invoke($this->table); + $this->table->compile_template(); $this->assertArrayHasKey('nonsense', $this->table->template); $this->assertEquals('foo', $this->table->template['nonsense']); // override default $this->table->set_template(array('table_close' => '</table junk>')); - $method->invoke($this->table); + $this->table->compile_template(); $this->assertArrayHasKey('table_close', $this->table->template); $this->assertEquals('</table junk>', $this->table->template['table_close']); @@ -250,13 +212,8 @@ class Table_test extends CI_TestCase public function test_set_from_array() { - $reflectionOfTable = new ReflectionClass($this->table); - $method = $reflectionOfTable->getMethod('_set_from_array'); - - $method->setAccessible(true); - - $this->assertFalse($method->invokeArgs($this->table, array('bogus'))); - $this->assertFalse($method->invoke($this->table, array())); + $this->assertFalse($this->table->set_from_array('bogus')); + $this->assertFalse($this->table->set_from_array(NULL)); $data = array( array('name', 'color', 'number'), @@ -264,7 +221,7 @@ class Table_test extends CI_TestCase array('Katie', 'Blue') ); - $method->invokeArgs($this->table, array($data, FALSE)); + $this->table->set_from_array($data, FALSE); $this->assertEmpty($this->table->heading); $this->table->clear(); @@ -280,7 +237,7 @@ class Table_test extends CI_TestCase array('data' => 'Blue'), ); - $method->invokeArgs($this->table, array($data)); + $this->table->set_from_array($data); $this->assertEquals(count($this->table->rows), 2); $this->assertEquals( @@ -296,11 +253,6 @@ class Table_test extends CI_TestCase function test_set_from_object() { - $reflectionOfTable = new ReflectionClass($this->table); - $method = $reflectionOfTable->getMethod('_set_from_object'); - - $method->setAccessible(true); - // Make a stub of query instance $query = new CI_TestCase(); $query->list_fields = function(){ @@ -326,7 +278,7 @@ class Table_test extends CI_TestCase 'email' => array('data' => 'foo@bar.com'), ); - $method->invokeArgs($this->table, array($query)); + $this->table->set_from_object($query); $this->assertEquals( $expected_heading, @@ -339,6 +291,26 @@ class Table_test extends CI_TestCase ); } - // Test main generate method - // -------------------------------------------------------------------- + function test_generate() + { + // Prepare the data + $data = array( + array('Name', 'Color', 'Size'), + array('Fred', 'Blue', 'Small'), + array('Mary', 'Red', 'Large'), + array('John', 'Green', 'Medium') + ); + + $table = $this->table->generate($data); + + // Test the table header + $this->assertTrue(strpos($table, '<th>Name</th>') !== FALSE); + $this->assertTrue(strpos($table, '<th>Color</th>') !== FALSE); + $this->assertTrue(strpos($table, '<th>Size</th>') !== FALSE); + + // Test the first entry + $this->assertTrue(strpos($table, '<td>Fred</td>') !== FALSE); + $this->assertTrue(strpos($table, '<td>Blue</td>') !== FALSE); + $this->assertTrue(strpos($table, '<td>Small</td>') !== FALSE); + } }
\ No newline at end of file diff --git a/tests/codeigniter/libraries/Typography_test.php b/tests/codeigniter/libraries/Typography_test.php index a0533bae0..250aefb24 100644 --- a/tests/codeigniter/libraries/Typography_test.php +++ b/tests/codeigniter/libraries/Typography_test.php @@ -1,14 +1,11 @@ <?php -require BASEPATH.'libraries/Typography.php'; - -class Typography_test extends CI_TestCase -{ +class Typography_test extends CI_TestCase { public function set_up() { $obj = new StdClass; - $obj->type = new CI_Typography(); + $obj->type = new Mock_Libraries_Typography(); $this->ci_instance($obj); diff --git a/tests/codeigniter/libraries/User_agent_test.php b/tests/codeigniter/libraries/Useragent_test.php index 6f9e87196..7dad7ac54 100644 --- a/tests/codeigniter/libraries/User_agent_test.php +++ b/tests/codeigniter/libraries/Useragent_test.php @@ -1,11 +1,7 @@ <?php -require BASEPATH.'libraries/User_agent.php'; - -// This class needs some work... - -class UserAgent_test extends CI_TestCase -{ +class UserAgent_test extends CI_TestCase { + protected $_user_agent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-us) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27'; protected $_mobile_ua = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8B117 Safari/6531.22.7'; @@ -15,7 +11,7 @@ class UserAgent_test extends CI_TestCase $_SERVER['HTTP_USER_AGENT'] = $this->_user_agent; $obj = new StdClass; - $obj->agent = new CI_User_agent(); + $obj->agent = new Mock_Libraries_UserAgent(); $this->ci_instance($obj); diff --git a/tests/lib/common.php b/tests/lib/common.php deleted file mode 100644 index 4a832587d..000000000 --- a/tests/lib/common.php +++ /dev/null @@ -1,132 +0,0 @@ -<?php - -// Set up the global CI functions in their most minimal core representation - -function &get_instance() -{ - $test = CI_TestCase::instance(); - $instance = $test->ci_instance(); - return $instance; -} - -// -------------------------------------------------------------------- - -function &get_config() { - $test = CI_TestCase::instance(); - $config = $test->ci_get_config(); - - return $config; -} - -function config_item($item) -{ - $config =& get_config(); - - if ( ! isset($config[$item])) - { - return FALSE; - } - - return $config[$item]; -} - -// -------------------------------------------------------------------- - -function load_class($class, $directory = 'libraries', $prefix = 'CI_') -{ - if ($directory != 'core' OR $prefix != 'CI_') - { - 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.''); - } - - return $obj; -} - -// This is sort of meh. Should probably be mocked up with -// controllable output, so that we can test some of our -// security code. The function itself will be tested in the -// bootstrap testsuite. -// -------------------------------------------------------------------- - -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 - { - $str = preg_replace($non_displayables, '', $str, -1, $count); - } - while ($count); - - return $str; -} - - -// Clean up error messages -// -------------------------------------------------------------------- - -function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered') -{ - throw new RuntimeException('CI Error: '.$message); -} - -function show_404($page = '', $log_error = TRUE) -{ - throw new RuntimeException('CI Error: 404'); -} - -function _exception_handler($severity, $message, $filepath, $line) -{ - throw new RuntimeException('CI Exception: '.$message.' | '.$filepath.' | '.$line); -} - - -// We assume a few things about our environment ... -// -------------------------------------------------------------------- - -function is_php($version = '5.0.0') -{ - return ! (version_compare(PHP_VERSION, $version) < 0); -} - -function is_really_writable($file) -{ - return is_writable($file); -} - -function is_loaded() -{ - throw new Exception('Bad Isolation: mock up environment'); -} - -function log_message($level = 'error', $message, $php_error = FALSE) -{ - return TRUE; -} - -function set_status_header($code = 200, $text = '') -{ - return TRUE; -} - -// EOF diff --git a/tests/mocks/autoloader.php b/tests/mocks/autoloader.php new file mode 100644 index 000000000..90aabcbe6 --- /dev/null +++ b/tests/mocks/autoloader.php @@ -0,0 +1,89 @@ +<?php + +// This autoloader provide convinient way to working with mock object +// make the test looks natural. This autoloader support cascade file loading as well +// within mocks directory. +// +// Prototype : +// +// $mock_table = new Mock_Libraries_Table(); // Will load ./mocks/libraries/table.php +// $mock_database_driver = new Mock_Database_Driver(); // Will load ./mocks/database/driver.php +// and so on... +function autoload($class) +{ + $dir = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR; + + $ci_core = array( + 'Benchmark', 'Config', 'Controller', + 'Exceptions', 'Hooks', 'Input', + 'Lang', 'Loader', 'Model', + 'Output', 'Router', 'Security', + 'URI', 'Utf8', + ); + + $ci_libraries = array( + 'Calendar', 'Cart', 'Driver_Library', + 'Email', 'Encrypt', 'Form_validation', + 'Ftp', 'Image_lib', 'Javascript', + 'Log', 'Migration', 'Pagination', + 'Parser', 'Profiler', 'Session', + 'Table', 'Trackback', 'Typography', + 'Unit_test', 'Upload', 'User_agent', + 'Xmlrpc', 'Zip', + ); + + if (strpos($class, 'Mock_') === 0) + { + $class = str_replace(array('Mock_', '_'), array('', DIRECTORY_SEPARATOR), $class); + $class = strtolower($class); + } + elseif (strpos($class, 'CI_') === 0) + { + $fragments = explode('_', $class, 2); + $subclass = next($fragments); + + if (in_array($subclass, $ci_core)) + { + $dir = BASEPATH.'core'.DIRECTORY_SEPARATOR; + $class = $subclass; + } + elseif (in_array($subclass, $ci_libraries)) + { + $dir = BASEPATH.'libraries'.DIRECTORY_SEPARATOR; + $class = ($subclass === 'Driver_Library') ? 'Driver' : $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 = (isset($file)) ? $file : $dir.$class.'.php'; + + if ( ! file_exists($file)) + { + $trace = debug_backtrace(); + + // If the autoload call came from `class_exists` or `file_exists`, + // we skipped and return FALSE + if ($trace[2]['function'] === 'class_exists' OR $trace[2]['function'] === 'file_exists') + { + return FALSE; + } + + throw new InvalidArgumentException("Unable to load $class."); + } + + include_once($file); +}
\ No newline at end of file diff --git a/tests/lib/ci_testcase.php b/tests/mocks/ci_testcase.php index afccee017..f327e6b07 100644 --- a/tests/lib/ci_testcase.php +++ b/tests/mocks/ci_testcase.php @@ -1,9 +1,5 @@ <?php - -// Need a way to change dependencies (core libs and laoded libs) -// Need a way to set the CI class - class CI_TestCase extends PHPUnit_Framework_TestCase { protected $ci_config; @@ -20,7 +16,6 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { 'security' => 'sec', 'input' => 'in', 'lang' => 'lang', - // @todo the loader is an edge case 'loader' => 'load', 'model' => 'model' ); @@ -53,6 +48,13 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { $this->tear_down(); } } + + // -------------------------------------------------------------------- + + public static function instance() + { + return self::$ci_test_instance; + } // -------------------------------------------------------------------- @@ -67,6 +69,13 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { $this->ci_config[$key] = $val; } } + + // -------------------------------------------------------------------- + + function ci_get_config() + { + return $this->ci_config; + } // -------------------------------------------------------------------- @@ -158,19 +167,12 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { self::$ci_test_instance = $this; parent::runBare(); } - - // -------------------------------------------------------------------- - - public static function instance() - { - return self::$ci_test_instance; - } - + // -------------------------------------------------------------------- - function ci_get_config() + function helper($name) { - return $this->ci_config; + require_once(BASEPATH.'helpers/'.$name.'_helper.php'); } // -------------------------------------------------------------------- diff --git a/tests/mocks/core/benchmark.php b/tests/mocks/core/benchmark.php new file mode 100644 index 000000000..d92be21db --- /dev/null +++ b/tests/mocks/core/benchmark.php @@ -0,0 +1,3 @@ +<?php + +class Mock_Core_Benchmark extends CI_Benchmark {}
\ No newline at end of file diff --git a/tests/mocks/core/common.php b/tests/mocks/core/common.php new file mode 100644 index 000000000..e1c493aa0 --- /dev/null +++ b/tests/mocks/core/common.php @@ -0,0 +1,171 @@ +<?php + +// Set up the global CI functions in their most minimal core representation + +if ( ! function_exists('get_instance')) +{ + function &get_instance() + { + $test = CI_TestCase::instance(); + $instance = $test->ci_instance(); + return $instance; + } +} + +// -------------------------------------------------------------------- + +if ( ! function_exists('get_config')) +{ + function &get_config() { + $test = CI_TestCase::instance(); + $config = $test->ci_get_config(); + + return $config; + } +} + +if ( ! function_exists('config_item')) +{ + function config_item($item) + { + $config =& get_config(); + + if ( ! isset($config[$item])) + { + return FALSE; + } + + return $config[$item]; + } +} + +// -------------------------------------------------------------------- + +if ( ! function_exists('load_class')) +{ + function load_class($class, $directory = 'libraries', $prefix = 'CI_') + { + if ($directory !== 'core' OR $prefix !== 'CI_') + { + 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.''); + } + + return $obj; + } +} + +// This is sort of meh. Should probably be mocked up with +// controllable output, so that we can test some of our +// security code. The function itself will be tested in the +// bootstrap testsuite. +// -------------------------------------------------------------------- + +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 + { + $str = preg_replace($non_displayables, '', $str, -1, $count); + } + while ($count); + + return $str; + } +} + + +// Clean up error messages +// -------------------------------------------------------------------- + +if ( ! function_exists('show_error')) +{ + function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered') + { + throw new RuntimeException('CI Error: '.$message); + } +} + +if ( ! function_exists('show_404')) +{ + function show_404($page = '', $log_error = TRUE) + { + throw new RuntimeException('CI Error: 404'); + } +} + +if ( ! function_exists('_exception_handler')) +{ + function _exception_handler($severity, $message, $filepath, $line) + { + throw new RuntimeException('CI Exception: '.$message.' | '.$filepath.' | '.$line); + } +} + + +// We assume a few things about our environment ... +// -------------------------------------------------------------------- + +if ( ! function_exists('is_php')) +{ + function is_php($version = '5.0.0') + { + return ! (version_compare(PHP_VERSION, $version) < 0); + } +} + +if ( ! function_exists('is_really_writable')) +{ + function is_really_writable($file) + { + return is_writable($file); + } +} + +if ( ! function_exists('is_loaded')) +{ + function is_loaded() + { + throw new Exception('Bad Isolation: mock up environment'); + } +} + +if ( ! function_exists('log_message')) +{ + function log_message($level = 'error', $message, $php_error = FALSE) + { + return TRUE; + } +} + +if ( ! function_exists('set_status_header')) +{ + function set_status_header($code = 200, $text = '') + { + return TRUE; + } +} + +// EOF
\ No newline at end of file diff --git a/tests/mocks/core/input.php b/tests/mocks/core/input.php new file mode 100644 index 000000000..8a337d2ef --- /dev/null +++ b/tests/mocks/core/input.php @@ -0,0 +1,31 @@ +<?php + +class Mock_Core_Input extends CI_Input { + + /** + * Since we use GLOBAL to fetch Security and Utf8 classes, + * we need to use inversion of control to mock up + * the same process within CI_Input class constructor. + * + * @covers CI_Input::__construct() + */ + public function __construct($security, $utf8) + { + $this->_allow_get_array = (config_item('allow_get_array') === TRUE); + $this->_enable_xss = (config_item('global_xss_filtering') === TRUE); + $this->_enable_csrf = (config_item('csrf_protection') === TRUE); + + // Assign Security and Utf8 classes + $this->security = $security; + $this->uni = $utf8; + + // Sanitize global arrays + $this->_sanitize_globals(); + } + + public function fetch_from_array($array, $index = '', $xss_clean = FALSE) + { + return parent::_fetch_from_array($array, $index, $xss_clean); + } + +}
\ No newline at end of file diff --git a/tests/mocks/core/loader.php b/tests/mocks/core/loader.php new file mode 100644 index 000000000..d4b29bb3d --- /dev/null +++ b/tests/mocks/core/loader.php @@ -0,0 +1,30 @@ +<?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/core/security.php b/tests/mocks/core/security.php new file mode 100644 index 000000000..d7ea0e6bd --- /dev/null +++ b/tests/mocks/core/security.php @@ -0,0 +1,30 @@ +<?php + +class Mock_Core_Security extends CI_Security { + + public function csrf_set_cookie() + { + // We cannot set cookie in CLI mode, so for csrf test, who rely on $_COOKIE, + // we superseded set_cookie with directly set the cookie variable, + // @see : ./tests/codeigniter/core/Security_test.php, line 8 + return $this; + } + + // Overide inaccesible protected properties + public function __get($property) + { + return isset($this->{'_'.$property}) ? $this->{'_'.$property} : NULL; + } + + // 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/core/uri.php b/tests/mocks/core/uri.php new file mode 100644 index 000000000..b6946091e --- /dev/null +++ b/tests/mocks/core/uri.php @@ -0,0 +1,25 @@ +<?php + +class Mock_Core_URI extends CI_URI { + + public function __construct() + { + $test = CI_TestCase::instance(); + $cls =& $test->ci_core_class('cfg'); + + // set predictable config values + $test->ci_set_config(array( + 'index_page' => 'index.php', + 'base_url' => 'http://example.com/', + 'subclass_prefix' => 'MY_' + )); + + $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 new file mode 100644 index 000000000..b77d717e7 --- /dev/null +++ b/tests/mocks/core/utf8.php @@ -0,0 +1,27 @@ +<?php + +class Mock_Core_Utf8 extends CI_Utf8 { + + /** + * We need to define several constants as + * the same process within CI_Utf8 class constructor. + * + * @covers CI_Utf8::__construct() + */ + public function __construct() + { + defined('UTF8_ENABLED') or define('UTF8_ENABLED', TRUE); + + if (extension_loaded('mbstring')) + { + defined('MB_ENABLED') or define('MB_ENABLED', TRUE); + mb_internal_encoding('UTF-8'); + } + else + { + defined('MB_ENABLED') or define('MB_ENABLED', FALSE); + } + + } + +}
\ No newline at end of file diff --git a/tests/mocks/database/ci_test.sqlite b/tests/mocks/database/ci_test.sqlite Binary files differnew file mode 100755 index 000000000..44dcef9ec --- /dev/null +++ b/tests/mocks/database/ci_test.sqlite diff --git a/tests/mocks/database/config/mysql.php b/tests/mocks/database/config/mysql.php new file mode 100644 index 000000000..ace0a31b1 --- /dev/null +++ b/tests/mocks/database/config/mysql.php @@ -0,0 +1,34 @@ +<?php + +return array( + + // Typical Database configuration + 'mysql' => array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'travis', + 'password' => '', + 'database' => 'ci_test', + 'dbdriver' => 'mysql', + ), + + // Database configuration with failover + 'mysql_failover' => array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'not_travis', + 'password' => 'wrong password', + 'database' => 'not_ci_test', + 'dbdriver' => 'mysql', + 'failover' => array( + array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'travis', + '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 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/pgsql.php b/tests/mocks/database/config/pgsql.php new file mode 100644 index 000000000..c06af8ce0 --- /dev/null +++ b/tests/mocks/database/config/pgsql.php @@ -0,0 +1,34 @@ +<?php + +return array( + + // Typical Database configuration + 'pgsql' => array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'postgres', + 'password' => '', + 'database' => 'ci_test', + 'dbdriver' => 'postgre', + ), + + // Database configuration with failover + 'pgsql_failover' => array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'not_travis', + 'password' => 'wrong password', + 'database' => 'not_ci_test', + 'dbdriver' => 'postgre', + 'failover' => array( + array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'postgres', + '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 new file mode 100644 index 000000000..755ce2a3a --- /dev/null +++ b/tests/mocks/database/config/sqlite.php @@ -0,0 +1,34 @@ +<?php + +return array( + + // Typical Database configuration + 'sqlite' => array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'sqlite', + 'password' => 'sqlite', + 'database' => realpath(__DIR__.'/..').'/ci_test.sqlite', + 'dbdriver' => 'sqlite3', + ), + + // Database configuration with failover + 'sqlite_failover' => array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'sqlite', + 'password' => 'sqlite', + 'database' => '../not_exists.sqlite', + 'dbdriver' => 'sqlite3', + 'failover' => array( + array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'sqlite', + 'password' => 'sqlite', + 'database' => realpath(__DIR__.'/..').'/ci_test.sqlite', + 'dbdriver' => 'sqlite3', + ), + ), + ), +);
\ No newline at end of file diff --git a/tests/mocks/database/db.php b/tests/mocks/database/db.php new file mode 100644 index 000000000..59028ed9c --- /dev/null +++ b/tests/mocks/database/db.php @@ -0,0 +1,100 @@ +<?php + +class Mock_Database_DB { + + /** + * @var array DB configuration + */ + private $config = array(); + + /** + * Prepare database configuration skeleton + * + * @param array DB configuration to set + * @return void + */ + public function __construct($config = array()) + { + $this->config = $config; + } + + /** + * Build DSN connection string for DB driver instantiate process + * + * @param string Group name + * @return string DSN Connection string + */ + public function set_dsn($group = 'default') + { + if ( ! isset($this->config[$group])) + { + throw new InvalidArgumentException('Group '.$group.' not exists'); + } + + $params = array( + 'dbprefix' => '', + 'pconnect' => FALSE, + 'db_debug' => FALSE, + 'cache_on' => FALSE, + 'cachedir' => '', + 'char_set' => 'utf8', + 'dbcollat' => 'utf8_general_ci', + 'swap_pre' => '', + 'autoinit' => TRUE, + 'stricton' => FALSE, + ); + + $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; + + $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); + } + + /** + * Return a database config array + * + * @see ./config + * @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 + */ + public static function DB($group, $query_builder = FALSE) + { + include_once(BASEPATH.'database/DB.php'); + + try + { + $db = DB($group, $query_builder); + } + catch (Exception $e) + { + throw new InvalidArgumentException($e->getMessage()); + } + + return $db; + } +}
\ No newline at end of file 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..15cefbf53 --- /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/skeleton.php b/tests/mocks/database/schema/skeleton.php new file mode 100644 index 000000000..05499f82f --- /dev/null +++ b/tests/mocks/database/schema/skeleton.php @@ -0,0 +1,147 @@ +<?php + +class Mock_Database_Schema_Skeleton { + + /** + * @var object Database Holder + */ + public static $db; + + /** + * @var object Forge Holder + */ + public static $forge; + + /** + * @var object Driver Holder + */ + public static $driver; + + /** + * Initialize both database and forge components + */ + public static function init($driver) + { + if (empty(static::$db) && empty(static::$forge)) + { + $config = Mock_Database_DB::config($driver); + $connection = new Mock_Database_DB($config); + $db = Mock_Database_DB::DB($connection->set_dsn($driver), TRUE); + + CI_TestCase::instance()->ci_instance_var('db', $db); + + $loader = new Mock_Core_Loader(); + $loader->dbforge(); + $forge = CI_TestCase::instance()->ci_instance_var('dbforge'); + + static::$db = $db; + static::$forge = $forge; + static::$driver = $driver; + } + + return static::$db; + } + + + /** + * Create the dummy tables + * + * @return void + */ + public static function create_tables() + { + // User Table + static::$forge->add_field(array( + 'id' => array( + 'type' => 'INTEGER', + 'constraint' => 3, + ), + 'name' => array( + 'type' => 'VARCHAR', + 'constraint' => 40, + ), + 'email' => array( + 'type' => 'VARCHAR', + 'constraint' => 100, + ), + 'country' => array( + 'type' => 'VARCHAR', + 'constraint' => 40, + ), + )); + static::$forge->add_key('id', TRUE); + static::$forge->create_table('user', (strpos(static::$driver, 'pgsql') === FALSE)); + + // Job Table + static::$forge->add_field(array( + 'id' => array( + 'type' => 'INTEGER', + 'constraint' => 3, + ), + 'name' => array( + 'type' => 'VARCHAR', + 'constraint' => 40, + ), + 'description' => array( + 'type' => 'TEXT', + ), + )); + static::$forge->add_key('id', TRUE); + static::$forge->create_table('job', (strpos(static::$driver, 'pgsql') === FALSE)); + + // Misc Table + static::$forge->add_field(array( + 'id' => array( + 'type' => 'INTEGER', + 'constraint' => 3, + ), + 'key' => array( + 'type' => 'VARCHAR', + 'constraint' => 40, + ), + 'value' => array( + 'type' => 'TEXT', + ), + )); + static::$forge->add_key('id', TRUE); + static::$forge->create_table('misc', (strpos(static::$driver, 'pgsql') === FALSE)); + } + + /** + * Create the dummy datas + * + * @return void + */ + public static function create_data() + { + // Job Data + $data = array( + 'user' => array( + 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'), + ), + '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'), + ), + 'misc' => array( + array('id' => 1, 'key' => '\\xxxfoo456', 'value' => 'Entry with \\xxx'), + array('id' => 2, 'key' => '\\%foo456', 'value' => 'Entry with \\%'), + ), + ); + + foreach ($data as $table => $dummy_data) + { + static::$db->truncate($table); + + foreach ($dummy_data as $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 new file mode 100644 index 000000000..a9bbaafdc --- /dev/null +++ b/tests/mocks/libraries/encrypt.php @@ -0,0 +1,15 @@ +<?php + +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'); + } +}
\ No newline at end of file diff --git a/tests/mocks/libraries/parser.php b/tests/mocks/libraries/parser.php new file mode 100644 index 000000000..81dcfb37e --- /dev/null +++ b/tests/mocks/libraries/parser.php @@ -0,0 +1,3 @@ +<?php + +class Mock_Libraries_Parser extends CI_Parser {}
\ No newline at end of file diff --git a/tests/mocks/libraries/table.php b/tests/mocks/libraries/table.php new file mode 100644 index 000000000..97fbb30bd --- /dev/null +++ b/tests/mocks/libraries/table.php @@ -0,0 +1,15 @@ +<?php + +class Mock_Libraries_Table extends CI_Table { + + // 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/typography.php b/tests/mocks/libraries/typography.php new file mode 100644 index 000000000..0f76c5745 --- /dev/null +++ b/tests/mocks/libraries/typography.php @@ -0,0 +1,3 @@ +<?php + +class Mock_Libraries_Typography extends CI_Typography {}
\ No newline at end of file diff --git a/tests/mocks/libraries/useragent.php b/tests/mocks/libraries/useragent.php new file mode 100644 index 000000000..c957cde5b --- /dev/null +++ b/tests/mocks/libraries/useragent.php @@ -0,0 +1,3 @@ +<?php + +class Mock_Libraries_UserAgent extends CI_User_agent {}
\ No newline at end of file diff --git a/tests/phpunit.xml b/tests/phpunit.xml index dfeecd19f..56cb8841c 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -1,7 +1,7 @@ <?xml version="1.0" encoding="UTF-8"?> <phpunit - bootstrap="Bootstrap.php" + bootstrap="./Bootstrap.php" colors="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" @@ -11,27 +11,15 @@ stopOnSkipped="false"> <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> - <!-- We'll worry about these later ... - <directory suffix="test.php">codeigniter/libraries</directory> - <directory suffix="test.php">codeigniter/helpers</directory> - --> + <directory suffix="test.php">./codeigniter/core</directory> + <directory suffix="test.php">./codeigniter/helpers</directory> + <directory suffix="test.php">./codeigniter/libraries</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> - <whitelist> - <!-- - <directory suffix=".php">'../system/core'</directory> - --> - </whitelist> </filters> </phpunit>
\ No newline at end of file diff --git a/tests/travis/mysql.phpunit.xml b/tests/travis/mysql.phpunit.xml new file mode 100644 index 000000000..38c8eba48 --- /dev/null +++ b/tests/travis/mysql.phpunit.xml @@ -0,0 +1,25 @@ +<?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="mysql"/> + </php> + <testsuites> + <testsuite name="CodeIgniter Core Test Suite"> + <directory suffix="test.php">../codeigniter</directory> + </testsuite> + </testsuites> + <filter> + <whitelist addUncoveredFilesFromWhitelist="true"> + <directory suffix=".php">../../system</directory> + </whitelist> + </filter> +</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..c3113a66f --- /dev/null +++ b/tests/travis/pdo/mysql.phpunit.xml @@ -0,0 +1,25 @@ +<?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"> + <directory suffix="test.php">../../codeigniter</directory> + </testsuite> + </testsuites> + <filter> + <whitelist addUncoveredFilesFromWhitelist="true"> + <directory suffix=".php">../../../system</directory> + </whitelist> + </filter> +</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..232025523 --- /dev/null +++ b/tests/travis/pdo/pgsql.phpunit.xml @@ -0,0 +1,25 @@ +<?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"> + <directory suffix="test.php">../../codeigniter</directory> + </testsuite> + </testsuites> + <filter> + <whitelist addUncoveredFilesFromWhitelist="true"> + <directory suffix=".php">../../../system</directory> + </whitelist> + </filter> +</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..3d1256721 --- /dev/null +++ b/tests/travis/pdo/sqlite.phpunit.xml @@ -0,0 +1,25 @@ +<?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"> + <directory suffix="test.php">../../codeigniter</directory> + </testsuite> + </testsuites> + <filter> + <whitelist addUncoveredFilesFromWhitelist="true"> + <directory suffix=".php">../../../system</directory> + </whitelist> + </filter> +</phpunit>
\ No newline at end of file diff --git a/tests/travis/pgsql.phpunit.xml b/tests/travis/pgsql.phpunit.xml new file mode 100644 index 000000000..51e433d76 --- /dev/null +++ b/tests/travis/pgsql.phpunit.xml @@ -0,0 +1,25 @@ +<?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="pgsql"/> + </php> + <testsuites> + <testsuite name="CodeIgniter Core Test Suite"> + <directory suffix="test.php">../codeigniter</directory> + </testsuite> + </testsuites> + <filter> + <whitelist addUncoveredFilesFromWhitelist="true"> + <directory suffix=".php">../../system</directory> + </whitelist> + </filter> +</phpunit>
\ No newline at end of file diff --git a/tests/travis/sqlite.phpunit.xml b/tests/travis/sqlite.phpunit.xml new file mode 100644 index 000000000..701165734 --- /dev/null +++ b/tests/travis/sqlite.phpunit.xml @@ -0,0 +1,25 @@ +<?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="sqlite"/> + </php> + <testsuites> + <testsuite name="CodeIgniter Core Test Suite"> + <directory suffix="test.php">../codeigniter</directory> + </testsuite> + </testsuites> + <filter> + <whitelist addUncoveredFilesFromWhitelist="true"> + <directory suffix=".php">../../system</directory> + </whitelist> + </filter> +</phpunit>
\ No newline at end of file |