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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
|
<?php
class Loader_test extends CI_TestCase {
private $ci_obj;
public function set_up()
{
// Instantiate a new loader
$loader = $this->ci_core_class('loader');
$this->load = new $loader();
// Get CI instance
$this->ci_obj = $this->ci_instance();
// Set subclass prefix
$this->prefix = 'MY_';
$this->ci_set_config('subclass_prefix', $this->prefix);
}
// --------------------------------------------------------------------
public function test_library()
{
// Create library in VFS
$lib = 'unit_test_lib';
$class = 'CI_'.ucfirst($lib);
$this->ci_vfs_create($lib, '<?php class '.$class.' { }', $this->ci_base_root, 'libraries');
// Test is_loaded fail
$this->assertFalse($this->load->is_loaded($lib));
// Test loading as an array.
$this->assertNull($this->load->library(array($lib)));
$this->assertTrue(class_exists($class), $class.' does not exist');
$this->assertAttributeInstanceOf($class, $lib, $this->ci_obj);
// Test no lib given
$this->assertFalse($this->load->library());
// Test a string given to params
$this->assertNull($this->load->library($lib, ' '));
// Create library w/o class
$lib = 'bad_test_lib';
$this->ci_vfs_create($lib, '', $this->ci_base_root, 'libraries');
// Test non-existent class
$this->setExpectedException(
'RuntimeException',
'CI Error: Non-existent class: '.$lib
);
$this->assertNull($this->load->library($lib));
}
// --------------------------------------------------------------------
public function test_library_extension()
{
// Create library and extension in VFS
$name = 'ext_test_lib';
$lib = ucfirst($name);
$class = 'CI_'.$lib;
$ext = $this->prefix.$lib;
$this->ci_vfs_create($lib, '<?php class '.$class.' { }', $this->ci_base_root, 'libraries');
$this->ci_vfs_create($ext, '<?php class '.$ext.' extends '.$class.' { }', $this->ci_app_root, 'libraries');
// Test loading with extension
$this->assertNull($this->load->library($lib));
$this->assertTrue(class_exists($class), $class.' does not exist');
$this->assertTrue(class_exists($ext), $ext.' does not exist');
$this->assertAttributeInstanceOf($class, $name, $this->ci_obj);
$this->assertAttributeInstanceOf($ext, $name, $this->ci_obj);
// Test reloading with object name
$obj = 'exttest';
$this->assertNull($this->load->library($lib, NULL, $obj));
$this->assertAttributeInstanceOf($class, $obj, $this->ci_obj);
$this->assertAttributeInstanceOf($ext, $obj, $this->ci_obj);
// Test reloading
unset($this->ci_obj->$name);
$this->assertNull($this->load->library($lib));
$this->assertObjectNotHasAttribute($name, $this->ci_obj);
// Create baseless library
$name = 'ext_baseless_lib';
$lib = ucfirst($name);
$class = $this->prefix.$lib;
$this->ci_vfs_create($class, '<?php class '.$class.' { }', $this->ci_app_root, 'libraries');
// Test missing base class
$this->setExpectedException(
'RuntimeException',
'CI Error: Unable to load the requested class: '.$lib
);
$this->assertNull($this->load->library($lib));
}
// --------------------------------------------------------------------
public function test_library_config()
{
// Create library in VFS
$lib = 'unit_test_config_lib';
$class = 'CI_'.ucfirst($lib);
$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->ci_vfs_create($lib, '<?php $config = '.var_export($cfg, TRUE).';', $this->ci_app_root, 'config');
// Test object name and config
$obj = 'testy';
$this->assertNull($this->load->library($lib, NULL, $obj));
$this->assertTrue(class_exists($class), $class.' does not exist');
$this->assertAttributeInstanceOf($class, $obj, $this->ci_obj);
$this->assertEquals($cfg, $this->ci_obj->$obj->config);
// Test is_loaded
$this->assertEquals($obj, $this->load->is_loaded($lib));
}
// --------------------------------------------------------------------
public function test_load_library_in_application_dir()
{
// Create library in VFS
$lib = 'super_test_library';
$class = ucfirst($lib);
$this->ci_vfs_create($lib, '<?php class '.$class.' { }', $this->ci_app_root, 'libraries');
// Load library
$this->assertNull($this->load->library($lib));
// Was the model class instantiated.
$this->assertTrue(class_exists($class), $class.' does not exist');
$this->assertAttributeInstanceOf($class, $lib, $this->ci_obj);
}
// --------------------------------------------------------------------
public function test_driver()
{
// Create driver in VFS
$driver = 'unit_test_driver';
$dir = ucfirst($driver);
$class = 'CI_'.$dir;
$content = '<?php class '.$class.' { } ';
$this->ci_vfs_create($driver, $content, $this->ci_base_root, 'libraries/'.$dir);
// Test loading as an array.
$this->assertNull($this->load->driver(array($driver)));
$this->assertTrue(class_exists($class), $class.' does not exist');
$this->assertAttributeInstanceOf($class, $driver, $this->ci_obj);
// Test loading as a library with a name
$obj = 'testdrive';
$this->assertNull($this->load->library($driver, NULL, $obj));
$this->assertAttributeInstanceOf($class, $obj, $this->ci_obj);
// Test no driver given
$this->assertFalse($this->load->driver());
// Test a string given to params
$this->assertNull($this->load->driver($driver, ' '));
}
// --------------------------------------------------------------------
public function test_models()
{
$this->ci_set_core_class('model', 'CI_Model');
// Create model in VFS
$model = 'unit_test_model';
$class = ucfirst($model);
$content = '<?php class '.$class.' extends CI_Model {} ';
$this->ci_vfs_create($model, $content, $this->ci_app_root, 'models');
// Load model
$this->assertNull($this->load->model($model));
// Was the model class instantiated.
$this->assertTrue(class_exists($class));
// Test no model given
$this->assertNull($this->load->model(''));
}
// --------------------------------------------------------------------
public function test_model_subdir()
{
// Make sure base class is loaded - we'll test _ci_include later
$this->ci_core_class('model');
// Create modelin VFS
$model = 'test_sub_model';
$base = 'CI_Model';
$class = ucfirst($model);
$subdir = 'cars';
$this->ci_vfs_create($model, '<?php class '.$class.' extends '.$base.' { }', $this->ci_app_root,
array('models', $subdir));
// Load model
$name = 'testors';
$this->assertNull($this->load->model($subdir.'/'.$model, $name));
// Was the model class instantiated?
$this->assertTrue(class_exists($class));
$this->assertObjectHasAttribute($name, $this->ci_obj);
$this->assertAttributeInstanceOf($base, $name, $this->ci_obj);
$this->assertAttributeInstanceOf($class, $name, $this->ci_obj);
// Test name conflict
$obj = 'conflict';
$this->ci_obj->$obj = new StdClass();
$this->setExpectedException(
'RuntimeException',
'CI Error: The model name you are loading is the name of a resource that is already being used: '.$obj
);
$this->load->model('not_real', $obj);
}
// --------------------------------------------------------------------
public function test_non_existent_model()
{
$this->setExpectedException(
'RuntimeException',
'CI Error: Unable to locate the model you have specified: ci_test_nonexistent_model.php'
);
$this->load->model('ci_test_nonexistent_model.php');
}
// --------------------------------------------------------------------
// public function testDatabase()
// {
// $this->assertNull($this->load->database());
// $this->assertNull($this->load->dbutil());
// }
// --------------------------------------------------------------------
public function test_load_view()
{
// Create view in VFS
$view = 'unit_test_view';
$var = 'hello';
$value = 'World!';
$content = 'This is my test page. ';
$this->ci_vfs_create($view, $content.'<?php echo $'.$var.';', $this->ci_app_root, 'views');
// Test returning view
$out = $this->load->view($view, array($var => $value), TRUE);
$this->assertEquals($content.$value, $out);
// Mock output class
$output = $this->getMock('CI_Output', array('append_output'));
$output->expects($this->once())->method('append_output')->with($content.$value);
$this->ci_instance_var('output', $output);
// Test view output
$this->assertNull($this->load->view($view, array($var => $value)));
}
// --------------------------------------------------------------------
public function test_non_existent_view()
{
$this->setExpectedException(
'RuntimeException',
'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()
{
// Create view in VFS
$dir = 'views';
$file = 'ci_test_mock_file';
$content = 'Here is a test file, which we will load now.';
$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(APPPATH.$dir.'/'.$file.'.php', TRUE);
$this->assertEquals($content, $out);
// Test non-existent file
$this->setExpectedException(
'RuntimeException',
'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()
{
$key1 = 'foo';
$val1 = 'bar';
$key2 = 'boo';
$val2 = 'hoo';
$this->assertNull($this->load->vars(array($key1 => $val1)));
$this->assertNull($this->load->vars($key2, $val2));
$this->assertEquals($val1, $this->load->get_var($key1));
$this->assertEquals(array($key1 => $val1, $key2 => $val2), $this->load->get_vars());
}
// --------------------------------------------------------------------
public function test_helper()
{
// Create helper in VFS
$helper = 'test';
$func = '_my_helper_test_func';
$content = '<?php function '.$func.'() { return true; } ';
$this->ci_vfs_create($helper.'_helper', $content, $this->ci_base_root, 'helpers');
// Create helper extension
$exfunc = '_my_extension_func';
$content = '<?php function '.$exfunc.'() { return true; } ';
$this->ci_vfs_create($this->prefix.$helper.'_helper', $content, $this->ci_app_root, 'helpers');
// Load helper
$this->assertNull($this->load->helper($helper));
$this->assertTrue(function_exists($func), $func.' does not exist');
$this->assertTrue(function_exists($exfunc), $exfunc.' does not exist');
// Create baseless extension
$ext = 'bad_ext';
$this->ci_vfs_create($this->prefix.$ext.'_helper', '', $this->ci_app_root, 'helpers');
// Test bad extension
$this->setExpectedException(
'RuntimeException',
'CI Error: Unable to load the requested file: helpers/'.$ext.'_helper.php'
);
$this->load->helper($ext);
}
// --------------------------------------------------------------------
public function test_non_existent_helper()
{
$this->setExpectedException(
'RuntimeException',
'CI Error: Unable to load the requested file: helpers/bad_helper.php'
);
$this->load->helper('bad');
}
// --------------------------------------------------------------------
public function test_loading_multiple_helpers()
{
// Create helpers in VFS
$helpers = array();
$funcs = array();
$files = array();
for ($i = 1; $i <= 3; ++$i) {
$helper = 'test'.$i;
$helpers[] = $helper;
$func = '_my_helper_test_func'.$i;
$funcs[] = $func;
$files[$helper.'_helper'] = '<?php function '.$func.'() { return true; } ';
}
$this->ci_vfs_create($files, NULL, $this->ci_base_root, 'helpers');
// Load helpers
$this->assertNull($this->load->helpers($helpers));
// Verify helper existence
foreach ($funcs as $func) {
$this->assertTrue(function_exists($func), $func.' does not exist');
}
}
// --------------------------------------------------------------------
public function test_language()
{
// Mock lang class and test load call
$file = 'test';
$lang = $this->getMock('CI_Lang', array('load'));
$lang->expects($this->once())->method('load')->with($file);
$this->ci_instance_var('lang', $lang);
$this->assertNull($this->load->language($file));
}
// --------------------------------------------------------------------
public function test_packages()
{
// Create model in VFS package path
$dir = 'third-party';
$lib = 'unit_test_package';
$class = 'CI_'.ucfirst($lib);
$this->ci_vfs_create($lib, '<?php class '.$class.' { }', $this->ci_app_root, array($dir, 'libraries'));
// Get paths
$paths = $this->load->get_package_paths(TRUE);
// Add path and verify
$path = APPPATH.$dir.'/';
$this->assertNull($this->load->add_package_path($path));
$this->assertContains($path, $this->load->get_package_paths(TRUE));
// Test successful load
$this->assertNull($this->load->library($lib));
$this->assertTrue(class_exists($class), $class.' does not exist');
// Add another path
$path2 = APPPATH.'another/';
$this->assertNull($this->load->add_package_path($path2));
$this->assertContains($path2, $this->load->get_package_paths(TRUE));
// Remove last path
$this->assertNull($this->load->remove_package_path());
$this->assertNotContains($path2, $this->load->get_package_paths(TRUE));
// Remove path and verify restored paths
$this->assertNull($this->load->remove_package_path($path));
$this->assertEquals($paths, $this->load->get_package_paths(TRUE));
// Test failed load without path
$this->setExpectedException(
'RuntimeException',
'CI Error: Unable to load the requested class: '.$lib
);
$this->load->library($lib);
}
// --------------------------------------------------------------------
public function test_load_config()
{
$cfg = 'someconfig';
$this->assertNull($this->load->config($cfg, FALSE));
$this->assertContains($cfg, $this->ci_obj->config->loaded);
}
// --------------------------------------------------------------------
public function test_initialize()
{
// Create helper in VFS
$helper = 'autohelp';
$hlp_func = '_autohelp_test_func';
$content = '<?php function '.$hlp_func.'() { return true; }';
$this->ci_vfs_create($helper.'_helper', $content, $this->ci_app_root, 'helpers');
// Create library in VFS
$lib = 'autolib';
$lib_class = 'CI_'.ucfirst($lib);
$this->ci_vfs_create($lib, '<?php class '.$lib_class.' { }', $this->ci_base_root, 'libraries');
// Create driver in VFS
$drv = 'autodrv';
$subdir = ucfirst($drv);
$drv_class = 'CI_'.$subdir;
$this->ci_vfs_create($drv, '<?php class '.$drv_class.' { }', $this->ci_base_root, array('libraries', $subdir));
// Create model in VFS package path
$dir = 'testdir';
$path = APPPATH.$dir.'/';
$model = 'automod';
$mod_class = ucfirst($model);
$this->ci_vfs_create($model, '<?php class '.$mod_class.' { }', $this->ci_app_root, array($dir, 'models'));
// Create autoloader config
$cfg = array(
'packages' => array($path),
'helper' => array($helper),
'libraries' => array($lib),
'drivers' => array($drv),
'model' => array($model),
'config' => array('config1', 'config2')
);
$this->ci_vfs_create('autoload', '<?php $autoload = '.var_export($cfg, TRUE).';', $this->ci_app_root, 'config');
// Run initialize and autoloader
$this->load->initialize();
// Verify path
$this->assertContains($path, $this->load->get_package_paths());
// Verify helper
$this->assertTrue(function_exists($hlp_func), $hlp_func.' does not exist');
// Verify library
$this->assertTrue(class_exists($lib_class), $lib_class.' does not exist');
$this->assertAttributeInstanceOf($lib_class, $lib, $this->ci_obj);
// Verify driver
$this->assertTrue(class_exists($drv_class), $drv_class.' does not exist');
$this->assertAttributeInstanceOf($drv_class, $drv, $this->ci_obj);
// Verify model
$this->assertTrue(class_exists($mod_class), $mod_class.' does not exist');
$this->assertAttributeInstanceOf($mod_class, $model, $this->ci_obj);
// Verify config calls
$this->assertEquals($cfg['config'], $this->ci_obj->config->loaded);
}
}
|