增加Log类测试

This commit is contained in:
thinkphp
2016-02-29 10:29:52 +08:00
parent e7ed4be654
commit 9496e4a42e
8 changed files with 108 additions and 37 deletions

View File

@@ -75,22 +75,31 @@ class Log
}
/**
* 保存调试信息
* 清空日志信息
* @return void
*/
public static function clear()
{
self::$log = [];
}
/**
* 保存调试信息
* @return bool
*/
public static function save()
{
if (is_null(self::$driver)) {
self::init(Config::get('log'));
}
self::$driver->save(self::$log);
return self::$driver->save(self::$log);
}
/**
* 实时写入日志信息 并支持行为
* @param mixed $msg 调试信息
* @param string $type 信息类型
* @return void
* @return bool
*/
public static function write($msg, $type = 'log')
{
@@ -106,7 +115,7 @@ class Log
self::init(Config::get('log'));
}
// 写入日志
self::$driver->save($log);
return self::$driver->save($log);
}
/**

View File

@@ -33,7 +33,7 @@ class File
* 日志写入接口
* @access public
* @param array $log 日志信息
* @return void
* @return bool
*/
public function save(array $log = [])
{
@@ -74,6 +74,7 @@ class File
$remote = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0';
$uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
error_log("[{$now}] {$server} {$remote} {$uri}\r\n{$info}\r\n", 3, $destination);
return true;
}
}

View File

@@ -29,7 +29,7 @@ class Sae
* 日志写入接口
* @access public
* @param array $log 日志信息
* @return void
* @return bool
*/
public function save(array $log = [])
{
@@ -73,7 +73,7 @@ class Sae
if ($is_debug) {
sae_set_display_errors(true);
}
return true;
}
}

View File

@@ -48,10 +48,16 @@ class Socket
}
}
/**
* 日志写入接口
* @access public
* @param array $logs 日志信息
* @return bool
*/
public function save(array $logs = [])
{
if (!$this->check()) {
return;
return false;
}
$runtime = number_format(microtime(true) - START_TIME, 6);
$reqs = number_format(1 / $runtime, 2);
@@ -112,7 +118,7 @@ class Socket
} else {
$this->sendToClient($tabid, $client_id, $logs, '');
}
return true;
}
/**

View File

@@ -19,10 +19,11 @@ class Test
* 日志写入接口
* @access public
* @param array $log 日志信息
* @return void
* @return bool
*/
public function save(array $log = [])
{
return true;
}
}

View File

@@ -34,13 +34,13 @@ class Trace
* 日志写入接口
* @access public
* @param array $log 日志信息
* @return void
* @return bool
*/
public function save(array $log = [])
{
if (IS_AJAX || IS_CLI || IS_API || 'html' != Config::get('default_return_type')) {
// ajax cli api方式下不输出
return;
return false;
}
// 获取基本信息
$runtime = number_format(microtime(true) - START_TIME, 6);
@@ -94,6 +94,7 @@ class Trace
ob_start();
include $this->config['trace_file'];
echo ob_get_clean();
return true;
}
}

View File

@@ -0,0 +1,48 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
/**
* Log测试
* @author liu21st <liu21st@gmail.com>
*/
namespace tests\thinkphp\library\think;
use think\Log;
class logTest extends \PHPUnit_Framework_TestCase
{
public function testRecord(){
Log::clear();
Log::record('test');
$this->assertEquals([['type'=>'log','msg'=>'test']], Log::getLog());
Log::record('hello','info');
$this->assertEquals([['type'=>'log','msg'=>'test'],['type'=>'info','msg'=>'hello']], Log::getLog());
Log::clear();
Log::info('test');
$this->assertEquals([['type'=>'info','msg'=>'test']], Log::getLog());
}
public function testSave(){
Log::init(['type'=>'test']);
Log::clear();
Log::record('test');
Log::record([1,2,3]);
$this->assertTrue(Log::save());
}
public function testWrite(){
Log::init(['type'=>'test']);
Log::clear();
$this->assertTrue(Log::write('hello','info'));
$this->assertTrue(Log::write([1,2,3],'log'));
}
}

View File

@@ -10,50 +10,55 @@
// +----------------------------------------------------------------------
/**
* Route测试
* Url测试
* @author liu21st <liu21st@gmail.com>
*/
namespace tests\thinkphp\library\think;
use think\Url;
use think\Route;
use think\Config;
use think\Route;
use think\Url;
class urlTest extends \PHPUnit_Framework_TestCase
{
public function testBuildModule(){
Route::get('hello/:name','index/hello');
Route::get('hello/:id','index/hello');
Config::set('pathinfo_depr','/');
$this->assertEquals('/hello/thinkphp',Url::build('index/hello?name=thinkphp'));
$this->assertEquals('/hello/thinkphp.html',Url::build('index/hello','name=thinkphp','html'));
$this->assertEquals('/hello/10',Url::build('index/hello?id=10'));
$this->assertEquals('/hello/10.html',Url::build('index/hello','id=10','html'));
public function testBuildModule()
{
Route::get('hello/:name', 'index/hello');
Route::get('hello/:id', 'index/hello');
Config::set('pathinfo_depr', '/');
$this->assertEquals('/hello/thinkphp', Url::build('index/hello?name=thinkphp'));
$this->assertEquals('/hello/thinkphp.html', Url::build('index/hello', 'name=thinkphp', 'html'));
$this->assertEquals('/hello/10', Url::build('index/hello?id=10'));
$this->assertEquals('/hello/10.html', Url::build('index/hello', 'id=10', 'html'));
}
public function testBuildController(){
Route::get('blog/:id','@index/blog/read');
$this->assertEquals('/blog/10.html',Url::build('@index/blog/read','id=10','html'));
public function testBuildController()
{
Route::get('blog/:id', '@index/blog/read');
$this->assertEquals('/blog/10.html', Url::build('@index/blog/read', 'id=10', 'html'));
}
public function testBuildMethod(){
Route::get('blog/:id', ['\app\index\controller\blog','read']);
$this->assertEquals('/blog/10.html',Url::build('\app\index\controller\blog\read','id=10','html'));
public function testBuildMethod()
{
Route::get('blog/:id', ['\app\index\controller\blog', 'read']);
$this->assertEquals('/blog/10.html', Url::build('\app\index\controller\blog\read', 'id=10', 'html'));
}
public function testBuildRoute(){
public function testBuildRoute()
{
Route::get('blog/:id', 'index/blog');
Config::set('url_html_suffix','shtml');
$this->assertNotEquals('/blog/10.html',Url::build('/blog/10'));
$this->assertEquals('/blog/10.shtml',Url::build('/blog/10'));
Config::set('url_html_suffix', 'shtml');
$this->assertNotEquals('/blog/10.html', Url::build('/blog/10'));
$this->assertEquals('/blog/10.shtml', Url::build('/blog/10'));
}
public function testBuildAnchor(){
public function testBuildAnchor()
{
Route::get('blog/:id', 'index/blog');
Config::set('url_html_suffix','shtml');
$this->assertEquals('/blog/10.shtml#detail',Url::build('/blog/10#detail'));
Config::set('url_html_suffix', 'shtml');
$this->assertEquals('/blog/10.shtml#detail', Url::build('/blog/10#detail'));
}
}