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