From 62ab8b24fc37a25eab9205c46321fa41729e5faf Mon Sep 17 00:00:00 2001 From: Eric Barnes Date: Sat, 28 Jul 2012 14:57:04 -0400 Subject: Adding optional attributes as array or object for html helper --- tests/codeigniter/helpers/html_helper_test.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/codeigniter/helpers/html_helper_test.php b/tests/codeigniter/helpers/html_helper_test.php index 9a7bb48bf..4dd717ff7 100644 --- a/tests/codeigniter/helpers/html_helper_test.php +++ b/tests/codeigniter/helpers/html_helper_test.php @@ -22,6 +22,22 @@ class Html_helper_test extends CI_TestCase { $this->assertEquals('

foobar

', heading('foobar', 2, 'class="bar"')); } + public function test_heading_array_attributes() + { + // Test array of attributes + $this->assertEquals('

foobar

', heading('foobar', 2, array('class' => 'bar', 'id' => 'foo'))); + } + + public function test_heading_object_attributes() + { + // Test array of attributes + $this->assertEquals('

foobar

', heading('foobar', 2, array('class' => 'bar', 'id' => 'foo'))); + $test = new stdClass; + $test->class = "bar"; + $test->id = "foo"; + $this->assertEquals('

foobar

', heading('foobar', 2, $test)); + } + // ------------------------------------------------------------------------ public function test_Ul() @@ -72,5 +88,4 @@ EOH; $this->assertEquals($expect, meta(array('name' => 'foo'))); } - } \ No newline at end of file -- cgit v1.2.3-24-g4f1b From acedd2b1a37b22cb04b01038f21876ddfe38b83a Mon Sep 17 00:00:00 2001 From: Eric Barnes Date: Sun, 29 Jul 2012 00:15:40 -0400 Subject: Adding a common stringify_attributes function for dealing with attributes through out various helpers. Signed-off-by: Eric Barnes --- tests/codeigniter/core/Common_test.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'tests') diff --git a/tests/codeigniter/core/Common_test.php b/tests/codeigniter/core/Common_test.php index f9bf6c27f..27d48efc2 100644 --- a/tests/codeigniter/core/Common_test.php +++ b/tests/codeigniter/core/Common_test.php @@ -10,4 +10,35 @@ class Common_test extends CI_TestCase { $this->assertEquals(FALSE, is_php('9999.9.9')); } + // ------------------------------------------------------------------------ + + public function test_stringify_attributes() + { + $this->assertEquals(' class="foo" id="bar"', _stringify_attributes(array('class' => 'foo', 'id' => 'bar'))); + + $atts = new Stdclass; + $atts->class = 'foo'; + $atts->id = 'bar'; + $this->assertEquals(' class="foo" id="bar"', _stringify_attributes($atts)); + + $atts = new Stdclass; + $this->assertEquals('', _stringify_attributes($atts)); + + $this->assertEquals(' class="foo" id="bar"', _stringify_attributes('class="foo" id="bar"')); + + $this->assertEquals('', _stringify_attributes(array())); + } + + // ------------------------------------------------------------------------ + + public function test_stringify_js_attributes() + { + $this->assertEquals('width=800,height=600', _stringify_attributes(array('width' => '800', 'height' => '600'), TRUE)); + + $atts = new Stdclass; + $atts->width = 800; + $atts->height = 600; + $this->assertEquals('width=800,height=600', _stringify_attributes($atts, TRUE)); + } + } \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 57486009573a86d9e286a49c92216ba17aae0d5a Mon Sep 17 00:00:00 2001 From: dchill42 Date: Tue, 31 Jul 2012 09:39:53 -0400 Subject: Added Session driver library unit tests and added driver library autoloading to test framework --- tests/codeigniter/libraries/Session_test.php | 304 +++++++++++++++++++++++++++ tests/mocks/autoloader.php | 23 +- tests/mocks/libraries/session.php | 43 ++++ 3 files changed, 365 insertions(+), 5 deletions(-) create mode 100644 tests/codeigniter/libraries/Session_test.php create mode 100644 tests/mocks/libraries/session.php (limited to 'tests') diff --git a/tests/codeigniter/libraries/Session_test.php b/tests/codeigniter/libraries/Session_test.php new file mode 100644 index 000000000..db7efb6b7 --- /dev/null +++ b/tests/codeigniter/libraries/Session_test.php @@ -0,0 +1,304 @@ + 0, 'use_only_cookies' => 0, 'cache_limiter' => false); + protected $setting_vals = array(); + protected $cookie_vals; + protected $session; + + /** + * Set up test framework + */ + public function set_up() + { + // Override settings + foreach ($this->settings as $name => $value) { + $this->setting_vals[$name] = ini_get('session.'.$name); + ini_set('session.'.$name, $value); + } + + // Start with clean environment + $this->cookie_vals = $_COOKIE; + $_COOKIE = array(); + + // Establish necessary support classes + $obj = new stdClass; + foreach (array('config' => 'cfg', 'load' => 'load', 'input' => 'in') as $name => $abbr) { + $class = $this->ci_core_class($abbr); + $obj->$name = new $class; + } + $this->ci_instance($obj); + + // Attach session instance locally + $config = array('sess_encrypt_cookie' => FALSE, 'sess_use_database' => FALSE, 'sess_table_name' => '', + 'sess_expiration' => 7200, 'sess_expire_on_close' => FALSE, 'sess_match_ip' => FALSE, + 'sess_match_useragent' => TRUE, 'sess_cookie_name' => 'ci_session', 'cookie_path' => '', + 'cookie_domain' => '', 'cookie_secure' => FALSE, 'cookie_httponly' => FALSE, 'sess_time_to_update' => 300, + 'time_reference' => 'local', 'cookie_prefix' => '', 'encryption_key' => 'foobar', + 'sess_valid_drivers' => array('Mock_Libraries_Session_native', 'Mock_Libraries_Session_cookie')); + $this->session = new Mock_Libraries_Session($config); + } + + /** + * Tear down test framework + */ + public function tear_down() + { + // Restore environment + if (session_id()) session_destroy(); + $_SESSION = array(); + $_COOKIE = $this->cookie_vals; + + // Restore settings + foreach ($this->settings as $name => $value) { + ini_set('session.'.$name, $this->setting_vals[$name]); + } + } + + /** + * Test set_userdata() function + */ + public function test_set_userdata() + { + // Set a userdata message for each driver + $cmsg = 'Some test data'; + $this->session->cookie->set_userdata('test1', $cmsg); + $nmsg = 'Other test data'; + $this->session->native->set_userdata('test1', $nmsg); + + // Verify independent messages + $this->assertEquals($this->session->cookie->userdata('test1'), $cmsg); + $this->assertEquals($this->session->native->userdata('test1'), $nmsg); + } + + /** + * Test the has_userdata() function + */ + public function test_has_userdata() + { + // Set a userdata message for each driver + $cmsg = 'My test data'; + $this->session->cookie->set_userdata('test2', $cmsg); + $nmsg = 'Your test data'; + $this->session->native->set_userdata('test2', $nmsg); + + // Verify independent messages + $this->assertTrue($this->session->cookie->has_userdata('test2')); + $this->assertTrue($this->session->native->has_userdata('test2')); + } + + /** + * Test the all_userdata() function + */ + public function test_all_userdata() + { + // Set a specific series of data for each driver + $cdata = array('one' => 'first', 'two' => 'second', 'three' => 'third', 'foo' => 'bar', 'bar' => 'baz'); + $this->session->cookie->set_userdata($cdata); + $ndata = array('one' => 'gold', 'two' => 'silver', 'three' => 'bronze', 'foo' => 'baz', 'bar' => 'foo'); + $this->session->native->set_userdata($ndata); + + // Make sure all values are present + $call = $this->session->cookie->all_userdata(); + foreach ($cdata as $key => $value) { + $this->assertEquals($value, $call[$key]); + } + $nall = $this->session->native->all_userdata(); + foreach ($ndata as $key => $value) { + $this->assertEquals($value, $nall[$key]); + } + } + + /** + * Test the unset_userdata() function + */ + public function test_unset_userdata() + { + // Set a userdata message for each driver + $cmsg = 'Other test data'; + $this->session->cookie->set_userdata('test3', $cmsg); + $nmsg = 'Sundry test data'; + $this->session->native->set_userdata('test3', $nmsg); + + // Verify independent messages + $this->assertEquals($this->session->cookie->userdata('test3'), $cmsg); + $this->assertEquals($this->session->native->userdata('test3'), $nmsg); + + // Unset them and verify absence + $this->session->cookie->unset_userdata('test3'); + $this->session->native->unset_userdata('test3'); + $this->assertEquals($this->session->cookie->userdata('test3'), NULL); + $this->assertEquals($this->session->native->userdata('test3'), NULL); + } + + /** + * Test the set_flashdata() function + */ + public function test_set_flashdata() + { + // Set flashdata message for each driver + $cmsg = 'Some flash data'; + $this->session->cookie->set_flashdata('test4', $cmsg); + $nmsg = 'Other flash data'; + $this->session->native->set_flashdata('test4', $nmsg); + + // Simulate page reload + $this->session->cookie->reload(); + $this->session->native->reload(); + + // Verify independent messages + $this->assertEquals($this->session->cookie->flashdata('test4'), $cmsg); + $this->assertEquals($this->session->native->flashdata('test4'), $nmsg); + + // Simulate next page reload + $this->session->cookie->reload(); + $this->session->native->reload(); + + // Verify absence of messages + $this->assertEquals($this->session->cookie->flashdata('test4'), NULL); + $this->assertEquals($this->session->native->flashdata('test4'), NULL); + } + + /** + * Test the keep_flashdata() function + */ + public function test_keep_flashdata() + { + // Set flashdata message for each driver + $cmsg = 'My flash data'; + $this->session->cookie->set_flashdata('test5', $cmsg); + $nmsg = 'Your flash data'; + $this->session->native->set_flashdata('test5', $nmsg); + + // Simulate page reload and verify independent messages + $this->session->cookie->reload(); + $this->session->native->reload(); + $this->assertEquals($this->session->cookie->flashdata('test5'), $cmsg); + $this->assertEquals($this->session->native->flashdata('test5'), $nmsg); + + // Keep messages + $this->session->cookie->keep_flashdata('test5'); + $this->session->native->keep_flashdata('test5'); + + // Simulate next page reload and verify message persistence + $this->session->cookie->reload(); + $this->session->native->reload(); + $this->assertEquals($this->session->cookie->flashdata('test5'), $cmsg); + $this->assertEquals($this->session->native->flashdata('test5'), $nmsg); + + // Simulate next page reload and verify absence of messages + $this->session->cookie->reload(); + $this->session->native->reload(); + $this->assertEquals($this->session->cookie->flashdata('test5'), NULL); + $this->assertEquals($this->session->native->flashdata('test5'), NULL); + } + + /** + * Test the all_flashdata() function + */ + public function test_all_flashdata() + { + // Set a specific series of data for each driver + $cdata = array('one' => 'first', 'two' => 'second', 'three' => 'third', 'foo' => 'bar', 'bar' => 'baz'); + $this->session->cookie->set_flashdata($cdata); + $ndata = array('one' => 'gold', 'two' => 'silver', 'three' => 'bronze', 'foo' => 'baz', 'bar' => 'foo'); + $this->session->native->set_flashdata($ndata); + + // Simulate page reload and make sure all values are present + $this->session->cookie->reload(); + $this->session->native->reload(); + $this->assertEquals($cdata, $this->session->cookie->all_flashdata()); + $this->assertEquals($ndata, $this->session->native->all_flashdata()); + } + + /** + * Test the set_tempdata() function + */ + public function test_set_tempdata() + { + // Set tempdata message for each driver - 1 second timeout + $cmsg = 'Some temp data'; + $this->session->cookie->set_tempdata('test6', $cmsg, 1); + $nmsg = 'Other temp data'; + $this->session->native->set_tempdata('test6', $nmsg, 1); + + // Simulate page reload and verify independent messages + $this->session->cookie->reload(); + $this->session->native->reload(); + $this->assertEquals($this->session->cookie->tempdata('test6'), $cmsg); + $this->assertEquals($this->session->native->tempdata('test6'), $nmsg); + + // Wait 2 seconds, simulate page reload and verify message absence + sleep(2); + $this->session->cookie->reload(); + $this->session->native->reload(); + $this->assertEquals($this->session->cookie->tempdata('test6'), NULL); + $this->assertEquals($this->session->native->tempdata('test6'), NULL); + } + + /** + * Test the unset_tempdata() function + */ + public function test_unset_tempdata() + { + // Set tempdata message for each driver - 1 second timeout + $cmsg = 'My temp data'; + $this->session->cookie->set_tempdata('test7', $cmsg, 1); + $nmsg = 'Your temp data'; + $this->session->native->set_tempdata('test7', $nmsg, 1); + + // Verify independent messages + $this->assertEquals($this->session->cookie->tempdata('test7'), $cmsg); + $this->assertEquals($this->session->native->tempdata('test7'), $nmsg); + + // Unset data and verify message absence + $this->session->cookie->unset_tempdata('test7'); + $this->session->native->unset_tempdata('test7'); + $this->assertEquals($this->session->cookie->tempdata('test7'), NULL); + $this->assertEquals($this->session->native->tempdata('test7'), NULL); + } + + /** + * Test the sess_regenerate() function + */ + public function test_sess_regenerate() + { + // Get current session id, regenerate, and compare + // Cookie driver + $oldid = $this->session->cookie->userdata('session_id'); + $this->session->cookie->sess_regenerate(); + $newid = $this->session->cookie->userdata('session_id'); + $this->assertFalse($oldid === $newid); + + // Native driver - bug #55267 (https://bugs.php.net/bug.php?id=55267) prevents testing this + /*$oldid = session_id(); + $this->session->native->sess_regenerate(); + $oldid = session_id(); + $this->assertFalse($oldid === $newid);*/ + } + + /** + * Test the sess_destroy() function + */ + public function test_sess_destroy() + { + // Set a userdata message, destroy session, and verify absence + $msg = 'More test data'; + + // Cookie driver + $this->session->cookie->set_userdata('test8', $msg); + $this->assertEquals($this->session->cookie->userdata('test8'), $msg); + $this->session->cookie->sess_destroy(); + $this->assertEquals($this->session->cookie->userdata('test8'), NULL); + + // Native driver + $this->session->native->set_userdata('test8', $msg); + $this->assertEquals($this->session->native->userdata('test8'), $msg); + $this->session->native->sess_destroy(); + $this->assertEquals($this->session->native->userdata('test8'), NULL); + } +} + diff --git a/tests/mocks/autoloader.php b/tests/mocks/autoloader.php index be1c2220c..88d016bba 100644 --- a/tests/mocks/autoloader.php +++ b/tests/mocks/autoloader.php @@ -26,10 +26,14 @@ function autoload($class) 'Email', 'Encrypt', 'Form_validation', 'Ftp', 'Image_lib', 'Javascript', 'Log', 'Migration', 'Pagination', - 'Parser', 'Profiler', 'Session', - 'Table', 'Trackback', 'Typography', - 'Unit_test', 'Upload', 'User_agent', - 'Xmlrpc', 'Zip', + 'Parser', 'Profiler', 'Table', + 'Trackback', 'Typography', 'Unit_test', + 'Upload', 'User_agent', 'Xmlrpc', + 'Zip', + ); + + $ci_drivers = array( + 'Session', ); if (strpos($class, 'Mock_') === 0) @@ -52,6 +56,15 @@ function autoload($class) $dir = BASEPATH.'libraries'.DIRECTORY_SEPARATOR; $class = ($subclass === 'Driver_Library') ? 'Driver' : $subclass; } + elseif (in_array($subclass, $ci_drivers)) + { + $dir = BASEPATH.'libraries'.DIRECTORY_SEPARATOR.$subclass.DIRECTORY_SEPARATOR; + $class = $subclass; + } + elseif (in_array(($parent = strtok($subclass, '_')), $ci_drivers)) { + $dir = BASEPATH.'libraries'.DIRECTORY_SEPARATOR.$parent.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR; + $class = $subclass; + } elseif (preg_match('/^CI_DB_(.+)_(driver|forge|result|utility)$/', $class, $m) && count($m) === 3) { $driver_path = BASEPATH.'database'.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR; @@ -91,4 +104,4 @@ function autoload($class) } include_once($file); -} \ No newline at end of file +} diff --git a/tests/mocks/libraries/session.php b/tests/mocks/libraries/session.php new file mode 100644 index 000000000..9d6feee42 --- /dev/null +++ b/tests/mocks/libraries/session.php @@ -0,0 +1,43 @@ +_flashdata_sweep(); + $this->_flashdata_mark(); + $this->_tempdata_sweep(); + } +} + +/** + * Mock cookie driver to overload cookie setting + */ +class Mock_Libraries_Session_cookie extends CI_Session_cookie { + /** + * Overload _setcookie to manage $_COOKIE values, since actual cookies can't be set in unit testing + */ + protected function _setcookie($name, $value = '', $expire = 0, $path = '', $domain = '', $secure = false, + $httponly = false) + { + if (empty($value) || $expire <= time()) { + // Clear cookie + unset($_COOKIE[$name]); + } + else { + // Set cookie + $_COOKIE[$name] = $value; + } + } +} + +/** + * Mock native driver (just for consistency in loading) + */ +class Mock_Libraries_Session_native extends CI_Session_native { } + -- cgit v1.2.3-24-g4f1b From 4153c928229ff174743b78472ae27138cd715efe Mon Sep 17 00:00:00 2001 From: dchill42 Date: Tue, 31 Jul 2012 10:38:21 -0400 Subject: Coding standard cleanup and added userdata resync on sess_regenerate() --- tests/codeigniter/libraries/Session_test.php | 73 +++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 12 deletions(-) (limited to 'tests') diff --git a/tests/codeigniter/libraries/Session_test.php b/tests/codeigniter/libraries/Session_test.php index db7efb6b7..5d58a9e69 100644 --- a/tests/codeigniter/libraries/Session_test.php +++ b/tests/codeigniter/libraries/Session_test.php @@ -4,7 +4,11 @@ * Session driver library unit test */ class Session_test extends CI_TestCase { - protected $settings = array('use_cookies' => 0, 'use_only_cookies' => 0, 'cache_limiter' => false); + protected $settings = array( + 'use_cookies' => 0, + 'use_only_cookies' => 0, + 'cache_limiter' => false + ); protected $setting_vals = array(); protected $cookie_vals; protected $session; @@ -26,19 +30,40 @@ class Session_test extends CI_TestCase { // Establish necessary support classes $obj = new stdClass; - foreach (array('config' => 'cfg', 'load' => 'load', 'input' => 'in') as $name => $abbr) { + $classes = array( + 'config' => 'cfg', + 'load' => 'load', + 'input' => 'in' + ); + foreach ($classes as $name => $abbr) { $class = $this->ci_core_class($abbr); $obj->$name = new $class; } $this->ci_instance($obj); // Attach session instance locally - $config = array('sess_encrypt_cookie' => FALSE, 'sess_use_database' => FALSE, 'sess_table_name' => '', - 'sess_expiration' => 7200, 'sess_expire_on_close' => FALSE, 'sess_match_ip' => FALSE, - 'sess_match_useragent' => TRUE, 'sess_cookie_name' => 'ci_session', 'cookie_path' => '', - 'cookie_domain' => '', 'cookie_secure' => FALSE, 'cookie_httponly' => FALSE, 'sess_time_to_update' => 300, - 'time_reference' => 'local', 'cookie_prefix' => '', 'encryption_key' => 'foobar', - 'sess_valid_drivers' => array('Mock_Libraries_Session_native', 'Mock_Libraries_Session_cookie')); + $config = array( + 'sess_encrypt_cookie' => FALSE, + 'sess_use_database' => FALSE, + 'sess_table_name' => '', + 'sess_expiration' => 7200, + 'sess_expire_on_close' => FALSE, + 'sess_match_ip' => FALSE, + 'sess_match_useragent' => TRUE, + 'sess_cookie_name' => 'ci_session', + 'cookie_path' => '', + 'cookie_domain' => '', + 'cookie_secure' => FALSE, + 'cookie_httponly' => FALSE, + 'sess_time_to_update' => 300, + 'time_reference' => 'local', + 'cookie_prefix' => '', + 'encryption_key' => 'foobar', + 'sess_valid_drivers' => array( + 'Mock_Libraries_Session_native', + 'Mock_Libraries_Session_cookie' + ) + ); $this->session = new Mock_Libraries_Session($config); } @@ -96,9 +121,21 @@ class Session_test extends CI_TestCase { public function test_all_userdata() { // Set a specific series of data for each driver - $cdata = array('one' => 'first', 'two' => 'second', 'three' => 'third', 'foo' => 'bar', 'bar' => 'baz'); + $cdata = array( + 'one' => 'first', + 'two' => 'second', + 'three' => 'third', + 'foo' => 'bar', + 'bar' => 'baz' + ); + $ndata = array( + 'one' => 'gold', + 'two' => 'silver', + 'three' => 'bronze', + 'foo' => 'baz', + 'bar' => 'foo' + ); $this->session->cookie->set_userdata($cdata); - $ndata = array('one' => 'gold', 'two' => 'silver', 'three' => 'bronze', 'foo' => 'baz', 'bar' => 'foo'); $this->session->native->set_userdata($ndata); // Make sure all values are present @@ -202,9 +239,21 @@ class Session_test extends CI_TestCase { public function test_all_flashdata() { // Set a specific series of data for each driver - $cdata = array('one' => 'first', 'two' => 'second', 'three' => 'third', 'foo' => 'bar', 'bar' => 'baz'); + $cdata = array( + 'one' => 'first', + 'two' => 'second', + 'three' => 'third', + 'foo' => 'bar', + 'bar' => 'baz' + ); + $ndata = array( + 'one' => 'gold', + 'two' => 'silver', + 'three' => 'bronze', + 'foo' => 'baz', + 'bar' => 'foo' + ); $this->session->cookie->set_flashdata($cdata); - $ndata = array('one' => 'gold', 'two' => 'silver', 'three' => 'bronze', 'foo' => 'baz', 'bar' => 'foo'); $this->session->native->set_flashdata($ndata); // Simulate page reload and make sure all values are present -- cgit v1.2.3-24-g4f1b From 6030719346e9b0eb0e0ea99c679cadeb1fe4afde Mon Sep 17 00:00:00 2001 From: dchill42 Date: Mon, 27 Aug 2012 23:59:31 -0400 Subject: Improved VFS usage in Loader and Config units, added Loader driver test, and moved config load testing to Config unit Signed-off-by: dchill42 --- tests/codeigniter/core/Config_test.php | 52 +++++- tests/codeigniter/core/Loader_test.php | 287 ++++++++++++++++++++++++++++----- tests/mocks/core/loader.php | 32 ++-- 3 files changed, 312 insertions(+), 59 deletions(-) (limited to 'tests') diff --git a/tests/codeigniter/core/Config_test.php b/tests/codeigniter/core/Config_test.php index 30cb90a28..7782a7898 100644 --- a/tests/codeigniter/core/Config_test.php +++ b/tests/codeigniter/core/Config_test.php @@ -90,4 +90,54 @@ class Config_test extends CI_TestCase { $this->assertEquals('http://example.com/system/', $this->config->system_url()); } -} \ No newline at end of file + // -------------------------------------------------------------------- + + public function test_load() + { + // Create VFS tree of application config files + $file1 = 'test.php'; + $file2 = 'secttest'; + $key1 = 'testconfig'; + $val1 = 'my_value'; + $cfg1 = array( + $key1 => $val1 + ); + $cfg2 = array( + 'one' => 'prime', + 'two' => 2, + 'three' => true + ); + $tree = array( + 'application' => array( + 'config' => array( + $file1 => ' 'config->_config_paths = array(vfsStream::url('application').'/'); + + // Test regular load + $this->assertTrue($this->config->load($file1)); + $this->assertEquals($val1, $this->config->item($key1)); + + // Test section load + $this->assertTrue($this->config->load($file2, TRUE)); + $this->assertEquals($cfg2, $this->config->item($file2)); + + // Test graceful fail + $this->assertFalse($this->config->load('not_config_file', FALSE, TRUE)); + + // Test regular fail + $file3 = 'absentia'; + $this->setExpectedException( + 'RuntimeException', + 'CI Error: The configuration file '.$file3.'.php does not exist.' + ); + $this->assertNull($this->config->load($file3)); + } + +} diff --git a/tests/codeigniter/core/Loader_test.php b/tests/codeigniter/core/Loader_test.php index fdea962b7..bb8bfd733 100644 --- a/tests/codeigniter/core/Loader_test.php +++ b/tests/codeigniter/core/Loader_test.php @@ -18,54 +18,123 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- + /** + * @covers CI_Loader::library + */ public function test_library() { $this->_setup_config_mock(); + // Create libraries directory with test library + $lib = 'unit_test_lib'; + $class = 'CI_'.ucfirst($lib); + $content = '_create_content('libraries', $lib, $content, NULL, TRUE); + // Test loading as an array. - $this->assertNull($this->load->library(array('table'))); - $this->assertTrue(class_exists('CI_Table'), 'Table class exists'); - $this->assertAttributeInstanceOf('CI_Table', 'table', $this->ci_obj); + $this->assertNull($this->load->library(array($lib))); + $this->assertTrue(class_exists($class), $class.' does not exist'); + $this->assertAttributeInstanceOf($class, $lib, $this->ci_obj); // Test no lib given - $this->assertEquals(FALSE, $this->load->library()); + $this->assertFalse($this->load->library()); // Test a string given to params - $this->assertEquals(NULL, $this->load->library('table', ' ')); + $this->assertNull($this->load->library($lib, ' ')); + } + + // -------------------------------------------------------------------- + + /** + * @covers CI_Loader::library + */ + public function test_library_config() + { + $this->_setup_config_mock(); + + // Create libraries directory with test library + $lib = 'unit_test_config_lib'; + $class = 'CI_'.ucfirst($lib); + $content = 'config = $params; } } '; + $this->_create_content('libraries', $lib, $content, NULL, TRUE); + + // Create config file + $cfg = array( + 'foo' => 'bar', + 'bar' => 'baz', + 'baz' => false + ); + $this->_create_content('config', $lib, 'assertNull($this->load->library($lib, NULL, $obj)); + $this->assertTrue(class_exists($class), $class.' does not exist'); + $this->assertAttributeInstanceOf($class, $obj, $this->ci_obj); + $this->assertEquals($cfg, $this->ci_obj->$obj->config); } // -------------------------------------------------------------------- + /** + * @covers CI_Loader::library + */ public function test_load_library_in_application_dir() { $this->_setup_config_mock(); - $content = '_create_content('libraries', $lib, $content); - $model = vfsStream::newFile('Super_test_library.php')->withContent($content)->at($this->load->libs_dir); - $this->assertNull($this->load->library('super_test_library')); + // Load library + $this->assertNull($this->load->library($lib)); // Was the model class instantiated. - $this->assertTrue(class_exists('Super_test_library')); + $this->assertTrue(class_exists($class), $class.' does not exist'); + $this->assertAttributeInstanceOf($class, $lib, $this->ci_obj); } // -------------------------------------------------------------------- - private function _setup_config_mock() + /** + * @covers CI_Loader::driver + */ + public function test_driver() { - // Mock up a config object until we - // figure out how to test the library configs - $config = $this->getMock('CI_Config', NULL, array(), '', FALSE); - $config->expects($this->any()) - ->method('load') - ->will($this->returnValue(TRUE)); + $this->_setup_config_mock(); - // Add the mock to our stdClass - $this->ci_instance_var('config', $config); + // Create libraries directory with test driver + $driver = 'unit_test_driver'; + $dir = ucfirst($driver); + $class = 'CI_'.$dir; + $content = '_create_content('libraries', $driver, $content, $dir, TRUE); + + // Test loading as an array. + $this->assertNull($this->load->driver(array($driver))); + $this->assertTrue(class_exists($class), $class.' does not exist'); + $this->assertAttributeInstanceOf($class, $driver, $this->ci_obj); + + // Test loading as a library with a name + $obj = 'testdrive'; + $this->assertNull($this->load->library($driver, NULL, $obj)); + $this->assertAttributeInstanceOf($class, $obj, $this->ci_obj); + + // Test no driver given + $this->assertFalse($this->load->driver()); + + // Test a string given to params + $this->assertNull($this->load->driver($driver, ' ')); } // -------------------------------------------------------------------- + /** + * @covers CI_Loader::model + */ public function test_non_existent_model() { $this->setExpectedException( @@ -79,20 +148,23 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- /** - * @coverts CI_Loader::model + * @covers CI_Loader::model */ public function test_models() { $this->ci_set_core_class('model', 'CI_Model'); - $content = 'withContent($content)->at($this->load->models_dir); + // Create models directory with test model + $model = 'unit_test_model'; + $class = ucfirst($model); + $content = '_create_content('models', $model, $content); - $this->assertNull($this->load->model('unit_test_model')); + // Load model + $this->assertNull($this->load->model($model)); // Was the model class instantiated. - $this->assertTrue(class_exists('Unit_test_model')); + $this->assertTrue(class_exists($class)); // Test no model given $this->assertNull($this->load->model('')); @@ -109,26 +181,27 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- /** - * @coverts CI_Loader::view + * @covers CI_Loader::view */ public function test_load_view() { $this->ci_set_core_class('output', 'CI_Output'); + // Create views directory with test view + $view = 'unit_test_view'; $content = 'This is my test page. '; - $view = vfsStream::newFile('unit_test_view.php')->withContent($content)->at($this->load->views_dir); + $this->_create_content('views', $view, $content); // Use the optional return parameter in this test, so the view is not // run through the output class. - $this->assertEquals('This is my test page. World!', - $this->load->view('unit_test_view', array('hello' => "World!"), TRUE)); - + $out = $this->load->view($view, array('hello' => "World!"), TRUE); + $this->assertEquals('This is my test page. World!', $out); } // -------------------------------------------------------------------- /** - * @coverts CI_Loader::view + * @covers CI_Loader::view */ public function test_non_existent_view() { @@ -142,16 +215,22 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- + /** + * @covers CI_Loader::file + */ public function test_file() { + // Create views directory with test file + $dir = 'views'; + $file = 'ci_test_mock_file'; $content = 'Here is a test file, which we will load now.'; - $file = vfsStream::newFile('ci_test_mock_file.php')->withContent($content)->at($this->load->views_dir); + $this->_create_content($dir, $file, $content); // Just like load->view(), take the output class out of the mix here. - $load = $this->load->file(vfsStream::url('application').'/views/ci_test_mock_file.php', TRUE); - - $this->assertEquals($content, $load); + $out = $this->load->file($this->load->app_path.$dir.'/'.$file.'.php', TRUE); + $this->assertEquals($content, $out); + // Test non-existent file $this->setExpectedException( 'RuntimeException', 'CI Error: Unable to load the requested file: ci_test_file_not_exists' @@ -162,6 +241,9 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- + /** + * @covers CI_Loader::vars + */ public function test_vars() { $this->assertNull($this->load->vars(array('foo' => 'bar'))); @@ -170,10 +252,22 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- + /** + * @covers CI_Loader::helper + */ public function test_helper() { - $this->assertEquals(NULL, $this->load->helper('array')); + // Create helper directory in app path with test helper + $helper = 'test'; + $func = '_my_helper_test_func'; + $content = '_create_content('helpers', $helper.'_helper', $content); + + // Load helper + $this->assertEquals(NULL, $this->load->helper($helper)); + $this->assertTrue(function_exists($func), $func.' does not exist'); + // Test non-existent helper $this->setExpectedException( 'RuntimeException', 'CI Error: Unable to load the requested file: helpers/bad_helper.php' @@ -184,9 +278,31 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- + /** + * @covers CI_Loader::helper + */ public function test_loading_multiple_helpers() { - $this->assertEquals(NULL, $this->load->helpers(array('file', 'array', 'string'))); + // Create helper directory in base path with test helpers + $helpers = array(); + $funcs = array(); + $files = array(); + for ($i = 1; $i <= 3; ++$i) { + $helper = 'test'.$i; + $helpers[] = $helper; + $func = '_my_helper_test_func'.$i; + $funcs[] = $func; + $files[$helper.'_helper'] = '_create_content('helpers', $files, NULL, NULL, TRUE); + + // Load helpers + $this->assertEquals(NULL, $this->load->helpers($helpers)); + + // Verify helper existence + foreach ($funcs as $func) { + $this->assertTrue(function_exists($func), $func.' does not exist'); + } } // -------------------------------------------------------------------- @@ -198,6 +314,52 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- + /** + * @covers CI_Loader::add_package_path + * @covers CI_Loader::get_package_paths + * @covers CI_Loader::remove_package_path + */ + public function test_packages() + { + $this->_setup_config_mock(); + + // Create third-party directory in app path with model + $dir = 'third-party'; + $lib = 'unit_test_package'; + $class = 'CI_'.ucfirst($lib); + $content = '_create_content($dir, $lib, $content); + + // Test failed load without path + $this->setExpectedException( + 'RuntimeException', + 'CI Error: Unable to load the requested class: '.$lib + ); + $this->load->library($lib); + + // Clear exception and get paths + $this->setExpectedException(NULL); + $paths = $this->load->get_package_paths(TRUE); + + // Add path and verify + $path = $this->load->app_path.$dir; + $this->assertNull($this->load->add_package_path($path)); + $this->assertContains($path, $this->load->get_package_paths(TRUE)); + + // Test successful load + $this->assertNull($this->load->library($lib)); + $this->assertTrue(class_exists($class), $class.' does not exist'); + + // Remove path and verify restored paths + $this->assertNull($this->load->remove_package_path($path)); + $this->assertEquals($paths, $this->load->get_package_paths(TRUE)); + } + + // -------------------------------------------------------------------- + + /** + * @covers CI_Loader::config + */ public function test_load_config() { $this->_setup_config_mock(); @@ -206,16 +368,53 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- - public function test_load_bad_config() + private function _setup_config_mock() { - $this->_setup_config_mock(); + // Mock up a config object so config() has something to call + $config = $this->getMock('CI_Config', array('load'), array(), '', FALSE); + $config->expects($this->any()) + ->method('load') + ->will($this->returnValue(TRUE)); - $this->setExpectedException( - 'RuntimeException', - 'CI Error: The configuration file foobar.php does not exist.' - ); + // Reinitialize config paths to use VFS + $config->_config_paths = array($this->load->app_path); - $this->load->config('foobar', FALSE); + // Add the mock to our stdClass + $this->ci_instance_var('config', $config); + } + + // -------------------------------------------------------------------- + + private function _create_content($dir, $file, $content, $sub = NULL, $base = FALSE) + { + // Create structure containing directory + $tree = array($dir => array()); + + // Check for subdirectory + if ($sub) { + // Add subdirectory to tree and get reference + $tree[$dir][$sub] = array(); + $leaf =& $tree[$dir][$sub]; + } + else { + // Get reference to main directory + $leaf =& $tree[$dir]; + } + + // Check for multiple files + if (is_array($file)) { + // Add multiple files to directory + foreach ($file as $name => $data) { + $leaf[$name.'.php'] = $data; + } + } + else { + // Add single file with content + $leaf[$file.'.php'] = $content; + } + + // Create structure under app or base path + vfsStream::create($tree, $base ? $this->load->base_root : $this->load->app_root); } -} \ No newline at end of file +} diff --git a/tests/mocks/core/loader.php b/tests/mocks/core/loader.php index 53d88d55b..e28650dba 100644 --- a/tests/mocks/core/loader.php +++ b/tests/mocks/core/loader.php @@ -5,27 +5,31 @@ 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... + * in the application directory. This will give finer grained control over + * these tests. Also, by mocking the system directory, we eliminate dependency + * on any other classes so errors in libraries, helpers, etc. don't give false + * negatives for the actual loading process. 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')); + // Create VFS tree of loader locations + $this->root = vfsStream::setup(); + $this->app_root = vfsStream::newDirectory('application')->at($this->root); + $this->base_root = vfsStream::newDirectory('system')->at($this->root); - $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()); + // Get VFS app and base path URLs + $this->app_path = vfsStream::url('application').'/'; + $this->base_path = vfsStream::url('system').'/'; + // Set loader paths with VFS URLs $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); + $this->_ci_library_paths = array($this->app_path, $this->base_path); + $this->_ci_helper_paths = array($this->app_path, $this->base_path); + $this->_ci_model_paths = array($this->app_path); + $this->_ci_view_paths = array($this->app_path.'views/' => TRUE); } -} \ No newline at end of file +} -- cgit v1.2.3-24-g4f1b From 08c8304778e573d725b5565f350e44c34fb0d830 Mon Sep 17 00:00:00 2001 From: dchill42 Date: Tue, 28 Aug 2012 17:35:56 -0400 Subject: Minor session test improvements Signed-off-by: dchill42 --- tests/codeigniter/libraries/Session_test.php | 182 +++++++++++++++++---------- 1 file changed, 117 insertions(+), 65 deletions(-) (limited to 'tests') diff --git a/tests/codeigniter/libraries/Session_test.php b/tests/codeigniter/libraries/Session_test.php index 5d58a9e69..135f71806 100644 --- a/tests/codeigniter/libraries/Session_test.php +++ b/tests/codeigniter/libraries/Session_test.php @@ -85,38 +85,66 @@ class Session_test extends CI_TestCase { /** * Test set_userdata() function + * + * @covers CI_Session::set_userdata + * @covers CI_Session::userdata */ public function test_set_userdata() { - // Set a userdata message for each driver - $cmsg = 'Some test data'; - $this->session->cookie->set_userdata('test1', $cmsg); - $nmsg = 'Other test data'; - $this->session->native->set_userdata('test1', $nmsg); + // Set userdata values for each driver + $key1 = 'test1'; + $ckey2 = 'test2'; + $nkey2 = 'test3'; + $cmsg1 = 'Some test data'; + $cmsg2 = 42; + $nmsg1 = 'Other test data'; + $nmsg2 = true; + $this->session->cookie->set_userdata($key1, $cmsg1); + $this->session->set_userdata($ckey2, $cmsg2); + $this->session->native->set_userdata($key1, $nmsg1); + $this->session->set_userdata($nkey2, $nmsg2); // Verify independent messages - $this->assertEquals($this->session->cookie->userdata('test1'), $cmsg); - $this->assertEquals($this->session->native->userdata('test1'), $nmsg); + $this->assertEquals($cmsg1, $this->session->cookie->userdata($key1)); + $this->assertEquals($nmsg1, $this->session->native->userdata($key1)); + + // Verify pre-selected driver sets + $this->assertEquals($cmsg2, $this->session->cookie->userdata($ckey2)); + $this->assertEquals($nmsg2, $this->session->native->userdata($nkey2)); + + // Verify no crossover + $this->assertNull($this->session->cookie->userdata($nkey2)); + $this->assertNull($this->session->native->userdata($ckey2)); } /** * Test the has_userdata() function + * + * @covers CI_Session::has_userdata */ public function test_has_userdata() { - // Set a userdata message for each driver + // Set a userdata value for each driver + $key = 'hastest'; $cmsg = 'My test data'; - $this->session->cookie->set_userdata('test2', $cmsg); + $this->session->cookie->set_userdata($key, $cmsg); $nmsg = 'Your test data'; - $this->session->native->set_userdata('test2', $nmsg); + $this->session->native->set_userdata($key, $nmsg); - // Verify independent messages - $this->assertTrue($this->session->cookie->has_userdata('test2')); - $this->assertTrue($this->session->native->has_userdata('test2')); + // Verify values exist + $this->assertTrue($this->session->cookie->has_userdata($key)); + $this->assertTrue($this->session->native->has_userdata($key)); + + // Verify non-existent values + $nokey = 'hasnot'; + $this->assertFalse($this->session->cookie->has_userdata($nokey)); + $this->assertFalse($this->session->native->has_userdata($nokey)); } /** * Test the all_userdata() function + * + * @covers CI_Session::all_userdata */ public function test_all_userdata() { @@ -151,90 +179,102 @@ class Session_test extends CI_TestCase { /** * Test the unset_userdata() function + * + * @covers CI_Session::unset_userdata */ public function test_unset_userdata() { // Set a userdata message for each driver + $key = 'untest'; $cmsg = 'Other test data'; - $this->session->cookie->set_userdata('test3', $cmsg); + $this->session->cookie->set_userdata($key, $cmsg); $nmsg = 'Sundry test data'; - $this->session->native->set_userdata('test3', $nmsg); + $this->session->native->set_userdata($key, $nmsg); // Verify independent messages - $this->assertEquals($this->session->cookie->userdata('test3'), $cmsg); - $this->assertEquals($this->session->native->userdata('test3'), $nmsg); + $this->assertEquals($this->session->cookie->userdata($key), $cmsg); + $this->assertEquals($this->session->native->userdata($key), $nmsg); // Unset them and verify absence - $this->session->cookie->unset_userdata('test3'); - $this->session->native->unset_userdata('test3'); - $this->assertEquals($this->session->cookie->userdata('test3'), NULL); - $this->assertEquals($this->session->native->userdata('test3'), NULL); + $this->session->cookie->unset_userdata($key); + $this->session->native->unset_userdata($key); + $this->assertNull($this->session->cookie->userdata($key)); + $this->assertNull($this->session->native->userdata($key)); } /** - * Test the set_flashdata() function + * Test the flashdata() functions + * + * @covers CI_Session::set_flashdata + * @covers CI_Session::flashdata */ - public function test_set_flashdata() + public function test_flashdata() { // Set flashdata message for each driver + $key = 'fltest'; $cmsg = 'Some flash data'; - $this->session->cookie->set_flashdata('test4', $cmsg); + $this->session->cookie->set_flashdata($key, $cmsg); $nmsg = 'Other flash data'; - $this->session->native->set_flashdata('test4', $nmsg); + $this->session->native->set_flashdata($key, $nmsg); // Simulate page reload $this->session->cookie->reload(); $this->session->native->reload(); // Verify independent messages - $this->assertEquals($this->session->cookie->flashdata('test4'), $cmsg); - $this->assertEquals($this->session->native->flashdata('test4'), $nmsg); + $this->assertEquals($cmsg, $this->session->cookie->flashdata($key)); + $this->assertEquals($nmsg, $this->session->native->flashdata($key)); // Simulate next page reload $this->session->cookie->reload(); $this->session->native->reload(); // Verify absence of messages - $this->assertEquals($this->session->cookie->flashdata('test4'), NULL); - $this->assertEquals($this->session->native->flashdata('test4'), NULL); + $this->assertNull($this->session->cookie->flashdata($key)); + $this->assertNull($this->session->native->flashdata($key)); } /** * Test the keep_flashdata() function + * + * @covers CI_Session::keep_flashdata */ public function test_keep_flashdata() { // Set flashdata message for each driver + $key = 'kfltest'; $cmsg = 'My flash data'; - $this->session->cookie->set_flashdata('test5', $cmsg); + $this->session->cookie->set_flashdata($key, $cmsg); $nmsg = 'Your flash data'; - $this->session->native->set_flashdata('test5', $nmsg); + $this->session->native->set_flashdata($key, $nmsg); // Simulate page reload and verify independent messages $this->session->cookie->reload(); $this->session->native->reload(); - $this->assertEquals($this->session->cookie->flashdata('test5'), $cmsg); - $this->assertEquals($this->session->native->flashdata('test5'), $nmsg); + $this->assertEquals($cmsg, $this->session->cookie->flashdata($key)); + $this->assertEquals($nmsg, $this->session->native->flashdata($key)); // Keep messages - $this->session->cookie->keep_flashdata('test5'); - $this->session->native->keep_flashdata('test5'); + $this->session->cookie->keep_flashdata($key); + $this->session->native->keep_flashdata($key); // Simulate next page reload and verify message persistence $this->session->cookie->reload(); $this->session->native->reload(); - $this->assertEquals($this->session->cookie->flashdata('test5'), $cmsg); - $this->assertEquals($this->session->native->flashdata('test5'), $nmsg); + $this->assertEquals($cmsg, $this->session->cookie->flashdata($key)); + $this->assertEquals($nmsg, $this->session->native->flashdata($key)); // Simulate next page reload and verify absence of messages $this->session->cookie->reload(); $this->session->native->reload(); - $this->assertEquals($this->session->cookie->flashdata('test5'), NULL); - $this->assertEquals($this->session->native->flashdata('test5'), NULL); + $this->assertNull($this->session->cookie->flashdata($key)); + $this->assertNull($this->session->native->flashdata($key)); } /** * Test the all_flashdata() function + * + * @covers CI_Session::all_flashdata */ public function test_all_flashdata() { @@ -264,54 +304,63 @@ class Session_test extends CI_TestCase { } /** - * Test the set_tempdata() function + * Test the tempdata() functions + * + * @covers CI_Session::set_tempdata + * @covers CI_Session::tempdata */ public function test_set_tempdata() { // Set tempdata message for each driver - 1 second timeout + $key = 'tmptest'; $cmsg = 'Some temp data'; - $this->session->cookie->set_tempdata('test6', $cmsg, 1); + $this->session->cookie->set_tempdata($key, $cmsg, 1); $nmsg = 'Other temp data'; - $this->session->native->set_tempdata('test6', $nmsg, 1); + $this->session->native->set_tempdata($key, $nmsg, 1); // Simulate page reload and verify independent messages $this->session->cookie->reload(); $this->session->native->reload(); - $this->assertEquals($this->session->cookie->tempdata('test6'), $cmsg); - $this->assertEquals($this->session->native->tempdata('test6'), $nmsg); + $this->assertEquals($cmsg, $this->session->cookie->tempdata($key)); + $this->assertEquals($nmsg, $this->session->native->tempdata($key)); // Wait 2 seconds, simulate page reload and verify message absence sleep(2); $this->session->cookie->reload(); $this->session->native->reload(); - $this->assertEquals($this->session->cookie->tempdata('test6'), NULL); - $this->assertEquals($this->session->native->tempdata('test6'), NULL); + $this->assertNull($this->session->cookie->tempdata($key)); + $this->assertNull($this->session->native->tempdata($key)); } /** * Test the unset_tempdata() function + * + * @covers CI_Session::unset_tempdata */ public function test_unset_tempdata() { // Set tempdata message for each driver - 1 second timeout + $key = 'utmptest'; $cmsg = 'My temp data'; - $this->session->cookie->set_tempdata('test7', $cmsg, 1); + $this->session->cookie->set_tempdata($key, $cmsg, 1); $nmsg = 'Your temp data'; - $this->session->native->set_tempdata('test7', $nmsg, 1); + $this->session->native->set_tempdata($key, $nmsg, 1); // Verify independent messages - $this->assertEquals($this->session->cookie->tempdata('test7'), $cmsg); - $this->assertEquals($this->session->native->tempdata('test7'), $nmsg); + $this->assertEquals($cmsg, $this->session->cookie->tempdata($key)); + $this->assertEquals($nmsg, $this->session->native->tempdata($key)); // Unset data and verify message absence - $this->session->cookie->unset_tempdata('test7'); - $this->session->native->unset_tempdata('test7'); - $this->assertEquals($this->session->cookie->tempdata('test7'), NULL); - $this->assertEquals($this->session->native->tempdata('test7'), NULL); + $this->session->cookie->unset_tempdata($key); + $this->session->native->unset_tempdata($key); + $this->assertNull($this->session->cookie->tempdata($key)); + $this->assertNull($this->session->native->tempdata($key)); } /** * Test the sess_regenerate() function + * + * @covers CI_Session::sess_regenerate */ public function test_sess_regenerate() { @@ -320,34 +369,37 @@ class Session_test extends CI_TestCase { $oldid = $this->session->cookie->userdata('session_id'); $this->session->cookie->sess_regenerate(); $newid = $this->session->cookie->userdata('session_id'); - $this->assertFalse($oldid === $newid); + $this->assertNotEquals($oldid, $newid); // Native driver - bug #55267 (https://bugs.php.net/bug.php?id=55267) prevents testing this - /*$oldid = session_id(); - $this->session->native->sess_regenerate(); - $oldid = session_id(); - $this->assertFalse($oldid === $newid);*/ + // $oldid = session_id(); + // $this->session->native->sess_regenerate(); + // $oldid = session_id(); + // $this->assertNotEquals($oldid, $newid); } /** * Test the sess_destroy() function + * + * @covers CI_Session::sess_destroy */ public function test_sess_destroy() { // Set a userdata message, destroy session, and verify absence + $key = 'dsttest'; $msg = 'More test data'; // Cookie driver - $this->session->cookie->set_userdata('test8', $msg); - $this->assertEquals($this->session->cookie->userdata('test8'), $msg); + $this->session->cookie->set_userdata($key, $msg); + $this->assertEquals($msg, $this->session->cookie->userdata($key)); $this->session->cookie->sess_destroy(); - $this->assertEquals($this->session->cookie->userdata('test8'), NULL); + $this->assertNull($this->session->cookie->userdata($key)); // Native driver - $this->session->native->set_userdata('test8', $msg); - $this->assertEquals($this->session->native->userdata('test8'), $msg); + $this->session->native->set_userdata($key, $msg); + $this->assertEquals($msg, $this->session->native->userdata($key)); $this->session->native->sess_destroy(); - $this->assertEquals($this->session->native->userdata('test8'), NULL); + $this->assertNull($this->session->native->userdata($key)); } } -- cgit v1.2.3-24-g4f1b From 60d100f0fde3fba4fa4015f44f490b7ecac16138 Mon Sep 17 00:00:00 2001 From: dchill42 Date: Wed, 29 Aug 2012 12:58:26 -0400 Subject: Added autoloader unit test with minor supporting change in Loader Signed-off-by: dchill42 --- tests/codeigniter/core/Loader_test.php | 70 ++++++++++++++++++++++++++++++++++ tests/mocks/core/loader.php | 9 +++++ 2 files changed, 79 insertions(+) (limited to 'tests') diff --git a/tests/codeigniter/core/Loader_test.php b/tests/codeigniter/core/Loader_test.php index bb8bfd733..db79e6a8b 100644 --- a/tests/codeigniter/core/Loader_test.php +++ b/tests/codeigniter/core/Loader_test.php @@ -368,6 +368,76 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- + /** + * @covers CI_Loader::_ci_autoloader + */ + public function test_autoloader() + { + $this->_setup_config_mock(); + + // Create helper directory in app path with test helper + $helper = 'autohelp'; + $hlp_func = '_autohelp_test_func'; + $this->_create_content('helpers', $helper.'_helper', '_create_content('libraries', $lib, ' array($drv.'.php' => 'load->base_root->getChild('libraries')); + + // Create package directory in app path with model + $dir = 'testdir'; + $path = $this->load->app_path.$dir.'/'; + $model = 'automod'; + $mod_class = ucfirst($model); + $this->_create_content($dir, $model, ' array($path), + 'helper' => array($helper), + 'libraries' => array($lib), + 'drivers' => array($drv), + 'model' => array($model), + 'config' => array() + ); + $this->_create_content('config', 'autoload', 'load->autoload(); + + // Verify path + $this->assertContains($path, $this->load->get_package_paths()); + + // Verify helper + $this->assertTrue(function_exists($hlp_func), $hlp_func.' does not exist'); + + // Verify library + $this->assertTrue(class_exists($lib_class), $lib_class.' does not exist'); + $this->assertAttributeInstanceOf($lib_class, $lib, $this->ci_obj); + + // Verify driver + $this->assertTrue(class_exists($drv_class), $drv_class.' does not exist'); + $this->assertAttributeInstanceOf($drv_class, $drv, $this->ci_obj); + + // Verify model + $this->assertTrue(class_exists($mod_class), $mod_class.' does not exist'); + $this->assertAttributeInstanceOf($mod_class, $model, $this->ci_obj); + } + + // -------------------------------------------------------------------- + private function _setup_config_mock() { // Mock up a config object so config() has something to call diff --git a/tests/mocks/core/loader.php b/tests/mocks/core/loader.php index e28650dba..c0e02139e 100644 --- a/tests/mocks/core/loader.php +++ b/tests/mocks/core/loader.php @@ -30,6 +30,15 @@ class Mock_Core_Loader extends CI_Loader { $this->_ci_helper_paths = array($this->app_path, $this->base_path); $this->_ci_model_paths = array($this->app_path); $this->_ci_view_paths = array($this->app_path.'views/' => TRUE); + $this->_ci_autoloader_path = $this->app_path; + } + + /** + * Give public access to _ci_autoloader for testing + */ + public function autoload() + { + $this->_ci_autoloader(); } } -- cgit v1.2.3-24-g4f1b From 0c886901bc013e477b8c5eb9bc5e60c91d1fac56 Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Wed, 29 Aug 2012 23:23:22 +0100 Subject: Added lang mock and upload mock --- tests/mocks/core/lang.php | 15 +++++++++++++++ tests/mocks/libraries/upload.php | 3 +++ 2 files changed, 18 insertions(+) create mode 100644 tests/mocks/core/lang.php create mode 100644 tests/mocks/libraries/upload.php (limited to 'tests') diff --git a/tests/mocks/core/lang.php b/tests/mocks/core/lang.php new file mode 100644 index 000000000..1b99aedb3 --- /dev/null +++ b/tests/mocks/core/lang.php @@ -0,0 +1,15 @@ + Date: Wed, 29 Aug 2012 23:23:37 +0100 Subject: Added initial upload library tests --- tests/codeigniter/libraries/Upload_test.php | 231 ++++++++++++++++++++++++++++ 1 file changed, 231 insertions(+) create mode 100644 tests/codeigniter/libraries/Upload_test.php (limited to 'tests') diff --git a/tests/codeigniter/libraries/Upload_test.php b/tests/codeigniter/libraries/Upload_test.php new file mode 100644 index 000000000..021af28a7 --- /dev/null +++ b/tests/codeigniter/libraries/Upload_test.php @@ -0,0 +1,231 @@ +upload = new Mock_Libraries_Upload(); + $obj->security = new Mock_Core_Security(); + $obj->lang = new Mock_Core_Lang(); + + $this->ci_instance($obj); + $this->upload = $obj->upload; + + vfsStreamWrapper::register(); + vfsStreamWrapper::setRoot(new vfsStreamDirectory('testDir')); + + $this->_test_dir = vfsStreamWrapper::getRoot(); + } + + function test_do_upload() + { + $this->markTestIncomplete('TODO'); + } + + function test_data() + { + $data = array( + 'file_name' => 'hello.txt', + 'file_type' => 'text/plain', + 'file_path' => '/tmp/', + 'full_path' => '/tmp/hello.txt', + 'raw_name' => 'hello', + 'orig_name' => 'hello.txt', + 'client_name' => '', + 'file_ext' => '.txt', + 'file_size' => 100, + 'is_image' => FALSE, + 'image_width' => '', + 'image_height' => '', + 'image_type' => '', + 'image_size_str' => '' + ); + + $this->upload->set_upload_path('/tmp/'); + + foreach ($data as $k => $v) + { + $this->upload->{$k} = $v; + } + + $this->assertEquals('hello.txt', $this->upload->data('file_name')); + $this->assertEquals($data, $this->upload->data()); + } + + function test_set_upload_path() + { + $this->upload->set_upload_path('/tmp/'); + $this->assertEquals('/tmp/', $this->upload->upload_path); + + $this->upload->set_upload_path('/tmp'); + $this->assertEquals('/tmp/', $this->upload->upload_path); + } + + function test_set_filename() + { + $this->markTestIncomplete('TODO'); + } + + function test_set_max_filesize() + { + $this->upload->set_max_filesize(100); + $this->assertEquals(100, $this->upload->max_size); + } + + function test_set_max_filename() + { + $this->upload->set_max_filename(100); + $this->assertEquals(100, $this->upload->max_filename); + } + + function test_set_max_width() + { + $this->upload->set_max_width(100); + $this->assertEquals(100, $this->upload->max_width); + } + + function test_set_max_height() + { + $this->upload->set_max_height(100); + $this->assertEquals(100, $this->upload->max_height); + } + + function test_set_allowed_types() + { + $this->upload->set_allowed_types('*'); + $this->assertEquals('*', $this->upload->allowed_types); + + $this->upload->set_allowed_types('foo|bar'); + $this->assertEquals(array('foo', 'bar'), $this->upload->allowed_types); + } + + function test_set_image_properties() + { + $this->markTestIncomplete('TODO'); + } + + function test_set_xss_clean() + { + $this->upload->set_xss_clean(TRUE); + $this->assertTrue($this->upload->xss_clean); + + $this->upload->set_xss_clean(FALSE); + $this->assertFalse($this->upload->xss_clean); + } + + function test_is_image() + { + $this->upload->file_type = 'image/x-png'; + $this->assertTrue($this->upload->is_image()); + + $this->upload->file_type = 'text/plain'; + $this->assertFalse($this->upload->is_image()); + } + + function test_is_allowed_filetype() + { + $this->upload->allowed_types = array('html'); + + $this->upload->file_ext = '.txt'; + $this->upload->file_type = 'text/plain'; + $this->assertFalse($this->upload->is_allowed_filetype(FALSE)); + $this->assertFalse($this->upload->is_allowed_filetype(TRUE)); + + $this->upload->file_ext = '.html'; + $this->upload->file_type = 'text/html'; + $this->assertTrue($this->upload->is_allowed_filetype(FALSE)); + $this->assertTrue($this->upload->is_allowed_filetype(TRUE)); + + $this->markTestIncomplete('Image tests'); + } + + function test_is_allowed_filesize() + { + $this->upload->max_size = 100; + $this->upload->file_size = 99; + + $this->assertTrue($this->upload->is_allowed_filesize()); + + $this->upload->file_size = 101; + $this->assertFalse($this->upload->is_allowed_filesize()); + } + + function test_is_allowed_dimensions() + { + $this->markTestIncomplete('TODO'); + } + + function test_validate_upload_path() + { + $this->upload->upload_path = ''; + $this->assertFalse($this->upload->validate_upload_path()); + + $this->upload->upload_path = vfsStream::url('testDir'); + $this->assertTrue($this->upload->validate_upload_path()); + } + + function test_get_extension() + { + $this->assertEquals('.txt', $this->upload->get_extension('hello.txt')); + $this->assertEquals('.htaccess', $this->upload->get_extension('.htaccess')); + $this->assertEquals('', $this->upload->get_extension('hello')); + } + + function test_clean_file_name() + { + $this->assertEquals('hello.txt', $this->upload->clean_file_name('hello.txt')); + $this->assertEquals('hello.txt', $this->upload->clean_file_name('%253chell>o.txt')); + } + + function test_limit_filename_length() + { + $this->assertEquals('hello.txt', $this->upload->limit_filename_length('hello.txt', 10)); + $this->assertEquals('hello.txt', $this->upload->limit_filename_length('hello-world.txt', 9)); + } + + function test_do_xss_clean() + { + $file1 = vfsStream::newFile('file1.txt')->withContent('The billy goat was waiting for them.')->at($this->_test_dir); + $file2 = vfsStream::newFile('file2.txt')->at($this->_test_dir); + $file3 = vfsStream::newFile('file3.txt')->withContent('')->at($this->_test_dir); + + $this->upload->file_temp = vfsStream::url('file1.txt'); + $this->assertTrue($this->upload->do_xss_clean()); + + $this->upload->file_temp = vfsStream::url('file2.txt'); + $this->assertFalse($this->upload->do_xss_clean()); + + $this->upload->file_temp = vfsStream::url('file3.txt'); + $this->assertFalse($this->upload->do_xss_clean()); + } + + function test_set_error() + { + $errors = array( + 'An error!' + ); + + $another = 'Another error!'; + + $this->upload->set_error($errors); + $this->assertEquals($errors, $this->upload->error_msg); + + $errors[] = $another; + $this->upload->set_error($another); + $this->assertEquals($errors, $this->upload->error_msg); + } + + function test_display_errors() + { + $this->upload->error_msg[] = 'Error test'; + $this->assertEquals('

Error test

', $this->upload->display_errors()); + } + + function test_mimes_types() + { + $this->assertEquals('text/plain', $this->upload->mimes_types('txt')); + $this->assertFalse($this->upload->mimes_types('foobar')); + } + +} \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 096b894c7eb0db6ab13aa4478ebf52524b429fcb Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Thu, 30 Aug 2012 00:20:36 +0100 Subject: Finished the final tests (except do_upload) --- tests/codeigniter/libraries/Upload_test.php | 53 ++++++++++++++++++++++++---- tests/mocks/uploads/ci_logo.gif | Bin 0 -> 3270 bytes 2 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 tests/mocks/uploads/ci_logo.gif (limited to 'tests') diff --git a/tests/codeigniter/libraries/Upload_test.php b/tests/codeigniter/libraries/Upload_test.php index 021af28a7..d79a3ffc9 100644 --- a/tests/codeigniter/libraries/Upload_test.php +++ b/tests/codeigniter/libraries/Upload_test.php @@ -18,9 +18,9 @@ class Upload_test extends CI_TestCase { $this->_test_dir = vfsStreamWrapper::getRoot(); } - function test_do_upload() + function test_do_upload() { - $this->markTestIncomplete('TODO'); + $this->markTestIncomplete('We can\'t really test this at the moment because of the call to `is_uploaded_file` in do_upload which isn\'t supported by vfsStream'); } function test_data() @@ -64,7 +64,11 @@ class Upload_test extends CI_TestCase { function test_set_filename() { - $this->markTestIncomplete('TODO'); + $file1 = vfsStream::newFile('hello-world.txt')->withContent('Hello world.')->at($this->_test_dir); + $this->upload->file_ext = '.txt'; + + $this->assertEquals('helloworld.txt', $this->upload->set_filename(vfsStream::url('testDir').'/', 'helloworld.txt')); + $this->assertEquals('hello-world1.txt', $this->upload->set_filename(vfsStream::url('testDir').'/', 'hello-world.txt')); } function test_set_max_filesize() @@ -102,7 +106,22 @@ class Upload_test extends CI_TestCase { function test_set_image_properties() { - $this->markTestIncomplete('TODO'); + $this->upload->file_type = 'image/gif'; + $this->upload->file_temp = 'tests/mocks/uploads/ci_logo.gif'; + + $props = array( + 'image_width' => 170, + 'image_height' => 73, + 'image_type' => 'gif', + 'image_size_str' => 'width="170" height="73"' + ); + + $this->upload->set_image_properties($this->upload->file_temp); + + $this->assertEquals($props['image_width'], $this->upload->image_width); + $this->assertEquals($props['image_height'], $this->upload->image_height); + $this->assertEquals($props['image_type'], $this->upload->image_type); + $this->assertEquals($props['image_size_str'], $this->upload->image_size_str); } function test_set_xss_clean() @@ -125,7 +144,7 @@ class Upload_test extends CI_TestCase { function test_is_allowed_filetype() { - $this->upload->allowed_types = array('html'); + $this->upload->allowed_types = array('html', 'gif'); $this->upload->file_ext = '.txt'; $this->upload->file_type = 'text/plain'; @@ -137,7 +156,10 @@ class Upload_test extends CI_TestCase { $this->assertTrue($this->upload->is_allowed_filetype(FALSE)); $this->assertTrue($this->upload->is_allowed_filetype(TRUE)); - $this->markTestIncomplete('Image tests'); + $this->upload->file_temp = 'tests/mocks/uploads/ci_logo.gif'; + $this->upload->file_ext = '.gif'; + $this->upload->file_type = 'image/gif'; + $this->assertTrue($this->upload->is_allowed_filetype()); } function test_is_allowed_filesize() @@ -153,7 +175,21 @@ class Upload_test extends CI_TestCase { function test_is_allowed_dimensions() { - $this->markTestIncomplete('TODO'); + $this->upload->file_type = 'text/plain'; + $this->assertTrue($this->upload->is_allowed_dimensions()); + + $this->upload->file_type = 'image/gif'; + $this->upload->file_temp = 'tests/mocks/uploads/ci_logo.gif'; + + $this->upload->max_width = 10; + $this->assertFalse($this->upload->is_allowed_dimensions()); + + $this->upload->max_width = 170; + $this->upload->max_height = 10; + $this->assertFalse($this->upload->is_allowed_dimensions()); + + $this->upload->max_height = 73; + $this->assertTrue($this->upload->is_allowed_dimensions()); } function test_validate_upload_path() @@ -198,6 +234,9 @@ class Upload_test extends CI_TestCase { $this->upload->file_temp = vfsStream::url('file3.txt'); $this->assertFalse($this->upload->do_xss_clean()); + + $this->upload->file_temp = 'tests/mocks/uploads/ci_logo.gif'; + $this->assertTrue($this->upload->do_xss_clean()); } function test_set_error() diff --git a/tests/mocks/uploads/ci_logo.gif b/tests/mocks/uploads/ci_logo.gif new file mode 100644 index 000000000..073ec14b4 Binary files /dev/null and b/tests/mocks/uploads/ci_logo.gif differ -- cgit v1.2.3-24-g4f1b From eeb6a480a4a25b7fe96e9ba0cf3aef273fd13c67 Mon Sep 17 00:00:00 2001 From: dchill42 Date: Thu, 30 Aug 2012 09:46:20 -0400 Subject: Better way - autoloader uses first config path Signed-off-by: dchill42 --- tests/mocks/core/loader.php | 1 - 1 file changed, 1 deletion(-) (limited to 'tests') diff --git a/tests/mocks/core/loader.php b/tests/mocks/core/loader.php index c0e02139e..9eb78253b 100644 --- a/tests/mocks/core/loader.php +++ b/tests/mocks/core/loader.php @@ -30,7 +30,6 @@ class Mock_Core_Loader extends CI_Loader { $this->_ci_helper_paths = array($this->app_path, $this->base_path); $this->_ci_model_paths = array($this->app_path); $this->_ci_view_paths = array($this->app_path.'views/' => TRUE); - $this->_ci_autoloader_path = $this->app_path; } /** -- cgit v1.2.3-24-g4f1b From 5e9710842dc6981f4eb03c1622eabee9b3171dea Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Mon, 3 Sep 2012 18:57:35 +0100 Subject: Added Calendar library unit test --- tests/codeigniter/libraries/Calendar_test.php | 204 ++++++++++++++++++++++++++ tests/mocks/libraries/calendar.php | 25 ++++ 2 files changed, 229 insertions(+) create mode 100644 tests/codeigniter/libraries/Calendar_test.php create mode 100644 tests/mocks/libraries/calendar.php (limited to 'tests') diff --git a/tests/codeigniter/libraries/Calendar_test.php b/tests/codeigniter/libraries/Calendar_test.php new file mode 100644 index 000000000..95668d70d --- /dev/null +++ b/tests/codeigniter/libraries/Calendar_test.php @@ -0,0 +1,204 @@ +calendar = new Mock_Libraries_Calendar(); + + $this->calendar = $obj->calendar; + } + + function test_initialize() + { + $this->calendar->initialize(array( + 'month_type' => 'short', + 'start_day' => 'monday' + )); + $this->assertEquals('short', $this->calendar->month_type); + $this->assertEquals('monday', $this->calendar->start_day); + } + + /** + * @covers Mock_Libraries_Calendar::parse_template + */ + function test_generate() + { + $no_events = ' + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
September 2011
SuMoTuWeThFrSa
    123
45678910
11121314151617
18192021222324
252627282930 
'; + + $this->assertEquals($no_events, $this->calendar->generate(2011, 9)); + + $data = array( + 3 => 'http://example.com/news/article/2006/03/', + 7 => 'http://example.com/news/article/2006/07/', + 13 => 'http://example.com/news/article/2006/13/', + 26 => 'http://example.com/news/article/2006/26/' + ); + + $events = ' + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
September 2011
SuMoTuWeThFrSa
    123
45678910
11121314151617
18192021222324
252627282930 
'; + + $this->assertEquals($events, $this->calendar->generate(2011, 9, $data)); + } + + function test_get_month_name() + { + $this->calendar->month_type = NULL; + $this->assertEquals('January', $this->calendar->get_month_name('01')); + + $this->calendar->month_type = 'short'; + $this->assertEquals('Jan', $this->calendar->get_month_name('01')); + } + + function test_get_day_names() + { + $this->assertEquals(array( + 'Sunday', + 'Monday', + 'Tuesday', + 'Wednesday', + 'Thursday', + 'Friday', + 'Saturday' + ), $this->calendar->get_day_names('long')); + + $this->assertEquals(array( + 'Sun', + 'Mon', + 'Tue', + 'Wed', + 'Thu', + 'Fri', + 'Sat' + ), $this->calendar->get_day_names('short')); + + $this->calendar->day_type = NULL; + + $this->assertEquals(array( + 'Su', + 'Mo', + 'Tu', + 'We', + 'Th', + 'Fr', + 'Sa' + ), $this->calendar->get_day_names()); + } + + function test_adjust_date() + { + $this->assertEquals(array('month' => 8, 'year' => 2012), $this->calendar->adjust_date(8, 2012)); + $this->assertEquals(array('month' => 1, 'year' => 2013), $this->calendar->adjust_date(13, 2012)); + } + + function test_get_total_days() + { + $this->assertEquals(0, $this->calendar->get_total_days(13, 2012)); + + $this->assertEquals(31, $this->calendar->get_total_days(1, 2012)); + $this->assertEquals(28, $this->calendar->get_total_days(2, 2011)); + $this->assertEquals(29, $this->calendar->get_total_days(2, 2012)); + $this->assertEquals(31, $this->calendar->get_total_days(3, 2012)); + $this->assertEquals(30, $this->calendar->get_total_days(4, 2012)); + $this->assertEquals(31, $this->calendar->get_total_days(5, 2012)); + $this->assertEquals(30, $this->calendar->get_total_days(6, 2012)); + $this->assertEquals(31, $this->calendar->get_total_days(7, 2012)); + $this->assertEquals(31, $this->calendar->get_total_days(8, 2012)); + $this->assertEquals(30, $this->calendar->get_total_days(9, 2012)); + $this->assertEquals(31, $this->calendar->get_total_days(10, 2012)); + $this->assertEquals(30, $this->calendar->get_total_days(11, 2012)); + $this->assertEquals(31, $this->calendar->get_total_days(12, 2012)); + } + + function test_default_template() + { + $array = array( + 'table_open' => '', + 'heading_row_start' => '', + 'heading_previous_cell' => '', + 'heading_title_cell' => '', + 'heading_next_cell' => '', + 'heading_row_end' => '', + 'week_row_start' => '', + 'week_day_cell' => '', + 'week_row_end' => '', + 'cal_row_start' => '', + 'cal_cell_start' => '', + 'cal_cell_end_today' => '', + 'cal_row_end' => '', + 'table_close' => '
<<{heading}>>
{week_day}
', + 'cal_cell_start_today' => '', + 'cal_cell_content' => '{day}', + 'cal_cell_content_today' => '{day}', + 'cal_cell_no_content' => '{day}', + 'cal_cell_no_content_today' => '{day}', + 'cal_cell_blank' => ' ', + 'cal_cell_end' => '
' + ); + + $this->assertEquals($array, $this->calendar->default_template()); + } + +} \ No newline at end of file diff --git a/tests/mocks/libraries/calendar.php b/tests/mocks/libraries/calendar.php new file mode 100644 index 000000000..8fee5365e --- /dev/null +++ b/tests/mocks/libraries/calendar.php @@ -0,0 +1,25 @@ +CI = new stdClass; + $this->CI->lang = new Mock_Core_Lang(); + + if ( ! in_array('calendar_lang.php', $this->CI->lang->is_loaded, TRUE)) + { + $this->CI->lang->load('calendar'); + } + + $this->local_time = time(); + + if (count($config) > 0) + { + $this->initialize($config); + } + + log_message('debug', 'Calendar Class Initialized'); + } + +} \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 8280885499ca4b1cffacc9ad78a9eff07a84de25 Mon Sep 17 00:00:00 2001 From: vlakoff Date: Thu, 13 Sep 2012 05:19:59 +0200 Subject: directory_map() was skipping files and directories named "0" Close #1757. Thanks @BennyC! --- tests/codeigniter/helpers/directory_helper_test.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/codeigniter/helpers/directory_helper_test.php b/tests/codeigniter/helpers/directory_helper_test.php index 176ff1d78..c39ccd8d0 100644 --- a/tests/codeigniter/helpers/directory_helper_test.php +++ b/tests/codeigniter/helpers/directory_helper_test.php @@ -19,6 +19,7 @@ class Directory_helper_test extends CI_TestCase { 'benchmark.html' => '', 'database' => array('active_record.html' => '', 'binds.html' => ''), 'email.html' => '', + '0' => '', '.hiddenfile.txt' => '' ) ); @@ -30,7 +31,8 @@ class Directory_helper_test extends CI_TestCase { 'libraries' => array( 'benchmark.html', 'database' => array('active_record.html', 'binds.html'), - 'email.html' + 'email.html', + '0' ) ); -- cgit v1.2.3-24-g4f1b From 431afaedaea55bc20716babe21557fd2b0ad531c Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Fri, 14 Sep 2012 00:57:41 +0700 Subject: Set code-coverage limit scope Signed-off-by: Taufan Aditya --- tests/travis/mysql.phpunit.xml | 2 +- tests/travis/pdo/mysql.phpunit.xml | 2 +- tests/travis/pdo/pgsql.phpunit.xml | 2 +- tests/travis/pdo/sqlite.phpunit.xml | 2 +- tests/travis/pgsql.phpunit.xml | 2 +- tests/travis/sqlite.phpunit.xml | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) (limited to 'tests') diff --git a/tests/travis/mysql.phpunit.xml b/tests/travis/mysql.phpunit.xml index 38c8eba48..06d4a011b 100644 --- a/tests/travis/mysql.phpunit.xml +++ b/tests/travis/mysql.phpunit.xml @@ -18,7 +18,7 @@ - + ../../system diff --git a/tests/travis/pdo/mysql.phpunit.xml b/tests/travis/pdo/mysql.phpunit.xml index c3113a66f..7121edc45 100644 --- a/tests/travis/pdo/mysql.phpunit.xml +++ b/tests/travis/pdo/mysql.phpunit.xml @@ -18,7 +18,7 @@ - + ../../../system diff --git a/tests/travis/pdo/pgsql.phpunit.xml b/tests/travis/pdo/pgsql.phpunit.xml index 232025523..df3ff986e 100644 --- a/tests/travis/pdo/pgsql.phpunit.xml +++ b/tests/travis/pdo/pgsql.phpunit.xml @@ -18,7 +18,7 @@ - + ../../../system diff --git a/tests/travis/pdo/sqlite.phpunit.xml b/tests/travis/pdo/sqlite.phpunit.xml index 3d1256721..7d867f6d1 100644 --- a/tests/travis/pdo/sqlite.phpunit.xml +++ b/tests/travis/pdo/sqlite.phpunit.xml @@ -18,7 +18,7 @@ - + ../../../system diff --git a/tests/travis/pgsql.phpunit.xml b/tests/travis/pgsql.phpunit.xml index 51e433d76..bfddbf6b5 100644 --- a/tests/travis/pgsql.phpunit.xml +++ b/tests/travis/pgsql.phpunit.xml @@ -18,7 +18,7 @@ - + ../../system diff --git a/tests/travis/sqlite.phpunit.xml b/tests/travis/sqlite.phpunit.xml index 701165734..75c946aee 100644 --- a/tests/travis/sqlite.phpunit.xml +++ b/tests/travis/sqlite.phpunit.xml @@ -18,7 +18,7 @@ - + ../../system -- cgit v1.2.3-24-g4f1b From 13be5578e7ea440f4f33bc4c5c22096686224d66 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 4 Oct 2012 12:43:03 +0300 Subject: Fix escape_like_str() tests --- tests/codeigniter/database/query_builder/escape_test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/codeigniter/database/query_builder/escape_test.php b/tests/codeigniter/database/query_builder/escape_test.php index c6380ddf1..27e678f22 100644 --- a/tests/codeigniter/database/query_builder/escape_test.php +++ b/tests/codeigniter/database/query_builder/escape_test.php @@ -27,7 +27,7 @@ class Escape_test extends CI_TestCase { if (strpos(DB_DRIVER, 'mysql') !== FALSE) { - $sql = "SELECT `value` FROM `misc` WHERE `key` LIKE '$string%' ESCAPE '';"; + $sql = "SELECT `value` FROM `misc` WHERE `key` LIKE '$string%' ESCAPE '!';"; } else { @@ -52,7 +52,7 @@ class Escape_test extends CI_TestCase { if (strpos(DB_DRIVER, 'mysql') !== FALSE) { - $sql = "SELECT `value` FROM `misc` WHERE `key` LIKE '$string%' ESCAPE '';"; + $sql = "SELECT `value` FROM `misc` WHERE `key` LIKE '$string%' ESCAPE '!';"; } else { -- cgit v1.2.3-24-g4f1b From 0df3585eec575d2d2e7d17c5339ed219a33dfde8 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 5 Oct 2012 23:10:23 +0300 Subject: Add unit test --- tests/codeigniter/helpers/date_helper_test.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'tests') diff --git a/tests/codeigniter/helpers/date_helper_test.php b/tests/codeigniter/helpers/date_helper_test.php index 1b79b9480..9feade71c 100644 --- a/tests/codeigniter/helpers/date_helper_test.php +++ b/tests/codeigniter/helpers/date_helper_test.php @@ -290,6 +290,29 @@ class Date_helper_test extends CI_TestCase { $this->assertEquals(0, timezones('non_existant')); } + // ------------------------------------------------------------------------ + + public function test_date_range() + { + $dates = array( + '29-01-2012', '30-01-2012', '31-01-2012', + '01-02-2012', '02-02-2012', '03-02-2012', + '04-02-2012', '05-02-2012', '06-02-2012', + '07-02-2012', '08-02-2012', '09-02-2012', + '10-02-2012', '11-02-2012', '12-02-2012', + '13-02-2012', '14-02-2012', '15-02-2012', + '16-02-2012', '17-02-2012', '18-02-2012', + '19-02-2012', '20-02-2012', '21-02-2012', + '22-02-2012', '23-02-2012', '24-02-2012', + '25-02-2012', '26-02-2012', '27-02-2012', + '28-02-2012', '29-02-2012', '01-03-2012' + ); + + $this->assertEquals($dates, date_range(mktime(12, 0, 0, 1, 29, 2012), mktime(12, 0, 0, 3, 1, 2012), TRUE, 'd-m-Y')); + array_pop($dates); + $this->assertEquals($dates, date_range(mktime(12, 0, 0, 1, 29, 2012), 31, FALSE, 'd-m-Y')); + } + } /* End of file date_helper_test.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 580fe8ec482f5df7ca5b91e11b13b72a8f3ed0b8 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 9 Oct 2012 13:27:50 +0300 Subject: Set REMOTE_ADDR in tests --- tests/Bootstrap.php | 1 + 1 file changed, 1 insertion(+) (limited to 'tests') diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php index 5216038c6..1c666d503 100644 --- a/tests/Bootstrap.php +++ b/tests/Bootstrap.php @@ -11,6 +11,7 @@ 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.''); +isset($_SERVER['REMOTE_ADDR']) OR $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; // Get vfsStream either via PEAR or composer foreach (explode(PATH_SEPARATOR, get_include_path()) as $path) -- cgit v1.2.3-24-g4f1b From 82003dac7e1da25ffb0d925ba197121b5b7b27f5 Mon Sep 17 00:00:00 2001 From: dchill42 Date: Tue, 9 Oct 2012 13:28:17 -0400 Subject: Overloaded is_cli_request in Input mock for Session test Signed-off-by: dchill42 --- tests/codeigniter/libraries/Session_test.php | 17 ++++++----------- tests/mocks/core/input.php | 10 ++++++++++ 2 files changed, 16 insertions(+), 11 deletions(-) (limited to 'tests') diff --git a/tests/codeigniter/libraries/Session_test.php b/tests/codeigniter/libraries/Session_test.php index 135f71806..60d3a5b30 100644 --- a/tests/codeigniter/libraries/Session_test.php +++ b/tests/codeigniter/libraries/Session_test.php @@ -30,15 +30,11 @@ class Session_test extends CI_TestCase { // Establish necessary support classes $obj = new stdClass; - $classes = array( - 'config' => 'cfg', - 'load' => 'load', - 'input' => 'in' - ); - foreach ($classes as $name => $abbr) { - $class = $this->ci_core_class($abbr); - $obj->$name = new $class; - } + $cfg = $this->ci_core_class('cfg'); + $obj->config = new $cfg(); + $ldr = $this->ci_core_class('load'); + $obj->load = new $ldr(); + $obj->input = new Mock_Core_Input(NULL, NULL); $this->ci_instance($obj); // Attach session instance locally @@ -401,5 +397,4 @@ class Session_test extends CI_TestCase { $this->session->native->sess_destroy(); $this->assertNull($this->session->native->userdata($key)); } -} - +} \ No newline at end of file diff --git a/tests/mocks/core/input.php b/tests/mocks/core/input.php index 2a4aa4997..0d1873849 100644 --- a/tests/mocks/core/input.php +++ b/tests/mocks/core/input.php @@ -28,4 +28,14 @@ class Mock_Core_Input extends CI_Input { return parent::_fetch_from_array($array, $index, $xss_clean); } + /** + * Lie about being a CLI request + * + * We take advantage of this in libraries/Session_test + */ + public function is_cli_request() + { + return FALSE; + } + } \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 7ecc5cda6647a4b316b44dc40d5925d9ef63c908 Mon Sep 17 00:00:00 2001 From: dchill42 Date: Fri, 12 Oct 2012 16:25:51 -0400 Subject: Integrated vfsStream better and made paths constants VFS-based Signed-off-by: dchill42 --- tests/Bootstrap.php | 14 +- tests/codeigniter/core/Lang_test.php | 2 + tests/codeigniter/core/Loader_test.php | 124 ++++----------- tests/codeigniter/helpers/date_helper_test.php | 2 + tests/codeigniter/helpers/form_helper_test.php | 8 +- tests/codeigniter/helpers/number_helper_test.php | 17 +- tests/codeigniter/helpers/text_helper_test.php | 1 + tests/codeigniter/libraries/Encrypt_test.php | 7 +- tests/codeigniter/libraries/Parser_test.php | 8 +- tests/codeigniter/libraries/Session_test.php | 12 +- tests/codeigniter/libraries/Table_test.php | 8 +- tests/codeigniter/libraries/Typography_test.php | 8 +- tests/codeigniter/libraries/Upload_test.php | 57 +++---- tests/codeigniter/libraries/Useragent_test.php | 7 +- tests/mocks/autoloader.php | 12 +- tests/mocks/ci_testcase.php | 192 ++++++++++++++++++++++- tests/mocks/ci_testconfig.php | 18 +++ tests/mocks/core/common.php | 26 ++- tests/mocks/core/loader.php | 30 ---- 19 files changed, 330 insertions(+), 223 deletions(-) create mode 100644 tests/mocks/ci_testconfig.php (limited to 'tests') diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php index 1c666d503..ea8d8aea8 100644 --- a/tests/Bootstrap.php +++ b/tests/Bootstrap.php @@ -8,10 +8,7 @@ $dir = realpath(dirname(__FILE__)); // Path constants 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.''); -isset($_SERVER['REMOTE_ADDR']) OR $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; +defined('SYSTEM_PATH') OR define('SYSTEM_PATH', PROJECT_BASE.'system/'); // Get vfsStream either via PEAR or composer foreach (explode(PATH_SEPARATOR, get_include_path()) as $path) @@ -31,8 +28,17 @@ if ( ! class_exists('vfsStream') && file_exists(PROJECT_BASE.'vendor/autoload.ph class_alias('org\bovigo\vfs\vfsStreamWrapper', 'vfsStreamWrapper'); } +// Define CI path constants to VFS (filesystem setup in CI_TestCase::setUp) +defined('BASEPATH') OR define('BASEPATH', vfsStream::url('system/')); +defined('APPPATH') OR define('APPPATH', vfsStream::url('application/')); +defined('VIEWPATH') OR define('VIEWPATH', APPPATH.'views/'); + +// Set localhost "remote" IP +isset($_SERVER['REMOTE_ADDR']) OR $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; + // Prep our test environment include_once $dir.'/mocks/core/common.php'; +include_once SYSTEM_PATH.'core/common.php'; include_once $dir.'/mocks/autoloader.php'; spl_autoload_register('autoload'); diff --git a/tests/codeigniter/core/Lang_test.php b/tests/codeigniter/core/Lang_test.php index a410dabfa..3364362e0 100644 --- a/tests/codeigniter/core/Lang_test.php +++ b/tests/codeigniter/core/Lang_test.php @@ -17,6 +17,7 @@ class Lang_test extends CI_TestCase { public function test_load() { + $this->ci_vfs_clone('system/language/english/profiler_lang.php'); $this->assertTrue($this->lang->load('profiler', 'english')); $this->assertEquals('URI STRING', $this->lang->line('profiler_uri_string')); } @@ -25,6 +26,7 @@ class Lang_test extends CI_TestCase { public function test_load_with_unspecified_language() { + $this->ci_vfs_clone('system/language/english/profiler_lang.php'); $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 db79e6a8b..69b2afb63 100644 --- a/tests/codeigniter/core/Loader_test.php +++ b/tests/codeigniter/core/Loader_test.php @@ -9,11 +9,11 @@ class Loader_test extends CI_TestCase { // Instantiate a new loader $this->load = new Mock_Core_Loader(); - // mock up a ci instance - $this->ci_obj = new stdClass; + // Get CI instance + $this->ci_obj = $this->ci_instance(); - // Fix get_instance() - $this->ci_instance($this->ci_obj); + // Set subclass prefix + $this->ci_set_config('subclass_prefix', 'MY_'); } // -------------------------------------------------------------------- @@ -23,13 +23,10 @@ class Loader_test extends CI_TestCase { */ public function test_library() { - $this->_setup_config_mock(); - // Create libraries directory with test library $lib = 'unit_test_lib'; $class = 'CI_'.ucfirst($lib); - $content = '_create_content('libraries', $lib, $content, NULL, TRUE); + $this->ci_vfs_create($lib, 'ci_base_root, 'libraries'); // Test loading as an array. $this->assertNull($this->load->library(array($lib))); @@ -50,13 +47,11 @@ class Loader_test extends CI_TestCase { */ public function test_library_config() { - $this->_setup_config_mock(); - // Create libraries directory with test library $lib = 'unit_test_config_lib'; $class = 'CI_'.ucfirst($lib); - $content = 'config = $params; } } '; - $this->_create_content('libraries', $lib, $content, NULL, TRUE); + $content = 'config = $params; } }'; + $this->ci_vfs_create($lib, $content, $this->ci_base_root, 'libraries'); // Create config file $cfg = array( @@ -64,7 +59,7 @@ class Loader_test extends CI_TestCase { 'bar' => 'baz', 'baz' => false ); - $this->_create_content('config', $lib, 'ci_vfs_create($lib, 'ci_app_root, 'config'); // Test object name and config $obj = 'testy'; @@ -81,13 +76,10 @@ class Loader_test extends CI_TestCase { */ public function test_load_library_in_application_dir() { - $this->_setup_config_mock(); - // Create libraries directory in app path with test library $lib = 'super_test_library'; $class = ucfirst($lib); - $content = '_create_content('libraries', $lib, $content); + $this->ci_vfs_create($lib, 'ci_app_root, 'libraries'); // Load library $this->assertNull($this->load->library($lib)); @@ -104,14 +96,12 @@ class Loader_test extends CI_TestCase { */ public function test_driver() { - $this->_setup_config_mock(); - // Create libraries directory with test driver $driver = 'unit_test_driver'; $dir = ucfirst($driver); $class = 'CI_'.$dir; $content = '_create_content('libraries', $driver, $content, $dir, TRUE); + $this->ci_vfs_create($driver, $content, $this->ci_base_root, 'libraries/'.$dir); // Test loading as an array. $this->assertNull($this->load->driver(array($driver))); @@ -158,7 +148,7 @@ class Loader_test extends CI_TestCase { $model = 'unit_test_model'; $class = ucfirst($model); $content = '_create_content('models', $model, $content); + $this->ci_vfs_create($model, $content, $this->ci_app_root, 'models'); // Load model $this->assertNull($this->load->model($model)); @@ -190,7 +180,7 @@ class Loader_test extends CI_TestCase { // Create views directory with test view $view = 'unit_test_view'; $content = 'This is my test page. '; - $this->_create_content('views', $view, $content); + $this->ci_vfs_create($view, $content, $this->ci_app_root, 'views'); // Use the optional return parameter in this test, so the view is not // run through the output class. @@ -224,10 +214,10 @@ class Loader_test extends CI_TestCase { $dir = 'views'; $file = 'ci_test_mock_file'; $content = 'Here is a test file, which we will load now.'; - $this->_create_content($dir, $file, $content); + $this->ci_vfs_create($file, $content, $this->ci_app_root, $dir); // Just like load->view(), take the output class out of the mix here. - $out = $this->load->file($this->load->app_path.$dir.'/'.$file.'.php', TRUE); + $out = $this->load->file(APPPATH.$dir.'/'.$file.'.php', TRUE); $this->assertEquals($content, $out); // Test non-existent file @@ -261,7 +251,7 @@ class Loader_test extends CI_TestCase { $helper = 'test'; $func = '_my_helper_test_func'; $content = '_create_content('helpers', $helper.'_helper', $content); + $this->ci_vfs_create($helper.'_helper', $content, $this->ci_app_root, 'helpers'); // Load helper $this->assertEquals(NULL, $this->load->helper($helper)); @@ -294,7 +284,7 @@ class Loader_test extends CI_TestCase { $funcs[] = $func; $files[$helper.'_helper'] = '_create_content('helpers', $files, NULL, NULL, TRUE); + $this->ci_vfs_create($files, NULL, $this->ci_base_root, 'helpers'); // Load helpers $this->assertEquals(NULL, $this->load->helpers($helpers)); @@ -321,14 +311,12 @@ class Loader_test extends CI_TestCase { */ public function test_packages() { - $this->_setup_config_mock(); - // Create third-party directory in app path with model $dir = 'third-party'; $lib = 'unit_test_package'; $class = 'CI_'.ucfirst($lib); $content = '_create_content($dir, $lib, $content); + $this->ci_vfs_create($lib, $content, $this->ci_app_root, $dir); // Test failed load without path $this->setExpectedException( @@ -342,7 +330,7 @@ class Loader_test extends CI_TestCase { $paths = $this->load->get_package_paths(TRUE); // Add path and verify - $path = $this->load->app_path.$dir; + $path = APPPATH.$dir; $this->assertNull($this->load->add_package_path($path)); $this->assertContains($path, $this->load->get_package_paths(TRUE)); @@ -362,7 +350,6 @@ class Loader_test extends CI_TestCase { */ public function test_load_config() { - $this->_setup_config_mock(); $this->assertNull($this->load->config('config', FALSE)); } @@ -373,35 +360,29 @@ class Loader_test extends CI_TestCase { */ public function test_autoloader() { - $this->_setup_config_mock(); - // Create helper directory in app path with test helper $helper = 'autohelp'; $hlp_func = '_autohelp_test_func'; - $this->_create_content('helpers', $helper.'_helper', 'ci_vfs_create($helper.'_helper', $content, $this->ci_app_root, 'helpers'); // Create libraries directory in base path with test library $lib = 'autolib'; $lib_class = 'CI_'.ucfirst($lib); - $this->_create_content('libraries', $lib, 'ci_vfs_create($lib, 'ci_base_root, 'libraries'); - // Create libraries subdirectory with test driver - // Since libraries/ now exists, we have to look it up and - // add the subdir directly instead of using _create_content + // Create test driver $drv = 'autodrv'; $subdir = ucfirst($drv); $drv_class = 'CI_'.$subdir; - $tree = array( - $subdir => array($drv.'.php' => 'load->base_root->getChild('libraries')); + $this->ci_vfs_create($drv, 'ci_base_root, 'libraries/'.$subdir); // Create package directory in app path with model $dir = 'testdir'; - $path = $this->load->app_path.$dir.'/'; + $path = APPPATH.$dir.'/'; $model = 'automod'; $mod_class = ucfirst($model); - $this->_create_content($dir, $model, 'ci_vfs_create($model, 'ci_app_root, $dir.'/models'); // Create autoloader config $cfg = array( @@ -412,7 +393,7 @@ class Loader_test extends CI_TestCase { 'model' => array($model), 'config' => array() ); - $this->_create_content('config', 'autoload', 'ci_vfs_create('autoload', 'ci_app_root, 'config'); // Run autoloader $this->load->autoload(); @@ -436,55 +417,4 @@ class Loader_test extends CI_TestCase { $this->assertAttributeInstanceOf($mod_class, $model, $this->ci_obj); } - // -------------------------------------------------------------------- - - private function _setup_config_mock() - { - // Mock up a config object so config() has something to call - $config = $this->getMock('CI_Config', array('load'), array(), '', FALSE); - $config->expects($this->any()) - ->method('load') - ->will($this->returnValue(TRUE)); - - // Reinitialize config paths to use VFS - $config->_config_paths = array($this->load->app_path); - - // Add the mock to our stdClass - $this->ci_instance_var('config', $config); - } - - // -------------------------------------------------------------------- - - private function _create_content($dir, $file, $content, $sub = NULL, $base = FALSE) - { - // Create structure containing directory - $tree = array($dir => array()); - - // Check for subdirectory - if ($sub) { - // Add subdirectory to tree and get reference - $tree[$dir][$sub] = array(); - $leaf =& $tree[$dir][$sub]; - } - else { - // Get reference to main directory - $leaf =& $tree[$dir]; - } - - // Check for multiple files - if (is_array($file)) { - // Add multiple files to directory - foreach ($file as $name => $data) { - $leaf[$name.'.php'] = $data; - } - } - else { - // Add single file with content - $leaf[$file.'.php'] = $content; - } - - // Create structure under app or base path - vfsStream::create($tree, $base ? $this->load->base_root : $this->load->app_root); - } - -} +} \ No newline at end of file diff --git a/tests/codeigniter/helpers/date_helper_test.php b/tests/codeigniter/helpers/date_helper_test.php index 9feade71c..1458acd3e 100644 --- a/tests/codeigniter/helpers/date_helper_test.php +++ b/tests/codeigniter/helpers/date_helper_test.php @@ -168,6 +168,8 @@ class Date_helper_test extends CI_TestCase { public function test_timespan() { + $this->ci_vfs_clone('system/language/english/date_lang.php'); + $loader_cls = $this->ci_core_class('load'); $this->ci_instance_var('load', new $loader_cls); diff --git a/tests/codeigniter/helpers/form_helper_test.php b/tests/codeigniter/helpers/form_helper_test.php index 1a30ed993..48628d2e5 100644 --- a/tests/codeigniter/helpers/form_helper_test.php +++ b/tests/codeigniter/helpers/form_helper_test.php @@ -1,10 +1,12 @@ helper('form'); + } + public function test_form_hidden() { $expected = <<getMock($lang_cls, array('load')); $lang->expects($this->once()) ->method('load') ->with($this->equalTo('number')); // Assign the proper language array - - $lang->language = $this->_get_lang('number'); + $lang->language = $this->lang('number'); // We don't have a controller, so just create // a cheap class to act as our super object. // Make sure it has a lang attribute. - - $obj = new stdClass; - $obj->lang = $lang; - $this->ci_instance($obj); - } - - // Quick helper to actually grab the language - // file. Consider moving this to ci_testcase? - public function _get_lang($name) - { - require BASEPATH.'language/english/'.$name.'_lang.php'; - return $lang; + $this->ci_instance_var('lang', $lang); } public function test_byte_format() diff --git a/tests/codeigniter/helpers/text_helper_test.php b/tests/codeigniter/helpers/text_helper_test.php index f131469cb..d75d26208 100644 --- a/tests/codeigniter/helpers/text_helper_test.php +++ b/tests/codeigniter/helpers/text_helper_test.php @@ -64,6 +64,7 @@ class Text_helper_test extends CI_TestCase { public function test_convert_accented_characters() { + $this->ci_vfs_clone('application/config/foreign_chars.php'); $this->assertEquals('AAAeEEEIIOOEUUUeY', convert_accented_characters('ÀÂÄÈÊËÎÏÔŒÙÛÜŸ')); $this->assertEquals('a e i o u n ue', convert_accented_characters('á é í ó ú ñ ü')); } diff --git a/tests/codeigniter/libraries/Encrypt_test.php b/tests/codeigniter/libraries/Encrypt_test.php index 153a25e1d..998d806b3 100644 --- a/tests/codeigniter/libraries/Encrypt_test.php +++ b/tests/codeigniter/libraries/Encrypt_test.php @@ -4,11 +4,8 @@ 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->encrypt = new Mock_Libraries_Encrypt(); + $this->ci_instance_var('encrypt', $this->encrypt); $this->ci_set_config('encryption_key', "Encryptin'glike@boss!"); $this->msg = 'My secret message'; diff --git a/tests/codeigniter/libraries/Parser_test.php b/tests/codeigniter/libraries/Parser_test.php index b68f44a33..394c22692 100644 --- a/tests/codeigniter/libraries/Parser_test.php +++ b/tests/codeigniter/libraries/Parser_test.php @@ -4,12 +4,8 @@ class Parser_test extends CI_TestCase { public function set_up() { - $obj = new stdClass; - $obj->parser = new Mock_Libraries_Parser(); - - $this->ci_instance($obj); - - $this->parser = $obj->parser; + $this->parser = new Mock_Libraries_Parser(); + $this->ci_instance_var('parser', $this->parser); } // -------------------------------------------------------------------- diff --git a/tests/codeigniter/libraries/Session_test.php b/tests/codeigniter/libraries/Session_test.php index 60d3a5b30..14469f7fa 100644 --- a/tests/codeigniter/libraries/Session_test.php +++ b/tests/codeigniter/libraries/Session_test.php @@ -29,13 +29,15 @@ class Session_test extends CI_TestCase { $_COOKIE = array(); // Establish necessary support classes - $obj = new stdClass; $cfg = $this->ci_core_class('cfg'); - $obj->config = new $cfg(); $ldr = $this->ci_core_class('load'); - $obj->load = new $ldr(); - $obj->input = new Mock_Core_Input(NULL, NULL); - $this->ci_instance($obj); + $ci = $this->ci_instance(); + $ci->config = new $cfg(); + $ci->load = new $ldr(); + $ci->input = new Mock_Core_Input(NULL, NULL); + + // Make sure string helper is available + $this->ci_vfs_clone('system/helpers/string_helper.php'); // Attach session instance locally $config = array( diff --git a/tests/codeigniter/libraries/Table_test.php b/tests/codeigniter/libraries/Table_test.php index edfc83dd0..ce04b6a6d 100644 --- a/tests/codeigniter/libraries/Table_test.php +++ b/tests/codeigniter/libraries/Table_test.php @@ -4,12 +4,8 @@ class Table_test extends CI_TestCase { public function set_up() { - $obj = new stdClass; - $obj->table = new Mock_Libraries_Table(); - - $this->ci_instance($obj); - - $this->table = $obj->table; + $this->table = new Mock_Libraries_Table(); + $this->ci_instance_var('table', $this->table); } // Setter Methods diff --git a/tests/codeigniter/libraries/Typography_test.php b/tests/codeigniter/libraries/Typography_test.php index eb6dacb73..5dba06243 100644 --- a/tests/codeigniter/libraries/Typography_test.php +++ b/tests/codeigniter/libraries/Typography_test.php @@ -4,12 +4,8 @@ class Typography_test extends CI_TestCase { public function set_up() { - $obj = new stdClass; - $obj->type = new Mock_Libraries_Typography(); - - $this->ci_instance($obj); - - $this->type = $obj->type; + $this->type = new Mock_Libraries_Typography(); + $this->ci_instance('type', $this->type); } // -------------------------------------------------------------------- diff --git a/tests/codeigniter/libraries/Upload_test.php b/tests/codeigniter/libraries/Upload_test.php index d79a3ffc9..827942773 100644 --- a/tests/codeigniter/libraries/Upload_test.php +++ b/tests/codeigniter/libraries/Upload_test.php @@ -4,18 +4,11 @@ class Upload_test extends CI_TestCase { function set_up() { - $obj = new stdClass; - $obj->upload = new Mock_Libraries_Upload(); - $obj->security = new Mock_Core_Security(); - $obj->lang = new Mock_Core_Lang(); - - $this->ci_instance($obj); - $this->upload = $obj->upload; - - vfsStreamWrapper::register(); - vfsStreamWrapper::setRoot(new vfsStreamDirectory('testDir')); - - $this->_test_dir = vfsStreamWrapper::getRoot(); + $ci = $this->ci_instance(); + $ci->upload = new Mock_Libraries_Upload(); + $ci->security = new Mock_Core_Security(); + $ci->lang = new Mock_Core_Lang(); + $this->upload = $ci->upload; } function test_do_upload() @@ -64,11 +57,15 @@ class Upload_test extends CI_TestCase { function test_set_filename() { - $file1 = vfsStream::newFile('hello-world.txt')->withContent('Hello world.')->at($this->_test_dir); + $dir = 'uploads'; + $isnew = 'helloworld.txt'; + $exists = 'hello-world.txt'; + $this->ci_vfs_create($exists, 'Hello world.', $this->ci_app_root, $dir); + $path = $this->ci_vfs_path($dir.'/', APPPATH); $this->upload->file_ext = '.txt'; - $this->assertEquals('helloworld.txt', $this->upload->set_filename(vfsStream::url('testDir').'/', 'helloworld.txt')); - $this->assertEquals('hello-world1.txt', $this->upload->set_filename(vfsStream::url('testDir').'/', 'hello-world.txt')); + $this->assertEquals($isnew, $this->upload->set_filename($path, $isnew)); + $this->assertEquals('hello-world1.txt', $this->upload->set_filename($path, $exists)); } function test_set_max_filesize() @@ -107,7 +104,7 @@ class Upload_test extends CI_TestCase { function test_set_image_properties() { $this->upload->file_type = 'image/gif'; - $this->upload->file_temp = 'tests/mocks/uploads/ci_logo.gif'; + $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif'); $props = array( 'image_width' => 170, @@ -156,7 +153,7 @@ class Upload_test extends CI_TestCase { $this->assertTrue($this->upload->is_allowed_filetype(FALSE)); $this->assertTrue($this->upload->is_allowed_filetype(TRUE)); - $this->upload->file_temp = 'tests/mocks/uploads/ci_logo.gif'; + $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif'); $this->upload->file_ext = '.gif'; $this->upload->file_type = 'image/gif'; $this->assertTrue($this->upload->is_allowed_filetype()); @@ -179,7 +176,7 @@ class Upload_test extends CI_TestCase { $this->assertTrue($this->upload->is_allowed_dimensions()); $this->upload->file_type = 'image/gif'; - $this->upload->file_temp = 'tests/mocks/uploads/ci_logo.gif'; + $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif'); $this->upload->max_width = 10; $this->assertFalse($this->upload->is_allowed_dimensions()); @@ -197,7 +194,9 @@ class Upload_test extends CI_TestCase { $this->upload->upload_path = ''; $this->assertFalse($this->upload->validate_upload_path()); - $this->upload->upload_path = vfsStream::url('testDir'); + $dir = 'uploads'; + $this->ci_vfs_mkdir($dir); + $this->upload->upload_path = $this->ci_vfs_path($dir); $this->assertTrue($this->upload->validate_upload_path()); } @@ -222,20 +221,24 @@ class Upload_test extends CI_TestCase { function test_do_xss_clean() { - $file1 = vfsStream::newFile('file1.txt')->withContent('The billy goat was waiting for them.')->at($this->_test_dir); - $file2 = vfsStream::newFile('file2.txt')->at($this->_test_dir); - $file3 = vfsStream::newFile('file3.txt')->withContent('')->at($this->_test_dir); - - $this->upload->file_temp = vfsStream::url('file1.txt'); + $dir = 'uploads'; + $file1 = 'file1.txt'; + $file2 = 'file2.txt'; + $file3 = 'file3.txt'; + $this->ci_vfs_create($file1, 'The billy goat was waiting for them.', $this->ci_vfs_root, $dir); + $this->ci_vfs_create($file2, '', $this->ci_vfs_root, $dir); + $this->ci_vfs_create($file3, '', $this->ci_vfs_root, $dir); + + $this->upload->file_temp = $this->ci_vfs_path($file1, $dir); $this->assertTrue($this->upload->do_xss_clean()); - $this->upload->file_temp = vfsStream::url('file2.txt'); + $this->upload->file_temp = $this->ci_vfs_path($file2, $dir); $this->assertFalse($this->upload->do_xss_clean()); - $this->upload->file_temp = vfsStream::url('file3.txt'); + $this->upload->file_temp = $this->ci_vfs_path($file3, $dir); $this->assertFalse($this->upload->do_xss_clean()); - $this->upload->file_temp = 'tests/mocks/uploads/ci_logo.gif'; + $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif'); $this->assertTrue($this->upload->do_xss_clean()); } diff --git a/tests/codeigniter/libraries/Useragent_test.php b/tests/codeigniter/libraries/Useragent_test.php index 89383f807..e3726554e 100644 --- a/tests/codeigniter/libraries/Useragent_test.php +++ b/tests/codeigniter/libraries/Useragent_test.php @@ -10,12 +10,11 @@ class UserAgent_test extends CI_TestCase { // set a baseline user agent $_SERVER['HTTP_USER_AGENT'] = $this->_user_agent; - $obj = new stdClass; - $obj->agent = new Mock_Libraries_UserAgent(); + $this->ci_vfs_clone('application/config/user_agents.php'); - $this->ci_instance($obj); + $this->agent = new Mock_Libraries_UserAgent(); - $this->agent = $obj->agent; + $this->ci_instance_var('agent', $this->agent); } // -------------------------------------------------------------------- diff --git a/tests/mocks/autoloader.php b/tests/mocks/autoloader.php index 88d016bba..431c310d4 100644 --- a/tests/mocks/autoloader.php +++ b/tests/mocks/autoloader.php @@ -48,32 +48,32 @@ function autoload($class) if (in_array($subclass, $ci_core)) { - $dir = BASEPATH.'core'.DIRECTORY_SEPARATOR; + $dir = SYSTEM_PATH.'core'.DIRECTORY_SEPARATOR; $class = $subclass; } elseif (in_array($subclass, $ci_libraries)) { - $dir = BASEPATH.'libraries'.DIRECTORY_SEPARATOR; + $dir = SYSTEM_PATH.'libraries'.DIRECTORY_SEPARATOR; $class = ($subclass === 'Driver_Library') ? 'Driver' : $subclass; } elseif (in_array($subclass, $ci_drivers)) { - $dir = BASEPATH.'libraries'.DIRECTORY_SEPARATOR.$subclass.DIRECTORY_SEPARATOR; + $dir = SYSTEM_PATH.'libraries'.DIRECTORY_SEPARATOR.$subclass.DIRECTORY_SEPARATOR; $class = $subclass; } elseif (in_array(($parent = strtok($subclass, '_')), $ci_drivers)) { - $dir = BASEPATH.'libraries'.DIRECTORY_SEPARATOR.$parent.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR; + $dir = SYSTEM_PATH.'libraries'.DIRECTORY_SEPARATOR.$parent.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR; $class = $subclass; } elseif (preg_match('/^CI_DB_(.+)_(driver|forge|result|utility)$/', $class, $m) && count($m) === 3) { - $driver_path = BASEPATH.'database'.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR; + $driver_path = SYSTEM_PATH.'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; + $dir = SYSTEM_PATH.'database'.DIRECTORY_SEPARATOR; $file = $dir.str_replace(array('CI_DB','active_record'), array('DB', 'active_rec'), $subclass).'.php'; } else diff --git a/tests/mocks/ci_testcase.php b/tests/mocks/ci_testcase.php index eda9e1b60..980e912c5 100644 --- a/tests/mocks/ci_testcase.php +++ b/tests/mocks/ci_testcase.php @@ -2,7 +2,9 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { - protected $ci_config; + public $ci_vfs_root; + public $ci_app_root; + public $ci_base_root; protected $ci_instance; protected static $ci_test_instance; @@ -25,13 +27,18 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { public function __construct() { parent::__construct(); - $this->ci_config = array(); + $this->ci_instance = new StdClass(); } // -------------------------------------------------------------------- public function setUp() { + // Setup VFS with base directories + $this->ci_vfs_root = vfsStream::setup(); + $this->ci_app_root = vfsStream::newDirectory('application')->at($this->ci_vfs_root); + $this->ci_base_root = vfsStream::newDirectory('system')->at($this->ci_vfs_root); + if (method_exists($this, 'set_up')) { $this->set_up(); @@ -57,15 +64,27 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { // -------------------------------------------------------------------- - public function ci_set_config($key, $val = '') + public function ci_set_config($key = '', $val = '') { + // Add test config + if ( ! isset($this->ci_instance->config)) + { + $this->ci_instance->config = new CI_TestConfig(); + } + + // Empty key means just do setup above + if ($key === '') + { + return; + } + if (is_array($key)) { - $this->ci_config = $key; + $this->ci_instance->config->config = $key; } else { - $this->ci_config[$key] = $val; + $this->ci_instance->config->config[$key] = $val; } } @@ -73,7 +92,7 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { public function ci_get_config() { - return $this->ci_config; + return isset($this->ci_instance->config) ? $this->ci_instance->config->config : array(); } // -------------------------------------------------------------------- @@ -132,7 +151,7 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { if ( ! class_exists('CI_'.$class_name)) { - require_once BASEPATH.'core/'.$class_name.'.php'; + require_once SYSTEM_PATH.'core/'.$class_name.'.php'; } $GLOBALS[strtoupper($global_name)] = 'CI_'.$class_name; @@ -148,6 +167,155 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { $orig = $obj; } + /** + * Create VFS directory + * + * @param string Directory name + * @param object Optional root to create in + * @return object New directory object + */ + public function ci_vfs_mkdir($name, $root = NULL) + { + // Check for root + if ( ! $root) + { + $root = $this->ci_vfs_root; + } + + // Return new directory object + return vfsStream::newDirectory($name)->at($root); + } + + // -------------------------------------------------------------------- + + /** + * Create VFS content + * + * @param string File name + * @param string File content + * @param object VFS directory object + * @param mixed Optional subdirectory path or array of subs + * @return void + */ + public function ci_vfs_create($file, $content = '', $root = NULL, $path = NULL) + { + // Check for array + if (is_array($file)) + { + foreach ($file as $name => $content) + { + $this->ci_vfs_create($name, $content, $root, $path); + } + return; + } + + // Assert .php extension if none given + if (pathinfo($file, PATHINFO_EXTENSION) == '') + { + $file .= '.php'; + } + + // Build content + $tree = array($file => $content); + + // Check for path + $subs = array(); + if ($path) + { + // Explode if not array + $subs = is_array($path) ? $path : explode('/', trim($path, '/')); + } + + // Check for root + if ( ! $root) + { + // Use base VFS root + $root = $this->ci_vfs_root; + } + + // Handle subdirectories + while (($dir = array_shift($subs))) + { + // See if subdir exists under current root + $dir_root = $root->getChild($dir); + if ($dir_root) + { + // Yes - recurse into subdir + $root = $dir_root; + } + else + { + // No - put subdirectory back and quit + array_unshift($subs, $dir); + break; + } + } + + // Create any remaining subdirectories + if ($subs) + { + foreach (array_reverse($subs) as $dir) + { + // Wrap content in subdirectory for creation + $tree = array($dir => $tree); + } + } + + // Create tree + vfsStream::create($tree, $root); + } + + // -------------------------------------------------------------------- + + /** + * Clone a real file into VFS + * + * @param string Path from base directory + * @return bool TRUE on success, otherwise FALSE + */ + public function ci_vfs_clone($path) + { + // Get real file contents + $content = file_get_contents(PROJECT_BASE.$path); + if ($content === FALSE) + { + // Couldn't find file to clone + return FALSE; + } + + $this->ci_vfs_create(basename($path), $content, NULL, dirname($path)); + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * Helper to get a VFS URL path + * + * @param string Path + * @param string Optional base path + * @return string Path URL + */ + public function ci_vfs_path($path, $base = '') + { + // Check for base path + if ($base) + { + // Prepend to path + $path = rtrim($base, '/').'/'.ltrim($path, '/'); + + // Is it already in URL form? + if (strpos($path, '://') !== FALSE) + { + // Done - return path + return $path; + } + } + + // Trim leading slash and return URL + return vfsStream::url(ltrim($path, '/')); + } + // -------------------------------------------------------------------- // Internals // -------------------------------------------------------------------- @@ -171,7 +339,15 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { public function helper($name) { - require_once(BASEPATH.'helpers/'.$name.'_helper.php'); + require_once(SYSTEM_PATH.'helpers/'.$name.'_helper.php'); + } + + // -------------------------------------------------------------------- + + public function lang($name) + { + require(SYSTEM_PATH.'language/english/'.$name.'_lang.php'); + return $lang; } // -------------------------------------------------------------------- diff --git a/tests/mocks/ci_testconfig.php b/tests/mocks/ci_testconfig.php new file mode 100644 index 000000000..eb318ddeb --- /dev/null +++ b/tests/mocks/ci_testconfig.php @@ -0,0 +1,18 @@ +config[$key]) ? $this->config[$key] : FALSE; + } + + public function load($arg1, $arg2, $arg3) + { + return TRUE; + } + +} diff --git a/tests/mocks/core/common.php b/tests/mocks/core/common.php index a655ee1db..b001074c8 100644 --- a/tests/mocks/core/common.php +++ b/tests/mocks/core/common.php @@ -39,6 +39,30 @@ if ( ! function_exists('config_item')) } } +if ( ! function_exists('get_mimes')) +{ + /** + * Returns the MIME types array from config/mimes.php + * + * @return array + */ + function &get_mimes() + { + static $_mimes = array(); + + if (empty($_mimes)) + { + $path = realpath(PROJECT_BASE.'application/config/mimes.php'); + if (is_file($path)) + { + $_mimes = include($path); + } + } + + return $_mimes; + } +} + // -------------------------------------------------------------------- if ( ! function_exists('load_class')) @@ -166,4 +190,4 @@ if ( ! function_exists('set_status_header')) { return TRUE; } -} \ No newline at end of file +} diff --git a/tests/mocks/core/loader.php b/tests/mocks/core/loader.php index 9eb78253b..7ea4da369 100644 --- a/tests/mocks/core/loader.php +++ b/tests/mocks/core/loader.php @@ -2,36 +2,6 @@ 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. Also, by mocking the system directory, we eliminate dependency - * on any other classes so errors in libraries, helpers, etc. don't give false - * negatives for the actual loading process. 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() - { - // Create VFS tree of loader locations - $this->root = vfsStream::setup(); - $this->app_root = vfsStream::newDirectory('application')->at($this->root); - $this->base_root = vfsStream::newDirectory('system')->at($this->root); - - // Get VFS app and base path URLs - $this->app_path = vfsStream::url('application').'/'; - $this->base_path = vfsStream::url('system').'/'; - - // Set loader paths with VFS URLs - $this->_ci_ob_level = ob_get_level(); - $this->_ci_library_paths = array($this->app_path, $this->base_path); - $this->_ci_helper_paths = array($this->app_path, $this->base_path); - $this->_ci_model_paths = array($this->app_path); - $this->_ci_view_paths = array($this->app_path.'views/' => TRUE); - } - /** * Give public access to _ci_autoloader for testing */ -- cgit v1.2.3-24-g4f1b From e9435dc7e5a4a9779eb83d8adf172fabf47ab5a6 Mon Sep 17 00:00:00 2001 From: dchill42 Date: Sun, 14 Oct 2012 15:44:39 -0400 Subject: Adapted DB for VFS changes and fixed Common case in Bootstrap.php Signed-off-by: dchill42 --- tests/Bootstrap.php | 2 +- tests/mocks/autoloader.php | 8 +++++++- tests/mocks/ci_testcase.php | 10 ++++++++++ tests/mocks/database/db.php | 45 +++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 61 insertions(+), 4 deletions(-) (limited to 'tests') diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php index ea8d8aea8..8ce80b3fd 100644 --- a/tests/Bootstrap.php +++ b/tests/Bootstrap.php @@ -38,7 +38,7 @@ isset($_SERVER['REMOTE_ADDR']) OR $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; // Prep our test environment include_once $dir.'/mocks/core/common.php'; -include_once SYSTEM_PATH.'core/common.php'; +include_once SYSTEM_PATH.'core/Common.php'; include_once $dir.'/mocks/autoloader.php'; spl_autoload_register('autoload'); diff --git a/tests/mocks/autoloader.php b/tests/mocks/autoloader.php index 431c310d4..5b202f159 100644 --- a/tests/mocks/autoloader.php +++ b/tests/mocks/autoloader.php @@ -65,6 +65,12 @@ function autoload($class) $dir = SYSTEM_PATH.'libraries'.DIRECTORY_SEPARATOR.$parent.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR; $class = $subclass; } + elseif (preg_match('/^CI_DB_(.+)_(.+)_(driver|forge|result|utility)$/', $class, $m) && count($m) === 4) + { + $driver_path = SYSTEM_PATH.'database'.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR; + $dir = $driver_path.$m[1].DIRECTORY_SEPARATOR.'subdrivers'.DIRECTORY_SEPARATOR; + $file = $dir.$m[1].'_'.$m[2].'_'.$m[3].'.php'; + } elseif (preg_match('/^CI_DB_(.+)_(driver|forge|result|utility)$/', $class, $m) && count($m) === 3) { $driver_path = SYSTEM_PATH.'database'.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR; @@ -104,4 +110,4 @@ function autoload($class) } include_once($file); -} +} \ No newline at end of file diff --git a/tests/mocks/ci_testcase.php b/tests/mocks/ci_testcase.php index 980e912c5..e581d4b02 100644 --- a/tests/mocks/ci_testcase.php +++ b/tests/mocks/ci_testcase.php @@ -275,6 +275,16 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { */ public function ci_vfs_clone($path) { + // Check for array + if (is_array($path)) + { + foreach ($path as $file) + { + $this->ci_vfs_clone($file); + } + return; + } + // Get real file contents $content = file_get_contents(PROJECT_BASE.$path); if ($content === FALSE) diff --git a/tests/mocks/database/db.php b/tests/mocks/database/db.php index 75658530b..7e0030e15 100644 --- a/tests/mocks/database/db.php +++ b/tests/mocks/database/db.php @@ -7,6 +7,16 @@ class Mock_Database_DB { */ private $config = array(); + /** + * @var string DB driver name + */ + private static $dbdriver = ''; + + /** + * @var string DB sub-driver name + */ + private static $subdriver = ''; + /** * Prepare database configuration skeleton * @@ -31,6 +41,12 @@ class Mock_Database_DB { throw new InvalidArgumentException('Group '.$group.' not exists'); } + self::$dbdriver = $this->config[$group]['dbdriver']; + if (isset($this->config[$group]['subdriver'])) + { + self::$subdriver = $this->config[$group]['subdriver']; + } + $params = array( 'dbprefix' => '', 'pconnect' => FALSE, @@ -50,7 +66,7 @@ class Mock_Database_DB { $failover = empty($config['failover']) ? FALSE : $config['failover']; $dsn = $config['dbdriver'].'://'.$config['username'].':'.$config['password'] - .'@'.$config['hostname'].'/'.$config['database']; + .'@'.$config['hostname'].'/'.$config['database']; // Build the parameter $other_params = array_slice($config, 6); @@ -83,7 +99,32 @@ class Mock_Database_DB { */ public static function DB($group, $query_builder = FALSE) { - include_once(BASEPATH.'database/DB.php'); + // Create dummy driver and builder files to "load" - the mocks have + // already triggered autoloading of the real files + $case = CI_TestCase::instance(); + $driver = self::$dbdriver; + $subdriver = self::$subdriver; + $case->ci_vfs_create(array( + 'DB_driver.php' => '', + 'DB_forge.php' => '', + 'DB_query_builder.php' => '' + ), '', $case->ci_base_root, 'database'); + if (file_exists(SYSTEM_PATH.'database/drivers/'.$driver.'/'.$driver.'_driver.php')) + { + $case->ci_vfs_create(array( + $driver.'_driver.php' => '', + $driver.'_forge.php' => '' + ), '', $case->ci_base_root, 'database/drivers/'.$driver); + } + if ($subdriver) + { + $case->ci_vfs_create(array( + $driver.'_'.$subdriver.'_driver.php' => '', + $driver.'_'.$subdriver.'_forge.php' => '' + ), '', $case->ci_base_root, 'database/drivers/'.$driver.'/subdrivers'); + } + + include_once(SYSTEM_PATH.'database/DB.php'); try { -- cgit v1.2.3-24-g4f1b From e3621cc79fa4b4658768fea0694cc0ae52835d85 Mon Sep 17 00:00:00 2001 From: dchill42 Date: Sun, 14 Oct 2012 18:23:52 -0400 Subject: Adapted Config load test to VFS APPPATH Signed-off-by: dchill42 --- tests/codeigniter/core/Config_test.php | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) (limited to 'tests') diff --git a/tests/codeigniter/core/Config_test.php b/tests/codeigniter/core/Config_test.php index 7782a7898..80e0862ff 100644 --- a/tests/codeigniter/core/Config_test.php +++ b/tests/codeigniter/core/Config_test.php @@ -94,7 +94,7 @@ class Config_test extends CI_TestCase { public function test_load() { - // Create VFS tree of application config files + // Create config files in VFS $file1 = 'test.php'; $file2 = 'secttest'; $key1 = 'testconfig'; @@ -107,18 +107,10 @@ class Config_test extends CI_TestCase { 'two' => 2, 'three' => true ); - $tree = array( - 'application' => array( - 'config' => array( - $file1 => ' 'config->_config_paths = array(vfsStream::url('application').'/'); + $this->ci_vfs_create(array( + $file1 => ' 'ci_app_root, 'config'); // Test regular load $this->assertTrue($this->config->load($file1)); @@ -140,4 +132,4 @@ class Config_test extends CI_TestCase { $this->assertNull($this->config->load($file3)); } -} +} \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 60108a735fa7c9af4ab0b71e74b226d4b554e32f Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Tue, 16 Oct 2012 17:35:54 +0100 Subject: Cookie helper testsuite --- tests/codeigniter/helpers/cookie_helper_test.php | 59 ++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 tests/codeigniter/helpers/cookie_helper_test.php (limited to 'tests') diff --git a/tests/codeigniter/helpers/cookie_helper_test.php b/tests/codeigniter/helpers/cookie_helper_test.php new file mode 100644 index 000000000..3c7c9fd2e --- /dev/null +++ b/tests/codeigniter/helpers/cookie_helper_test.php @@ -0,0 +1,59 @@ +helper('cookie'); + } + + // ------------------------------------------------------------------------ + + function test_set_cookie() + { + /*$input_cls = $this->ci_core_class('input'); + $this->ci_instance_var('input', new $input_cls); + + $this->assertTrue(set_cookie( + 'my_cookie', + 'foobar' + ));*/ + + $this->markTestIncomplete('Need to find a way to overcome a headers already set exception'); + } + + // ------------------------------------------------------------------------ + + function test_get_cookie() + { + $_COOKIE['foo'] = 'bar'; + + $security = new Mock_Core_Security(); + $utf8 = new Mock_Core_Utf8(); + $input_cls = $this->ci_core_class('input'); + $this->ci_instance_var('input', new Mock_Core_Input($security, $utf8)); + + $this->assertEquals('bar', get_cookie('foo', FALSE)); + $this->assertEquals('bar', get_cookie('foo', TRUE)); + + $_COOKIE['bar'] = "Hello, i try to your site"; + + $this->assertEquals("Hello, i try to [removed]alert('Hack');[removed] your site", get_cookie('bar', TRUE)); + $this->assertEquals("Hello, i try to your site", get_cookie('bar', FALSE)); + } + + // ------------------------------------------------------------------------ + + function test_delete_cookie() + { + /*$input_cls = $this->ci_core_class('input'); + $this->ci_instance_var('input', new $input_cls); + + $this->assertTrue(delete_cookie( + 'my_cookie' + ));*/ + + $this->markTestIncomplete('Need to find a way to overcome a headers already set exception'); + } + +} \ No newline at end of file -- cgit v1.2.3-24-g4f1b From a0f99d8930f175b7869ccaca834b3437b1a7b4ef Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Tue, 16 Oct 2012 18:18:54 +0100 Subject: Added captcha helper test Note, test isn't implemented Signed-off-by: Alex Bilbie --- tests/codeigniter/helpers/captcha_helper_test.php | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 tests/codeigniter/helpers/captcha_helper_test.php (limited to 'tests') diff --git a/tests/codeigniter/helpers/captcha_helper_test.php b/tests/codeigniter/helpers/captcha_helper_test.php new file mode 100644 index 000000000..4fbda2a88f --- /dev/null +++ b/tests/codeigniter/helpers/captcha_helper_test.php @@ -0,0 +1,10 @@ +markTestIncomplete(); + } + +} \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 1624b4297c89334362f6ade5f5b766c916ffcbb1 Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Tue, 16 Oct 2012 18:19:06 +0100 Subject: Added download helper test Note, test isn't implemented Signed-off-by: Alex Bilbie --- tests/codeigniter/helpers/download_helper_test.php | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 tests/codeigniter/helpers/download_helper_test.php (limited to 'tests') diff --git a/tests/codeigniter/helpers/download_helper_test.php b/tests/codeigniter/helpers/download_helper_test.php new file mode 100644 index 000000000..b41a8532a --- /dev/null +++ b/tests/codeigniter/helpers/download_helper_test.php @@ -0,0 +1,10 @@ +markTestIncomplete(); + } + +} \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 526d88f7d1d91bd51b3aa27ee8e170b7fa39a639 Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Tue, 16 Oct 2012 18:19:30 +0100 Subject: Added language helper unit test Signed-off-by: Alex Bilbie --- tests/codeigniter/helpers/language_helper_test.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 tests/codeigniter/helpers/language_helper_test.php (limited to 'tests') diff --git a/tests/codeigniter/helpers/language_helper_test.php b/tests/codeigniter/helpers/language_helper_test.php new file mode 100644 index 000000000..06932b9fd --- /dev/null +++ b/tests/codeigniter/helpers/language_helper_test.php @@ -0,0 +1,14 @@ +helper('language'); + $this->ci_instance_var('lang', new Mock_Core_Lang()); + + $this->assertFalse(lang(1)); + $this->assertEquals('', lang(1, 'foo')); + } + +} \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 0f5b3060ff04b064b1a4bee9b114357ccd5a57de Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Tue, 16 Oct 2012 18:19:40 +0100 Subject: Added security helper unit test Signed-off-by: Alex Bilbie --- tests/codeigniter/helpers/security_helper_test.php | 64 ++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tests/codeigniter/helpers/security_helper_test.php (limited to 'tests') diff --git a/tests/codeigniter/helpers/security_helper_test.php b/tests/codeigniter/helpers/security_helper_test.php new file mode 100644 index 000000000..effd3ec02 --- /dev/null +++ b/tests/codeigniter/helpers/security_helper_test.php @@ -0,0 +1,64 @@ +helper('security'); + $obj = new stdClass; + $obj->security = new Mock_Core_Security(); + $this->ci_instance($obj); + } + + function test_xss_clean() + { + $this->assertEquals('foo', xss_clean('foo')); + + $this->assertEquals("Hello, i try to [removed]alert('Hack');[removed] your site", xss_clean("Hello, i try to your site")); + } + + function test_sanitize_filename() + { + $this->assertEquals('hello.doc', sanitize_filename('hello.doc')); + + $filename = './'; + $this->assertEquals('foo', sanitize_filename($filename)); + } + + function test_do_hash() + { + $md5 = md5('foo'); + $sha1 = sha1('foo'); + + $algos = hash_algos(); + $algo_results = array(); + foreach ($algos as $k => $v) + { + $algo_results[$v] = hash($v, 'foo'); + } + + $this->assertEquals($sha1, do_hash('foo')); + $this->assertEquals($sha1, do_hash('foo', 'sha1')); + $this->assertEquals($md5, do_hash('foo', 'md5')); + $this->assertEquals($md5, do_hash('foo', 'foobar')); + + // Test each algorithm available to PHP + foreach ($algo_results as $algo => $result) + { + $this->assertEquals($result, do_hash('foo', $algo)); + } + } + + function test_strip_image_tags() + { + $this->assertEquals('http://example.com/spacer.gif', strip_image_tags('http://example.com/spacer.gif')); + + $this->assertEquals('http://example.com/spacer.gif', strip_image_tags('Who needs CSS when you have a spacer.gif?')); + } + + function test_encode_php_tags() + { + $this->assertEquals('<? echo $foo; ?>', encode_php_tags('')); + } + +} \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 4b3cf8d825e13d4f24654fc6b26f2ca15d73c4f8 Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Thu, 18 Oct 2012 16:44:54 +0100 Subject: Marked tests that can't easily be tests as skipped Signed-off-by: Alex Bilbie --- tests/codeigniter/helpers/captcha_helper_test.php | 2 +- tests/codeigniter/helpers/cookie_helper_test.php | 4 ++-- tests/codeigniter/helpers/download_helper_test.php | 2 +- tests/codeigniter/libraries/Upload_test.php | 10 +++++----- 4 files changed, 9 insertions(+), 9 deletions(-) (limited to 'tests') diff --git a/tests/codeigniter/helpers/captcha_helper_test.php b/tests/codeigniter/helpers/captcha_helper_test.php index 4fbda2a88f..fc86305e3 100644 --- a/tests/codeigniter/helpers/captcha_helper_test.php +++ b/tests/codeigniter/helpers/captcha_helper_test.php @@ -4,7 +4,7 @@ class Captcha_helper_test extends CI_TestCase { public function test_create_captcha() { - $this->markTestIncomplete(); + $this->markTestSkipped('Cant easily test'); } } \ No newline at end of file diff --git a/tests/codeigniter/helpers/cookie_helper_test.php b/tests/codeigniter/helpers/cookie_helper_test.php index 3c7c9fd2e..fba68f20f 100644 --- a/tests/codeigniter/helpers/cookie_helper_test.php +++ b/tests/codeigniter/helpers/cookie_helper_test.php @@ -19,7 +19,7 @@ class Cookie_helper_test extends CI_TestCase { 'foobar' ));*/ - $this->markTestIncomplete('Need to find a way to overcome a headers already set exception'); + $this->markTestSkipped('Need to find a way to overcome a headers already set exception'); } // ------------------------------------------------------------------------ @@ -53,7 +53,7 @@ class Cookie_helper_test extends CI_TestCase { 'my_cookie' ));*/ - $this->markTestIncomplete('Need to find a way to overcome a headers already set exception'); + $this->markTestSkipped('Need to find a way to overcome a headers already set exception'); } } \ No newline at end of file diff --git a/tests/codeigniter/helpers/download_helper_test.php b/tests/codeigniter/helpers/download_helper_test.php index b41a8532a..d2b42e46b 100644 --- a/tests/codeigniter/helpers/download_helper_test.php +++ b/tests/codeigniter/helpers/download_helper_test.php @@ -4,7 +4,7 @@ class Download_helper_test extends CI_TestCase { public function test_force_download() { - $this->markTestIncomplete(); + $this->markTestSkipped('Cant easily test'); } } \ No newline at end of file diff --git a/tests/codeigniter/libraries/Upload_test.php b/tests/codeigniter/libraries/Upload_test.php index d79a3ffc9..b4ef7bbd1 100644 --- a/tests/codeigniter/libraries/Upload_test.php +++ b/tests/codeigniter/libraries/Upload_test.php @@ -18,9 +18,9 @@ class Upload_test extends CI_TestCase { $this->_test_dir = vfsStreamWrapper::getRoot(); } - function test_do_upload() + function test_do_upload() { - $this->markTestIncomplete('We can\'t really test this at the moment because of the call to `is_uploaded_file` in do_upload which isn\'t supported by vfsStream'); + $this->markTestSkipped('We can\'t really test this at the moment because of the call to `is_uploaded_file` in do_upload which isn\'t supported by vfsStream'); } function test_data() @@ -75,7 +75,7 @@ class Upload_test extends CI_TestCase { { $this->upload->set_max_filesize(100); $this->assertEquals(100, $this->upload->max_size); - } + } function test_set_max_filename() { @@ -87,7 +87,7 @@ class Upload_test extends CI_TestCase { { $this->upload->set_max_width(100); $this->assertEquals(100, $this->upload->max_width); - } + } function test_set_max_height() { @@ -181,7 +181,7 @@ class Upload_test extends CI_TestCase { $this->upload->file_type = 'image/gif'; $this->upload->file_temp = 'tests/mocks/uploads/ci_logo.gif'; - $this->upload->max_width = 10; + $this->upload->max_width = 10; $this->assertFalse($this->upload->is_allowed_dimensions()); $this->upload->max_width = 170; -- cgit v1.2.3-24-g4f1b From 187632748fd2694dd735b48eee157744dd83f3b4 Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Thu, 18 Oct 2012 16:45:20 +0100 Subject: Exclude libs installed via Composer from code coverage tests Signed-off-by: Alex Bilbie --- tests/phpunit.xml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 56cb8841c..96c3af9bb 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -1,6 +1,6 @@ -./codeigniter/libraries - + PEAR_INSTALL_DIR PHP_LIBDIR + ../vendor - + \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 929e1241879c94bff85203d2e00623284d72dc87 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 19 Oct 2012 10:09:28 +0300 Subject: Manually apply an improved version of PR #1797 (auto_link() URL helper) --- tests/codeigniter/helpers/url_helper_test.php | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tests') diff --git a/tests/codeigniter/helpers/url_helper_test.php b/tests/codeigniter/helpers/url_helper_test.php index c81c5f1b8..5fc364238 100644 --- a/tests/codeigniter/helpers/url_helper_test.php +++ b/tests/codeigniter/helpers/url_helper_test.php @@ -51,6 +51,8 @@ class Url_helper_test extends CI_TestCase { 'www.codeigniter.com test' => 'http://www.codeigniter.com test', 'This is my noreply@codeigniter.com test' => 'This is my noreply@codeigniter.com test', '
www.google.com' => '
http://www.google.com', + 'Download CodeIgniter at www.codeigniter.com. Period test.' => 'Download CodeIgniter at http://www.codeigniter.com. Period test.', + 'Download CodeIgniter at www.codeigniter.com, comma test' => 'Download CodeIgniter at http://www.codeigniter.com, comma test' ); foreach ($strings as $in => $out) -- cgit v1.2.3-24-g4f1b From 3a43f130a02be38b7244ffe4507c6cefd874f285 Mon Sep 17 00:00:00 2001 From: dchill42 Date: Sat, 20 Oct 2012 22:44:16 -0400 Subject: Raised CI_Config test coverage to 100% Signed-off-by: dchill42 --- tests/codeigniter/core/Config_test.php | 197 ++++++++++++++++++++++++++------- 1 file changed, 158 insertions(+), 39 deletions(-) (limited to 'tests') diff --git a/tests/codeigniter/core/Config_test.php b/tests/codeigniter/core/Config_test.php index 80e0862ff..d652a625e 100644 --- a/tests/codeigniter/core/Config_test.php +++ b/tests/codeigniter/core/Config_test.php @@ -7,11 +7,12 @@ class Config_test extends CI_TestCase { $cls =& $this->ci_core_class('cfg'); // set predictable config values - $this->ci_set_config(array( + $this->cfg = array( 'index_page' => 'index.php', 'base_url' => 'http://example.com/', 'subclass_prefix' => 'MY_' - )); + ); + $this->ci_set_config($this->cfg); $this->config = new $cls; } @@ -20,7 +21,7 @@ class Config_test extends CI_TestCase { public function test_item() { - $this->assertEquals('http://example.com/', $this->config->item('base_url')); + $this->assertEquals($this->cfg['base_url'], $this->config->item('base_url')); // Bad Config value $this->assertFalse($this->config->item('no_good_item')); @@ -48,36 +49,103 @@ class Config_test extends CI_TestCase { // Bad Config value $this->assertFalse($this->config->slash_item('no_good_item')); - $this->assertEquals('http://example.com/', $this->config->slash_item('base_url')); + $this->assertEquals($this->cfg['base_url'], $this->config->slash_item('base_url')); - $this->assertEquals('MY_/', $this->config->slash_item('subclass_prefix')); + $this->assertEquals($this->cfg['subclass_prefix'].'/', $this->config->slash_item('subclass_prefix')); } // -------------------------------------------------------------------- - public function test_site_url() + public function test_base_url() { - $this->assertEquals('http://example.com/index.php', $this->config->site_url()); + // Test regular base URL + $base_url = $this->cfg['base_url']; + $this->assertEquals($base_url, $this->config->base_url()); + + // Test with URI + $uri = 'test'; + $this->assertEquals($base_url.$uri, $this->config->base_url($uri)); + + // Clear base_url + $this->ci_set_config('base_url', ''); + + // Rerun constructor + $cls =& $this->ci_core_class('cfg'); + $this->config = new $cls; - $base_url = $this->config->item('base_url'); + // Test default base + $this->assertEquals('http://localhost/', $this->config->base_url()); + // Capture server vars + $old_host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : NULL; + $old_script = isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : NULL; + $old_https = isset($_SERVER['HTTPS']) ? $_SERVER['HTTPS'] : NULL; + + // Setup server vars for detection + $host = 'test.com'; + $path = '/path/'; + $script = 'base_test.php'; + $_SERVER['HTTP_HOST'] = $host; + $_SERVER['SCRIPT_NAME'] = $path.$script; + + // Rerun constructor + $this->config = new $cls; + + // Test plain detected + $this->assertEquals('http://'.$host.$path, $this->config->base_url()); + + // Rerun constructor + $_SERVER['HTTPS'] = 'on'; + $this->config = new $cls; + + // Test secure detected + $this->assertEquals('https://'.$host.$path, $this->config->base_url()); + + // Restore server vars + if ($old_host === NULL) unset($_SERVER['HTTP_HOST']); + else $_SERVER['HTTP_HOST'] = $old_host; + if ($old_script === NULL) unset($_SERVER['SCRIPT_NAME']); + else $_SERVER['SCRIPT_NAME'] = $old_script; + if ($old_https === NULL) unset($_SERVER['HTTPS']); + else $_SERVER['HTTPS'] = $old_https; + } + + // -------------------------------------------------------------------- + + public function test_site_url() + { + $base_url = $this->cfg['base_url']; + $index_page = $this->cfg['index_page']; + $this->assertEquals($base_url.$index_page, $this->config->site_url()); + + $old_base = $this->config->item('base_url'); $this->config->set_item('base_url', ''); $q_string = $this->config->item('enable_query_strings'); - $this->config->set_item('enable_query_strings', FALSE); - $this->assertEquals('index.php/test', $this->config->site_url('test')); - $this->assertEquals('index.php/test/1', $this->config->site_url(array('test', '1'))); + $uri= 'test'; + $uri2 = '1'; + $this->assertEquals($index_page.'/'.$uri, $this->config->site_url($uri)); + $this->assertEquals($index_page.'/'.$uri.'/'.$uri2, $this->config->site_url(array($uri, $uri2))); + + $suffix = 'ing'; + $this->config->set_item('url_suffix', $suffix); + + $arg = 'pass'; + $this->assertEquals($index_page.'/'.$uri.$suffix, $this->config->site_url($uri)); + $this->assertEquals($index_page.'/'.$uri.$suffix.'?'.$arg, $this->config->site_url($uri.'?'.$arg)); + + $this->config->set_item('url_suffix', FALSE); $this->config->set_item('enable_query_strings', TRUE); - $this->assertEquals('index.php?test', $this->config->site_url('test')); - $this->assertEquals('index.php?0=test&1=1', $this->config->site_url(array('test', '1'))); + $this->assertEquals($index_page.'?'.$uri, $this->config->site_url($uri)); + $this->assertEquals($index_page.'?0='.$uri.'&1='.$uri2, $this->config->site_url(array($uri, $uri2))); - $this->config->set_item('base_url', $base_url); + $this->config->set_item('base_url', $old_base); - $this->assertEquals('http://example.com/index.php?test', $this->config->site_url('test')); + $this->assertEquals($base_url.$index_page.'?'.$uri, $this->config->site_url($uri)); // back to home base $this->config->set_item('enable_query_strings', $q_string); @@ -87,49 +155,100 @@ class Config_test extends CI_TestCase { public function test_system_url() { - $this->assertEquals('http://example.com/system/', $this->config->system_url()); + $this->assertEquals($this->cfg['base_url'].'system/', $this->config->system_url()); } // -------------------------------------------------------------------- public function test_load() { - // Create config files in VFS - $file1 = 'test.php'; - $file2 = 'secttest'; - $key1 = 'testconfig'; - $val1 = 'my_value'; - $cfg1 = array( - $key1 => $val1 - ); - $cfg2 = array( + // Test regular load + $file = 'test.php'; + $key = 'testconfig'; + $val = 'my_value'; + $cfg = array($key => $val); + $this->ci_vfs_create($file, 'ci_app_root, 'config'); + $this->assertTrue($this->config->load($file)); + $this->assertEquals($val, $this->config->item($key)); + + // Test reload - value should not change + $val2 = 'new_value'; + $cfg = array($key => $val2); + $this->ci_vfs_create($file, 'ci_app_root, 'config'); + $this->assertTrue($this->config->load($file)); + $this->assertEquals($val, $this->config->item($key)); + + // Test section load + $file = 'secttest'; + $cfg = array( 'one' => 'prime', 'two' => 2, 'three' => true ); - $this->ci_vfs_create(array( - $file1 => ' 'ci_app_root, 'config'); + $this->ci_vfs_create($file.'.php', 'ci_app_root, 'config'); + $this->assertTrue($this->config->load($file, TRUE)); + $this->assertEquals($cfg, $this->config->item($file)); - // Test regular load - $this->assertTrue($this->config->load($file1)); - $this->assertEquals($val1, $this->config->item($key1)); + // Test section merge + $cfg2 = array( + 'three' => 'tres', + 'number' => 42, + 'letter' => 'Z' + ); + $pkg_dir = 'package'; + $this->ci_vfs_create($file.'.php', 'ci_app_root, + array($pkg_dir, 'config')); + array_push($this->config->_config_paths, $this->ci_vfs_path($pkg_dir.'/', APPPATH)); + $this->assertTrue($this->config->load($file, TRUE)); + $this->assertEquals(array_merge($cfg, $cfg2), $this->config->item($file)); + array_pop($this->config->_config_paths); + + // Test graceful fail of invalid file + $file = 'badfile'; + $this->ci_vfs_create($file, '', $this->ci_app_root, 'config'); + $this->assertFalse($this->config->load($file, FALSE, TRUE)); + + // Test regular fail of invalid file + $this->setExpectedException( + 'RuntimeException', + 'CI Error: Your '.$this->ci_vfs_path('config/'.$file.'.php', APPPATH). + ' file does not appear to contain a valid configuration array.' + ); + $this->assertNull($this->config->load($file)); + } - // Test section load - $this->assertTrue($this->config->load($file2, TRUE)); - $this->assertEquals($cfg2, $this->config->item($file2)); + // -------------------------------------------------------------------- - // Test graceful fail + public function test_load_nonexistent() + { + // Test graceful fail of nonexistent file $this->assertFalse($this->config->load('not_config_file', FALSE, TRUE)); // Test regular fail - $file3 = 'absentia'; + $file = 'absentia'; $this->setExpectedException( 'RuntimeException', - 'CI Error: The configuration file '.$file3.'.php does not exist.' + 'CI Error: The configuration file '.$file.'.php does not exist.' + ); + $this->assertNull($this->config->load($file)); + } + + // -------------------------------------------------------------------- + + public function test_assign_to_config() + { + $key1 = 'test'; + $key2 = '1'; + $val1 = 'foo'; + $val2 = 'bar'; + $cfg = array( + $key1 => $val1, + $key2 => $val2 ); - $this->assertNull($this->config->load($file3)); + + $this->assertNull($this->config->_assign_to_config($cfg)); + $this->assertEquals($val1, $this->config->item($key1)); + $this->assertEquals($val2, $this->config->item($key2)); } } \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 4f42be55dc5ca3141f3d93f7f59989d3e52bc5a3 Mon Sep 17 00:00:00 2001 From: dchill42 Date: Sun, 21 Oct 2012 21:31:19 -0400 Subject: Raised CI_Loader test coverage to 93% Signed-off-by: dchill42 --- tests/codeigniter/core/Loader_test.php | 343 ++++++++++++++++++++----------- tests/mocks/ci_testcase.php | 1 + tests/mocks/ci_testconfig.php | 4 +- tests/mocks/core/common.php | 7 +- tests/mocks/core/loader.php | 13 -- tests/mocks/database/schema/skeleton.php | 2 +- 6 files changed, 235 insertions(+), 135 deletions(-) delete mode 100644 tests/mocks/core/loader.php (limited to 'tests') diff --git a/tests/codeigniter/core/Loader_test.php b/tests/codeigniter/core/Loader_test.php index 69b2afb63..a8a2de78f 100644 --- a/tests/codeigniter/core/Loader_test.php +++ b/tests/codeigniter/core/Loader_test.php @@ -7,27 +7,29 @@ class Loader_test extends CI_TestCase { public function set_up() { // Instantiate a new loader - $this->load = new Mock_Core_Loader(); + $loader = $this->ci_core_class('loader'); + $this->load = new $loader(); // Get CI instance $this->ci_obj = $this->ci_instance(); // Set subclass prefix - $this->ci_set_config('subclass_prefix', 'MY_'); + $this->prefix = 'MY_'; + $this->ci_set_config('subclass_prefix', $this->prefix); } // -------------------------------------------------------------------- - /** - * @covers CI_Loader::library - */ public function test_library() { - // Create libraries directory with test library + // Create library in VFS $lib = 'unit_test_lib'; $class = 'CI_'.ucfirst($lib); $this->ci_vfs_create($lib, 'ci_base_root, 'libraries'); + // Test is_loaded fail + $this->assertFalse($this->load->is_loaded($lib)); + // Test loading as an array. $this->assertNull($this->load->library(array($lib))); $this->assertTrue(class_exists($class), $class.' does not exist'); @@ -38,16 +40,68 @@ class Loader_test extends CI_TestCase { // Test a string given to params $this->assertNull($this->load->library($lib, ' ')); + + // Create library w/o class + $lib = 'bad_test_lib'; + $this->ci_vfs_create($lib, '', $this->ci_base_root, 'libraries'); + + // Test non-existent class + $this->setExpectedException( + 'RuntimeException', + 'CI Error: Non-existent class: '.$lib + ); + $this->assertNull($this->load->library($lib)); + } + + // -------------------------------------------------------------------- + + public function test_library_extension() + { + // Create library and extension in VFS + $name = 'ext_test_lib'; + $lib = ucfirst($name); + $class = 'CI_'.$lib; + $ext = $this->prefix.$lib; + $this->ci_vfs_create($lib, 'ci_base_root, 'libraries'); + $this->ci_vfs_create($ext, 'ci_app_root, 'libraries'); + + // Test loading with extension + $this->assertNull($this->load->library($lib)); + $this->assertTrue(class_exists($class), $class.' does not exist'); + $this->assertTrue(class_exists($ext), $ext.' does not exist'); + $this->assertAttributeInstanceOf($class, $name, $this->ci_obj); + $this->assertAttributeInstanceOf($ext, $name, $this->ci_obj); + + // Test reloading with object name + $obj = 'exttest'; + $this->assertNull($this->load->library($lib, NULL, $obj)); + $this->assertAttributeInstanceOf($class, $obj, $this->ci_obj); + $this->assertAttributeInstanceOf($ext, $obj, $this->ci_obj); + + // Test reloading + unset($this->ci_obj->$name); + $this->assertNull($this->load->library($lib)); + $this->assertObjectNotHasAttribute($name, $this->ci_obj); + + // Create baseless library + $name = 'ext_baseless_lib'; + $lib = ucfirst($name); + $class = $this->prefix.$lib; + $this->ci_vfs_create($class, 'ci_app_root, 'libraries'); + + // Test missing base class + $this->setExpectedException( + 'RuntimeException', + 'CI Error: Unable to load the requested class: '.$lib + ); + $this->assertNull($this->load->library($lib)); } // -------------------------------------------------------------------- - /** - * @covers CI_Loader::library - */ public function test_library_config() { - // Create libraries directory with test library + // Create library in VFS $lib = 'unit_test_config_lib'; $class = 'CI_'.ucfirst($lib); $content = 'config = $params; } }'; @@ -67,16 +121,16 @@ class Loader_test extends CI_TestCase { $this->assertTrue(class_exists($class), $class.' does not exist'); $this->assertAttributeInstanceOf($class, $obj, $this->ci_obj); $this->assertEquals($cfg, $this->ci_obj->$obj->config); + + // Test is_loaded + $this->assertEquals($obj, $this->load->is_loaded($lib)); } // -------------------------------------------------------------------- - /** - * @covers CI_Loader::library - */ public function test_load_library_in_application_dir() { - // Create libraries directory in app path with test library + // Create library in VFS $lib = 'super_test_library'; $class = ucfirst($lib); $this->ci_vfs_create($lib, 'ci_app_root, 'libraries'); @@ -91,12 +145,9 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- - /** - * @covers CI_Loader::driver - */ public function test_driver() { - // Create libraries directory with test driver + // Create driver in VFS $driver = 'unit_test_driver'; $dir = ucfirst($driver); $class = 'CI_'.$dir; @@ -122,29 +173,11 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- - /** - * @covers CI_Loader::model - */ - public function test_non_existent_model() - { - $this->setExpectedException( - 'RuntimeException', - 'CI Error: Unable to locate the model you have specified: ci_test_nonexistent_model.php' - ); - - $this->load->model('ci_test_nonexistent_model.php'); - } - - // -------------------------------------------------------------------- - - /** - * @covers CI_Loader::model - */ public function test_models() { $this->ci_set_core_class('model', 'CI_Model'); - // Create models directory with test model + // Create model in VFS $model = 'unit_test_model'; $class = ucfirst($model); $content = 'ci_core_class('model'); + + // Create modelin VFS + $model = 'test_sub_model'; + $base = 'CI_Model'; + $class = ucfirst($model); + $subdir = 'cars'; + $this->ci_vfs_create($model, 'ci_app_root, + array('models', $subdir)); + + // Load model + $name = 'testors'; + $this->assertNull($this->load->model($subdir.'/'.$model, $name)); + + // Was the model class instantiated? + $this->assertTrue(class_exists($class)); + $this->assertObjectHasAttribute($name, $this->ci_obj); + $this->assertAttributeInstanceOf($base, $name, $this->ci_obj); + $this->assertAttributeInstanceOf($class, $name, $this->ci_obj); + + // Test name conflict + $obj = 'conflict'; + $this->ci_obj->$obj = new StdClass(); + $this->setExpectedException( + 'RuntimeException', + 'CI Error: The model name you are loading is the name of a resource that is already being used: '.$obj + ); + $this->load->model('not_real', $obj); + } + + // -------------------------------------------------------------------- + + public function test_non_existent_model() + { + $this->setExpectedException( + 'RuntimeException', + 'CI Error: Unable to locate the model you have specified: ci_test_nonexistent_model.php' + ); + + $this->load->model('ci_test_nonexistent_model.php'); + } + + // -------------------------------------------------------------------- + // public function testDatabase() // { - // $this->assertEquals(NULL, $this->load->database()); - // $this->assertEquals(NULL, $this->load->dbutil()); + // $this->assertNull($this->load->database()); + // $this->assertNull($this->load->dbutil()); // } // -------------------------------------------------------------------- - /** - * @covers CI_Loader::view - */ public function test_load_view() { - $this->ci_set_core_class('output', 'CI_Output'); - - // Create views directory with test view + // Create view in VFS $view = 'unit_test_view'; - $content = 'This is my test page. '; - $this->ci_vfs_create($view, $content, $this->ci_app_root, 'views'); - - // Use the optional return parameter in this test, so the view is not - // run through the output class. - $out = $this->load->view($view, array('hello' => "World!"), TRUE); - $this->assertEquals('This is my test page. World!', $out); + $var = 'hello'; + $value = 'World!'; + $content = 'This is my test page. '; + $this->ci_vfs_create($view, $content.'ci_app_root, 'views'); + + // Test returning view + $out = $this->load->view($view, array($var => $value), TRUE); + $this->assertEquals($content.$value, $out); + + // Mock output class + $class = 'Mock_Load_Output'; + $prop = 'output'; + eval('class '.$class.' { public function append_output($out) { $this->'.$prop.' = $out; } }'); + $this->ci_instance_var('output', new $class()); + + // Test view output + $this->load->view($view, array($var => $value)); + $this->assertObjectHasAttribute($prop, $this->ci_obj->output); + $this->assertEquals($content.$value, $this->ci_obj->output->$prop); } // -------------------------------------------------------------------- - /** - * @covers CI_Loader::view - */ public function test_non_existent_view() { $this->setExpectedException( @@ -205,12 +289,9 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- - /** - * @covers CI_Loader::file - */ public function test_file() { - // Create views directory with test file + // Create view in VFS $dir = 'views'; $file = 'ci_test_mock_file'; $content = 'Here is a test file, which we will load now.'; @@ -231,49 +312,66 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- - /** - * @covers CI_Loader::vars - */ public function test_vars() { - $this->assertNull($this->load->vars(array('foo' => 'bar'))); - $this->assertNull($this->load->vars('foo', 'bar')); + $key1 = 'foo'; + $val1 = 'bar'; + $key2 = 'boo'; + $val2 = 'hoo'; + $this->assertNull($this->load->vars(array($key1 => $val1))); + $this->assertNull($this->load->vars($key2, $val2)); + $this->assertEquals($val1, $this->load->get_var($key1)); + $this->assertEquals(array($key1 => $val1, $key2 => $val2), $this->load->get_vars()); } // -------------------------------------------------------------------- - /** - * @covers CI_Loader::helper - */ public function test_helper() { - // Create helper directory in app path with test helper + // Create helper in VFS $helper = 'test'; $func = '_my_helper_test_func'; $content = 'ci_vfs_create($helper.'_helper', $content, $this->ci_app_root, 'helpers'); + $this->ci_vfs_create($helper.'_helper', $content, $this->ci_base_root, 'helpers'); + + // Create helper extension + $exfunc = '_my_extension_func'; + $content = 'ci_vfs_create($this->prefix.$helper.'_helper', $content, $this->ci_app_root, 'helpers'); // Load helper - $this->assertEquals(NULL, $this->load->helper($helper)); + $this->assertNull($this->load->helper($helper)); $this->assertTrue(function_exists($func), $func.' does not exist'); + $this->assertTrue(function_exists($exfunc), $exfunc.' does not exist'); + + // Create baseless extension + $ext = 'bad_ext'; + $this->ci_vfs_create($this->prefix.$ext.'_helper', '', $this->ci_app_root, 'helpers'); - // Test non-existent helper + // Test bad extension $this->setExpectedException( 'RuntimeException', - 'CI Error: Unable to load the requested file: helpers/bad_helper.php' + 'CI Error: Unable to load the requested file: helpers/'.$ext.'_helper.php' ); + $this->load->helper($ext); + } + + // -------------------------------------------------------------------- + public function test_non_existent_helper() + { + $this->setExpectedException( + 'RuntimeException', + 'CI Error: Unable to load the requested file: helpers/bad_helper.php' + ); $this->load->helper('bad'); } // -------------------------------------------------------------------- - /** - * @covers CI_Loader::helper - */ public function test_loading_multiple_helpers() { - // Create helper directory in base path with test helpers + // Create helpers in VFS $helpers = array(); $funcs = array(); $files = array(); @@ -287,7 +385,7 @@ class Loader_test extends CI_TestCase { $this->ci_vfs_create($files, NULL, $this->ci_base_root, 'helpers'); // Load helpers - $this->assertEquals(NULL, $this->load->helpers($helpers)); + $this->assertNull($this->load->helpers($helpers)); // Verify helper existence foreach ($funcs as $func) { @@ -297,40 +395,36 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- - // public function testLanguage() - // { - // $this->assertEquals(NULL, $this->load->language('test')); - // } + public function test_language() + { + // Create mock Lang class with load stub + $class = 'Mock_Load_Lang'; + $prop = '_file'; + eval('class '.$class.' { public function load($file, $lang) { $this->'.$prop.' = $file; } }'); + $this->ci_instance_var('lang', new $class()); + + // Does the right file get loaded? + $file = 'test'; + $this->assertNull($this->load->language($file)); + $this->assertObjectHasAttribute($prop, $this->ci_obj->lang); + $this->assertEquals($file, $this->ci_obj->lang->$prop); + } // -------------------------------------------------------------------- - /** - * @covers CI_Loader::add_package_path - * @covers CI_Loader::get_package_paths - * @covers CI_Loader::remove_package_path - */ public function test_packages() { - // Create third-party directory in app path with model + // Create model in VFS package path $dir = 'third-party'; $lib = 'unit_test_package'; $class = 'CI_'.ucfirst($lib); - $content = 'ci_vfs_create($lib, $content, $this->ci_app_root, $dir); - - // Test failed load without path - $this->setExpectedException( - 'RuntimeException', - 'CI Error: Unable to load the requested class: '.$lib - ); - $this->load->library($lib); + $this->ci_vfs_create($lib, 'ci_app_root, array($dir, 'libraries')); - // Clear exception and get paths - $this->setExpectedException(NULL); + // Get paths $paths = $this->load->get_package_paths(TRUE); // Add path and verify - $path = APPPATH.$dir; + $path = APPPATH.$dir.'/'; $this->assertNull($this->load->add_package_path($path)); $this->assertContains($path, $this->load->get_package_paths(TRUE)); @@ -338,51 +432,63 @@ class Loader_test extends CI_TestCase { $this->assertNull($this->load->library($lib)); $this->assertTrue(class_exists($class), $class.' does not exist'); + // Add another path + $path2 = APPPATH.'another/'; + $this->assertNull($this->load->add_package_path($path2)); + $this->assertContains($path2, $this->load->get_package_paths(TRUE)); + + // Remove last path + $this->assertNull($this->load->remove_package_path()); + $this->assertNotContains($path2, $this->load->get_package_paths(TRUE)); + // Remove path and verify restored paths $this->assertNull($this->load->remove_package_path($path)); $this->assertEquals($paths, $this->load->get_package_paths(TRUE)); + + // Test failed load without path + $this->setExpectedException( + 'RuntimeException', + 'CI Error: Unable to load the requested class: '.$lib + ); + $this->load->library($lib); } // -------------------------------------------------------------------- - /** - * @covers CI_Loader::config - */ public function test_load_config() { - $this->assertNull($this->load->config('config', FALSE)); + $cfg = 'someconfig'; + $this->assertNull($this->load->config($cfg, FALSE)); + $this->assertContains($cfg, $this->ci_obj->config->loaded); } // -------------------------------------------------------------------- - /** - * @covers CI_Loader::_ci_autoloader - */ - public function test_autoloader() + public function test_initialize() { - // Create helper directory in app path with test helper + // Create helper in VFS $helper = 'autohelp'; $hlp_func = '_autohelp_test_func'; - $content = 'ci_vfs_create($helper.'_helper', $content, $this->ci_app_root, 'helpers'); - // Create libraries directory in base path with test library + // Create library in VFS $lib = 'autolib'; $lib_class = 'CI_'.ucfirst($lib); $this->ci_vfs_create($lib, 'ci_base_root, 'libraries'); - // Create test driver + // Create driver in VFS $drv = 'autodrv'; $subdir = ucfirst($drv); $drv_class = 'CI_'.$subdir; - $this->ci_vfs_create($drv, 'ci_base_root, 'libraries/'.$subdir); + $this->ci_vfs_create($drv, 'ci_base_root, array('libraries', $subdir)); - // Create package directory in app path with model + // Create model in VFS package path $dir = 'testdir'; $path = APPPATH.$dir.'/'; $model = 'automod'; $mod_class = ucfirst($model); - $this->ci_vfs_create($model, 'ci_app_root, $dir.'/models'); + $this->ci_vfs_create($model, 'ci_app_root, array($dir, 'models')); // Create autoloader config $cfg = array( @@ -391,12 +497,12 @@ class Loader_test extends CI_TestCase { 'libraries' => array($lib), 'drivers' => array($drv), 'model' => array($model), - 'config' => array() + 'config' => array('config1', 'config2') ); $this->ci_vfs_create('autoload', 'ci_app_root, 'config'); - // Run autoloader - $this->load->autoload(); + // Run initialize and autoloader + $this->load->initialize(); // Verify path $this->assertContains($path, $this->load->get_package_paths()); @@ -415,6 +521,9 @@ class Loader_test extends CI_TestCase { // Verify model $this->assertTrue(class_exists($mod_class), $mod_class.' does not exist'); $this->assertAttributeInstanceOf($mod_class, $model, $this->ci_obj); + + // Verify config calls + $this->assertEquals($cfg['config'], $this->ci_obj->config->loaded); } } \ No newline at end of file diff --git a/tests/mocks/ci_testcase.php b/tests/mocks/ci_testcase.php index e581d4b02..f16492945 100644 --- a/tests/mocks/ci_testcase.php +++ b/tests/mocks/ci_testcase.php @@ -38,6 +38,7 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { $this->ci_vfs_root = vfsStream::setup(); $this->ci_app_root = vfsStream::newDirectory('application')->at($this->ci_vfs_root); $this->ci_base_root = vfsStream::newDirectory('system')->at($this->ci_vfs_root); + $this->ci_view_root = vfsStream::newDirectory('views')->at($this->ci_app_root); if (method_exists($this, 'set_up')) { diff --git a/tests/mocks/ci_testconfig.php b/tests/mocks/ci_testconfig.php index eb318ddeb..0c52bb984 100644 --- a/tests/mocks/ci_testconfig.php +++ b/tests/mocks/ci_testconfig.php @@ -4,14 +4,16 @@ class CI_TestConfig { public $config = array(); public $_config_paths = array(APPPATH); + public $loaded = array(); public function item($key) { return isset($this->config[$key]) ? $this->config[$key] : FALSE; } - public function load($arg1, $arg2, $arg3) + public function load($file, $arg2 = FALSE, $arg3 = FALSE) { + $this->loaded[] = $file; return TRUE; } diff --git a/tests/mocks/core/common.php b/tests/mocks/core/common.php index b001074c8..9289b2716 100644 --- a/tests/mocks/core/common.php +++ b/tests/mocks/core/common.php @@ -170,9 +170,10 @@ if ( ! function_exists('is_really_writable')) if ( ! function_exists('is_loaded')) { - function is_loaded() + function &is_loaded() { - throw new Exception('Bad Isolation: mock up environment'); + $loaded = array(); + return $loaded; } } @@ -190,4 +191,4 @@ if ( ! function_exists('set_status_header')) { return TRUE; } -} +} \ No newline at end of file diff --git a/tests/mocks/core/loader.php b/tests/mocks/core/loader.php deleted file mode 100644 index 7ea4da369..000000000 --- a/tests/mocks/core/loader.php +++ /dev/null @@ -1,13 +0,0 @@ -_ci_autoloader(); - } - -} diff --git a/tests/mocks/database/schema/skeleton.php b/tests/mocks/database/schema/skeleton.php index 18e1ddd4d..69e3c187e 100644 --- a/tests/mocks/database/schema/skeleton.php +++ b/tests/mocks/database/schema/skeleton.php @@ -30,7 +30,7 @@ class Mock_Database_Schema_Skeleton { CI_TestCase::instance()->ci_instance_var('db', $db); - $loader = new Mock_Core_Loader(); + $loader = new CI_Loader(); $loader->dbforge(); $forge = CI_TestCase::instance()->ci_instance_var('dbforge'); -- cgit v1.2.3-24-g4f1b From cf99aac39914d821e8864443d3aaa759f87258e9 Mon Sep 17 00:00:00 2001 From: dchill42 Date: Sun, 21 Oct 2012 21:32:20 -0400 Subject: Added documentation of new unit test tools and VFS additions Signed-off-by: dchill42 --- tests/README.md | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 5 deletions(-) (limited to 'tests') diff --git a/tests/README.md b/tests/README.md index d600951ee..a5f89a2b1 100644 --- a/tests/README.md +++ b/tests/README.md @@ -64,6 +64,30 @@ with a base for application and package tests. That gives us: 3. Application Test - bootstrapping for application/tests [not started] 4. Package Test - bootstrapping for /tests [not started] +### Test Environment: + +The test/Bootstrap.php file establishes global constants such as BASEPATH, +APPPATH, and VIEWPATH, initializing them to point to VFS locations. The +test case class employs vfsStream to make a clean virtual filesystem with +the necessary paths for every individual test. + +Within each test case, VFS directory objects are available to use as arguments +to the VFS convenience functions (see below): + +- ci_vfs_root: VFS filesystem root +- ci_app_root: Application directory +- ci_base_root: System directory +- ci_view_root: Views directory + +Classes being instantiated for testing are read from the actual filesystem +by the unit test autoloader, as are mockups created in tests/mocks. If you +need access to the real system directory, the SYSTEM_PATH constant always +points to it. + +Any other resources which need to be read from the path constants must be +created or cloned within your test. Functions for doing so are outlined +below. + ### CI_TestCase Documentation Test cases should extend CI_TestCase. This internally extends @@ -78,8 +102,14 @@ Current API is *not stable*. Names and implementations will change. $this->ci_set_config($key, $val) -Set the global config variables. If key is an array, it will -replace the entire config array. They are _not_ merged. +Set the global config variables in a mock Config object. If key is an array, +it will replace the entire config array. They are _not_ merged. If called +without any parameters, it will create the mock object but not set any values. +The mock Config object also provides rudimentary item() and load() stubs for +delivering configured values to classes being tested and handling config load +calls, respectively. The load() stub does _not_ actually load any files, it +only records the filename provided. Check the config->loaded array to verify +calls made. $this->ci_instance($obj) @@ -103,11 +133,48 @@ $GLOBALS key. For example: $cfg = new $cfg; // instantiates config and overwrites the CFG global $this->ci_set_core_class($name, $obj) - + An alternative way to set one of the core globals. + $this->ci_vfs_mkdir($name, $root) + +Creates a new directory in the test VFS. Pass a directory object to be the +parent directory or none to create a root-level directory. Returns the new +directory object. + + $this->ci_vfs_create($file, $content, $root, $path) + +Creates a new VFS file. '.php' is automatically appended to the filename if +it has no extension. Pass a directory object as the root, and an optional path +to recurse and/or create for containing the file. Path may be a string (such +as 'models/subdir') or an array (e.g. - array('models', 'subdir') ). Existing +directories in the VFS root will be recursed until a new directory is +identified - all others in the path will be created, so you can mix-and-match +old and new directories. If $file is an array (key = name, value = content), +multiple files will be created in the same path. + + $this->ci_vfs_clone($path) + +Clones an existing file from the real filesystem to exist in the same path of +the VFS. Path must be relative to the project root (i.e. - starting with +'system' or 'application'). + + $this->ci_vfs_path($path, $base) + +Creates a VFS file path string suitable for use with PHP file operations. Path +may be absolute from the VFS root, or relative to a base path. It is often +useful to use APPPATH or BASEPATH as the base. + + $this->helper($name) + +Loads a helper from the real filesystem. + + $this->lang($name) + +Loads a language file from the real filesystem and returns the $lang array. + $this->ci_get_config() __internal__ - + Returns the global config array. Internal as you shouldn't need to call this (you're setting it, after all). Used internally to make CI's get_config() work. @@ -155,4 +222,4 @@ I don't have a clue how this will work. Needs to be able to handle packages that are used multiple times within the application (i.e. EE/Pyro modules) -as well as packages that are used by multiple applications (library distributions) +as well as packages that are used by multiple applications (library distributions) \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 96a4ca6605d6a8a94eea96ed00ab1cf31a8cdd35 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 23 Oct 2012 01:11:48 +0300 Subject: Add a test for issue #273 --- tests/codeigniter/database/query_builder/like_test.php | 16 ++++++++++++++++ tests/mocks/database/schema/skeleton.php | 3 ++- 2 files changed, 18 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/codeigniter/database/query_builder/like_test.php b/tests/codeigniter/database/query_builder/like_test.php index 5f3e52228..20ce5d99c 100644 --- a/tests/codeigniter/database/query_builder/like_test.php +++ b/tests/codeigniter/database/query_builder/like_test.php @@ -87,4 +87,20 @@ class Like_test extends CI_TestCase { $this->assertEquals('Musician', $jobs[2]['name']); } + // ------------------------------------------------------------------------ + + /** + * GitHub issue #273 + * + * @see ./mocks/schema/skeleton.php + */ + public function test_like_spaces_and_tabs() + { + $spaces = $this->db->like('key', ' ')->get('misc')->result_array(); + $tabs = $this->db->like('key', "\t")->get('misc')->result_array(); + + $this->assertEquals(1, count($spaces)); + $this->assertEquals(1, count($tabs)); + } + } \ No newline at end of file diff --git a/tests/mocks/database/schema/skeleton.php b/tests/mocks/database/schema/skeleton.php index 18e1ddd4d..fb9aaefee 100644 --- a/tests/mocks/database/schema/skeleton.php +++ b/tests/mocks/database/schema/skeleton.php @@ -129,7 +129,8 @@ class Mock_Database_Schema_Skeleton { ), 'misc' => array( array('id' => 1, 'key' => '\\xxxfoo456', 'value' => 'Entry with \\xxx'), - array('id' => 2, 'key' => '\\%foo456', 'value' => 'Entry with \\%') + array('id' => 2, 'key' => '\\%foo456', 'value' => 'Entry with \\%'), + array('id' => 3, 'key' => ' One two three tab') ) ); -- cgit v1.2.3-24-g4f1b From 348bd1e2623ae13ef0dc576464d98eb3673ef15a Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 23 Oct 2012 01:24:11 +0300 Subject: Fix a mock db entry --- tests/codeigniter/database/query_builder/like_test.php | 4 ++-- tests/mocks/database/schema/skeleton.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/codeigniter/database/query_builder/like_test.php b/tests/codeigniter/database/query_builder/like_test.php index 20ce5d99c..2736fbe0b 100644 --- a/tests/codeigniter/database/query_builder/like_test.php +++ b/tests/codeigniter/database/query_builder/like_test.php @@ -96,8 +96,8 @@ class Like_test extends CI_TestCase { */ public function test_like_spaces_and_tabs() { - $spaces = $this->db->like('key', ' ')->get('misc')->result_array(); - $tabs = $this->db->like('key', "\t")->get('misc')->result_array(); + $spaces = $this->db->like('value', ' ')->get('misc')->result_array(); + $tabs = $this->db->like('value', "\t")->get('misc')->result_array(); $this->assertEquals(1, count($spaces)); $this->assertEquals(1, count($tabs)); diff --git a/tests/mocks/database/schema/skeleton.php b/tests/mocks/database/schema/skeleton.php index fb9aaefee..2226835c4 100644 --- a/tests/mocks/database/schema/skeleton.php +++ b/tests/mocks/database/schema/skeleton.php @@ -130,7 +130,7 @@ class Mock_Database_Schema_Skeleton { 'misc' => array( array('id' => 1, 'key' => '\\xxxfoo456', 'value' => 'Entry with \\xxx'), array('id' => 2, 'key' => '\\%foo456', 'value' => 'Entry with \\%'), - array('id' => 3, 'key' => ' One two three tab') + array('id' => 3, 'key' => 'spaces and tabs', 'value' => ' One two three tab') ) ); -- cgit v1.2.3-24-g4f1b From f5f898f8f30968fb36413a14de2dc6a4599b79a6 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 23 Oct 2012 02:13:29 +0300 Subject: Fix issue #779 --- tests/codeigniter/core/URI_test.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'tests') diff --git a/tests/codeigniter/core/URI_test.php b/tests/codeigniter/core/URI_test.php index 60ed1a4e9..e2deabe51 100644 --- a/tests/codeigniter/core/URI_test.php +++ b/tests/codeigniter/core/URI_test.php @@ -40,13 +40,13 @@ class URI_test extends CI_TestCase { '/index.php?/controller/method/?var=foo' => 'controller/method' ); - foreach($requests as $request => $expected) + foreach ($requests as $request => $expected) { $_SERVER['SCRIPT_NAME'] = '/index.php'; $_SERVER['REQUEST_URI'] = $request; $this->uri->_fetch_uri_string(); - $this->assertEquals($expected, $this->uri->uri_string ); + $this->assertEquals($expected, $this->uri->uri_string); } // Test a subfolder @@ -60,10 +60,10 @@ class URI_test extends CI_TestCase { unset($_SERVER['REQUEST_URI']); // life to path info - $_SERVER['PATH_INFO'] = $a = '/controller/method/'; + $_SERVER['PATH_INFO'] = '/controller/method/'; $this->uri->_fetch_uri_string(); - $this->assertEquals($a, $this->uri->uri_string); + $this->assertEquals('controller/method', $this->uri->uri_string); // death to path info // At this point your server must be seriously drunk @@ -72,7 +72,7 @@ class URI_test extends CI_TestCase { $_SERVER['QUERY_STRING'] = '/controller/method/'; $this->uri->_fetch_uri_string(); - $this->assertEquals($a, $this->uri->uri_string); + $this->assertEquals('controller/method', $this->uri->uri_string); // At this point your server is a labotomy victim unset($_SERVER['QUERY_STRING']); @@ -80,7 +80,7 @@ class URI_test extends CI_TestCase { $_GET['/controller/method/'] = ''; $this->uri->_fetch_uri_string(); - $this->assertEquals($a, $this->uri->uri_string); + $this->assertEquals('controller/method', $this->uri->uri_string); // Test coverage implies that these will work // uri_protocol: REQUEST_URI -- cgit v1.2.3-24-g4f1b From 8889db7e1b1768ecfb76e9e73598541042a9edc1 Mon Sep 17 00:00:00 2001 From: dchill42 Date: Mon, 22 Oct 2012 23:16:26 -0400 Subject: Replaced evals with getMock usage in Loader tests Signed-off-by: dchill42 --- tests/codeigniter/core/Loader_test.php | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) (limited to 'tests') diff --git a/tests/codeigniter/core/Loader_test.php b/tests/codeigniter/core/Loader_test.php index a8a2de78f..f7c338a20 100644 --- a/tests/codeigniter/core/Loader_test.php +++ b/tests/codeigniter/core/Loader_test.php @@ -264,15 +264,12 @@ class Loader_test extends CI_TestCase { $this->assertEquals($content.$value, $out); // Mock output class - $class = 'Mock_Load_Output'; - $prop = 'output'; - eval('class '.$class.' { public function append_output($out) { $this->'.$prop.' = $out; } }'); - $this->ci_instance_var('output', new $class()); + $output = $this->getMock('CI_Output', array('append_output')); + $output->expects($this->once())->method('append_output')->with($content.$value); + $this->ci_instance_var('output', $output); // Test view output - $this->load->view($view, array($var => $value)); - $this->assertObjectHasAttribute($prop, $this->ci_obj->output); - $this->assertEquals($content.$value, $this->ci_obj->output->$prop); + $this->assertNull($this->load->view($view, array($var => $value))); } // -------------------------------------------------------------------- @@ -397,17 +394,12 @@ class Loader_test extends CI_TestCase { public function test_language() { - // Create mock Lang class with load stub - $class = 'Mock_Load_Lang'; - $prop = '_file'; - eval('class '.$class.' { public function load($file, $lang) { $this->'.$prop.' = $file; } }'); - $this->ci_instance_var('lang', new $class()); - - // Does the right file get loaded? + // Mock lang class and test load call $file = 'test'; + $lang = $this->getMock('CI_Lang', array('load')); + $lang->expects($this->once())->method('load')->with($file); + $this->ci_instance_var('lang', $lang); $this->assertNull($this->load->language($file)); - $this->assertObjectHasAttribute($prop, $this->ci_obj->lang); - $this->assertEquals($file, $this->ci_obj->lang->$prop); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 5fd3ae8d33a4f5d3159b86683b9a670e973a63f5 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 24 Oct 2012 14:55:35 +0300 Subject: [ci skip] style and phpdoc-related changes (rel #1295) --- tests/mocks/core/lang.php | 4 ++-- tests/mocks/libraries/session.php | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) (limited to 'tests') diff --git a/tests/mocks/core/lang.php b/tests/mocks/core/lang.php index 1b99aedb3..27ea3faba 100644 --- a/tests/mocks/core/lang.php +++ b/tests/mocks/core/lang.php @@ -2,12 +2,12 @@ class Mock_Core_Lang extends CI_Lang { - function line($line = '') + public function line($line = '') { return FALSE; } - function load($langfile, $idiom = '', $return = false, $add_suffix = true, $alt_path = '') + public function load($langfile, $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '') { return; } diff --git a/tests/mocks/libraries/session.php b/tests/mocks/libraries/session.php index 9d6feee42..c6e194f58 100644 --- a/tests/mocks/libraries/session.php +++ b/tests/mocks/libraries/session.php @@ -4,6 +4,7 @@ * Mock library to add testing features to Session driver library */ class Mock_Libraries_Session extends CI_Session { + /** * Simulate new page load */ @@ -19,18 +20,18 @@ class Mock_Libraries_Session extends CI_Session { * Mock cookie driver to overload cookie setting */ class Mock_Libraries_Session_cookie extends CI_Session_cookie { + /** * Overload _setcookie to manage $_COOKIE values, since actual cookies can't be set in unit testing */ - protected function _setcookie($name, $value = '', $expire = 0, $path = '', $domain = '', $secure = false, - $httponly = false) + protected function _setcookie($name, $value = '', $expire = 0, $path = '', $domain = '', $secure = FALSE, $httponly = FALSE) { - if (empty($value) || $expire <= time()) { - // Clear cookie + if (empty($value) OR $expire <= time()) + { unset($_COOKIE[$name]); } - else { - // Set cookie + else + { $_COOKIE[$name] = $value; } } @@ -39,5 +40,4 @@ class Mock_Libraries_Session_cookie extends CI_Session_cookie { /** * Mock native driver (just for consistency in loading) */ -class Mock_Libraries_Session_native extends CI_Session_native { } - +class Mock_Libraries_Session_native extends CI_Session_native { } \ No newline at end of file -- cgit v1.2.3-24-g4f1b From d98785a1661b46f0a1cb83168347fda4c4c7dde9 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 24 Oct 2012 15:33:35 +0300 Subject: Fix #1922 --- tests/codeigniter/libraries/Encrypt_test.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'tests') diff --git a/tests/codeigniter/libraries/Encrypt_test.php b/tests/codeigniter/libraries/Encrypt_test.php index 998d806b3..21ac85f03 100644 --- a/tests/codeigniter/libraries/Encrypt_test.php +++ b/tests/codeigniter/libraries/Encrypt_test.php @@ -9,6 +9,7 @@ class Encrypt_test extends CI_TestCase { $this->ci_set_config('encryption_key', "Encryptin'glike@boss!"); $this->msg = 'My secret message'; + $this->mcrypt = extension_loaded('mcrypt'); } // -------------------------------------------------------------------- @@ -39,6 +40,12 @@ class Encrypt_test extends CI_TestCase { public function test_default_cipher() { + if ( ! $this->mcrypt) + { + $this->markTestSkipped('MCrypt not available'); + return; + } + $this->assertEquals('rijndael-256', $this->encrypt->get_cipher()); } @@ -47,6 +54,12 @@ class Encrypt_test extends CI_TestCase { public function test_set_cipher() { + if ( ! $this->mcrypt) + { + $this->markTestSkipped('MCrypt not available'); + return; + } + $this->encrypt->set_cipher(MCRYPT_BLOWFISH); $this->assertEquals('blowfish', $this->encrypt->get_cipher()); } @@ -55,6 +68,12 @@ class Encrypt_test extends CI_TestCase { public function test_default_mode() { + if ( ! $this->mcrypt) + { + $this->markTestSkipped('MCrypt not available'); + return; + } + $this->assertEquals('cbc', $this->encrypt->get_mode()); } @@ -62,6 +81,12 @@ class Encrypt_test extends CI_TestCase { public function test_set_mode() { + if ( ! $this->mcrypt) + { + $this->markTestSkipped('MCrypt not available'); + return; + } + $this->encrypt->set_mode(MCRYPT_MODE_CFB); $this->assertEquals('cfb', $this->encrypt->get_mode()); } -- cgit v1.2.3-24-g4f1b From a7001e968a4791312391eb245ad84888893cda8f Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 24 Oct 2012 18:51:15 +0300 Subject: Fix insert_batch() test case --- tests/codeigniter/database/query_builder/insert_test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/codeigniter/database/query_builder/insert_test.php b/tests/codeigniter/database/query_builder/insert_test.php index a9aafb66e..30c055680 100644 --- a/tests/codeigniter/database/query_builder/insert_test.php +++ b/tests/codeigniter/database/query_builder/insert_test.php @@ -52,7 +52,7 @@ class Insert_test extends CI_TestCase { // Do insert batch except for sqlite driver if (strpos(DB_DRIVER, 'sqlite') === FALSE) { - $this->assertTrue($this->db->insert_batch('job', $job_datas)); + $this->assertEquals(2, $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(); -- cgit v1.2.3-24-g4f1b