php自带的简易的unit函数,可以代替echo调试,但是意义好象不太大

原创 2006年06月15日 11:59:00

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

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

具体的使用方法可以看 <<php手册>> 或者 <<php in a nutshell>>

同时可以结合 <<php手册>>中 "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));

 

php源码str_repeat有趣的实现

php 源码str_repeat实现分析
  • wolfqong
  • wolfqong
  • 2017年06月06日 17:38
  • 561

Java中关于==与equal的比较

在java面试中常常会遇到考官问道java中==与equal的区别,这里参考了各方面的资料做一个总结。...
  • u010921701
  • u010921701
  • 2016年10月15日 11:41
  • 794

Java中equal和==的区别

equal和==均表示相等的意思,但是它们进行实际的相等的判定的时候,却有
  • lin446591998
  • lin446591998
  • 2014年09月15日 14:49
  • 610

HashCode和equal方法的区别和联系

equals()反映的是对象或变量具体的值,即两个对象里面包含的值--可能是对象的引用,也可能是值类型的值。 而hashCode()是对象或变量通过哈希算法计算出的哈希值。 之所以有hashCod...
  • liu1pan2min3
  • liu1pan2min3
  • 2015年09月26日 01:45
  • 1355

Hadoop自定义数据类型和输入格式

Hadoop自定义数据类型和输入格式 一,自定义一个数据类型:User类 该类实现WritableComparable接口,并且带三个属性:name,sex,age 重写其...
  • young_so_nice
  • young_so_nice
  • 2016年05月03日 19:47
  • 1055

java中==和equal的区别

值类型是存储在内存中的堆栈(以后简称栈),而引用类型的变量在栈中仅仅是存储引用类型变量的地址,而其本身则存储在堆中。     ==操作比较的是两个变量的值是否相等,对于引用型变量表示的是两个变量在堆...
  • huanongjingchao
  • huanongjingchao
  • 2014年08月08日 19:53
  • 7527

重写equal()时为什么也得重写hashCode()之深度解读equal方法与hashCode方法渊源

今天这篇文章我们打算来深度解读一下equal方法以及其关联方法hashCode(),我们准备从以下几点入手分析: 1.equals()的所属以及内部原理(即Object中equals方法的实现...
  • javazejian
  • javazejian
  • 2016年05月08日 23:14
  • 7667

关于equal和==

最近面试的时候总是遇到这个问题的笔试,以前学的时候没有深究,现在是时候来总结一下了1.基本类型和包装类诸如 new Integer(32)==32 new Integer...
  • u012810034
  • u012810034
  • 2017年05月05日 11:27
  • 166

String中==和equal()的区别

==是判断两个字符窜引用是否相等。 .equal()判断的是两个字符窜值是否相等。 不同对象总是产生不同的散列值,即便是他们是相等的值也可以。 因此,如果你想要判断两个字符串是否相等你最好使用.equ...
  • l975764577
  • l975764577
  • 2014年03月09日 19:18
  • 1309

关于Java中equal函数和==的一些区别

equal函数比较的到底是什么,很明显是比较的值,但是什么值?与==比较的堆中的内存地址不同,其比较的是对象的值,包括各个属性的值。我们在重新overriding此方法时首先要判断是否为同一对象,如果...
  • qq_25827845
  • qq_25827845
  • 2015年09月14日 14:57
  • 2322
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:php自带的简易的unit函数,可以代替echo调试,但是意义好象不太大
举报原因:
原因补充:

(最多只允许输入30个字)