Permalink
Browse files

Integrated vfsStream better and made paths constants VFS-based

Signed-off-by: dchill42 <dchill42@gmail.com>
  • Loading branch information...
1 parent 2716398 commit 7ecc5cda6647a4b316b44dc40d5925d9ef63c908 @dchill42 committed Oct 12, 2012
View
@@ -8,10 +8,7 @@
// 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 @@ class_alias('org\bovigo\vfs\vfsStreamDirectory', 'vfsStreamDirectory');
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');
@@ -17,6 +17,7 @@ public function set_up()
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 @@ public function test_load()
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'));
}
@@ -9,11 +9,11 @@ public function set_up()
// 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 @@ public function set_up()
*/
public function test_library()
{
- $this->_setup_config_mock();
-
// Create libraries directory with test library
$lib = 'unit_test_lib';
$class = 'CI_'.ucfirst($lib);
- $content = '<?php class '.$class.' { } ';
- $this->_create_content('libraries', $lib, $content, NULL, TRUE);
+ $this->ci_vfs_create($lib, '<?php class '.$class.' { }', $this->ci_base_root, 'libraries');
// Test loading as an array.
$this->assertNull($this->load->library(array($lib)));
@@ -50,21 +47,19 @@ public function test_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 = '<?php class '.$class.' { public function __construct($params) { $this->config = $params; } } ';
- $this->_create_content('libraries', $lib, $content, NULL, TRUE);
+ $content = '<?php class '.$class.' { public function __construct($params) { $this->config = $params; } }';
+ $this->ci_vfs_create($lib, $content, $this->ci_base_root, 'libraries');
// Create config file
$cfg = array(
'foo' => 'bar',
'bar' => 'baz',
'baz' => false
);
- $this->_create_content('config', $lib, '<?php $config = '.var_export($cfg, TRUE).';');
+ $this->ci_vfs_create($lib, '<?php $config = '.var_export($cfg, TRUE).';', $this->ci_app_root, 'config');
// Test object name and config
$obj = 'testy';
@@ -81,13 +76,10 @@ public function test_library_config()
*/
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 = '<?php class '.$class.' {} ';
- $this->_create_content('libraries', $lib, $content);
+ $this->ci_vfs_create($lib, '<?php class '.$class.' { }', $this->ci_app_root, 'libraries');
// Load library
$this->assertNull($this->load->library($lib));
@@ -104,14 +96,12 @@ public function test_load_library_in_application_dir()
*/
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 = '<?php class '.$class.' { } ';
- $this->_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 @@ public function test_models()
$model = 'unit_test_model';
$class = ucfirst($model);
$content = '<?php class '.$class.' extends CI_Model {} ';
- $this->_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 @@ public function test_load_view()
// Create views directory with test view
$view = 'unit_test_view';
$content = 'This is my test page. <?php echo $hello; ?>';
- $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 @@ public function test_file()
$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 @@ public function test_helper()
$helper = 'test';
$func = '_my_helper_test_func';
$content = '<?php function '.$func.'() { return true; } ';
- $this->_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 @@ public function test_loading_multiple_helpers()
$funcs[] = $func;
$files[$helper.'_helper'] = '<?php function '.$func.'() { return true; } ';
}
- $this->_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 @@ public function test_loading_multiple_helpers()
*/
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 = '<?php class '.$class.' { } ';
- $this->_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 @@ public function test_packages()
$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 @@ public function test_packages()
*/
public function test_load_config()
{
- $this->_setup_config_mock();
$this->assertNull($this->load->config('config', FALSE));
}
@@ -373,35 +360,29 @@ public function test_load_config()
*/
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', '<?php function '.$hlp_func.'() { return true; } ');
+ $content = '<?php function '.$hlp_func.'() { return true; } ';
+ $this->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, '<?php class '.$lib_class.' { } ', NULL, TRUE);
+ $this->ci_vfs_create($lib, '<?php class '.$lib_class.' { }', $this->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' => '<?php class '.$drv_class.' { } ')
- );
- vfsStream::create($tree, $this->load->base_root->getChild('libraries'));
+ $this->ci_vfs_create($drv, '<?php class '.$drv_class.' { }', $this->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, '<?php class '.$mod_class.' { } ', 'models');
+ $this->ci_vfs_create($model, '<?php class '.$mod_class.' { }', $this->ci_app_root, $dir.'/models');
// Create autoloader config
$cfg = array(
@@ -412,7 +393,7 @@ public function test_autoloader()
'model' => array($model),
'config' => array()
);
- $this->_create_content('config', 'autoload', '<?php $autoload = '.var_export($cfg, TRUE).';');
+ $this->ci_vfs_create('autoload', '<?php $autoload = '.var_export($cfg, TRUE).';', $this->ci_app_root, 'config');
// Run autoloader
$this->load->autoload();
@@ -436,55 +417,4 @@ public function test_autoloader()
$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);
- }
-
-}
+}
@@ -168,6 +168,8 @@ public function test_standard_date_w3c()
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);
@@ -1,10 +1,12 @@
<?php
-require BASEPATH . 'core/Common.php';
-require BASEPATH . 'helpers/form_helper.php';
-
class Form_helper_test extends CI_TestCase
{
+ public function set_up()
+ {
+ $this->helper('form');
+ }
+
public function test_form_hidden()
{
$expected = <<<EOH
@@ -11,31 +11,18 @@ public function set_up()
// Mock away load, too much going on in there,
// we'll just check for the expected parameter
-
$lang = $this->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()
@@ -64,6 +64,7 @@ public function test_entities_to_ascii()
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('á é í ó ú ñ ü'));
}
Oops, something went wrong.

0 comments on commit 7ecc5cd

Please sign in to comment.