今天發現個函數 assert 和 assert_options, 他們組合可以完成一個簡單的phpunit的功能, 但是實在是太簡單, 所以用處不太大, 但是還是記錄一下好了.


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

  同時可以結合 <>中 "XXVII. Error Handling and Logging Functions" 章節裡的東西,共同使用.

  下面是我寫的一個測試檔案, 包含了所有的功能的測試,不過ASSERT_QUIET_EVAL一直不太明白,沒測試出來具體有什麼樣作用

function assert_failed($file, $line, $expr) {
    print "Assertion failed in $file [ $line ] : $expr <br/>";
//error_reporting設定為0, 相當於調用assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_BAIL, 0);
//assert_options(ASSERT_QUIET_EVAL, 0);
echo "step 1 <br />";
echo "step 2 <br />";
echo "step 3 <br />";
assert_options(ASSERT_CALLBACK, 'assert_failed');
//assert_options(ASSERT_WARNING, 1); 
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( ) []: 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 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.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));


E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >



如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。