From 23a33c7708b1272453e5ef9f85f87962bfdf59e3 Mon Sep 17 00:00:00 2001 From: NewFuture Date: Wed, 17 Aug 2016 18:16:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95,=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E6=B5=8B=E8=AF=95=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit tests tests yaf yaf php5 php5 yaf pecl update trusty yaf2.3.3 script install Permission yaf ini path path testpath no mail enviroment test lib tests test config file env tests testConf file message config TRACER_OFF spl on config all conifg update tests init global init php5.3 backup false config 完成配置测试 all namespace finish config test --- .travis.yml | 48 +++++++++++++++++++ app/Bootstrap.dev.php | 6 ++- app/controllers/Index.php | 27 +++++++++++ library/Config.php | 89 ++++++++++++++++-------------------- tests/EnvTest.php | 18 ++++++++ tests/ini/yaf.dev.ini | 4 ++ tests/ini/yaf.product.ini | 6 +++ tests/init.php | 6 +++ tests/install-yaf.sh | 15 ++++++ tests/library/ConfigTest.php | 72 +++++++++++++++++++++++++++++ tests/phpunit.xml | 29 ++++++++++++ 11 files changed, 269 insertions(+), 51 deletions(-) create mode 100644 .travis.yml create mode 100644 tests/EnvTest.php create mode 100755 tests/ini/yaf.dev.ini create mode 100755 tests/ini/yaf.product.ini create mode 100644 tests/init.php create mode 100755 tests/install-yaf.sh create mode 100644 tests/library/ConfigTest.php create mode 100644 tests/phpunit.xml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..8505765 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,48 @@ +# http://about.travis-ci.org/docs/user/languages/php/ +dist: trusty +language: php + +# list any PHP version +php: + # using major version aliases + # aliased to a recent 7.x version + - 7.0 + # aliased to a recent 5.6.x version + - 5.6 + # aliased to a recent 5.5.x version + - 5.5 + # aliased to a recent 5.4.x version + - 5.4 + # aliased to 5.3.29 + - 5.3 + + +# https://docs.travis-ci.com/user/environment-variables/ +# optionally specify a list of environments +env: + - environ=dev + - environ=product + +# optionally set up exclutions and allowed failures in the matrix +# matrix: +# # allow_failures: +# # - php: 5.3 + + +install: + #init tests environment + - ./tests/install-yaf.sh + + +# execute any number of scripts before the test run, custom env's are available as variables +before_script: + #init YYF + - echo 0 | ./init.cmd + +# omitting "script:" will default to phpunit +# use the env variable to determine the phpunit.xml to use +script: phpunit --coverage-text --configuration tests/phpunit.xml + +# configure notifications (email, IRC, campfire etc) +notifications: + email: false diff --git a/app/Bootstrap.dev.php b/app/Bootstrap.dev.php index f1a5bd1..f634904 100644 --- a/app/Bootstrap.dev.php +++ b/app/Bootstrap.dev.php @@ -144,7 +144,9 @@ public function _initMode() */ public function _initPlugin(Yaf_Dispatcher $dispatcher) { - $tracer = new TracerPlugin(); - $dispatcher->registerPlugin($tracer); + if (false===defined('TRACER_OFF')) { + $tracer = new TracerPlugin(); + $dispatcher->registerPlugin($tracer); + } } } diff --git a/app/controllers/Index.php b/app/controllers/Index.php index 8c69400..4968474 100644 --- a/app/controllers/Index.php +++ b/app/controllers/Index.php @@ -13,6 +13,33 @@ public function indexAction() $this->getView()->assign('version', Config::get('version'))->assign('url',$url); } + public function confAction() + { + $config=parse_ini_file(APP_PATH.'/conf/app.ini',true); + // var_dump($config); + $env=Yaf_Application::app()->environ(); + echo $env; + $current=$config['common']+$config[$env.':common']; + + // var_dump($current); + foreach($current as $k=>$v){ + if(Config::get($k)===$v) + { + echo 1; + }else{ + var_dump($k); + var_dump($v); + var_dump(Config::get($k)); + $o= Yaf_Application::app()->getConfig()->get($k); + var_dump(is_object($o)); + var_dump($o); + } + // echo Config::get($k)===$v?1:0; + } + // var_dump(Yaf_Application::app()->getConfig()->get('ssss')); + // var_dump(Config::get()==$current); + } + /** * GET /index/test * @method GET_testAction diff --git a/library/Config.php b/library/Config.php index 323a53d..99beb1a 100644 --- a/library/Config.php +++ b/library/Config.php @@ -5,55 +5,46 @@ */ class Config { - private static $_config = null; + private static $_config = null; - /** - * 获取配置 - * @method get - * @param [string] $key [键值] - * @param [type] $default [默认值] - * @return [mixed] [返回结果] - * @author NewFuture - */ - public static function get($key = null, $default = null) - { - if ($value = self::getConfig()->get($key)) - { - return is_object($value) ? $value->toArray() : $value; - } - else - { - return $default; - } - } + /** + * 获取配置 + * @method get + * @param [string] $key [键值] + * @param [type] $default [默认值] + * @return [mixed] [返回结果] + * @author NewFuture + */ + public static function get($key = null, $default = null) + { + if (null===($value = self::getConfig()->get($key))) { + return $default; + } + return is_object($value) ? $value->toArray():$value; + } - /** - * 获取私密配置 - * @method secret - * @param [string] $name [配置名] - * @param [string] $key [键值] - * @return [midex] [description] - * @author NewFuture - * @example - * Config::getSecrect('encrypt') 获取取私密配置中的encrypt所有配置 - * Config::getSecrect('encrypt','key') 获取取私密配置中的encrypt配置的secret值 - */ - public static function getSecret($name = '', $key = null) - { - if ($path = self::getConfig()->get('secret_config_path')) - { - $secretConfig = new Yaf_Config_Ini($path, $name); - return $key ? $secretConfig->get($key) : $secretConfig->toArray(); - } - } + /** + * 获取私密配置 + * @method secret + * @param [string] $name [配置名] + * @param [string] $key [键值] + * @return [midex] [description] + * @author NewFuture + * @example + * Config::getSecrect('encrypt') 获取取私密配置中的encrypt所有配置 + * Config::getSecrect('encrypt','key') 获取取私密配置中的encrypt配置的secret值 + */ + public static function getSecret($name = '', $key = null) + { + if ($path = self::getConfig()->get('secret_config_path')) { + $secretConfig = new Yaf_Config_Ini($path, $name); + return $key ? $secretConfig->get($key) : $secretConfig->toArray(); + } + } - /*获取配置*/ - private static function getConfig() - { - if (null === self::$_config) - { - self::$_config = $GLOBALS['app']->getConfig(); - } - return self::$_config; - } -} \ No newline at end of file + /*获取配置*/ + private static function getConfig() + { + return self::$_config ?: (self::$_config = Yaf_Application::app()->getConfig()); + } +} diff --git a/tests/EnvTest.php b/tests/EnvTest.php new file mode 100644 index 0000000..550e68d --- /dev/null +++ b/tests/EnvTest.php @@ -0,0 +1,18 @@ +assertTrue(extension_loaded('yaf'),'yaf extension not loaded'); + } + + /*conf is exist?*/ + public function testConfFile() + { + $conf=APP_PATH . '/conf/app.ini'; + $this->assertFileExists($conf,$conf.' can not find'); + } +} + diff --git a/tests/ini/yaf.dev.ini b/tests/ini/yaf.dev.ini new file mode 100755 index 0000000..0fbc13b --- /dev/null +++ b/tests/ini/yaf.dev.ini @@ -0,0 +1,4 @@ +extension=yaf.so +[yaf] +yaf.environ=dev +yaf.use_spl_autoload = 1 \ No newline at end of file diff --git a/tests/ini/yaf.product.ini b/tests/ini/yaf.product.ini new file mode 100755 index 0000000..b658493 --- /dev/null +++ b/tests/ini/yaf.product.ini @@ -0,0 +1,6 @@ +extension=yaf.so + +[yaf] +yaf.environ=product +yaf.cache_config=1 +yaf.use_spl_autoload=1 \ No newline at end of file diff --git a/tests/init.php b/tests/init.php new file mode 100644 index 0000000..5b67c2a --- /dev/null +++ b/tests/init.php @@ -0,0 +1,6 @@ +bootstrap(); diff --git a/tests/install-yaf.sh b/tests/install-yaf.sh new file mode 100755 index 0000000..1e62898 --- /dev/null +++ b/tests/install-yaf.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +TESTS_PATH=`dirname $(readlink -f $0)` +if [[ ${TRAVIS_PHP_VERSION:0:2} == "5." ]]; then + YAF_VERSION=yaf-2.3.5 +else + YAF_VERSION=yaf-3.0.3 +fi + +curl https://pecl.php.net/get/${YAF_VERSION}.tgz | tar zx -C ./ +cd ${YAF_VERSION}; phpize; + +./configure && make && make install + +phpenv config-add $TESTS_PATH/ini/yaf.$environ.ini \ No newline at end of file diff --git a/tests/library/ConfigTest.php b/tests/library/ConfigTest.php new file mode 100644 index 0000000..f130cd7 --- /dev/null +++ b/tests/library/ConfigTest.php @@ -0,0 +1,72 @@ +environ(); + $config=parse_ini_file(APP_PATH.'/conf/app.ini', true); + $current=$config[$env.':common']+$config['common']; + + foreach ($current as $key => $value) { + $this->assertSame($current[$key], Config::get($key), $key); + } + } + + /*检测空值*/ + public function testEmpty() + { + $this->assertSame(Config::get(uniqid('_te_', true)), null); + } + + /*测试默认值*/ + public function testDefault() + { + $key=uniqid('_td_', true); + $default=array(false,null,1,true,array(1,2,4),'test'); + foreach ($default as $k=>$d) { + $this->assertSame(Config::get($k.$key, $d), $d); + } + } + + /*测试secret路径是否存在*/ + public function testSecretPath() + { + $secret_ini=Config::get('secret_config_path'); + $this->assertFileExists($secret_ini, $secret_ini.' Config cannot find'); + return $secret_ini; + } + + /** + * @depends testSecretPath + */ + public function testSecret($path) + { + $secret=parse_ini_file($path, true); + foreach ($secret as $name => &$key) { + foreach ($key as $k => $v) { + $this->assertSame(Config::getSecret($name, $k), $v, "$name.$k"); + } + } + } + + public function testSecretArray() + { + $default_db=Config::getSecret('database', 'db._'); + $this->assertNotEmpty($default_db); + $this->assertArrayHasKey('dsn', $default_db); + } + + /*检测sceret空值*/ + public function testSecretEmpty() + { + $key=uniqid('_tse_', true); + $this->assertSame(Config::getSecret('database', $key), null); + } +} diff --git a/tests/phpunit.xml b/tests/phpunit.xml new file mode 100644 index 0000000..ca9fe16 --- /dev/null +++ b/tests/phpunit.xml @@ -0,0 +1,29 @@ + + + + + + + + EnvTest.php + + + + library + + + + + + + ../library + + + +