summaryrefslogtreecommitdiffstats
path: root/tests/codeigniter/core/Loader_test.php
blob: 1e32c7e0aad3d43fab1cc54605f511e1023b25ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?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
		$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 testLibrary()
	{
		$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 testLoadLibraryInApplicationDir()
	{
		$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 testNonExistentModel()
	{
		$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 testModels()
	{
		$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 testLoadView()
	{
		$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 testNonExistentView()
	{
		$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.
		// $this->load->file('foo');
		
	}

	// --------------------------------------------------------------------
	
	public function testVars()
	{
		$vars = array(
			'foo'	=> 'bar'
		);
		
		$this->assertEquals(NULL, $this->load->vars($vars));
		$this->assertEquals(NULL, $this->load->vars('foo', 'bar'));
	}

	// --------------------------------------------------------------------
	
	public function testHelper()
	{
		$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 testLoadingMultipleHelpers()
	{
		$this->assertEquals(NULL, $this->load->helpers(array('file', 'array', 'string')));
	}
	
	// --------------------------------------------------------------------
	
	// public function testLanguage()
	// {
	// 	$this->assertEquals(NULL, $this->load->language('test'));
	// }	

	// --------------------------------------------------------------------

	// public function testLoadConfig()
	// {
	// 	$this->assertEquals(NULL, $this->load->config('config', FALSE, TRUE));
	// }
}