PHP自带可以代替echo调试的unit函数

  今天发现个函数 assert 和 assert_options, 他们组合可以完成一个简单的phpunit的功能, 但是实在是太简单, 所以用处不太大, 但是还是记录一下好了.

  主要问题是不能灵活的自己定义错误的提示信息,只能提示出问题的文件和行数.

  具体的使用方法可以看 <> 或者 <>

  同时可以结合 <>中 "XXVII. Error Handling and Logging Functions" 章节里的东西,共同使用.

  下面是我写的一个测试文件, 包含了所有的功能的测试,不过ASSERT_QUIET_EVAL一直不太明白,没测试出来具体有什么样作用

<?php
function assert_failed($file, $line, $expr) {
    print "Assertion failed in $file [ $line ] : $expr <br/>";
}
//error_reporting设置为0, 相当于调用assert_options(ASSERT_WARNING, 0);
//error_reporting(0);
//是否启用对ASSERT_ACTIVE的支持
assert_options(ASSERT_ACTIVE, 1);
//是否在发送第一次wanning的时候,停止脚本的执行
assert_options(ASSERT_BAIL, 0);
//没搞定,还不明白具体怎么用,偶测试不出来
//assert_options(ASSERT_QUIET_EVAL, 0);
echo "step 1 <br />";
assert(1==1);
echo "step 2 <br />";
assert(2==1);
echo "step 3 <br />";
//设定assert的callback样式,可以自己定义wanning信息显示时的样式
assert_options(ASSERT_CALLBACK, 'assert_failed');
//不显示assert()自己产生warnning信息,如果设置了ASSERT_CALLBACK,仍然还会显示ASSERT_CALLBACK函数对应的信息,但是函数中传入的$expr参数不起作用.
//assert_options(ASSERT_WARNING, 1); 
assert(1==1);
assert((1/0)>2);
echo "step 4 <br />";
?>

  下面的一段话是直接从  中copy出来的

The assert( ) function is a clever one that works along the same lines as our print statements, but it only works if a certain condition is not matched. Essentially, assert( ) is used to say "This statement must be trueif it isn't, please tell me." For example:
    print "Stage 1\n";
    assert(1 =  = 1);
    print "Stage 2\n";
    assert(1 =  = 2);
    print "Stage 3\n";

Here we have two assert( )s, with the first call asserting that one must be equal to one, and the second call asserting that one must be equal to two. As it is impossible to redefine constants like 1 and 2, the first assert( ) will always evaluate to true, and the second will always evaluate to false. Here is the output from the script:
    Stage 1
    Stage 2
    Warning: assert( ) [http://www.php.net/function.assert]: Assertion failed
            in /home/paul/sandbox/php/assert.php on line 5
    Stage 3

The first assert( ) is not seen in the output at all because it evaluated to TRue, whereas the second assert( ) evaluated to false, so we get a warning about an assertion failure. However, script execution carries on so that we see "Stage 3" after the assertion failure warning. As long as assertions evaluate to true, they have no effect on the running of the script, which means you can insert them for debugging purposes and not have to worry about taking them out once you are finished debugging.
If you are worried about your assertions slowing execution down, which, although the speed hit will be minimal, is still a valid concern, you can disable execution of assert( ) by using the assert_options( ) function or by setting assert.active to Off in your php.ini file. If you want to use assert_options( ), it takes two parameters: the option to set and the value you wish to set it to.
Table 22-1 shows the list of options you can use for the first parameter of assert_options( ):
Table 22-1. First parameter of assert_options( ) 
Parameter          Default    Description
 
ASSERT_ACTIVE      On         Enables evaluation of assert( ) calls
 
ASSERT_WARNING     On         Makes PHP output a warning for each failed assertion
 
ASSERT_BAIL        Off        Forces PHP to end script execution on a failed assertion
 
ASSERT_QUIET_EVAL  Off        Ignores errors in assert( ) calls
 
ASSERT_CALLBACK    Off        Names user function to call on a failed assertion
 

To disable assert( ) calls, use this line of code:
    assert_options(ASSERT_ACTIVE, 0);

And to make PHP end script execution rather than just issue a warning, we can use this line of code:
    assert_options(ASSERT_BAIL, 1);

Note that all of these options can be set in your php.ini file so that they are always in effect. The options to change there are assert.active, assert.warning, assert.bail, assert.quiet_eval, and assert_callback.
ASSERT_CALLBACK is a useful option, as it allows you to write an error handler for when your code fails an assertion. It takes the string name of a function to execute when assertions fail, and the function you define must take three parameters: one to hold the file where the assertion occurred, one to hold the line, and one to hold the expression. Using all three together in your callback function allows you to generate meaningful error messages that you can debug. For example:
    function assert_failed($file, $line, $expr) {
            print "Assertion failed in $file on line $line: $expr\n";
    }
    assert_options(ASSERT_CALLBACK, 'assert_failed');
    assert_options(ASSERT_WARNING, 0);
    $foo = 10;
    $bar = 11;
    assert($foo > $bar);

That example shows a callback function defined that takes $file, $line, and $expr for the three variables passed in, and outputs them whenever an assertion fails. To make that result actually happen, assert_options( ) is called to let PHP know that assert_failed( ) is the correct function to use as a callbacknote that there are no brackets after the string being passed into assert_options( ).
ASSERT_WARNING is also disabled, which stops PHP from outputting a warning as well as running the callback function. Finally, two variables are set, and are used as part of a call to assert( )as you can see, $foo is quite clearly not greater than $bar, which means the assertion will fail and call our callback. So, the output from the script is: Assertion failed in /home/paul/tmp/blerg.php on line 9: $foo > $bar.
You can assert( ) any statement you like, as long as it will return either TRue or false. This makes the assert( ) function incredibly powerfuleven more so when you think that you can just turn off assertion execution to make the code run at full speed.
Here are some more examples of assert( )able things:
    assert($savings >= $salary / 10);
    assert($myarray =  = array("apone", "burke", "hicks"));
    assert(preg_match("/wild sheep chase/", $book));

时间: 2024-07-28 14:04:16

PHP自带可以代替echo调试的unit函数的相关文章

在PL/SQL 开发中调试存储过程和函数的一般性方法

存储过程|函数 在PL/SQL 开发中调试存储过程和函数的一般性方法摘要: Oracle 在PLSQL中提供的强大特性使得数据库开发人员可以在数据库端完成功能足够复杂的任务, 本文将结合Oracle提供的相关程序包(package)以及一个非常优秀的第三方开发工具来介绍在PLSQL中开发及调试存储过程的方法,当然也适用于函数. 版权声明: 本文可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息.原文出处: http://www.aiview.com/notes/ora_using_

mfc-VC6.0调试到阻塞函数的时候进不进去

问题描述 VC6.0调试到阻塞函数的时候进不进去 因为在学习MFC,想进系统函数里看看,每次调试到阻塞函数像AfxMessageBox:DoModal这样的函数的时候f11怎么也进不去会提示Unhandled exception in xxx.exe (xxx.DLL):0xC0000005:Access Violation 解决方案 明显是因为你的指针有问题造成了错误,检查下对话框构造函数,调试下看看哪行错了. 解决方案二: 这是不是因为它是库函数 解决方案三: 你这个是你的程序发生异常了.

asp net ajax-visual studio调试asp.net程序时单步调试时进入函数内部很慢

问题描述 visual studio调试asp.net程序时单步调试时进入函数内部很慢 我的页面采用三层架构,使用jQuery的ajax向后台的一般处理程序请求数据,数据量不是很大,最多也就 几十KB左右,我在一般处理程序中调用读数据库的数据的函数入口添加断点,然后按F11单步调试,从一般处理程序进入逻辑层的函数内部需要很长时间,接近一分钟左右!但是同一个请求,第二次请求时,速度又变得很快了.请问哪位高手遇到过这种类似的问题?有没有什么办法解决?谢谢. 解决方案 是不是第一次需要链接数据库,初始

PHP echo,print,printf,sprintf函数之间的区别与用法详解_php技巧

1. echo函数: 输出函数,是命令,不能返回值.echo后面可以跟很多个参数,之间用分号隔开,如: echo $myvar1; echo 1,2,$myvar,"<b>bold</b>"; 2. print函数: 是函数,可以返回一个值,只能有一个参数. int print ( string arg ) Outputs arg . Returns 1 , always. 3. printf函数: int printf ( string format [, m

2012-08-02 15:07 VC++ 往输出窗口打印调试信息调用函数

VC++提供了一个叫输出窗口的窗口,在调试程序和生成是可以看到输出信息,这个信息如果是MFC程序可以用TRACE宏来打印,在控制台程序里就没有了.所以我们直接调用API来实现上面的功能. 首先在程序中引入头文件windows.h或winbase.h 调用函数有两种版本 ANSI和UNICODE OutputDebugStringA OutputDebugStringW 自动版本 OutputDebugString 输出方法 OutputDebugString(_T("字符串")); O

调试makefile—subst函数

操作系统:ubuntu10.04 Makefile里的subst用法是$(subst FROM,TO,TEXT),即将TEXT中的东西从FROM变为TO Makefile中的字符串处理函数格式:    $(subst ;,;,;)名称:字符串替换函数--subst.功能:把字串;中的;字符串替换成;.返回:函数返回被替换过后的字符串. 示例:$(subst a,the,There is a big tree),把"There is a big tree"中的"a"替

宏定义编写技巧__调试技巧【原创】

带颜色打印: printk("\033[1;33;40m misc.c InterIoctl() action=%d\033[0m\r\n", action); 方法一. 1 #ifndef _PMU_DUMMY_ 2 3 #define _PMU_DUMMY_ 4 5 6 7 #define PAX_PMU_DUMMY_DEBUG_ENABLE 8 9 10 11 #ifdef PAX_PMU_DUMMY_DEBUG_ENABLE 12 13 #define PAX_PMU_DUMM

Win32调试接口设计与实现浅析

所谓调试器实际上是一个很宽泛的概念,凡是能够以某种形式监控其他程序执行过程的程序,都可以泛称为调试器.在Windows平台上,根据调试器的实现原理大概可以将之分为三类:内核态调试器.用户态调试器和伪代码调试器. 内核态调试器直接工作在操作系统内核一级,在硬件与操作系统之间针对系统核心或驱动进行调试,常见的有SoftICE.WinDbg.WDEB386和i386KD等等:用户态调试器则通过操作系统提供的调试接口,在操作系统和用户态程序之间针对用户态程序进行调试,常见的有各种开发环境如VC/Delp

如何编写属于自己的Java / Scala的调试器

译者:赖辉强  原文地址 在本帖中,我们将探讨Java和Scala的调试器是如何编写和工作的:系统自带的调试器,例如:Windows中的WinDbg或者是Linux/Unix中的gdb,会获取操作系统直接提供给他们的链接入口来启动,从而指导和操作外部程序的状态.工作在操作系统顶部抽象层的Java虚拟机对字节码的调试有独立的处理架构. 这个调试的框架和APIs具有全开源.文档化.可扩展的特点,这意味着你可以轻松毫无顾忌的编写自己的调试器.该框架当前的设计由两大部分构成-JDWP协议和JVMTI A