예제 #1
0
 /**
  * 目录不存在
  * 
  * 不支持这样做的原因:
  * 出错了就应该给开发者提示,不能忽略过去
  * 
  * @expectedException PHPUnit_Framework_Error
  */
 public function testDirNotExists()
 {
     $autoloader = new LtAutoloader();
     $autoloader->autoloadPath = dirname(__FILE__) . "/dir_not_exists";
     //这个目录不存在
     $autoloader->init();
 }
 public function testPerformance()
 {
     // 准备autoloadPath
     $autoloadPath = array(dirname(__FILE__) . "/test_data/class_dir_1", dirname(__FILE__) . "/test_data/class_dir_2", dirname(__FILE__) . "/test_data/function_dir_1", dirname(__FILE__) . "/test_data/function_dir_2");
     /**
      * 用LtStoreFile作存储层提升性能
      */
     $cacheHandle = new LtStoreFile();
     $prefix = sprintf("%u", crc32(serialize($autoloadPath)));
     $cacheHandle->prefix = 'Lotus-' . $prefix;
     $cacheHandle->init();
     /**
      * 运行autoloader成功加载一个类
      * 这是为了证明:使用LtCache作为LtAutoloader的存储,功能是正常的
      */
     $autoloader = new LtAutoloader();
     $autoloader->devMode = false;
     // 关闭开发模式
     $autoloader->storeHandle = $cacheHandle;
     $autoloader->autoloadPath = $autoloadPath;
     $autoloader->init();
     $this->assertTrue(class_exists("HelloWorld"));
     /**
      * 运行1500次,要求在1秒内运行完
      */
     $base_memory_usage = memory_get_usage();
     $times = 1500;
     $startTime = microtime(true);
     for ($i = 0; $i < $times; $i++) {
         $autoloader = new LtAutoloader();
         $autoloader->devMode = false;
         // 关闭开发模式
         $autoloader->storeHandle = $cacheHandle;
         $autoloader->autoloadPath = $autoloadPath;
         $autoloader->init();
         unset($autoloader);
     }
     $endTime = microtime(true);
     $totalTime = round($endTime - $startTime, 6);
     $averageTime = round($totalTime / $times, 6);
     $memory_usage = memory_get_usage() - $base_memory_usage;
     $averageMemory = formatSize($memory_usage / $times);
     $memory_usage = formatSize($memory_usage);
     if (LOTUS_UNITTEST_DEBUG) {
         echo "\n---------------------autoloader--------------------------\n";
         echo "times      \t{$times}\n";
         echo "totalTime   \t{$totalTime}s\taverageTime   \t{$averageTime}s\n";
         echo "memoryUsage \t{$memory_usage}\taverageMemory \t{$averageMemory}";
         echo "\n---------------------------------------------------------\n";
     }
     $this->assertTrue(1 > $totalTime);
 }
예제 #3
0
 /**
  * -------------------------------------------------------------------
  * LtAutoloader要求:
  *  # 需要被自动加载的文件都以.php或者.inc结尾
  *    如果既有php文件,又有html文件,html文件将被忽略,php文件正常加载
  *    可配置,详情参见LtAutoloaderCofig
  * 
  * -------------------------------------------------------------------
  * LtAutoloader不在意:
  *  # 目录名有没有拖尾斜线
  *  # 目录下面有无子目录
  *  # 文件名和文件路径跟类名有无关联
  *  # 定义和使用类时,类名是大写还是小写 
  * 
  * -------------------------------------------------------------------
  * LtAutoloader不支持(出错演示和不支持的原因参见WrongWayToUse.php):
  *  # 传入的参数不是真实存在的目录(如http://some_dir这样的) 
  *  # 目录名或者文件名带空格(如"Zend Framework"这样的目录名) 
  *  # 类或接口重名,函数和函数重名
  * 
  * -------------------------------------------------------------------
  * LtAutoloader建议(不强求):
  *  # 传入autoloadPath时使用绝对路径
  *  # 目录名和文件名只由字母、数字、下划线、中横线、小数点组成
  *  # 使用class而不是function来封装你的逻辑
  *  # 每个class都放在单独的一个文件中,且不要在已经定义了类的文件里再定义函数
  *  # class/function里不要使用__FILE__魔术变量
  * 
  * -------------------------------------------------------------------
  * 本测试用例期望效果:
  * 在new CLASS_NAME, class_exists("CLASS_NAME"), extends CLASS_NAME的时候 
  * 自动把包含该类的文件加载进来
  */
 public function testMostUsedWay()
 {
     /**
      * Lotus组件初始化三步曲
      */
     // 1. 实例化
     $autoloader = new LtAutoloader();
     // 2. 设置属性
     $autoloader->autoloadPath = array(dirname(__FILE__) . "/test_data/class_dir_1", dirname(__FILE__) . "/test_data/class_dir_2", dirname(__FILE__) . "/test_data/function_dir_1", dirname(__FILE__) . "/test_data/function_dir_2");
     // 3. 调init()方法
     $autoloader->init();
     //初始化完毕,测试其效果
     $this->assertTrue(new Goodbye() instanceof GoodBye);
     $this->assertTrue(class_exists("HelloWorld"));
     $this->assertEquals("hello", HelloLotus::sayHello());
     $this->assertEquals("hello", say_hello());
     $this->assertEquals("hello_2", say_hello_2());
 }
예제 #4
0
 /**
  * Autoload all lotus components and user-defined libraries;
  */
 protected function prepareAutoloader()
 {
     require_once $this->lotusRuntimeDir . "Autoloader/Autoloader.php";
     $autoloader = new LtAutoloader();
     $autoloader->autoloadPath[] = $this->lotusRuntimeDir;
     if (isset($this->option["autoload_dir"])) {
         $autoloader->autoloadPath[] = $this->option["autoload_dir"];
     }
     if ($this->proj_dir) {
         is_dir($this->proj_dir . 'lib') && ($autoloader->autoloadPath[] = $this->proj_dir . 'lib');
         is_dir($this->app_dir . 'action') && ($autoloader->autoloadPath[] = $this->app_dir . 'action');
         is_dir($this->app_dir . 'lib') && ($autoloader->autoloadPath[] = $this->app_dir . 'lib');
     }
     if (!$this->devMode) {
         $autoloader->storeHandle = $this->coreCacheHandle;
     }
     $autoloader->init();
 }
예제 #5
0
 /**
  * Autoload all lotus components and user-defined libraries;
  */
 protected function prepareAutoloader()
 {
     require_once $this->lotusRuntimeDir . "Autoloader/Autoloader.php";
     $autoloader = new LtAutoloader();
     // 设置工作模式
     $autoloader->devMode = $this->devMode;
     $autoloader->autoloadPath[] = $this->lotusRuntimeDir;
     if (isset($this->option["autoload_dir"])) {
         $autoloader->autoloadPath[] = $this->option["autoload_dir"];
     }
     if ($this->proj_dir) {
         foreach (array($this->proj_dir . 'lib', $this->app_dir . 'action', $this->app_dir . 'lib') as $dir) {
             if (is_dir($dir)) {
                 $autoloader->autoloadPath[] = $dir;
             }
         }
     }
     $autoloader->storeHandle = clone $this->coreCacheHandle;
     $autoloader->storeHandle->prefix = $this->coreCacheHandle->prefix . '-cls';
     $autoloader->init();
 }
예제 #6
0
 public function getFilePathByClassName($className)
 {
     return parent::getFilePathByClassName($className);
 }