diff options
Diffstat (limited to 'tests/codeigniter')
46 files changed, 1928 insertions, 209 deletions
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); |