Skip to content

Commit

Permalink
添加单元测试,提交代码自动测试
Browse files Browse the repository at this point in the history
tests

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
  • Loading branch information
NewFuture committed Aug 18, 2016
1 parent a38752b commit 23a33c7
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 51 deletions.
48 changes: 48 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 4 additions & 2 deletions app/Bootstrap.dev.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
27 changes: 27 additions & 0 deletions app/controllers/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
89 changes: 40 additions & 49 deletions library/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
/*获取配置*/
private static function getConfig()
{
return self::$_config ?: (self::$_config = Yaf_Application::app()->getConfig());
}
}
18 changes: 18 additions & 0 deletions tests/EnvTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace tests;

class EnvTest extends \PHPUnit_Framework_TestCase
{
public function testExt()
{
$this->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');
}
}

4 changes: 4 additions & 0 deletions tests/ini/yaf.dev.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
extension=yaf.so
[yaf]
yaf.environ=dev
yaf.use_spl_autoload = 1
6 changes: 6 additions & 0 deletions tests/ini/yaf.product.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extension=yaf.so

[yaf]
yaf.environ=product
yaf.cache_config=1
yaf.use_spl_autoload=1
6 changes: 6 additions & 0 deletions tests/init.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
define('APP_PATH', dirname(dirname(__FILE__)));
define('TRACER_OFF', true);

$app=new Yaf_Application(APP_PATH . '/conf/app.ini');
$app->bootstrap();
15 changes: 15 additions & 0 deletions tests/install-yaf.sh
Original file line number Diff line number Diff line change
@@ -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
72 changes: 72 additions & 0 deletions tests/library/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
namespace tests\library;

use \Config as Config;
use \Yaf_Application as Application;

class ConfigTest extends \PHPUnit_Framework_TestCase
{

/*测试配置和配置文件是否一致*/
public function testConfigConsistency()
{
$env=Application::app()->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);
}
}
29 changes: 29 additions & 0 deletions tests/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="./init.php"
backupGlobals="false"
backupStaticAttributes="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
colors="true">

<testsuites>

<testsuite name="Environment">
<file>EnvTest.php</file>
</testsuite>

<testsuite name="Library">
<directory>library</directory>
</testsuite>

</testsuites>

<filter>
<whitelist>
<directory suffix=".php">../library</directory>
</whitelist>
</filter>

</phpunit>

0 comments on commit 23a33c7

Please sign in to comment.