Permalink
Browse files

Raised CI_Config test coverage to 100%

Signed-off-by: dchill42 <dchill42@gmail.com>
  • Loading branch information...
1 parent 9e4ebf1 commit 3a43f130a02be38b7244ffe4507c6cefd874f285 @dchill42 committed Oct 21, 2012
Showing with 158 additions and 39 deletions.
  1. +158 −39 tests/codeigniter/core/Config_test.php
@@ -7,11 +7,12 @@ public function set_up()
$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 @@ public function set_up()
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 @@ 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($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 @@ public function test_site_url()
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, '<?php $config = '.var_export($cfg, TRUE).';', $this->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, '<?php $config = '.var_export($cfg, TRUE).';', $this->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 => '<?php $config = '.var_export($cfg1, TRUE).';',
- $file2.'.php' => '<?php $config = '.var_export($cfg2, TRUE).';'
- ), '', $this->ci_app_root, 'config');
+ $this->ci_vfs_create($file.'.php', '<?php $config = '.var_export($cfg, TRUE).';', $this->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', '<?php $config = '.var_export($cfg2, TRUE).';', $this->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));
}
}

0 comments on commit 3a43f13

Please sign in to comment.