| [ Index ] |
PHP Cross Reference of Limb3 |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * Limb PHP Framework 4 * 5 * @link http://limb-project.com 6 * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com) 7 * @license LGPL http://www.gnu.org/copyleft/lesser.html 8 */ 9 10 class lmbRequireTest extends UnitTestCase 11 { 12 var $tmp_dir; 13 14 function setUp() 15 { 16 if(!is_dir(LIMB_VAR_DIR)) 17 mkdir(LIMB_VAR_DIR); 18 19 $this->tmp_dir = LIMB_VAR_DIR . '/lmb_require/'; 20 $this->_rm($this->tmp_dir); 21 mkdir($this->tmp_dir); 22 } 23 24 function tearDown() 25 { 26 $this->_rm($this->tmp_dir); 27 } 28 29 function testLazyLoadClass() 30 { 31 $name1 = $this->_rndName(); 32 $path1 = $this->_writeClassFile($name1); 33 34 $name2 = $this->_rndName(); 35 $path2 = $this->_writeClassFile($name2); 36 37 lmb_require($path1); 38 lmb_require($path2); 39 40 $this->assertFalse(in_array($name1, get_declared_classes())); 41 $this->assertFalse(in_array($name2, get_declared_classes())); 42 43 $this->assertTrue(class_exists($name1, true));//triggers autoload 44 45 $this->assertTrue(in_array($name1, get_declared_classes())); 46 $this->assertFalse(in_array($name2, get_declared_classes())); 47 } 48 49 function testLazyLoadInterface() 50 { 51 $name1 = $this->_rndName(); 52 $path1 = $this->_writeInterfaceFile($name1); 53 $name2 = $this->_rndName(); 54 $path2 = $this->_writeInterfaceFile($name2); 55 56 lmb_require($path1); 57 lmb_require($path2); 58 59 $this->assertFalse(in_array($name1, get_declared_interfaces())); 60 $this->assertFalse(in_array($name2, get_declared_interfaces())); 61 62 $this->assertTrue(interface_exists($name1, true));//triggers autoload 63 64 $this->assertTrue(in_array($name1, get_declared_interfaces())); 65 $this->assertFalse(in_array($name2, get_declared_interfaces())); 66 } 67 68 function testRecursionProtection() 69 { 70 $name = $this->_rndName(); 71 $path = $this->_writeModule("$name.class.php", "<?php \$foo = new $name(); class $name {} ?>"); 72 73 lmb_require($path); 74 75 $foo = new $name(); 76 } 77 78 function testNoLazyLoadForModule() 79 { 80 $name = $this->_rndName(); 81 $path = $this->_writeModule("$name.inc.php", "<?php class $name {} ?>"); 82 83 lmb_require($path); 84 $this->assertTrue(in_array($name, get_declared_classes())); 85 } 86 87 function testGlobRequireWithAutoload() 88 { 89 //creating new unique directory 90 $old_dir = $this->tmp_dir; 91 $this->tmp_dir = $this->tmp_dir . $this->_rndName() . '/'; 92 mkdir($this->tmp_dir); 93 94 $c1 = $this->_rndName(); 95 $c2 = $this->_rndName(); 96 $c3 = $this->_rndName(); 97 98 $path1 = $this->_writeClassFile($c1); 99 $path2 = $this->_writeClassFile($c2); 100 $path3 = $this->_writeClassFile($c3); 101 102 lmb_require($this->tmp_dir . '/*.class.php'); 103 104 foreach(array($c1, $c2, $c3) as $c) 105 { 106 $this->assertFalse(in_array($c, get_declared_interfaces())); 107 $this->assertTrue(class_exists($c, true)); 108 } 109 110 $this->tmp_dir = $old_dir; 111 } 112 113 function testRequireFileCacheHit() 114 { 115 if(!function_exists('xdebug_start_code_coverage')) 116 return; 117 118 $name = $this->_rndName(); 119 $path = $this->_writeModule("$name.inc.php", "<?php class $name {} ?>"); 120 121 $func = new ReflectionFunction('lmb_require'); 122 $file = $func->getFileName(); 123 124 $line = $this->_locateIncludeOnceLine($file, $func->getStartLine()); 125 126 xdebug_start_code_coverage(); 127 lmb_require($path); 128 $cov1 = xdebug_get_code_coverage(); 129 xdebug_stop_code_coverage(); 130 131 xdebug_start_code_coverage(); 132 lmb_require($path); 133 $cov2 = xdebug_get_code_coverage(); 134 xdebug_stop_code_coverage(); 135 136 $this->assertEqual($cov1[$file][$line], 1); 137 $this->assertFalse(isset($cov2[$file][$line])); 138 } 139 140 function testRequireThrowsExceptionForNonExistingFile() 141 { 142 try 143 { 144 @lmb_require($file = 'foo_' . mt_rand() . '.inc.php'); 145 } 146 catch(lmbException $e) 147 { 148 $this->assertPattern('~' . preg_quote($file) . '~', $e->getMessage()); 149 } 150 } 151 152 function testRequireOptional() 153 { 154 $name = $this->_rndName(); 155 $path = $this->_writeModule("$name.class.php", "<?php class $name {} ?>"); 156 157 lmb_require_optional($path); 158 159 $foo = new $name(); 160 } 161 162 function testRequireOptionalDoesntThrowExceptionForNonExistingFile() 163 { 164 lmb_require_optional($file = 'foo_' . mt_rand() . '.inc.php'); 165 } 166 167 function testRequireOptionalGlobDoesntThrowExceptionForNonExistingFiles() 168 { 169 lmb_require_optional($file = 'foo_' . mt_rand() . '*.php'); 170 } 171 172 function _locateIncludeOnceLine($file, $start_line) 173 { 174 $c = 0; 175 foreach(file($file) as $line) 176 { 177 $c++; 178 if($c >= $start_line && strpos($line, 'include_once') !== false) 179 return $c; 180 } 181 } 182 183 function _writeClassFile($name, $body = null) 184 { 185 $path = $this->tmp_dir . $name . '.class.php'; 186 $this->_write($path, $body ? $body : $this->_classCode($name)); 187 return $path; 188 } 189 190 function _writeInterfaceFile($name) 191 { 192 $path = $this->tmp_dir . $name . '.interface.php'; 193 $this->_write($path, $this->_faceCode($name)); 194 return $path; 195 } 196 197 function _writeModule($name, $contents) 198 { 199 $path = $this->tmp_dir . $name; 200 $this->_write($path, $contents); 201 return $path; 202 } 203 204 function _classCode($name) 205 { 206 return "<?php class $name {} ?>"; 207 } 208 209 function _faceCode($name) 210 { 211 return "<?php interface $name {} ?>"; 212 } 213 214 function _rndName() 215 { 216 return 'Foo' . mt_rand(1, 1000); 217 } 218 219 function _rnd() 220 { 221 return mt_rand(1, 1000); 222 } 223 224 function _write($file, $contents='') 225 { 226 file_put_contents($file, $contents); 227 } 228 229 function _rm($path) 230 { 231 if(!is_dir($path)) 232 return; 233 $dir = opendir($path); 234 while($entry = readdir($dir)) 235 { 236 if(is_file("$path/$entry")) 237 unlink("$path/$entry"); 238 elseif(is_dir("$path/$entry") && $entry != '.' && $entry != '..') 239 $this->_rm("$path/$entry"); 240 } 241 closedir($dir); 242 return rmdir($path); 243 } 244 } 245 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Fri Dec 5 04:05:07 2008 | Cross-referenced by PHPXref 0.7 |