/** * config file不存在 * * @expectedException PHPUnit_Framework_Error */ public function testNotExistsConfigFile() { $conf = new LtConfig(); // $conf->configFile = dirname(__FILE__) . "/test_data/conf_not_exists.php"; $conf->init(); $conf->loadConfigFile(dirname(__FILE__) . "/test_data/conf_not_exists.php"); }
public function testMostUsedWay() { $username = '******'; $password = '******'; $dtds['username'] = new LtValidatorDtd($_POST['username'], "username", array("max_length" => 8, "mask" => "/^[a-z0-9]+\$/i", "ban" => "/f**k/"), array("mask" => "用户名只能由数字或字组成", "ban" => "用户名不能包含脏话")); $dtds['password'] = new LtValidatorDtd($_POST['password'], "password", array("max_length" => 8, "mask" => "/^[a-z0-9]+\$/i", "ban" => "/f**k/"), array("max_length" => "密码最长8位", "mask" => "密码只能由数字或字组成")); // 配置文件 $config['validator.error_messages'] = array('ban' => '%s contain banned words', 'mask' => '%s does not match the given format', 'max_length' => '%s is longer than %s', 'min_length' => '%s is shorter than %s', 'max_value' => '%s is bigger than %s', 'min_value' => '%s is smaller than %s', 'max_selected' => '%s is too much', 'min_selected' => '%s is too few', 'required' => '%s is empty', 'equal_to' => '%s is not equal to %s'); $configHandle = new LtConfig(); $configHandle->addConfig($config); $validator = new LtValidator(); $validator->configHandle = $configHandle; $validator->init(); $dtd = $dtds['username']; foreach ($dtd->rules as $ruleKey => $ruleValue) { if ($ruleValue instanceof LtConfigExpression) { eval('$_ruleValue = ' . $ruleValue->__toString()); $dtd->rules[$ruleKey] = $ruleValue; } } $error_messages = $validator->validate($dtd); $this->assertEquals(array('max_length' => 'username is longer than 8', 'mask' => '用户名只能由数字或字组成', 'ban' => '用户名不能包含脏话'), $error_messages); $dtd = $dtds['password']; foreach ($dtd->rules as $ruleKey => $ruleValue) { if ($ruleValue instanceof LtConfigExpression) { eval('$_ruleValue = ' . $ruleValue->__toString()); $dtd->rules[$ruleKey] = $ruleValue; } } $error_messages = $validator->validate($dtd); $this->assertEquals(array('max_length' => '密码最长8位', 'mask' => '密码只能由数字或字组成', 'ban' => 'password contain banned words'), $error_messages); }
public function testPathinfo() { $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; $_SERVER["REQUEST_URI"] = '/index.php/default2/index2/id/123456/page/12/q-%FFkey/%E7%A9%BA%20-%FF%E6%A0%BC.html'; $_SERVER['SCRIPT_NAME'] = '/index.php'; $config['router.routing_table']['default'] = array('module' => 'default', 'action' => 'index'); $config['router.routing_table']['protocol'] = 'PATH_INFO'; $configHandle = new LtConfig(); $configHandle->addConfig($config); $router = new LtRouter(); $router->configHandle = $configHandle; $router->init(); $this->assertEquals('default2', $router->module); $this->assertEquals('index2', $router->action); $this->assertEquals(array('module' => 'default2', 'action' => 'index2', 'id' => '123456', 'page' => '12', 'q-/key' => '空 -/格'), $_GET); }
public function testRewrite() { $config['router.routing_table']['protocol'] = 'REWRITE'; $configHandle = new LtConfig(); $configHandle->addConfig($config); $url = new LtUrl(); $url->configHandle = $configHandle; $url->init(); $params = array('id' => 123456, 'page' => '12', 'q-/key' => '空 -/格'); $url->baseUrl = 'http://localhost'; $link = $url->generate('goods', 'detail', $params); $this->assertEquals('http://localhost/goods-detail-id-123456-page-12-q%FF/key-%E7%A9%BA%20%FF%2F%E6%A0%BC.html', $link); $url->baseUrl = 'http://127.0.0.1'; $link = $url->generate('goods', 'detail', $params); $this->assertEquals('http://127.0.0.1/goods-detail-id-123456-page-12-q%FF/key-%E7%A9%BA%20%FF%2F%E6%A0%BC.html', $link); }
public function testPerformance() { /** * 用LtStoreFile作存储层提升性能 */ $cacheHandle = new LtStoreFile(); $cacheHandle->init(); // 准备confif_file $config_file = dirname(__FILE__) . "/test_data/conf.php"; /** * 运行autoloader成功取到一个配置 * 这是为了证明:使用LtCache作为LtConfig的存储,功能是正常的 */ $conf = new LtConfig(); $conf->storeHandle = $cacheHandle; $conf->loadConfigFile($config_file); $conf->init(); $this->assertEquals("localhost", $conf->get("db.conn.host")); /** * 运行100次,要求在1秒内运行完 */ $base_memory_usage = memory_get_usage(); $times = 100; $startTime = microtime(true); for ($i = 0; $i < $times; $i++) { $conf->get('db.conn.host'); } $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----------------------config-----------------------------\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); }
public function testMostUsedWay() { // 角色 可以是多个 $roles = array('Administrators', 'Users'); $roles = array_merge(array("*"), $roles); // 访问控制列表 deny优先 $acl['allow']['*'][] = 'Index/Index'; $acl['deny']['*'][] = ''; $acl['allow']['Administrators'][] = 'admin/*'; $acl['allow']['Administrators'][] = 'User/*'; $acl['allow']['Users'][] = 'User/View'; $acl['allow']['Users'][] = 'User/Signin'; $acl['allow']['Users'][] = 'User/DoSignin'; $acl['deny']['Users'][] = 'User/AddUser'; $configHandle = new LtConfig(); $configHandle->addConfig(array('rbac.acl' => $acl)); $rbac = new LtRbac(); $rbac->configHandle = $configHandle; $rbac->init(); $this->assertTrue($rbac->checkAcl($roles, 'admin/test')); $this->assertFalse($rbac->checkAcl($roles, 'User/AddUser')); }
/** * ------------------------------------------------------------------- * LtConfig要求: * # 配置文件以return array()形式返回一个配置数组 * * ------------------------------------------------------------------- * LtConfig不在意: * # 配置文件中用什么变量名和常量名 * * ------------------------------------------------------------------- * 本测试用例期望效果: * 通过LtConfig类能取到定义在config_file里面的配置信息 */ public function testMostUsedWay() { $conf = new LtConfig(); $conf->init(); $conf->loadConfigFile(dirname(__FILE__) . "/test_data/conf.php"); $this->assertEquals("localhost", $conf->get("db.conn.host")); $this->assertEquals($conf->get("misc.test_array"), array("test_array_key_1" => "test_array_value_1", "test_array_key_2" => "test_array_value_2")); $this->assertEquals(time(), $conf->get("misc.now")); }
/** * prepare config * @todo 先加载proj/conf下的配置,再加载app/conf下的配置,并自动覆盖重复项 * 注意避开array_merge的坑 * 或者,至少做个判断,app/conf不存在,去找proj/conf,不能不加载配置文件 */ protected function prepareConfig() { $this->configHandle = LtObjectUtil::singleton('LtConfig', false); if (!$this->devMode) { $configFile = 'conf/conf.php'; $this->configHandle->storeHandle = clone $this->coreCacheHandle; $this->configHandle->storeHandle->prefix = $this->coreCacheHandle->prefix . '-conf'; } else { $configFile = 'conf/conf_dev.php'; } $this->configHandle->init(); if ($this->app_dir && is_file($this->app_dir . $configFile)) { $this->configHandle->loadConfigFile($this->app_dir . $configFile); } }
* 加载MVC类文件 加载的类很多,且需要注意先后顺序,推荐使用LtAutoloader自动加载 */ $lotusHome = substr(__FILE__, 0, strpos(__FILE__, "example")) . '/'; include $lotusHome . "runtime/Config.php"; include $lotusHome . "runtime/Store.php"; include $lotusHome . "runtime/StoreMemory.php"; include $lotusHome . "runtime/ObjectUtil/ObjectUtil.php"; include $lotusHome . "runtime/MVC/Dispatcher.php"; include $lotusHome . "runtime/MVC/Action.php"; include $lotusHome . "runtime/MVC/Component.php"; include $lotusHome . "runtime/MVC/Context.php"; include $lotusHome . "runtime/MVC/View.php"; include $lotusHome . "runtime/Validator/Validator.php"; include $lotusHome . "runtime/Validator/ValidatorDtd.php"; // 配置文件 $config['validator.error_messages'] = array('ban' => '%s contain banned words', 'mask' => '%s does not match the given format', 'max_length' => '%s is longer than %s', 'min_length' => '%s is shorter than %s', 'max_value' => '%s is bigger than %s', 'min_value' => '%s is smaller than %s', 'max_selected' => '%s is too much', 'min_selected' => '%s is too few', 'required' => '%s is empty', 'equal_to' => '%s is not equal to %s'); $configHandle = new LtConfig(); $configHandle->addConfig($config); /** * 加载Action类文件 */ $appDir = "./simplest_app/"; include $appDir . "action/User-Signin.Action.php"; /** * 实例化 */ $dispatcher = LtObjectUtil::singleton('LtDispatcher'); $dispatcher->configHandle = $configHandle; $dispatcher->viewDir = "./simplest_app/view/"; $dispatcher->dispatchAction("User", "Signin");