Commits

Greg Aker committed b2152a9

Working on tests in the loader. Have generic model mocking working with vfsStream.

Comments (0)

Files changed (2)

tests/codeigniter/core/Loader_test.php

 <?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 setUp()
 	{
 		// Instantiate a new loader
-		$cls = $this->ci_core_class('load');
-		$this->_loader = new $cls;
+		$this->load = new Extended_Loader();
 		
 		// mock up a ci instance
 		$this->ci_obj = new StdClass;
 		$this->ci_instance_var('config', $config);
 		
 		// Test loading as an array.
-		$this->assertEquals(NULL, $this->_loader->library(array('table')));
+		$this->assertEquals(NULL, $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->_loader->library());
+		$this->assertEquals(FALSE, $this->load->library());
 		
 		// Test a string given to params
-		$this->assertEquals(NULL, $this->_loader->library('table', ' '));
+		$this->assertEquals(NULL, $this->load->library('table', ' '));
 	}	
 	
 	// --------------------------------------------------------------------
 	
-	public function testModels()
+	public function testNonExistentModel()
 	{
-		// Test loading as an array.
-		$this->assertEquals(NULL, $this->_loader->model(array('foobar')));
-		
-		// Test no model given
-		$this->assertEquals(FALSE, $this->_loader->model(''));
-		
-		// Test a string given to params
-		$this->assertEquals(NULL, $this->_loader->model('foobar', ' '));		
+		$this->setExpectedException(
+			'Exception',
+			'CI Error: Unable to locate the model you have specified: ci_test_nonexistent_mode.php'
+			);
+			
+		$this->load->model('ci_test_nonexistent_mode.php');
 	}
 
 	// --------------------------------------------------------------------
 	
-	public function testDatabase()
+	public function testModels()
 	{
-		$this->assertEquals(NULL, $this->_loader->database());
-		$this->assertEquals(NULL, $this->_loader->dbutil());		
+		$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'));
+		
+		// Test no model given
+		$this->assertEquals(FALSE, $this->load->model(''));
+		
+		// Test a string given to params
+		// $this->assertEquals(NULL, $this->load->model('foobar', ' '));		
 	}
 
 	// --------------------------------------------------------------------
 	
-	public function testView()
+	// public function testDatabase()
+	// {
+	// 	$this->assertEquals(NULL, $this->load->database());
+	// 	$this->assertEquals(NULL, $this->load->dbutil());		
+	// }
+
+	// --------------------------------------------------------------------
+	
+	public function testNonExistentView()
 	{
-		// I'm not entirely sure this is the proper way to handle this.
-		// So, let's revist it, m'kay?
-		try 
-		{
-			 $this->_loader->view('foo');
-		}
-		catch (Exception $expected)
-		{
-			return;
-		}
+		$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 testFile()
 	{
 		// I'm not entirely sure this is the proper way to handle this.
-		// So, let's revist it, m'kay?
 		try 
 		{
-			 $this->_loader->file('foo');
+			 $this->load->file('foo');
 		}
 		catch (Exception $expected)
 		{
 			'foo'	=> 'bar'
 		);
 		
-		$this->assertEquals(NULL, $this->_loader->vars($vars));
-		$this->assertEquals(NULL, $this->_loader->vars('foo', 'bar'));
+		$this->assertEquals(NULL, $this->load->vars($vars));
+		$this->assertEquals(NULL, $this->load->vars('foo', 'bar'));
 	}
 
 	// --------------------------------------------------------------------
 	
-	public function testHelper()
-	{
-		$this->assertEquals(NULL, $this->_loader->helper('array'));
-		$this->assertEquals(NULL, $this->_loader->helper('bad'));
-	}
+	// public function testHelper()
+	// {
+	// 	$this->assertEquals(NULL, $this->load->helper('array'));
+	// 	$this->assertEquals(NULL, $this->load->helper('bad'));
+	// }
 	
 	// --------------------------------------------------------------------
 
 	public function testHelpers()
 	{
-		$this->assertEquals(NULL, $this->_loader->helpers(array('file', 'array', 'string')));
+		$this->assertEquals(NULL, $this->load->helpers(array('file', 'array', 'string')));
 	}
 	
 	// --------------------------------------------------------------------
 	
 	// public function testLanguage()
 	// {
-	// 	$this->assertEquals(NULL, $this->_loader->language('test'));
+	// 	$this->assertEquals(NULL, $this->load->language('test'));
 	// }	
 
 	// --------------------------------------------------------------------
 
-	public function testLoadConfig()
-	{
-		$this->assertEquals(NULL, $this->_loader->config('config', FALSE, TRUE));
-	}
-	
-	
-	
-}
+	// public function testLoadConfig()
+	// {
+	// 	$this->assertEquals(NULL, $this->load->config('config', FALSE, TRUE));
+	// }
+}
+
+
+
+

tests/lib/ci_testcase.php

 		'security'	=> 'sec',
 		'input'		=> 'in',
 		'lang'		=> 'lang',
-		
 		// @todo the loader is an edge case
-		'loader'	=> 'load'
+		'loader'	=> 'load',
+		'model'		=> 'model'
 	);
 	
 	public function __construct()