blob: d98e8ab9852b0d3519550be2ff234233e1b4dc26 [file] [log] [blame]
dchill426262d052012-11-24 18:41:13 -05001<?php
2
3/**
4 * Driver library base class unit test
5 */
6class Driver_test extends CI_TestCase {
7 /**
8 * Set up test framework
9 */
10 public function set_up()
11 {
12 // Set our subclass prefix
13 $this->subclass = 'Mock_Libraries_';
14 $this->ci_set_config('subclass_prefix', $this->subclass);
15
16 // Mock Loader->get_package_paths
17 $paths = 'get_package_paths';
18 $ldr = $this->getMock('CI_Loader', array($paths));
19 $ldr->expects($this->any())->method($paths)->will($this->returnValue(array(APPPATH, BASEPATH)));
20 $this->ci_instance_var('load', $ldr);
21
22 // Create mock driver library
23 $this->name = 'Driver';
24 $this->lib = new Mock_Libraries_Driver();
25 }
26
27 /**
28 * Test driver child loading
29 */
30 public function test_load_driver()
31 {
32 // Create driver file
33 $driver = 'basic';
34 $file = $this->name.'_'.$driver;
35 $class = 'CI_'.$file;
36 $prop = 'called';
37 $content = '<?php class '.$class.' extends CI_Driver { public $'.$prop.' = FALSE; '.
38 'public function decorate($parent) { $this->'.$prop.' = TRUE; } }';
39 $this->ci_vfs_create($file, $content, $this->ci_base_root, array('libraries', $this->name, 'drivers'));
40
41 // Make driver valid
42 $this->lib->driver_list($driver);
43
44 // Load driver
45 $this->assertNotNull($this->lib->load_driver($driver));
46
47 // Did lib name get set?
48 $this->assertEquals($this->name, $this->lib->get_name());
49
50 // Was driver loaded?
51 $this->assertObjectHasAttribute($driver, $this->lib);
52 $this->assertAttributeInstanceOf($class, $driver, $this->lib);
53 $this->assertAttributeInstanceOf('CI_Driver', $driver, $this->lib);
54
55 // Was decorate called?
56 $this->assertObjectHasAttribute($prop, $this->lib->$driver);
57 $this->assertTrue($this->lib->$driver->$prop);
58
59 // Do we get an error for an invalid driver?
60 $driver = 'unlisted';
61 $this->setExpectedException('RuntimeException', 'CI Error: Invalid driver requested: '.$this->name.'_'.$driver);
62 $this->lib->load_driver($driver);
63 }
64
65 /**
66 * Test loading lowercase from app path
67 */
68 public function test_load_app_driver()
69 {
70 // Create driver file
71 $driver = 'lowpack';
72 $file = $this->name.'_'.$driver;
73 $class = 'CI_'.$file;
74 $content = '<?php class '.$class.' extends CI_Driver { }';
Andrey Andreev7e83f322012-11-25 16:03:21 +020075 $this->ci_vfs_create($file, $content, $this->ci_app_root,
dchill426262d052012-11-24 18:41:13 -050076 array('libraries', $this->name, 'drivers'));
77
78 // Make valid list
79 $nodriver = 'absent';
80 $this->lib->driver_list(array($driver, $nodriver));
81
82 // Load driver
83 $this->assertNotNull($this->lib->load_driver($driver));
84
85 // Was driver loaded?
86 $this->assertObjectHasAttribute($driver, $this->lib);
87 $this->assertAttributeInstanceOf($class, $driver, $this->lib);
88 $this->assertAttributeInstanceOf('CI_Driver', $driver, $this->lib);
89
90 // Do we get an error for a non-existent driver?
91 $this->setExpectedException('RuntimeException', 'CI Error: Unable to load the requested driver: CI_'.
92 $this->name.'_'.$nodriver);
93 $this->lib->load_driver($nodriver);
94 }
95
96 /**
97 * Test loading driver extension
98 */
99 public function test_load_driver_ext()
100 {
101 // Create base file
102 $driver = 'extend';
103 $base = $this->name.'_'.$driver;
104 $baseclass = 'CI_'.$base;
105 $content = '<?php class '.$baseclass.' extends CI_Driver { }';
106 $this->ci_vfs_create($base, $content, $this->ci_base_root, array('libraries', $this->name, 'drivers'));
107
108 // Create driver file
109 $class = $this->subclass.$base;
110 $content = '<?php class '.$class.' extends '.$baseclass.' { }';
111 $this->ci_vfs_create($class, $content, $this->ci_app_root, array('libraries', $this->name, 'drivers'));
112
113 // Make valid list
114 $this->lib->driver_list($driver);
115
116 // Load driver
117 $this->assertNotNull($this->lib->load_driver($driver));
118
119 // Was driver loaded?
120 $this->assertObjectHasAttribute($driver, $this->lib);
121 $this->assertAttributeInstanceOf($class, $driver, $this->lib);
122 $this->assertAttributeInstanceOf($baseclass, $driver, $this->lib);
123 $this->assertAttributeInstanceOf('CI_Driver', $driver, $this->lib);
124
125 // Create driver extension without base
126 $driver = 'baseless';
127 $base = $this->name.'_'.$driver;
128 $class = $this->subclass.$base;
129 $content = '<?php class '.$class.' extends CI_Driver { }';
130 $this->ci_vfs_create($class, $content, $this->ci_app_root, array('libraries', $this->name, 'drivers'));
131 $this->lib->driver_list($driver);
132
133 // Do we get an error when base class isn't found?
134 $this->setExpectedException('RuntimeException', 'CI Error: Unable to load the requested class: CI_'.$base);
135 $this->lib->load_driver($driver);
136 }
137
138 /**
139 * Test decorating driver with parent attributes
140 */
141 public function test_decorate()
142 {
143 // Create parent with a method and property to access
144 $pclass = 'Test_Parent';
145 $prop = 'parent_prop';
146 $value = 'initial';
147 $method = 'parent_func';
148 $return = 'func return';
149 $code = 'class '.$pclass.' { public $'.$prop.' = \''.$value.'\'; '.
150 'public function '.$method.'() { return \''.$return.'\'; } }';
151 eval($code);
152 $parent = new $pclass();
153
154 // Create child driver to decorate
155 $class = 'Test_Driver';
156 eval('class '.$class.' extends CI_Driver { }');
157 $child = new $class();
158
159 // Decorate child
160 $child->decorate($parent);
161
162 // Do we get the initial parent property value?
163 $this->assertEquals($value, $child->$prop);
164
165 // Can we change the parent property?
166 $newval = 'changed';
167 $child->$prop = $newval;
168 $this->assertEquals($newval, $parent->$prop);
169
170 // Do we get back the updated value?
171 $this->assertEquals($newval, $child->$prop);
172
173 // Can we call the parent method?
174 $this->assertEquals($return, $child->$method());
175 }
Andrey Andreev838a9d62012-12-03 14:37:47 +0200176
177}