diff options
author | Greg Aker <greg.aker@ellislab.com> | 2011-04-22 18:21:31 +0200 |
---|---|---|
committer | Greg Aker <greg.aker@ellislab.com> | 2011-04-22 18:21:31 +0200 |
commit | 9c5e13f6b8484f8edc6854ba9045752dc51efaa3 (patch) | |
tree | 738c864a8a74e2703fe978ec4859031b71dfd8cd | |
parent | c0b133c02e057b6506726b794ba4582bf18ed663 (diff) | |
parent | 5de50ec2fefc7dad1fd768573ca3f47538fd8988 (diff) |
Merging in default branch
22 files changed, 2142 insertions, 0 deletions
diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php new file mode 100644 index 000000000..657671ab0 --- /dev/null +++ b/tests/Bootstrap.php @@ -0,0 +1,30 @@ +<?php + +// Errors on full! +ini_set('display_errors', 1); +error_reporting(E_ALL | E_STRICT); + +$dir = realpath(dirname(__FILE__)); + + +// Path constants +define('PROJECT_BASE', realpath($dir.'/../').'/'); +define('BASEPATH', PROJECT_BASE.'system/'); +define('APPPATH', PROJECT_BASE.'application/'); + + +// Prep our test environment +require_once $dir.'/lib/common.php'; +require_once $dir.'/lib/ci_testcase.php'; + + +// Omit files in the PEAR & PHP Paths from ending up in the coverage report +PHP_CodeCoverage_Filter::getInstance()->addDirectoryToBlacklist(PEAR_INSTALL_DIR); +PHP_CodeCoverage_Filter::getInstance()->addDirectoryToBlacklist(PHP_LIBDIR); +PHP_CodeCoverage_Filter::getInstance()->addDirectoryToBlacklist(PROJECT_BASE.'tests'); + +// Omit Tests from the coverage reports. +// PHP_CodeCoverage_Filter::getInstance()->addDirectoryToWhiteList('../system/core'); +PHP_CodeCoverage_Filter::getInstance()->addFileToBlackList('../system/core/CodeIgniter.php'); + +unset($dir);
\ No newline at end of file diff --git a/tests/codeigniter/Setup_test.php b/tests/codeigniter/Setup_test.php new file mode 100644 index 000000000..550245f2f --- /dev/null +++ b/tests/codeigniter/Setup_test.php @@ -0,0 +1,13 @@ +<?php + +class Setup_test extends PHPUnit_Framework_TestCase { + + function test_nonsense() + { + $this->markTestIncomplete('not implemented'); + // ensure that our bootstrapped test environment + // is a good representation of an isolated CI install + //die('here'); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/core/Common_test.php b/tests/codeigniter/core/Common_test.php new file mode 100644 index 000000000..cec12982d --- /dev/null +++ b/tests/codeigniter/core/Common_test.php @@ -0,0 +1,16 @@ +<?php + +require_once(BASEPATH.'helpers/email_helper.php'); + +class Common_test extends CI_TestCase +{ + + // ------------------------------------------------------------------------ + + public function test_is_php() + { + $this->assertEquals(TRUE, is_php('1.2.0')); + $this->assertEquals(FALSE, is_php('9999.9.9')); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/core/Config_test.php b/tests/codeigniter/core/Config_test.php new file mode 100644 index 000000000..b6c57da70 --- /dev/null +++ b/tests/codeigniter/core/Config_test.php @@ -0,0 +1,93 @@ +<?php + +class Config_test extends CI_TestCase { + + public function set_up() + { + $cls =& $this->ci_core_class('cfg'); + + // set predictable config values + $this->ci_set_config(array( + 'index_page' => 'index.php', + 'base_url' => 'http://example.com/', + 'subclass_prefix' => 'MY_' + )); + + $this->config = new $cls; + } + + // -------------------------------------------------------------------- + + public function test_item() + { + $this->assertEquals('http://example.com/', $this->config->item('base_url')); + + // Bad Config value + $this->assertFalse($this->config->item('no_good_item')); + + // Index + $this->assertFalse($this->config->item('no_good_item', 'bad_index')); + $this->assertFalse($this->config->item('no_good_item', 'default')); + } + + // -------------------------------------------------------------------- + + public function test_set_item() + { + $this->assertFalse($this->config->item('not_yet_set')); + + $this->config->set_item('not_yet_set', 'is set'); + + $this->assertEquals('is set', $this->config->item('not_yet_set')); + } + + // -------------------------------------------------------------------- + + public function test_slash_item() + { + // 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('MY_/', $this->config->slash_item('subclass_prefix')); + } + + // -------------------------------------------------------------------- + + public function test_site_url() + { + $this->assertEquals('http://example.com/index.php', $this->config->site_url()); + + $base_url = $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'))); + + $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->config->set_item('base_url', $base_url); + + $this->assertEquals('http://example.com/index.php?test', $this->config->site_url('test')); + + // back to home base + $this->config->set_item('enable_query_strings', $q_string); + } + + // -------------------------------------------------------------------- + + public function test_system_url() + { + $this->assertEquals('http://example.com/system/', $this->config->system_url()); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/core/Lang_test.php b/tests/codeigniter/core/Lang_test.php new file mode 100644 index 000000000..dcc3d0879 --- /dev/null +++ b/tests/codeigniter/core/Lang_test.php @@ -0,0 +1,31 @@ +<?php + +class Lang_test extends CI_TestCase { + + protected $lang; + + public function set_up() + { + $cls = $this->ci_core_class('lang'); + $this->lang = new $cls; + } + + // -------------------------------------------------------------------- + + public function test_load() + { + // get_config needs work + $this->markTestIncomplete('get_config needs work'); + //$this->assertTrue($this->lang->load('profiler')); + } + + // -------------------------------------------------------------------- + + public function test_line() + { + $this->markTestIncomplete('get_config needs work'); + + $this->assertEquals('URI STRING', $this->lang->line('profiler_uri_string')); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/core/Loader_test.php b/tests/codeigniter/core/Loader_test.php new file mode 100644 index 000000000..9ba870b7d --- /dev/null +++ b/tests/codeigniter/core/Loader_test.php @@ -0,0 +1,271 @@ +<?php + +require_once 'vfsStream/vfsStream.php'; +require_once BASEPATH.'/core/Loader.php'; + +class Extended_Loader extends CI_Loader { + + /** + * Since we use paths to load up models, views, etc, we need the ability to + * mock up the file system so when core tests are run, we aren't mucking + * in the application directory. this will give finer grained control over + * these tests. So yeah, while this looks odd, I need to overwrite protected + * class vars in the loader. So here we go... + * + * @covers CI_Loader::__construct() + */ + public function __construct() + { + vfsStreamWrapper::register(); + vfsStreamWrapper::setRoot(new vfsStreamDirectory('application')); + + $this->models_dir = vfsStream::newDirectory('models')->at(vfsStreamWrapper::getRoot()); + $this->libs_dir = vfsStream::newDirectory('libraries')->at(vfsStreamWrapper::getRoot()); + $this->helpers_dir = vfsStream::newDirectory('helpers')->at(vfsStreamWrapper::getRoot()); + $this->views_dir = vfsStream::newDirectory('views')->at(vfsStreamWrapper::getRoot()); + + $this->_ci_ob_level = ob_get_level(); + $this->_ci_library_paths = array(vfsStream::url('application').'/', BASEPATH); + $this->_ci_helper_paths = array(vfsStream::url('application').'/', BASEPATH); + $this->_ci_model_paths = array(vfsStream::url('application').'/'); + $this->_ci_view_paths = array(vfsStream::url('application').'/views/' => TRUE); + } +} + + +class Loader_test extends CI_TestCase { + + private $ci_obj; + + public function set_up() + { + // Instantiate a new loader + $this->load = new Extended_Loader(); + + // mock up a ci instance + $this->ci_obj = new StdClass; + + // Fix get_instance() + $this->ci_instance($this->ci_obj); + } + + // -------------------------------------------------------------------- + + public function test_library() + { + $this->_setup_config_mock(); + + // 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); + + // Test no lib given + $this->assertEquals(FALSE, $this->load->library()); + + // Test a string given to params + $this->assertEquals(NULL, $this->load->library('table', ' ')); + } + + // -------------------------------------------------------------------- + + public function test_load_library_in_application_dir() + { + $this->_setup_config_mock(); + + $content = '<?php class Super_test_library {} '; + + $model = vfsStream::newFile('Super_test_library.php')->withContent($content) + ->at($this->load->libs_dir); + + $this->assertNull($this->load->library('super_test_library')); + + // Was the model class instantiated. + $this->assertTrue(class_exists('Super_test_library')); + } + + // -------------------------------------------------------------------- + + private function _setup_config_mock() + { + // 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)); + + // Add the mock to our stdClass + $this->ci_instance_var('config', $config); + } + + // -------------------------------------------------------------------- + + public function test_non_existent_model() + { + $this->setExpectedException( + 'Exception', + 'CI Error: Unable to locate the model you have specified: ci_test_nonexistent_model.php' + ); + + $this->load->model('ci_test_nonexistent_model.php'); + } + + // -------------------------------------------------------------------- + + /** + * @coverts CI_Loader::model + */ + public function test_models() + { + $this->ci_set_core_class('model', 'CI_Model'); + + $content = '<?php class Unit_test_model extends CI_Model {} '; + + $model = vfsStream::newFile('unit_test_model.php')->withContent($content) + ->at($this->load->models_dir); + + $this->assertNull($this->load->model('unit_test_model')); + + // Was the model class instantiated. + $this->assertTrue(class_exists('Unit_test_model')); + + // Test no model given + $this->assertNull($this->load->model('')); + } + + // -------------------------------------------------------------------- + + // public function testDatabase() + // { + // $this->assertEquals(NULL, $this->load->database()); + // $this->assertEquals(NULL, $this->load->dbutil()); + // } + + // -------------------------------------------------------------------- + + /** + * @coverts CI_Loader::view + */ + public function test_load_view() + { + $this->ci_set_core_class('output', 'CI_Output'); + + $content = 'This is my test page. <?php echo $hello; ?>'; + $view = vfsStream::newFile('unit_test_view.php')->withContent($content) + ->at($this->load->views_dir); + + // 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)); + + } + + // -------------------------------------------------------------------- + + /** + * @coverts CI_Loader::view + */ + public function test_non_existent_view() + { + $this->setExpectedException( + 'Exception', + 'CI Error: Unable to load the requested file: ci_test_nonexistent_view.php' + ); + + $this->load->view('ci_test_nonexistent_view', array('foo' => 'bar')); + } + + // -------------------------------------------------------------------- + + public function test_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); + + // 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); + + $this->setExpectedException( + 'Exception', + 'CI Error: Unable to load the requested file: ci_test_file_not_exists' + ); + + $this->load->file('ci_test_file_not_exists', TRUE); + + } + + // -------------------------------------------------------------------- + + public function test_vars() + { + $vars = array( + 'foo' => 'bar' + ); + + $this->assertNull($this->load->vars($vars)); + $this->assertNull($this->load->vars('foo', 'bar')); + } + + // -------------------------------------------------------------------- + + public function test_helper() + { + $this->assertEquals(NULL, $this->load->helper('array')); + + $this->setExpectedException( + 'Exception', + 'CI Error: Unable to load the requested file: helpers/bad_helper.php' + ); + + $this->load->helper('bad'); + } + + // -------------------------------------------------------------------- + + public function test_loading_multiple_helpers() + { + $this->assertEquals(NULL, $this->load->helpers(array('file', 'array', 'string'))); + } + + // -------------------------------------------------------------------- + + // public function testLanguage() + // { + // $this->assertEquals(NULL, $this->load->language('test')); + // } + + // -------------------------------------------------------------------- + + public function test_load_config() + { + $this->_setup_config_mock(); + + $this->assertNull($this->load->config('config', FALSE)); + } + + // -------------------------------------------------------------------- + + public function test_load_bad_config() + { + $this->_setup_config_mock(); + + $this->setExpectedException( + 'Exception', + 'CI Error: The configuration file foobar.php does not exist.' + ); + + $this->load->config('foobar', FALSE); + } + + // -------------------------------------------------------------------- + + + + +}
\ No newline at end of file diff --git a/tests/codeigniter/helpers/array_helper_test.php b/tests/codeigniter/helpers/array_helper_test.php new file mode 100644 index 000000000..62559de83 --- /dev/null +++ b/tests/codeigniter/helpers/array_helper_test.php @@ -0,0 +1,49 @@ +<?php + +// OLD TEST FORMAT: DO NOT COPY + +require_once(BASEPATH.'helpers/array_helper.php'); + +class Array_helper_test extends CI_TestCase +{ + public function set_up() + { + $this->my_array = array( + 'foo' => 'bar', + 'sally' => 'jim', + 'maggie' => 'bessie', + 'herb' => 'cook' + ); + } + + // ------------------------------------------------------------------------ + + public function test_element_with_existing_item() + { + $this->assertEquals(FALSE, element('testing', $this->my_array)); + + $this->assertEquals('not set', element('testing', $this->my_array, 'not set')); + + $this->assertEquals('bar', element('foo', $this->my_array)); + } + + // ------------------------------------------------------------------------ + + public function test_random_element() + { + // Send a string, not an array to random_element + $this->assertEquals('my string', random_element('my string')); + + // Test sending an array + $this->assertEquals(TRUE, in_array(random_element($this->my_array), $this->my_array)); + } + + // ------------------------------------------------------------------------ + + public function test_elements() + { + $this->assertEquals(TRUE, is_array(elements('test', $this->my_array))); + $this->assertEquals(TRUE, is_array(elements('foo', $this->my_array))); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/helpers/email_helper_test.php b/tests/codeigniter/helpers/email_helper_test.php new file mode 100644 index 000000000..7324e8109 --- /dev/null +++ b/tests/codeigniter/helpers/email_helper_test.php @@ -0,0 +1,16 @@ +<?php + +require_once(BASEPATH.'helpers/email_helper.php'); + +class Email_helper_test extends CI_TestCase +{ + + public function test_valid_email() + { + $this->assertEquals(FALSE, valid_email('test')); + $this->assertEquals(FALSE, valid_email('test@test@test.com')); + $this->assertEquals(TRUE, valid_email('test@test.com')); + $this->assertEquals(TRUE, valid_email('my.test@test.com')); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/helpers/html_helper_test.php b/tests/codeigniter/helpers/html_helper_test.php new file mode 100644 index 000000000..706874f9e --- /dev/null +++ b/tests/codeigniter/helpers/html_helper_test.php @@ -0,0 +1,78 @@ +<?php + +require_once(BASEPATH.'helpers/html_helper.php'); + +class Html_helper_test extends CI_TestCase +{ + + // ------------------------------------------------------------------------ + + public function test_br() + { + $this->assertEquals('<br /><br />', br(2)); + } + + // ------------------------------------------------------------------------ + + public function test_heading() + { + $this->assertEquals('<h1>foobar</h1>', heading('foobar')); + $this->assertEquals('<h2 class="bar">foobar</h2>', heading('foobar', 2, 'class="bar"')); + } + + // ------------------------------------------------------------------------ + + public function test_Ul() + { + $expect = <<<EOH +<ul> + <li>foo</li> + <li>bar</li> +</ul> + +EOH; + + $expect = ltrim($expect); + + $list = array('foo', 'bar'); + + $this->assertEquals($expect, ul($list)); + + + $expect = <<<EOH +<ul class="test"> + <li>foo</li> + <li>bar</li> +</ul> + +EOH; + + $expect = ltrim($expect); + + $list = array('foo', 'bar'); + + $this->assertEquals($expect, ul($list, ' class="test"')); + + $this->assertEquals($expect, ul($list, array('class' => 'test'))); + } + + // ------------------------------------------------------------------------ + + public function test_NBS() + { + $this->assertEquals(' ', nbs(3)); + } + + // ------------------------------------------------------------------------ + + public function test_meta() + { + $this->assertEquals("<meta name=\"test\" content=\"foo\" />\n", meta('test', 'foo')); + + $expect = "<meta name=\"foo\" content=\"\" />\n"; + + $this->assertEquals($expect, meta(array('name' => 'foo'))); + + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/helpers/inflector_helper_test.php b/tests/codeigniter/helpers/inflector_helper_test.php new file mode 100644 index 000000000..34bc34ebb --- /dev/null +++ b/tests/codeigniter/helpers/inflector_helper_test.php @@ -0,0 +1,91 @@ +<?php + +require_once(BASEPATH.'helpers/inflector_helper.php'); + +class Inflector_helper_test extends CI_TestCase { + + + public function test_singular() + { + $strs = array( + 'tellies' => 'telly', + 'smellies' => 'smelly', + 'abjectnesses' => 'abjectness', + 'smells' => 'smell' + ); + + foreach ($strs as $str => $expect) + { + $this->assertEquals($expect, singular($str)); + } + } + + // -------------------------------------------------------------------- + + public function test_plural() + { + $strs = array( + 'telly' => 'tellies', + 'smelly' => 'smellies', + 'abjectness' => 'abjectness', + 'smell' => 'smells', + 'witch' => 'witches' + ); + + foreach ($strs as $str => $expect) + { + $this->assertEquals($expect, plural($str)); + } + } + + // -------------------------------------------------------------------- + + public function test_camelize() + { + $strs = array( + 'this is the string' => 'thisIsTheString', + 'this is another one' => 'thisIsAnotherOne', + 'i-am-playing-a-trick' => 'i-am-playing-a-trick', + 'what_do_you_think-yo?' => 'whatDoYouThink-yo?', + ); + + foreach ($strs as $str => $expect) + { + $this->assertEquals($expect, camelize($str)); + } + } + + // -------------------------------------------------------------------- + + public function test_underscore() + { + $strs = array( + 'this is the string' => 'this_is_the_string', + 'this is another one' => 'this_is_another_one', + 'i-am-playing-a-trick' => 'i-am-playing-a-trick', + 'what_do_you_think-yo?' => 'what_do_you_think-yo?', + ); + + foreach ($strs as $str => $expect) + { + $this->assertEquals($expect, underscore($str)); + } + } + + // -------------------------------------------------------------------- + + public function test_humanize() + { + $strs = array( + 'this_is_the_string' => 'This Is The String', + 'this_is_another_one' => 'This Is Another One', + 'i-am-playing-a-trick' => 'I-am-playing-a-trick', + 'what_do_you_think-yo?' => 'What Do You Think-yo?', + ); + + foreach ($strs as $str => $expect) + { + $this->assertEquals($expect, humanize($str)); + } + } +}
\ No newline at end of file diff --git a/tests/codeigniter/helpers/number_helper_test.php b/tests/codeigniter/helpers/number_helper_test.php new file mode 100644 index 000000000..02fc49c3d --- /dev/null +++ b/tests/codeigniter/helpers/number_helper_test.php @@ -0,0 +1,13 @@ +<?php + +require_once(BASEPATH.'helpers/number_helper.php'); + +class Number_helper_test extends CI_TestCase +{ + + public function test_byte_format() + { + // $this->assertEquals('456 Bytes', byte_format(456)); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/helpers/string_helper_test.php b/tests/codeigniter/helpers/string_helper_test.php new file mode 100644 index 000000000..5e0ee45de --- /dev/null +++ b/tests/codeigniter/helpers/string_helper_test.php @@ -0,0 +1,118 @@ +<?php + +require_once(BASEPATH.'helpers/string_helper.php'); + +class String_helper_test extends CI_TestCase +{ + public function test_trim_slashes() + { + $strs = array( + '//Slashes//\/' => 'Slashes//\\', + '/var/www/html/' => 'var/www/html' + ); + + foreach ($strs as $str => $expect) + { + $this->assertEquals($expect, trim_slashes($str)); + } + } + + // -------------------------------------------------------------------- + + public function test_strip_quotes() + { + $strs = array( + '"me oh my!"' => 'me oh my!', + "it's a winner!" => 'its a winner!', + ); + + foreach ($strs as $str => $expect) + { + $this->assertEquals($expect, strip_quotes($str)); + } + } + + // -------------------------------------------------------------------- + + public function test_quotes_to_entities() + { + $strs = array( + '"me oh my!"' => '"me oh my!"', + "it's a winner!" => 'it's a winner!', + ); + + foreach ($strs as $str => $expect) + { + $this->assertEquals($expect, quotes_to_entities($str)); + } + } + + // -------------------------------------------------------------------- + + public function test_reduce_double_slashes() + { + $strs = array( + 'http://codeigniter.com' => 'http://codeigniter.com', + '//var/www/html/example.com/' => '/var/www/html/example.com/', + '/var/www/html//index.php' => '/var/www/html/index.php' + ); + + foreach ($strs as $str => $expect) + { + $this->assertEquals($expect, reduce_double_slashes($str)); + } + } + + // -------------------------------------------------------------------- + + public function test_reduce_multiples() + { + $strs = array( + 'Fred, Bill,, Joe, Jimmy' => 'Fred, Bill, Joe, Jimmy', + 'Ringo, John, Paul,,' => 'Ringo, John, Paul,' + ); + + foreach ($strs as $str => $expect) + { + $this->assertEquals($expect, reduce_multiples($str)); + } + + $strs = array( + 'Fred, Bill,, Joe, Jimmy' => 'Fred, Bill, Joe, Jimmy', + 'Ringo, John, Paul,,' => 'Ringo, John, Paul' + ); + + foreach ($strs as $str => $expect) + { + $this->assertEquals($expect, reduce_multiples($str, ',', TRUE)); + } + } + + // -------------------------------------------------------------------- + + public function test_repeater() + { + $strs = array( + 'a' => 'aaaaaaaaaa', + ' ' => ' ', + '<br>' => '<br><br><br><br><br><br><br><br><br><br>' + + ); + + foreach ($strs as $str => $expect) + { + $this->assertEquals($expect, repeater($str, 10)); + } + } + + // -------------------------------------------------------------------- + + + public function test_random_string() + { + $this->assertEquals(16, strlen(random_string('alnum', 16))); + $this->assertEquals(32, strlen(random_string('unique', 16))); + $this->assertInternalType('string', random_string('numeric', 16)); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/helpers/text_helper_test.php b/tests/codeigniter/helpers/text_helper_test.php new file mode 100644 index 000000000..a0866e638 --- /dev/null +++ b/tests/codeigniter/helpers/text_helper_test.php @@ -0,0 +1,159 @@ +<?php + +require_once(BASEPATH.'helpers/text_helper.php'); + +class Text_helper_test extends CI_TestCase +{ + private $_long_string; + + public function set_up() + { + $this->_long_string = 'Once upon a time, a framework had no tests. It sad. So some nice people began to write tests. The more time that went on, the happier it became. Everyone was happy.'; + } + + // ------------------------------------------------------------------------ + + public function test_word_limiter() + { + $this->assertEquals('Once upon a time,…', word_limiter($this->_long_string, 4)); + $this->assertEquals('Once upon a time,…', word_limiter($this->_long_string, 4, '…')); + $this->assertEquals('', word_limiter('', 4)); + } + + // ------------------------------------------------------------------------ + + public function test_character_limiter() + { + $this->assertEquals('Once upon a time, a…', character_limiter($this->_long_string, 20)); + $this->assertEquals('Once upon a time, a…', character_limiter($this->_long_string, 20, '…')); + $this->assertEquals('Short', character_limiter('Short', 20)); + $this->assertEquals('Short', character_limiter('Short', 5)); + } + + // ------------------------------------------------------------------------ + + public function test_ascii_to_entities() + { + $strs = array( + '“‘ “test”' => '“‘ “test”', + '†¥¨ˆøåß∂ƒ©˙∆˚¬' => '†¥¨ˆøåß∂ƒ©˙∆˚¬' + ); + + foreach ($strs as $str => $expect) + { + $this->assertEquals($expect, ascii_to_entities($str)); + } + } + + // ------------------------------------------------------------------------ + + public function test_entities_to_ascii() + { + $strs = array( + '“‘ “test”' => '“‘ “test”', + '†¥¨ˆøåß∂ƒ©˙∆˚¬' => '†¥¨ˆøåß∂ƒ©˙∆˚¬' + ); + + foreach ($strs as $str => $expect) + { + $this->assertEquals($expect, entities_to_ascii($str)); + } + } + + // ------------------------------------------------------------------------ + + function test_convert_accented_characters() + { + $this->assertEquals('AAAeEEEIIOOEUUUeY', convert_accented_characters('ÀÂÄÈÊËÎÏÔŒÙÛÜŸ')); + $this->assertEquals('a e i o u n ue', convert_accented_characters('á é í ó ú ñ ü')); + } + + // ------------------------------------------------------------------------ + + public function test_censored_words() + { + $censored = array('boob', 'nerd', 'ass', 'fart'); + + $strs = array( + 'Ted bobbled the ball' => 'Ted bobbled the ball', + 'Jake is a nerdo' => 'Jake is a nerdo', + 'The borg will assimilate you' => 'The borg will assimilate you', + 'Did Mary Fart?' => 'Did Mary $*#?', + 'Jake is really a boob' => 'Jake is really a $*#' + ); + + + foreach ($strs as $str => $expect) + { + $this->assertEquals($expect, word_censor($str, $censored, '$*#')); + } + + // test censored words being sent as a string + $this->assertEquals('test', word_censor('test', 'test')); + } + + // ------------------------------------------------------------------------ + + public function test_highlight_code() + { + $code = '<?php var_dump($this); ?>'; + $expect = "<code><span style=\"color: #000000\">\n<span style=\"color: #0000BB\"><?php var_dump</span><span style=\"color: #007700\">(</span><span style=\"color: #0000BB\">\$this</span><span style=\"color: #007700\">); </span><span style=\"color: #0000BB\">?> </span>\n</span>\n</code>"; + + $this->assertEquals($expect, highlight_code($code)); + } + + // ------------------------------------------------------------------------ + + public function test_highlight_phrase() + { + $strs = array( + 'this is a phrase' => '<strong>this is</strong> a phrase', + 'this is another' => '<strong>this is</strong> another', + 'Gimme a test, Sally' => 'Gimme a test, Sally', + 'Or tell me what this is' => 'Or tell me what <strong>this is</strong>', + '' => '' + ); + + foreach ($strs as $str => $expect) + { + $this->assertEquals($expect, highlight_phrase($str, 'this is')); + } + } + + // ------------------------------------------------------------------------ + + public function test_ellipsizing() + { + $strs = array( + '0' => array( + 'this is my string' => '… my string', + "here's another one" => '…nother one', + 'this one is just a bit longer' => '…bit longer', + 'short' => 'short' + ), + '.5' => array( + 'this is my string' => 'this …tring', + "here's another one" => "here'…r one", + 'this one is just a bit longer' => 'this …onger', + 'short' => 'short' + ), + '1' => array( + 'this is my string' => 'this is my…', + "here's another one" => "here's ano…", + 'this one is just a bit longer' => 'this one i…', + 'short' => 'short' + ), + ); + + foreach ($strs as $pos => $s) + { + foreach ($s as $str => $expect) + { + $this->assertEquals($expect, ellipsize($str, 10, $pos)); + } + } + } + + // ------------------------------------------------------------------------ + +}
\ No newline at end of file diff --git a/tests/codeigniter/helpers/xml_helper_test.php b/tests/codeigniter/helpers/xml_helper_test.php new file mode 100644 index 000000000..49f49e166 --- /dev/null +++ b/tests/codeigniter/helpers/xml_helper_test.php @@ -0,0 +1,13 @@ +<?php + +require_once(BASEPATH.'helpers/xml_helper.php'); + +class Xml_helper_test extends CI_TestCase +{ + + public function test_xml_convert() + { + $this->assertEquals('<tag>my & test - </tag>', xml_convert('<tag>my & test - </tag>')); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/libraries/Parser_test.php b/tests/codeigniter/libraries/Parser_test.php new file mode 100644 index 000000000..b4580a4b1 --- /dev/null +++ b/tests/codeigniter/libraries/Parser_test.php @@ -0,0 +1,113 @@ +<?php + +require BASEPATH.'libraries/Parser.php'; + +class Parser_test extends CI_TestCase +{ + + public function set_up() + { + $obj = new StdClass; + $obj->parser = new CI_Parser(); + + $this->ci_instance($obj); + + $this->parser = $obj->parser; + } + // -------------------------------------------------------------------- + + public function test_set_delimiters() + { + // Make sure default delimiters are there + $this->assertEquals('{', $this->parser->l_delim); + $this->assertEquals('}', $this->parser->r_delim); + + // Change them to square brackets + $this->parser->set_delimiters('[', ']'); + + // Make sure they changed + $this->assertEquals('[', $this->parser->l_delim); + $this->assertEquals(']', $this->parser->r_delim); + + // Reset them + $this->parser->set_delimiters(); + + // Make sure default delimiters are there + $this->assertEquals('{', $this->parser->l_delim); + $this->assertEquals('}', $this->parser->r_delim); + } + + // -------------------------------------------------------------------- + + public function test_parse_simple_string() + { + $data = array( + 'title' => 'Page Title', + 'body' => 'Lorem ipsum dolor sit amet.' + ); + + $template = "{title}\n{body}"; + + $result = implode("\n", $data); + + $this->assertEquals($result, $this->parser->parse_string($template, $data, TRUE)); + } + + // -------------------------------------------------------------------- + + public function test_parse() + { + $this->_parse_no_template(); + $this->_parse_var_pair(); + $this->_mismatched_var_pair(); + } + + // -------------------------------------------------------------------- + + private function _parse_no_template() + { + $this->assertFalse($this->parser->parse_string('', '', TRUE)); + } + + // -------------------------------------------------------------------- + + private function _parse_var_pair() + { + $data = array( + 'title' => 'Super Heroes', + 'powers' => array( + array( + 'invisibility' => 'yes', + 'flying' => 'no'), + ) + ); + + $template = "{title}\n{powers}{invisibility}\n{flying}{/powers}"; + + $result = "Super Heroes\nyes\nno"; + + $this->assertEquals($result, $this->parser->parse_string($template, $data, TRUE)); + } + + // -------------------------------------------------------------------- + + private function _mismatched_var_pair() + { + $data = array( + 'title' => 'Super Heroes', + 'powers' => array( + array( + 'invisibility' => 'yes', + 'flying' => 'no'), + ) + ); + + $template = "{title}\n{powers}{invisibility}\n{flying}"; + + $result = "Super Heroes\n{powers}{invisibility}\n{flying}"; + + $this->assertEquals($result, $this->parser->parse_string($template, $data, TRUE)); + } + + // -------------------------------------------------------------------- +}
\ No newline at end of file diff --git a/tests/codeigniter/libraries/Table_test.php b/tests/codeigniter/libraries/Table_test.php new file mode 100644 index 000000000..133179f3a --- /dev/null +++ b/tests/codeigniter/libraries/Table_test.php @@ -0,0 +1,291 @@ +<?php + +require BASEPATH.'libraries/Table.php'; + +class Table_test extends CI_TestCase +{ + + public function set_up() + { + $obj = new StdClass; + $obj->table = new CI_table(); + + $this->ci_instance($obj); + + $this->table = $obj->table; + } + + + // Setter Methods + // -------------------------------------------------------------------- + + public function test_set_template() + { + $this->assertFalse($this->table->set_template('not an array')); + + $template = array( + 'a' => 'b' + ); + + $this->table->set_template($template); + $this->assertEquals($template, $this->table->template); + } + + public function test_set_empty() + { + $this->table->set_empty('nada'); + $this->assertEquals('nada', $this->table->empty_cells); + } + + public function test_set_caption() + { + $this->table->set_caption('awesome cap'); + $this->assertEquals('awesome cap', $this->table->caption); + } + + + /* + * @depends testPrepArgs + */ + public function test_set_heading() + { + // uses _prep_args internally, so we'll just do a quick + // check to verify that func_get_args and prep_args are + // being called. + + $this->table->set_heading('name', 'color', 'size'); + + $this->assertEquals( + array( + array('data' => 'name'), + array('data' => 'color'), + array('data' => 'size') + ), + $this->table->heading + ); + } + + + /* + * @depends testPrepArgs + */ + public function test_add_row() + { + // uses _prep_args internally, so we'll just do a quick + // check to verify that func_get_args and prep_args are + // being called. + + $this->table->add_row('my', 'pony', 'sings'); + $this->table->add_row('your', 'pony', 'stinks'); + $this->table->add_row('my pony', '>', 'your pony'); + + $this->assertEquals(count($this->table->rows), 3); + + $this->assertEquals( + array( + array('data' => 'your'), + array('data' => 'pony'), + array('data' => 'stinks') + ), + $this->table->rows[1] + ); + } + + + // Uility Methods + // -------------------------------------------------------------------- + + public function test_prep_args() + { + $expected = array( + array('data' => 'name'), + array('data' => 'color'), + array('data' => 'size') + ); + + // test what would be discreet args, + // basically means a single array as the calling method + // will use func_get_args() + $this->assertEquals( + $expected, + $this->table->_prep_args(array( + 'name', 'color', 'size' + )), + 'discreet'); + + + // test what would be a single array argument. Again, nested + // due to func_get_args on calling methods + $this->assertEquals( + $expected, + $this->table->_prep_args(array( + array('name', 'color', 'size') + )), + 'array'); + + + // with cell attributes + + // need to add that new argument row to our expected outcome + $expected[] = array('data' => 'weight', 'class' => 'awesome'); + + $this->assertEquals( + $expected, + $this->table->_prep_args(array( + array('name', 'color', 'size', + array('data' => 'weight', 'class' => 'awesome') + ) + )), + 'attributes'); + } + + public function test_default_template_keys() + { + $deft_template = $this->table->_default_template(); + $keys = array( + 'table_open', + 'thead_open', 'thead_close', + 'heading_row_start', 'heading_row_end', 'heading_cell_start', 'heading_cell_end', + 'tbody_open', 'tbody_close', + 'row_start', 'row_end', 'cell_start', 'cell_end', + 'row_alt_start', 'row_alt_end', 'cell_alt_start', 'cell_alt_end', + 'table_close' + ); + + foreach ($keys as $key) + { + $this->assertArrayHasKey($key, $deft_template); + } + } + + public function test_compile_template() + { + $this->assertFalse($this->table->set_template('invalid_junk')); + + // non default key + $this->table->set_template(array('nonsense' => 'foo')); + $this->table->_compile_template(); + + $this->assertArrayHasKey('nonsense', $this->table->template); + $this->assertEquals('foo', $this->table->template['nonsense']); + + // override default + $this->table->set_template(array('table_close' => '</table junk>')); + $this->table->_compile_template(); + + $this->assertArrayHasKey('table_close', $this->table->template); + $this->assertEquals('</table junk>', $this->table->template['table_close']); + } + + public function test_make_columns() + { + // Test bogus parameters + $this->assertFalse($this->table->make_columns('invalid_junk')); + $this->assertFalse( $this->table->make_columns(array())); + // $this->assertFalse( + // $this->table->make_columns(array('one', 'two')), + // '2.5' // not an integer! + // ); + + + // Now on to the actual column creation + + $five_values = array( + 'Laura', 'Red', '15', + 'Katie', 'Blue' + ); + + // No column count - no changes to the array + $this->assertEquals( + $five_values, + $this->table->make_columns($five_values) + ); + + // Column count of 3 leaves us with one + $this->assertEquals( + array( + array('Laura', 'Red', '15'), + array('Katie', 'Blue', ' ') + ), + $this->table->make_columns($five_values, 3) + ); + + $this->markTestSkipped('Look at commented assertFalse above'); + } + + public function test_clear() + { + $this->table->set_heading('Name', 'Color', 'Size'); + + // Make columns changes auto_heading + $rows = $this->table->make_columns(array( + 'Laura', 'Red', '15', + 'Katie', 'Blue' + ), 3); + + foreach ($rows as $row) + { + $this->table->add_row($row); + } + + $this->assertFalse($this->table->auto_heading); + $this->assertEquals(count($this->table->heading), 3); + $this->assertEquals(count($this->table->rows), 2); + + $this->table->clear(); + + $this->assertTrue($this->table->auto_heading); + $this->assertEmpty($this->table->heading); + $this->assertEmpty($this->table->rows); + } + + + public function test_set_from_array() + { + $this->assertFalse($this->table->_set_from_array('bogus')); + $this->assertFalse($this->table->_set_from_array(array())); + + $data = array( + array('name', 'color', 'number'), + array('Laura', 'Red', '22'), + array('Katie', 'Blue') + ); + + $this->table->_set_from_array($data, FALSE); + $this->assertEmpty($this->table->heading); + + $this->table->clear(); + + $expected_heading = array( + array('data' => 'name'), + array('data' => 'color'), + array('data' => 'number') + ); + + $expected_second = array( + array('data' => 'Katie'), + array('data' => 'Blue'), + ); + + $this->table->_set_from_array($data); + $this->assertEquals(count($this->table->rows), 2); + + $this->assertEquals( + $expected_heading, + $this->table->heading + ); + + $this->assertEquals( + $expected_second, + $this->table->rows[1] + ); + } + + function test_set_from_object() + { + $this->markTestSkipped('Not yet implemented.'); + } + + // Test main generate method + // -------------------------------------------------------------------- +}
\ No newline at end of file diff --git a/tests/codeigniter/libraries/Typography_test.php b/tests/codeigniter/libraries/Typography_test.php new file mode 100644 index 000000000..a0533bae0 --- /dev/null +++ b/tests/codeigniter/libraries/Typography_test.php @@ -0,0 +1,191 @@ +<?php + +require BASEPATH.'libraries/Typography.php'; + +class Typography_test extends CI_TestCase +{ + + public function set_up() + { + $obj = new StdClass; + $obj->type = new CI_Typography(); + + $this->ci_instance($obj); + + $this->type = $obj->type; + } + + // -------------------------------------------------------------------- + + /** + * Tests the format_characters() function. + * + * this can and should grow. + */ + public function test_format_characters() + { + $strs = array( + '"double quotes"' => '“double quotes”', + '"testing" in "theory" that is' => '“testing” in “theory” that is', + "Here's what I'm" => 'Here’s what I’m', + '&' => '&', + '&' => '&', + ' ' => ' ', + '--' => '—', + 'foo...' => 'foo…', + 'foo..' => 'foo..', + 'foo...bar.' => 'foo…bar.', + 'test. new' => 'test. new', + ); + + foreach ($strs as $str => $expected) + { + $this->assertEquals($expected, $this->type->format_characters($str)); + } + } + + // -------------------------------------------------------------------- + + public function test_nl2br_except_pre() + { + $str = <<<EOH +Hello, I'm a happy string with some new lines. + +I like to skip. + +Jump + +and sing. + +<pre> +I am inside a pre tag. Please don't mess with me. + +k? +</pre> + +That's my story and I'm sticking to it. + +The End. +EOH; + + $expected = <<<EOH +Hello, I'm a happy string with some new lines. <br /> +<br /> +I like to skip.<br /> +<br /> +Jump<br /> +<br /> +and sing.<br /> +<br /> +<pre> +I am inside a pre tag. Please don't mess with me. + +k? +</pre><br /> +<br /> +That's my story and I'm sticking to it.<br /> +<br /> +The End. +EOH; + + $this->assertEquals($expected, + $this->type->nl2br_except_pre($str)); + } + + // -------------------------------------------------------------------- + + public function test_auto_typography() + { + $this->_blank_string(); + $this->_standardize_new_lines(); + $this->_reduce_linebreaks(); + $this->_remove_comments(); + $this->_protect_pre(); + $this->_no_opening_block(); + $this->_protect_braced_quotes(); + } + + // -------------------------------------------------------------------- + + private function _blank_string() + { + // Test blank string + $this->assertEquals('', $this->type->auto_typography('')); + } + + // -------------------------------------------------------------------- + + private function _standardize_new_lines() + { + $strs = array( + "My string\rhas return characters" => "<p>My string<br />\nhas return characters</p>", + 'This one does not!' => '<p>This one does not!</p>' + ); + + foreach ($strs as $str => $expect) + { + $this->assertEquals($expect, $this->type->auto_typography($str)); + } + } + + // -------------------------------------------------------------------- + + private function _reduce_linebreaks() + { + $str = "This has way too many linebreaks.\n\n\n\nSee?"; + $expect = "<p>This has way too many linebreaks.</p>\n\n<p>See?</p>"; + + $this->assertEquals($expect, $this->type->auto_typography($str, TRUE)); + } + + // -------------------------------------------------------------------- + + private function _remove_comments() + { + $str = '<!-- I can haz comments? --> But no!'; + $expect = '<p><!-- I can haz comments? --> But no!</p>'; + + $this->assertEquals($expect, $this->type->auto_typography($str)); + } + + // -------------------------------------------------------------------- + + private function _protect_pre() + { + $str = '<p>My Sentence</p><pre>var_dump($this);</pre>'; + $expect = '<p>My Sentence</p><pre>var_dump($this);</pre>'; + + $this->assertEquals($expect, $this->type->auto_typography($str)); + } + + // -------------------------------------------------------------------- + + private function _no_opening_block() + { + $str = 'My Sentence<pre>var_dump($this);</pre>'; + $expect = '<p>My Sentence</p><pre>var_dump($this);</pre>'; + + $this->assertEquals($expect, $this->type->auto_typography($str)); + } + + // -------------------------------------------------------------------- + + public function _protect_braced_quotes() + { + $this->type->protect_braced_quotes = TRUE; + + $str = 'Test {parse="foobar"}'; + $expect = '<p>Test {parse="foobar"}</p>'; + + $this->assertEquals($expect, $this->type->auto_typography($str)); + + $this->type->protect_braced_quotes = FALSE; + + $str = 'Test {parse="foobar"}'; + $expect = '<p>Test {parse=“foobar”}</p>'; + + $this->assertEquals($expect, $this->type->auto_typography($str)); + + + } +}
\ No newline at end of file diff --git a/tests/codeigniter/libraries/User_agent_test.php b/tests/codeigniter/libraries/User_agent_test.php new file mode 100644 index 000000000..277c12ed0 --- /dev/null +++ b/tests/codeigniter/libraries/User_agent_test.php @@ -0,0 +1,91 @@ +<?php + +require BASEPATH.'libraries/User_agent.php'; + +// This class needs some work... + +class UserAgent_test extends CI_TestCase +{ + protected $_user_agent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-us) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27'; + protected $_mobile_ua = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8B117 Safari/6531.22.7'; + + public function set_up() + { + // set a baseline user agent + $_SERVER['HTTP_USER_AGENT'] = $this->_user_agent; + + $obj = new StdClass; + $obj->agent = new CI_User_agent(); + + $this->ci_instance($obj); + + $this->agent = $obj->agent; + } + + // -------------------------------------------------------------------- + + public function test_accept_lang() + { + $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en'; + + $this->assertEquals('en', $this->agent->accept_lang()); + + unset($_SERVER['HTTP_ACCEPT_LANGUAGE']); + } + + // -------------------------------------------------------------------- + + public function test_mobile() + { + // Mobile Not Set + $_SERVER['HTTP_USER_AGENT'] = $this->_mobile_ua; + $this->assertEquals('', $this->agent->mobile()); + unset($_SERVER['HTTP_USER_AGENT']); + } + + // -------------------------------------------------------------------- + + public function test_util_is_functions() + { + $this->assertTrue($this->agent->is_browser()); + $this->assertFalse($this->agent->is_robot()); + $this->assertFalse($this->agent->is_mobile()); + $this->assertFalse($this->agent->is_referral()); + } + + // -------------------------------------------------------------------- + + public function test_agent_string() + { + $this->assertEquals($this->_user_agent, $this->agent->agent_string()); + } + + // -------------------------------------------------------------------- + + public function test_browser_info() + { + $this->assertEquals('Mac OS X', $this->agent->platform()); + $this->assertEquals('Safari', $this->agent->browser()); + $this->assertEquals('533.20.27', $this->agent->version()); + $this->assertEquals('', $this->agent->robot()); + $this->assertEquals('', $this->agent->referrer()); + } + + // -------------------------------------------------------------------- + + public function test_charsets() + { + $_SERVER['HTTP_ACCEPT_CHARSET'] = 'utf8'; + + $charsets = $this->agent->charsets(); + + $this->assertEquals('utf8', $charsets[0]); + + unset($_SERVER['HTTP_ACCEPT_CHARSET']); + + $this->assertFalse($this->agent->accept_charset()); + } + + // -------------------------------------------------------------------- + +}
\ No newline at end of file diff --git a/tests/lib/ci_testcase.php b/tests/lib/ci_testcase.php new file mode 100644 index 000000000..8ca71fdf2 --- /dev/null +++ b/tests/lib/ci_testcase.php @@ -0,0 +1,177 @@ +<?php + + +// Need a way to change dependencies (core libs and laoded libs) +// Need a way to set the CI class + +class CI_TestCase extends PHPUnit_Framework_TestCase { + + protected $ci_config; + protected $ci_instance; + protected static $ci_test_instance; + + private $global_map = array( + 'benchmark' => 'bm', + 'config' => 'cfg', + 'hooks' => 'ext', + 'utf8' => 'uni', + 'router' => 'rtr', + 'output' => 'out', + 'security' => 'sec', + 'input' => 'in', + 'lang' => 'lang', + // @todo the loader is an edge case + 'loader' => 'load', + 'model' => 'model' + ); + + // -------------------------------------------------------------------- + + public function __construct() + { + parent::__construct(); + + $this->ci_config = array(); + } + + // -------------------------------------------------------------------- + + public function setUp() + { + if (method_exists($this, 'set_up')) + { + $this->set_up(); + } + } + + // -------------------------------------------------------------------- + + public function tearDown() + { + if (method_exists($this, 'tear_down')) + { + $this->tear_down(); + } + } + + // -------------------------------------------------------------------- + + function ci_set_config($key, $val = '') + { + if (is_array($key)) + { + $this->ci_config = $key; + } + else + { + $this->ci_config[$key] = $val; + } + } + + // -------------------------------------------------------------------- + + function ci_instance($obj = FALSE) + { + if ( ! is_object($obj)) + { + return $this->ci_instance; + } + + $this->ci_instance = $obj; + } + + // -------------------------------------------------------------------- + + function ci_instance_var($name, $obj = FALSE) + { + if ( ! is_object($obj)) + { + return $this->ci_instance->$name; + } + + $this->ci_instance->$name =& $obj; + } + + // -------------------------------------------------------------------- + + /** + * Grab a core class + * + * Loads the correct core class without extensions + * and returns a reference to the class name in the + * globals array with the correct key. This way the + * test can modify the variable it assigns to and + * still maintain the global. + */ + function &ci_core_class($name) + { + $name = strtolower($name); + + if (isset($this->global_map[$name])) + { + $class_name = ucfirst($name); + $global_name = $this->global_map[$name]; + } + elseif (in_array($name, $this->global_map)) + { + $class_name = ucfirst(array_search($name, $this->global_map)); + $global_name = $name; + } + else + { + throw new Exception('Not a valid core class.'); + } + + if ( ! class_exists('CI_'.$class_name)) + { + require_once BASEPATH.'core/'.$class_name.'.php'; + } + + $GLOBALS[strtoupper($global_name)] = 'CI_'.$class_name; + return $GLOBALS[strtoupper($global_name)]; + } + + // -------------------------------------------------------------------- + + // convenience function for global mocks + function ci_set_core_class($name, $obj) + { + $orig =& $this->ci_core_class($name); + $orig = $obj; + } + + // -------------------------------------------------------------------- + // Internals + // -------------------------------------------------------------------- + + /** + * Overwrite runBare + * + * PHPUnit instantiates the test classes before + * running them individually. So right before a test + * runs we set our instance. Normally this step would + * happen in setUp, but someone is bound to forget to + * call the parent method and debugging this is no fun. + */ + public function runBare() + { + self::$ci_test_instance = $this; + parent::runBare(); + } + + // -------------------------------------------------------------------- + + public static function instance() + { + return self::$ci_test_instance; + } + + // -------------------------------------------------------------------- + + function ci_get_config() + { + return $this->ci_config; + } +} + +// EOF
\ No newline at end of file diff --git a/tests/lib/common.php b/tests/lib/common.php new file mode 100644 index 000000000..6d29eb0d6 --- /dev/null +++ b/tests/lib/common.php @@ -0,0 +1,132 @@ +<?php + +// Set up the global CI functions in their most minimal core representation + +function &get_instance() +{ + $test = CI_TestCase::instance(); + $instance = $test->ci_instance(); + return $instance; +} + +// -------------------------------------------------------------------- + +function &get_config() { + $test = CI_TestCase::instance(); + $config = $test->ci_get_config(); + + return $config; +} + +function config_item($item) +{ + $config =& get_config(); + + if ( ! isset($config[$item])) + { + return FALSE; + } + + return $config[$item]; +} + +// -------------------------------------------------------------------- + +function load_class($class, $directory = 'libraries', $prefix = 'CI_') +{ + if ($directory != 'core' OR $prefix != 'CI_') + { + throw new Exception('Not Implemented: Non-core load_class()'); + } + + $test = CI_TestCase::instance(); + + $obj =& $test->ci_core_class($class); + + if (is_string($obj)) + { + throw new Exception('Bad Isolation: Use ci_set_core_class to set '.$class.''); + } + + return $obj; +} + +// This is sort of meh. Should probably be mocked up with +// controllable output, so that we can test some of our +// security code. The function itself will be tested in the +// bootstrap testsuite. +// -------------------------------------------------------------------- + +function remove_invisible_characters($str, $url_encoded = TRUE) +{ + $non_displayables = array(); + + // every control character except newline (dec 10) + // carriage return (dec 13), and horizontal tab (dec 09) + + if ($url_encoded) + { + $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15 + $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31 + } + + $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127 + + do + { + $str = preg_replace($non_displayables, '', $str, -1, $count); + } + while ($count); + + return $str; +} + + +// Clean up error messages +// -------------------------------------------------------------------- + +function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered') +{ + throw new Exception('CI Error: '.$message); +} + +function show_404($page = '', $log_error = TRUE) +{ + throw new Exception('CI Error: 404'); +} + +function _exception_handler($severity, $message, $filepath, $line) +{ + throw new Exception('CI Exception: '.$message.' | '.$filepath.' | '.$line); +} + + +// We assume a few things about our environment ... +// -------------------------------------------------------------------- + +function is_php($version = '5.0.0') +{ + return ! (version_compare(PHP_VERSION, $version) < 0); +} + +function is_really_writable($file) +{ + return is_writable($file); +} + +function is_loaded() +{ + throw new Exception('Bad Isolation: mock up environment'); +} + +function log_message($level = 'error', $message, $php_error = FALSE) +{ + return TRUE; +} + +function set_status_header($code = 200, $text = '') +{ + return TRUE; +} + +// EOF
\ No newline at end of file diff --git a/tests/phpunit.xml b/tests/phpunit.xml new file mode 100644 index 000000000..e07aa96a7 --- /dev/null +++ b/tests/phpunit.xml @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<phpunit + bootstrap="./Bootstrap.php" + colors="true"> + <testsuites> + <testsuite name="CodeIgniter Core Test Suite"> + <file>codeigniter/Setup_test.php</file> + <directory suffix="test.php">codeigniter/core</directory> + <directory suffix="test.php">codeigniter/helpers</directory> + <directory suffix="test.php">codeigniter/libraries</directory> + <!-- We'll worry about these later ... + <directory suffix="test.php">codeigniter/libraries</directory> + <directory suffix="test.php">codeigniter/helpers</directory> + --> + + </testsuite> + </testsuites> +</phpunit>
\ No newline at end of file diff --git a/tests/readme.txt b/tests/readme.txt new file mode 100644 index 000000000..9a299544a --- /dev/null +++ b/tests/readme.txt @@ -0,0 +1,137 @@ +# Do not merge to default until this is blank # + +- Clean up naming conventions +- Figure out config stuff +- Figure out database testing + + + +# -------------- CodeIgniter Testing (4/20/2011) -------------- # + + +# Introduction: + +This is the preliminary CodeIgniter testing documentation. It +will cover both internal as well as external APIs and the reasoning +behind their implemenation, where appropriate. As with all CodeIgniter +documentation, this file should maintain a mostly human readable +format to facilitate clean api design. [see http://arrenbrecht.ch/testing/] + +*FIRST PUBLIC DRAFT: EVERYTHING IS SUBJECT TO CHANGE* + +# Requirements + +1. PHP Unit >= 3.5.6 + - pear channel-discover pear.phpunit.de + - pear install phpunit/PHPUnit + +2. vfsStream + - pear channel-discover pear.php-tools.net + - pear install pat/vfsStream-alpha + + +# Test Suites: + +CodeIgniter bootstraps a request very directly, with very flat class +hierarchy. As a result, there is no main CodeIgniter class until the +controller is instantiated. + +This has forced the core classes to be relatively decoupled, which is +a good thing. However, it makes that portion of code relatively hard +to test. + +Right now that means we'll probably have two core test suites, along +with a base for application and package tests. That gives us: + +1. Bootstrap Test - test common.php and sanity check codeigniter.php [in planning] +2. System Test - test core components in relative isolation [in development] +3. Application Test - bootstrapping for application/tests [not started] +4. Package Test - bootstrapping for <package>/tests [not started] + + +## 1. Bootstrap Test + +Testing common.php should be pretty simple. Include the file, and test the +functions. May require some tweaking so that we can grab the statics from all +methods (see is_loaded()). Testing the actual CodeIgniter.php file will most +likely be an output test for the default view, with some object checking after +the file runs. Needs consideration. + + +## 2. System Test + +Testing the core system relies on being able to isolate the core components +as much as possible. A few of them access other core classes as globals. These +should be mocked up and easy to manipulate. + +All functions in common.php should be a minimal implementation, or and mapped +to a method in the test's parent class to gives us full control of their output. + + +### CI_TestCase Documentation + + +Test cases should extend CI_TestCase. This internally extends +PHPUnit_Framework_TestCase, so you have access to all of your +usual PHPUnit methods. + +We need to provide a simple way to modify the globals and the +common function output. We also need to be able to mock up +the super object as we please. + +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. + +$this->ci_instance($obj) + set the object to use as the "super object", in a lot + of cases this will be a simple stdClass with the attributes + you need it to have. If no parameter, will return the instance. + +$this->ci_instance_var($name, $val) + add an attribute to the super object. This is useful if you + set up a simple instance in setUp and then need to add different + class mockups to your super object. + +$this->ci_core_class($name) + Get the _class name_ of a core class, so that you can instantiate + it. The variable is returned by reference and is tied to the correct + $GLOBALS key. For example: + $cfg =& $this->ci_core_class('cfg'); // returns 'CI_Config' + $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_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. + +CI_TestCase::instance() __internal__ + Returns an instance of the current test case. We force phpunit to + run with backup-globals enabled, so this will always be the instance + of the currently running test class. + +## 3. Application Test: + +Not sure yet, needs to handle: +- Libraries +- Helpers +- Models +- MY_* files +- Controllers (uh...?) +- Views? (watir, selenium, cucumber?) + +- Database Testing + + +## 4. Package Test: + +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)
\ No newline at end of file |